source: src/main/java/agents/anac/y2019/fsega2019/agent/FSEGA2019.java

Last change on this file was 205, checked in by Katsuhide Fujita, 5 years ago

ANAC 2019 well-worked agents

  • Property svn:executable set to *
File size: 12.0 KB
Line 
1package agents.anac.y2019.fsega2019.agent;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.List;
6
7import agents.anac.y2019.fsega2019.fsegaoppmodel.MyBayesianOpponentModel;
8import agents.anac.y2019.fsega2019.fsegaoppmodel.OpponentModel;
9import agents.anac.y2019.fsega2019.fsegaoppmodel.ReverseBidComparator;
10import genius.core.AgentID;
11import genius.core.Bid;
12import genius.core.BidIterator;
13import genius.core.actions.Accept;
14import genius.core.actions.Action;
15import genius.core.actions.EndNegotiation;
16import genius.core.actions.Offer;
17import genius.core.parties.AbstractNegotiationParty;
18import genius.core.parties.NegotiationInfo;
19import genius.core.uncertainty.ExperimentalUserModel;
20import genius.core.utility.AbstractUtilitySpace;
21
22public class FSEGA2019 extends AbstractNegotiationParty {
23
24 /**
25 *
26 */
27 private static final long serialVersionUID = 1L;
28
29 enum ACTIONTYPE {
30 START, OFFER, ACCEPT, BREAKOFF
31 };
32
33 enum STRATEGY {
34 SMART, SERIAL, RESPONSIVE, RANDOM, TIT_FOR_TAT
35 };
36
37 private Action lastOponentAction;
38 private Action myLastAction;
39 private OpponentModel opponentModel;
40 private ArrayList<Bid> leftBids;
41 private double elapsedTimeNorm;
42 private static final double SIGMA = 0.01;
43 private static final double MIN_ALLOWED_UTILITY = 0.5;
44 private int count = 0;
45 private NegotiationInfo info;
46 private double initialTime;
47 private static final double SIGMA_MAX = 0.5;
48 private AbstractUtilitySpace realUSpace;
49
50 public static String getVersion() {
51 return "1.0";
52 }
53
54 @Override
55 public void init(NegotiationInfo info) {
56
57 initialTime = info.getTimeline().getTime();
58 super.init(info);
59
60 this.info = info;
61
62 lastOponentAction = null;
63 myLastAction = null;
64
65 // initialize opponent model
66 opponentModel = new MyBayesianOpponentModel(utilitySpace);
67
68 // initialize left bids
69
70 leftBids = new ArrayList<Bid>();
71
72 // initialize left bids
73
74 double[] nrBids = { 0, 0, 0, 0, 0 };
75 BidIterator bIterCount = new BidIterator(utilitySpace.getDomain());
76
77 while (bIterCount.hasNext()) {
78 Bid tmpBid = bIterCount.next();
79 double utility;
80 try {
81 utility = utilitySpace.getUtility(tmpBid);
82 if ((utility > 1.0) && (utility > 0.9))
83 nrBids[0]++;
84 else if ((utility <= 0.9) && (utility > 0.8))
85 nrBids[1]++;
86 else if ((utility <= 0.8) && (utility > 0.7))
87 nrBids[2]++;
88 else if ((utility <= 0.7) && (utility > 0.6))
89 nrBids[3]++;
90 else if (utility >= MIN_ALLOWED_UTILITY)
91 nrBids[4]++;
92 } catch (Exception e) {
93 }
94 }
95 try {
96 Thread.sleep(1000);
97 } catch (Exception e) {
98 }
99
100 //
101 double arrayBidCount = 0;
102 int iMin = 0;
103 do {
104 arrayBidCount = nrBids[iMin];
105 iMin++;
106 } while ((arrayBidCount == 0) && (iMin < 5));
107
108 // end Test
109
110 // initialize left bids
111 BidIterator bIter = new BidIterator(utilitySpace.getDomain());
112 // proposedBids = new ArrayList<Bid>();
113 leftBids = new ArrayList<Bid>();
114
115 if (userModel instanceof ExperimentalUserModel) {
116 ExperimentalUserModel e = (ExperimentalUserModel) userModel;
117 realUSpace = e.getRealUtilitySpace();
118 }
119 Bid bestBid = null;
120 Bid secondBestBid = null;
121 double bestBidUt = -1, secondBestBidUt = -1;
122 int i = 0;
123 while (bIter.hasNext()) {
124 i++;
125 Bid tempBid = bIter.next();
126 if (getUtilitySpace().getUtility(tempBid) >= 0.9 - 0.1 * iMin) {
127 if (getUtilitySpace().getUtility(tempBid) > bestBidUt) {
128 bestBidUt = getUtilitySpace().getUtility(tempBid);
129 secondBestBid = bestBid;
130 bestBid = tempBid;
131 } else if (getUtilitySpace().getUtility(tempBid) > secondBestBidUt) {
132 secondBestBidUt = getUtilitySpace().getUtility(tempBid);
133 secondBestBid = tempBid;
134 }
135 }
136 }
137 leftBids.add(bestBid);
138 leftBids.add(secondBestBid);
139
140 }
141
142 @Override
143 public void receiveMessage(AgentID sender, Action act) {
144 super.receiveMessage(sender, act);
145 if (act instanceof Offer) {
146 lastOponentAction = (Offer) act;
147 } else if (act instanceof Accept) {
148 lastOponentAction = (Accept) act;
149 } else if (act instanceof EndNegotiation) {
150 lastOponentAction = (EndNegotiation) act;
151 }
152
153 }
154
155 @Override
156 public Action chooseAction(List<Class<? extends Action>> possibleActions) {
157
158 Action action = null;
159 Bid opponentBid = null;
160 try {
161 switch (getActionType(lastOponentAction)) {
162 case OFFER:
163
164 int timeCase; // the curent time interval id for cameleonic behaviour
165
166 elapsedTimeNorm = getTimeLine().getTime();
167
168 if (elapsedTimeNorm < 0.85)
169 timeCase = 0;
170 else if (elapsedTimeNorm < 0.95)
171 timeCase = 1;
172 else
173 timeCase = 2;
174
175 opponentBid = ((Offer) lastOponentAction).getBid();
176
177 // update beliefs
178 opponentModel.updateBeliefs(opponentBid);
179
180 if (myLastAction != null) {
181 // check if the opponent offer is acceptable
182 if (getUtilitySpace().getUtility(opponentBid) * 1.03 >= getUtilitySpace()
183 .getUtility(((Offer) myLastAction).getBid())) {
184 action = new Accept(getPartyId(), opponentBid);
185 } else {
186 Bid nextBid = getNextBid(timeCase);
187
188 if (nextBid != null)
189 action = new Offer(getPartyId(), nextBid);
190
191 // check if the opponent offer utility is greater than next bid
192 if (getUtilitySpace().getUtility(opponentBid) > getUtilitySpace().getUtility(nextBid)) {
193 action = new Accept(getPartyId(), opponentBid);
194 }
195
196 }
197 } else {
198 // opponent begins and this is my first action
199 // check if opponent's offer is acceptable
200 if (getUtilitySpace().getUtility(opponentBid) == getUtilitySpace()
201 .getUtility(utilitySpace.getMaxUtilityBid())) {
202 action = new Accept(getPartyId(), opponentBid);
203 break;
204 }
205 // this is the first offer of this node
206 // this bid have maximum utility
207 action = initialOffer();
208 }
209 break;
210 case ACCEPT:
211 case BREAKOFF:
212 // Nothing to do in this case
213 break;
214 default: // normally occurs when action type is START
215 // verify if is agent's first offer
216 if (myLastAction == null) {
217 // is the first offer of this node
218 action = initialOffer();
219 } else {
220 action = myLastAction;
221 }
222 }
223 } catch (Exception e) {
224 System.out.println("EXCEPTION description: " + e.getMessage() + " stack ");
225 e.printStackTrace();
226 }
227
228 myLastAction = action;
229
230 return action;
231 }
232
233 @Override
234 public String getDescription() {
235 return "FSEGA Agent with preference uncertainty";
236 }
237
238 @Override
239 public AbstractUtilitySpace estimateUtilitySpace() {
240 return super.estimateUtilitySpace();
241 }
242
243 private Action initialOffer() throws Exception {
244 return new Offer(getPartyId(), getUtilitySpace().getMaxUtilityBid());
245 }
246
247 private Bid getNextBid(int pTimeCase) throws Exception {
248 switch (pTimeCase) {
249 case 0:
250 Bid b = getSmartBid(pTimeCase);
251 return b;
252 case 1:
253 b = getSmartBid(1);
254 return b;
255 default:
256 b = getSmartBid(2);
257 return b;
258 }
259 }
260
261 private Bid getSmartBid(int pTimeCase) {
262
263 double currentOpponentUtility = 0.0;
264 double lastOpponentUtility;
265
266 double myBestUtil; // utility for remained best bid
267 double myNextUtility; // utility for next bid
268
269 Bid nextBest = null; // best for me and opponent
270 Bid theBest = null; // the best for me, top of the ordered list
271
272 count++;
273
274 // get first entry
275 if (leftBids.size() > 0 && leftBids.get(0) != null) {
276 theBest = leftBids.get(0);
277 try {
278 lastOpponentUtility = opponentModel.getExpectedUtility(nextBest);
279 } catch (Exception e) {
280 lastOpponentUtility = 0;
281 }
282
283 try {
284 myBestUtil = getUtilitySpace().getUtility(theBest);
285 } catch (Exception e) {
286 myBestUtil = 1;
287 }
288
289 nextBest = theBest;
290
291 } else {
292 return ((Offer) myLastAction).getBid();
293 }
294
295 Bid lNext = null;
296
297 double minUtilAllowed = Math.max(0.98 * Math.exp(Math.log(0.52) * elapsedTimeNorm), 0.5);
298
299 double minArrayUtility = 0;
300 try {
301 minArrayUtility = getUtilitySpace().getUtility(leftBids.get(leftBids.size() - 1));
302 } catch (Exception e) {
303 }
304
305 // !!! left bids update
306 Bid bestBid = null;
307 Bid secondBestBid = null;
308 double bestBidUt = -1, secondBestBidUt = -1;
309 if (((minArrayUtility > minUtilAllowed + SIGMA) || (minArrayUtility > (myBestUtil - SIGMA)))) {
310 BidIterator bIter = new BidIterator(utilitySpace.getDomain());
311
312 for (Bid tmpBid = bIter.next(); bIter.hasNext(); tmpBid = bIter.next()) {
313 double tmpBidUtil = getUtilitySpace().getUtility(tmpBid);
314 if ((tmpBidUtil > MIN_ALLOWED_UTILITY) && (tmpBidUtil < minArrayUtility)
315 && (tmpBidUtil > Math.min(minUtilAllowed, myBestUtil) - 0.1)) {
316 if (getUtilitySpace().getUtility(tmpBid) > bestBidUt) {
317 bestBidUt = getUtilitySpace().getUtility(tmpBid);
318 secondBestBid = bestBid;
319 bestBid = tmpBid;
320 } else if (getUtilitySpace().getUtility(tmpBid) > secondBestBidUt) {
321 secondBestBidUt = getUtilitySpace().getUtility(tmpBid);
322 secondBestBid = tmpBid;
323 }
324 }
325 }
326 leftBids.add(bestBid);
327 leftBids.add(secondBestBid);
328
329 Collections.sort(leftBids, new ReverseBidComparator(utilitySpace));
330
331 }
332
333 // get second entry in last bids
334 if (leftBids.size() > 1) {
335 lNext = leftBids.get(1);
336 } else {
337 leftBids.remove(nextBest);
338 return nextBest;
339 }
340
341 try {
342 myNextUtility = getUtilitySpace().getUtility(lNext);
343 } catch (Exception e) {
344 myNextUtility = 0;
345 }
346
347 double lowerAcceptableUtilLimit;
348
349 // if time case is 2 -> make concession
350 if (pTimeCase == 2) {
351 lowerAcceptableUtilLimit = myBestUtil
352 - (Math.exp(Math.log(SIGMA_MAX) / (0.05 * getTimeLine().getTotalTime())) * elapsedTimeNorm);
353
354 } else {
355 // minimum allowed utility for me at this time
356 if (pTimeCase == 0)
357 lowerAcceptableUtilLimit = Math.max(myBestUtil - SIGMA, minUtilAllowed);
358 else
359 lowerAcceptableUtilLimit = Math.min(myBestUtil - SIGMA, minUtilAllowed);
360 }
361
362 // eliminate first bid + next bid
363 java.util.Iterator<Bid> lbi = leftBids.iterator();
364 if (leftBids.size() > 1) {
365 lbi.next(); // first bid
366 lbi.next();
367 } else {
368 return ((Offer) myLastAction).getBid();
369 }
370
371 // get a bid in interval (max_util - SIGMA, max_util]
372 while ((myNextUtility > lowerAcceptableUtilLimit) && (myNextUtility <= minUtilAllowed)) {
373 // check (my next util) < (last opponent bid's utility for me)
374 // in this case, offer previous bid
375 // Cristi Litan 19.03.2010
376 if (pTimeCase == 0) // use behaviour 0
377 {
378 try // catch getUtility exceptions
379 {
380 // do not offer bids with utility smaller than that given by opponent in time
381 // case 0
382 if (myNextUtility < getUtilitySpace().getUtility(((Offer) lastOponentAction).getBid())) {
383 nextBest = ((Offer) myLastAction).getBid();
384 break;
385 }
386 } catch (Exception e) {
387 /* do nothing - ignore */ }
388 }
389
390 try {
391
392 currentOpponentUtility = opponentModel.getExpectedUtility(lNext);
393 } catch (Exception e) {
394 currentOpponentUtility = 0;
395 }
396
397 if (currentOpponentUtility > lastOpponentUtility) {
398 lastOpponentUtility = currentOpponentUtility;
399 nextBest = lNext;
400 }
401
402 if (lbi.hasNext()) {
403 lNext = lbi.next();
404
405 // get my utility for next possible bid
406 try {
407 myNextUtility = getUtilitySpace().getUtility(lNext);
408 } catch (Exception e) {
409 myNextUtility = 0;
410 }
411 } else
412 // log("no other in possible bids");
413 break;
414 }
415
416 try {
417 // test under limit case
418 if (getUtilitySpace().getUtility(nextBest) <= minUtilAllowed) {
419
420 return ((Offer) myLastAction).getBid();
421 }
422 } catch (Exception e) {
423 }
424
425 leftBids.remove(nextBest);
426
427 return nextBest;
428 }
429
430 private ACTIONTYPE getActionType(Action lAction) {
431 ACTIONTYPE lActionType = ACTIONTYPE.START;
432 if (lAction instanceof Offer)
433 lActionType = ACTIONTYPE.OFFER;
434 else if (lAction instanceof Accept)
435 lActionType = ACTIONTYPE.ACCEPT;
436 else if (lAction instanceof EndNegotiation)
437 lActionType = ACTIONTYPE.BREAKOFF;
438 return lActionType;
439 }
440}
Note: See TracBrowser for help on using the repository browser.