source: src/main/java/agents/anac/y2018/fullagent/OpponentModel_lgsmi.java@ 343

Last change on this file since 343 was 343, checked in by Tim Baarslag, 4 years ago

Fixed all errors in all 2018 agents

File size: 7.7 KB
Line 
1
2package agents.anac.y2018.fullagent;
3
4import java.util.HashMap;
5import java.util.HashSet;
6import java.util.Map;
7import java.util.Map.Entry;
8import java.util.Set;
9
10import genius.core.Bid;
11import genius.core.bidding.BidDetails;
12import genius.core.boaframework.BOAparameter;
13import genius.core.boaframework.NegotiationSession;
14import genius.core.boaframework.OpponentModel;
15import genius.core.issue.Issue;
16import genius.core.issue.IssueDiscrete;
17import genius.core.issue.Objective;
18import genius.core.issue.ValueDiscrete;
19import genius.core.utility.AdditiveUtilitySpace;
20import genius.core.utility.Evaluator;
21import genius.core.utility.EvaluatorDiscrete;
22
23/**
24 * BOA framework implementation of the HardHeaded Frequecy Model. My main
25 * contribution to this model is that I fixed a bug in the mainbranch which
26 * resulted in an equal preference of each bid in the ANAC 2011 competition.
27 * Effectively, the corrupt model resulted in the offering of a random bid in
28 * the ANAC 2011.
29 *
30 * Default: learning coef l = 0.2; learnValueAddition v = 1.0
31 *
32 * Adapted by Mark Hendrikx to be compatible with the BOA framework.
33 *
34 * Tim Baarslag, Koen Hindriks, Mark Hendrikx, Alex Dirkzwager and Catholijn M.
35 * Jonker. Decoupling Negotiating Agents to Explore the Space of Negotiation
36 * Strategies
37 *
38 *
39 */
40public class OpponentModel_lgsmi extends OpponentModel {
41
42
43 // the learning coefficient is the weight that is added each turn to the
44 // issue weights
45 // which changed. It's a trade-off between concession speed and accuracy.
46
47 /*********** can be reduced over time for giving less importance to later bids *******/
48 private double learnCoef;
49 // value which is added to a value if it is found. Determines how fast
50 // the value weights converge.
51 /*********************** can be reduced over time for giving less importance to later bids *********************/
52 private int learnValueAddition;
53
54 private int amountOfIssues;
55
56 /**
57 * Initializes the utility space of the opponent such that all value issue
58 * weights are equal.
59 */
60 @Override
61 public void init(NegotiationSession negotiationSession, Map<String, Double> parameters) {
62 super.init(negotiationSession, parameters);
63 this.negotiationSession = negotiationSession;
64 if (parameters != null && parameters.get("l") != null) {
65 learnCoef = parameters.get("l");
66 } else {
67 learnCoef = 0.2;
68 }
69 learnValueAddition = 1;
70 initializeModel();
71 }
72
73 private void initializeModel() {
74 opponentUtilitySpace = new AdditiveUtilitySpace(negotiationSession.getDomain());
75 amountOfIssues = opponentUtilitySpace.getDomain().getIssues().size();
76 double commonWeight = 1D / (double) amountOfIssues;
77
78 // initialize the weights
79 for (Entry<Objective, Evaluator> e : opponentUtilitySpace.getEvaluators()) {
80 // set the issue weights
81 opponentUtilitySpace.unlock(e.getKey());
82 e.getValue().setWeight(commonWeight);
83 try {
84 // set all value weights to one (they are normalized when
85 // calculating the utility)
86 for (ValueDiscrete vd : ((IssueDiscrete) e.getKey()).getValues())
87 ((EvaluatorDiscrete) e.getValue()).setEvaluation(vd, 1);
88 } catch (Exception ex) {
89 ex.printStackTrace();
90 }
91 }
92 }
93
94 /**
95 * Determines the difference between bids. For each issue, it is determined
96 * if the value changed. If this is the case, a 1 is stored in a hashmap for
97 * that issue, else a 0.
98 *
99 * @param first
100 * bid of the opponent
101 * @param second
102 * bid
103 * @return
104 */
105 private HashMap<Integer, Integer> determineDifference(BidDetails first, BidDetails second) {
106
107 HashMap<Integer, Integer> diff = new HashMap<Integer, Integer>();
108 try {
109 for (Issue i : opponentUtilitySpace.getDomain().getIssues()) {
110 diff.put(i.getNumber(), (((ValueDiscrete) first.getBid().getValue(i.getNumber()))
111 .equals((ValueDiscrete) second.getBid().getValue(i.getNumber()))) ? 0 : 1);
112 }
113 } catch (Exception ex) {
114 ex.printStackTrace();
115 }
116
117 return diff;
118 }
119
120 /**
121 * Updates the opponent model given a bid.
122 */
123 @Override
124 public void updateModel(Bid opponentBid, double time) {
125 if (negotiationSession.getOpponentBidHistory().size() < 2) {
126 return;
127 }
128 int numberOfUnchanged = 0;
129 BidDetails oppBid = negotiationSession.getOpponentBidHistory().getHistory()
130 .get(negotiationSession.getOpponentBidHistory().size() - 1);
131 BidDetails prevOppBid = negotiationSession.getOpponentBidHistory().getHistory()
132 .get(negotiationSession.getOpponentBidHistory().size() - 2);
133 HashMap<Integer, Integer> lastDiffSet = determineDifference(prevOppBid, oppBid);
134
135 // count the number of changes in value
136 for (Integer i : lastDiffSet.keySet()) {
137 if (lastDiffSet.get(i) == 0)
138 numberOfUnchanged++;
139 }
140
141 // This is the value to be added to weights of unchanged issues before
142 // normalization.
143 // Also the value that is taken as the minimum possible weight,
144 // (therefore defining the maximum possible also).
145
146 // the proportion given to last bid
147 double goldenValue = learnCoef / (double) amountOfIssues;
148 // The total sum of weights before normalization.
149 double totalSum = 1D + goldenValue * (double) numberOfUnchanged;
150 // The maximum possible weight
151 double maximumWeight = 1D - ((double) amountOfIssues) * goldenValue / totalSum;
152
153 // re-weighing issues while making sure that the sum remains 1
154 for (Integer i : lastDiffSet.keySet()) {
155
156 //if issue's value unchanged and the weight of the issue is smaller then maximumWeight
157 if (lastDiffSet.get(i) == 0 && opponentUtilitySpace.getWeight(i) < maximumWeight)
158 //if the new weight is legal, set the weight for this issue
159 opponentUtilitySpace.setWeight(opponentUtilitySpace.getDomain().getObjectives().get(i),
160 (opponentUtilitySpace.getWeight(i) + goldenValue) / totalSum);
161 else
162 // the assumption is that values that have been changed are values that the
163 // opponent is willing to compromise on them, so we reduce their weight
164 opponentUtilitySpace.setWeight(opponentUtilitySpace.getDomain().getObjectives().get(i),
165 opponentUtilitySpace.getWeight(i) / totalSum);
166 }
167
168 // Then for each issue's value that has been offered last time, a constant
169
170 // value is added to its corresponding ValueDiscrete.
171 try {
172 for (Entry<Objective, Evaluator> e : opponentUtilitySpace.getEvaluators()) {
173 // cast issue to discrete and retrieve value. Next, add constant
174 // learnValueAddition to the current preference of the value to
175
176 // make it more important
177
178 ((EvaluatorDiscrete) e.getValue()).setEvaluation(
179 oppBid.getBid().getValue(((IssueDiscrete) e.getKey()).getNumber()),
180 (learnValueAddition + ((EvaluatorDiscrete) e.getValue()).getEvaluationNotNormalized(
181 ((ValueDiscrete) oppBid.getBid().getValue(((IssueDiscrete) e.getKey()).getNumber())))));
182 }
183 } catch (Exception ex) {
184 ex.printStackTrace();
185 }
186 }
187
188 @Override
189 public double getBidEvaluation(Bid bid) {
190 double result = 0;
191 try {
192 result = opponentUtilitySpace.getUtility(bid);
193 } catch (Exception e) {
194 e.printStackTrace();
195 }
196 return result;
197 }
198
199 @Override
200 public String getName() {
201
202 return "OpponentModel_lgsmi";
203
204 }
205
206 @Override
207 public Set<BOAparameter> getParameterSpec() {
208 Set<BOAparameter> set = new HashSet<BOAparameter>();
209 set.add(new BOAparameter("l", 0.2,
210 "The learning coefficient determines how quickly the issue weights are learned"));
211 return set;
212 }
213
214
215 public Map<String, Double> getParameters() {
216 Map<String, Double> map = new HashMap<String, Double>();
217 //The learning coefficient determines how quickly the issue weights are learned
218 map.put("l", 0.2);
219 return map;
220 }
221
222}
Note: See TracBrowser for help on using the repository browser.