source: src/main/java/parties/in4010/q12015/group3/Group3.java@ 47

Last change on this file since 47 was 47, checked in by Tim Baarslag, 7 years ago

BOA agents get their getUtilitySpace() from the API, not the info object anymore

File size: 8.1 KB
Line 
1package parties.in4010.q12015.group3;
2
3import java.util.List;
4
5import java.util.ArrayList;
6import java.util.Arrays;
7
8import genius.core.AgentID;
9import genius.core.Bid;
10import genius.core.actions.Accept;
11import genius.core.actions.Action;
12import genius.core.actions.ActionWithBid;
13import genius.core.actions.DefaultAction;
14import genius.core.actions.EndNegotiation;
15import genius.core.actions.Offer;
16import genius.core.bidding.BidDetails;
17import genius.core.boaframework.OutcomeSpace;
18import genius.core.boaframework.SortedOutcomeSpace;
19import genius.core.issue.Value;
20import genius.core.parties.AbstractNegotiationParty;
21import genius.core.parties.NegotiationInfo;
22
23/**
24 * This is your negotiation party.
25 */
26public class Group3 extends AbstractNegotiationParty {
27 private double alfa;
28 private double beta;
29
30 private double MIN_BID_UTILITY;
31 private double LEARNING_TIME;
32 private SortedOutcomeSpace sortedBids;
33 private OutcomeSpace space;
34
35 private double opponentWeights[];
36 private double WEIGHT_PARAMETER = 0.1;
37
38 private List<Action> opponentActions;
39 private Bid previousBidOpponent;
40 private double HIGH_WEIGHT;
41 private int HAMMING_THRESHOLD;
42
43 private List<Double> opponentUtils = new ArrayList<Double>();
44 private double ACCEPTANCE_THRESHOLD;
45
46 private int roundsCounter;
47
48 public void receiveMessage(AgentID sender, Action action) {
49 super.receiveMessage(sender, action);
50 this.opponentActions.add(action);
51 }
52
53 public String getDescription() {
54 return "Group 3.oct-19-4.2";
55 }
56
57 @Override
58 public void init(NegotiationInfo info) {
59 super.init(info);
60 System.out.println("\n");
61 System.out.println("####### STARTING SESSION #########");
62 System.out.println("Initializing Agent");
63
64 this.space = new OutcomeSpace(getUtilitySpace());
65 sortedBids = new SortedOutcomeSpace(getUtilitySpace());
66 this.opponentActions = new ArrayList<Action>();
67
68 super.timeline = timeline;
69 super.utilitySpace = getUtilitySpace();
70
71 this.LEARNING_TIME = 0.1 * info.getDeadline().getValue();
72 this.HAMMING_THRESHOLD = 3;
73 this.HIGH_WEIGHT = 0.15;
74 this.ACCEPTANCE_THRESHOLD = 0.75;
75
76 this.alfa = 0.05;
77 this.beta = -Math.log(alfa) / info.getDeadline().getValue();
78 this.MIN_BID_UTILITY = 0.8;
79
80 opponentWeights = new double[getUtilitySpace().getDomain().getIssues().size()];
81 this.previousBidOpponent = null;
82
83 for (int i = 0; i < getUtilitySpace().getDomain().getIssues().size(); i++)
84 opponentWeights[i] = 1 / (double) (getUtilitySpace().getDomain().getIssues().size());
85
86 roundsCounter = 0;
87
88 System.out.println("Initialization Completed!");
89 System.out.println("Opponent Model: " + Arrays.toString(opponentWeights));
90 }
91
92 /**
93
94 */
95 @Override
96 public Action chooseAction(List<Class<? extends Action>> validActions) {
97 Action action = null;
98 Action lastBidResponse = null;
99 Bid nextBid = null;
100
101 double opponentUtility, myUtility;
102
103 System.out.println("\nROUND #" + roundsCounter++);
104
105 if (opponentActions.isEmpty())
106 return new Offer(getPartyId(), getNextBid(null));
107
108 try {
109 lastBidResponse = opponentActions.get(0);
110
111 Bid toBeAccepted = DefaultAction.getBidFromAction(opponentActions.get(opponentActions.size() - 1));
112
113 Bid toBeModeled = DefaultAction.getBidFromAction(lastBidResponse);
114
115 /* First we update the model of our opponent */
116 if (lastBidResponse instanceof Offer)
117 opponentWeights = this.updateModel(toBeModeled, opponentWeights);
118
119 if (lastBidResponse instanceof Accept) {
120 System.out.println("Reoffering previous Bid:");
121
122 toBeModeled = this.previousBidOpponent;
123 }
124
125 System.out.println("Opponent Model: " + Arrays.toString(opponentWeights));
126
127 /*
128 * Second, we choose the next bid wrt the preferences of the next
129 * opponent
130 */
131 nextBid = this.getNextBid(toBeModeled);
132
133 /* Last, we decide whether to accept the last bid or propose ours */
134 myUtility = getUtility(nextBid);
135 opponentUtility = getUtility(toBeAccepted);
136
137 if (isAcceptable(opponentUtility, myUtility)) {
138 System.out.println("Accepting Bid with utility: " + opponentUtility);
139 System.out.println(toBeAccepted.toString());
140 action = new Accept(getPartyId(), ((ActionWithBid) getLastReceivedAction()).getBid());
141 } else
142 action = new Offer(getPartyId(), nextBid);
143
144 /* Update parameters */
145 opponentUtils.add(opponentUtility);
146
147 this.previousBidOpponent = toBeModeled;
148 } catch (Exception e) {
149 System.out.println("CHOOSE ACTION");
150 e.printStackTrace();
151 System.out.println("List of opponent actions:\n" + opponentActions.toString());
152 action = new EndNegotiation(getPartyId());
153 }
154
155 this.opponentActions.clear();
156
157 return action;
158 }
159
160 /************************
161 * ACCEPTANCE STRATEGY
162 ****************************************/
163
164 // ACcombi(T, MAXw)
165 private boolean isAcceptable(double offeredUtilFromOpponent, double myOfferedUtil) {
166 double time = timeline.getCurrentTime();
167 double timeThreshold = ACCEPTANCE_THRESHOLD * getDeadlines().getValue();
168
169 // ACnext
170 if (offeredUtilFromOpponent > myOfferedUtil)
171 return true;
172
173 // ACtime
174 if (time > timeThreshold) {
175 // ACmaxW
176 if (offeredUtilFromOpponent > getMaxValue(opponentUtils))
177 return true;
178 }
179
180 return false;
181 }
182
183 public double getMaxValue(List<Double> values) {
184 double maxValue = Double.MIN_VALUE;
185
186 try {
187 for (Double d : values) {
188 if (d > maxValue) {
189 maxValue = d;
190 }
191 }
192 } catch (Exception e) {
193 System.out.println("GETMAXVALUE");
194 e.printStackTrace();
195 }
196
197 return maxValue;
198 }
199
200 /************************
201 * BIDDING STRATEGY
202 ****************************************/
203 private boolean compareValues(Value v1, Value v2) {
204 String v1Str = v1.toString();
205 String v2Str = v2.toString();
206
207 return v1Str.equals(v2Str);
208 }
209
210 private int computeHammingDistance(Bid b1, Bid b2) {
211 int nIssues = b1.getIssues().size();
212 int distance = 0;
213
214 /*
215 * IF issues are different IF weight for that issue is small distance =
216 * 1 ELSE distance = 2
217 */
218
219 for (int i = 1; i <= nIssues; i++) {
220 try {
221 if (!compareValues(b1.getValue(i), b2.getValue(i)))
222 distance += opponentWeights[i - 1] > HIGH_WEIGHT ? 2 : 1;
223 } catch (Exception e) {
224 System.out.println("HAMMING");
225 e.printStackTrace();
226 }
227 }
228
229 return distance;
230 }
231
232 private Bid getNextBid(Bid opponent) {
233 double time = timeline.getCurrentTime();
234 double targetUtility;
235
236 if (opponent == null) {
237 System.out.println("No previous bids to evaluate.");
238 return space.getMaxBidPossible().getBid();
239 }
240
241 List<BidDetails> allBids = this.sortedBids.getAllOutcomes();// space.getAllOutcomes();
242
243 double maxUtility = 0;
244 Bid maxUtilityBid = null;
245
246 if (time < LEARNING_TIME) {
247 System.out.println("Learning period: bidding max.");
248 return space.getMaxBidPossible().getBid();
249 }
250
251 for (java.util.Iterator<BidDetails> i = allBids.iterator(); i.hasNext();) {
252 Bid bid = i.next().getBid();
253
254 if (getUtility(bid) < MIN_BID_UTILITY)
255 break;
256
257 if (computeHammingDistance(opponent, bid) < HAMMING_THRESHOLD)
258
259 if (getUtility(bid) > maxUtility) {
260 maxUtility = getUtility(bid);
261 maxUtilityBid = bid;
262 }
263 }
264
265 if (maxUtility > MIN_BID_UTILITY) {
266 System.out.println("Hamming Utility: " + maxUtility);
267 return maxUtilityBid;
268 }
269
270 targetUtility = 1 - alfa * Math.exp(beta * (time - LEARNING_TIME));
271
272 System.out.println("Exponential decay: " + targetUtility);
273
274 if (targetUtility < MIN_BID_UTILITY) {
275 System.out.println("Lower threshold reached.");
276 targetUtility = MIN_BID_UTILITY;
277 }
278
279 return sortedBids.getBidNearUtility(targetUtility).getBid();
280 }
281
282 /** OPPONENT MODELING */
283 private double[] updateModel(Bid newBid, double[] currentWeights) {
284 double sum = 0;
285
286 if (previousBidOpponent == null)
287 return currentWeights;
288
289 for (int i = 1; i <= newBid.getIssues().size(); i++) {
290 try {
291 if (newBid.getValue(i) == previousBidOpponent.getValue(i))
292 currentWeights[i - 1] += WEIGHT_PARAMETER;
293
294 sum += currentWeights[i - 1];
295 } catch (Exception e) {
296 System.out.println("UPDATE 1");
297 e.printStackTrace();
298 }
299 }
300
301 try {
302 for (int i = 0; i < newBid.getIssues().size(); i++)
303 currentWeights[i] = currentWeights[i] / sum;
304 } catch (Exception e) {
305 System.out.println("UPDATE 2");
306 e.printStackTrace();
307 }
308
309 return currentWeights;
310 }
311
312}
Note: See TracBrowser for help on using the repository browser.