source: src/main/java/agents/anac/y2014/AgentYK/AgentYK.java

Last change on this file was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 17.8 KB
Line 
1package agents.anac.y2014.AgentYK;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.HashSet;
6import java.util.LinkedList;
7import java.util.List;
8import java.util.Random;
9
10import agents.SimpleAgent;
11import genius.core.Agent;
12import genius.core.Bid;
13import genius.core.BidHistory;
14import genius.core.Domain;
15import genius.core.actions.Accept;
16import genius.core.actions.Action;
17import genius.core.actions.ActionWithBid;
18import genius.core.actions.EndNegotiation;
19import genius.core.actions.Offer;
20import genius.core.bidding.BidDetails;
21import genius.core.issue.Issue;
22import genius.core.issue.IssueDiscrete;
23import genius.core.issue.IssueInteger;
24import genius.core.issue.IssueReal;
25import genius.core.issue.Value;
26import genius.core.issue.ValueInteger;
27import genius.core.issue.ValueReal;
28
29public class AgentYK extends Agent {
30
31 private Action actionOfPartner = null;
32 /**
33 * Note: {@link SimpleAgent} does not account for the discount factor in its
34 * computations
35 */
36 private static double MINIMUM_BID_UTILITY = 0.0;
37
38 private BidElementHistory opponentBidElementHistory;
39 private PairBidElementHistory opponentPairBidElementHistory;
40 private BidElementHistory myBidElementHistory;
41 private PairBidElementHistory myPairBidElementHistory;
42 private BidHistory opponentHistory;
43 private double oppAverage;
44 private double myAverage;
45 private BidHistory myHistory;
46 private Bid oppMaxBid = null;
47 private Bid myMinBid = null;
48 private HashSet<String> opponentBidPool;
49 private HashSet<String> myBidPool;
50 private long totalCombiNum;
51 private double logTCN;
52 private List<Integer> issueNrs;
53 private int stopNr;
54 private int seedNr;
55 private double now;
56 private double baseBE = 0;
57 private double basePBE = 0;
58 private double basedBE = 0;
59 private double basedPBE = 0;
60 private double allowedAdditional = 0.0;
61 private double df;
62 private double noValueUtil = 1.05;
63 private boolean timeBonus = true;
64 private double termRefresh;
65 private int coefRefresh = 1;
66 private int preRefresh = 0;
67 private LinkedList<Double> timeQue = new LinkedList<Double>();
68 private int linkedNum;
69 private double preTime = 0.0;
70 private int leftTime = -1;
71 private int totalTime = -1;
72 private int FaceOfBuddha = 3;
73
74 @Override
75 public void init() {
76 MINIMUM_BID_UTILITY = utilitySpace.getReservationValueUndiscounted();
77 df = utilitySpace.getDiscountFactor();
78 opponentBidElementHistory = new BidElementHistory();
79 opponentPairBidElementHistory = new PairBidElementHistory();
80 myBidElementHistory = new BidElementHistory();
81 myPairBidElementHistory = new PairBidElementHistory();
82 opponentBidPool = new HashSet<String>();
83 myBidPool = new HashSet<String>();
84 opponentHistory = new BidHistory();
85 myHistory = new BidHistory();
86 totalCombiNum = getTotalCombinationNumber();
87 logTCN = Math.log(totalCombiNum);
88 issueNrs = new ArrayList<Integer>();
89 for (Issue issue : utilitySpace.getDomain().getIssues())
90 issueNrs.add(issue.getNumber());
91
92 stopNr = 1 + (int) Math.pow(Math.log(Math.sqrt(totalCombiNum)), 2);
93 if (stopNr > 300)
94 stopNr = 300;
95 if (stopNr < 100)
96 stopNr = 100;
97 seedNr = 1 + (int) logTCN;
98 if (seedNr > 30)
99 seedNr = 30;
100 if (seedNr < 10)
101 seedNr = 10;
102 try {
103 myMinBid = getHillClimbBid(stopNr);
104 for (int i = 0; i < seedNr; i++) {
105 Bid tmpBid = getHillClimbBid(stopNr);
106 if (evaluateBid(myMinBid) < evaluateBid(tmpBid)) {
107 myMinBid = new Bid(tmpBid);
108 }
109 }
110 } catch (Exception e) {
111
112 }
113 now = 0.0;
114 termRefresh = 2 / df;
115 linkedNum = 30 + 1;
116 }
117
118 @Override
119 public String getVersion() {
120 return "1.0";
121 }
122
123 @Override
124 public String getName() {
125 return "AgentYK";
126 }
127
128 @Override
129 public void ReceiveMessage(Action opponentAction) {
130 actionOfPartner = opponentAction;
131 }
132
133 @Override
134 public Action chooseAction() {
135 Action action = null;
136 now = timeline.getTime();
137 updateLeftTime();
138
139 try {
140 if (actionOfPartner == null)
141 action = new Offer(getAgentID(), myMinBid);
142 if (actionOfPartner instanceof Offer) {
143 Bid offeredBid = ((Offer) actionOfPartner).getBid();
144 opponentBidManager(offeredBid);
145 if (isAccept(offeredBid)) {
146 action = new Accept(getAgentID(), offeredBid);
147 } else {
148 if (0 <= leftTime
149 && leftTime < (5 + (double) totalTime / 30)
150 * FaceOfBuddha
151 && myMinBid != null) {
152 FaceOfBuddha--;
153 allowedAdditional *= 1.1;
154 noValueUtil = utilitySpace.getUtility(myMinBid);
155 if (FaceOfBuddha == 0) {
156 stopNr /= 2;
157 seedNr /= 2;
158 noValueUtil = (utilitySpace.getUtility(myMinBid)
159 + utilitySpace.getUtility(oppMaxBid)) / 2;
160 MINIMUM_BID_UTILITY = utilitySpace
161 .getUtility(oppMaxBid);
162 }
163 }
164 if (0 <= leftTime && leftTime < 2 && myMinBid != null) {
165 double diffMyOpp = utilitySpace.getUtility(myMinBid)
166 - utilitySpace.getUtility(oppMaxBid);
167 if (diffMyOpp <= 0.25
168 || utilitySpace.getUtility(myMinBid)
169 / 2 <= utilitySpace
170 .getUtility(oppMaxBid)) {
171 myBidManager(oppMaxBid);
172 if (utilitySpace
173 .getReservationValue() >= utilitySpace
174 .getUtility(oppMaxBid))
175 return new EndNegotiation(getAgentID());
176 return new Offer(getAgentID(), new Bid(oppMaxBid));
177 }
178 }
179 Bid nextBid = getHillClimbBid(stopNr);
180 for (int i = 0; i < seedNr; i++) {
181 Bid tmpBid = getHillClimbBid(stopNr);
182 if (evaluateBid(nextBid) < evaluateBid(tmpBid)) {
183 nextBid = new Bid(tmpBid);
184 }
185 }
186 if (utilitySpace.getUtility(nextBid) < MINIMUM_BID_UTILITY)
187 nextBid = new Bid(myHistory.getRandom().getBid());
188 if (utilitySpace.getUtility(nextBid) < utilitySpace
189 .getUtility(oppMaxBid))
190 nextBid = new Bid(oppMaxBid);
191 if (myBidPool.contains(nextBid.toString()))
192 nextBid = new Bid(myHistory.getRandom().getBid());
193 myBidManager(nextBid);
194 refreshHistory();
195 action = new Offer(getAgentID(), nextBid);
196 if (utilitySpace.getReservationValue() >= utilitySpace
197 .getUtility(myMinBid))
198 action = new EndNegotiation(getAgentID());
199 }
200 }
201 } catch (Exception e) {
202 action = new Accept(getAgentID(),
203 ((ActionWithBid) actionOfPartner).getBid());
204 }
205
206 return action;
207 }
208
209 private void refreshHistory() throws Exception {
210 if (now > coefRefresh * termRefresh
211 || (1 <= coefRefresh * termRefresh
212 && coefRefresh * termRefresh < 1.2 && now > 0.9)
213 || opponentHistory.size() + preRefresh > 1
214 + Math.pow(logTCN, 2)) {
215 List<BidDetails> oppList = new ArrayList<BidDetails>(opponentHistory
216 .filterBetweenUtility(oppAverage, 1.0).getHistory());
217 List<BidDetails> myList = new ArrayList<BidDetails>(myHistory
218 .filterBetweenUtility(0.0, myAverage).getHistory());
219 opponentBidElementHistory = new BidElementHistory();
220 opponentPairBidElementHistory = new PairBidElementHistory();
221 myBidElementHistory = new BidElementHistory();
222 myPairBidElementHistory = new PairBidElementHistory();
223 opponentHistory = new BidHistory();
224 myHistory = new BidHistory();
225 HashSet<String> tmpOppBidPool = new HashSet<String>(
226 opponentBidPool);
227 HashSet<String> tmpMyBidPool = new HashSet<String>(myBidPool);
228 opponentBidPool = new HashSet<String>();
229 myBidPool = new HashSet<String>();
230 for (BidDetails bd : myList) {
231 now = bd.getTime();
232 myBidManager(bd.getBid());
233 }
234 for (BidDetails bd : oppList) {
235 now = bd.getTime();
236 opponentBidManager(bd.getBid());
237 }
238 for (String str : tmpOppBidPool) {
239 if (!opponentBidPool.contains(str))
240 opponentBidPool.add(str);
241 }
242 for (String str : tmpMyBidPool) {
243 if (!myBidPool.contains(str))
244 myBidPool.add(str);
245 }
246 coefRefresh++;
247 preRefresh = opponentHistory.size();
248 }
249 }
250
251 private void opponentBidManager(Bid oppBid) throws Exception {
252 double sum;
253
254 BidElementIterator bei = new BidElementIterator(oppBid);
255 PairBidElementIterator pbei = new PairBidElementIterator(oppBid);
256 if (!opponentBidPool.contains(oppBid.toString())) {
257 opponentBidPool.add(oppBid.toString());
258 if (opponentHistory.isEmpty()) {
259 opponentHistory.add(new BidDetails(oppBid,
260 utilitySpace.getUtility(oppBid), now));
261 while (bei.hasNext())
262 opponentBidElementHistory
263 .add(new BidElementDetails(bei.next(), now));
264 while (pbei.hasNext())
265 opponentPairBidElementHistory
266 .add(new PairBidElementDetails(pbei.next(), now));
267 } else {
268 oppAverage = 0.0;
269 for (BidDetails bd : opponentHistory.getHistory()) {
270 oppAverage += bd.getMyUndiscountedUtil();
271 }
272 oppAverage /= opponentHistory.size();
273
274 opponentHistory.add(new BidDetails(oppBid,
275 utilitySpace.getUtility(oppBid), now));
276 while (bei.hasNext())
277 opponentBidElementHistory
278 .add(new BidElementDetails(bei.next(), now));
279 while (pbei.hasNext())
280 opponentPairBidElementHistory
281 .add(new PairBidElementDetails(pbei.next(), now));
282 }
283 }
284
285 if (basedBE > 0 && basedPBE > 0
286 && oppAverage <= utilitySpace.getUtility(oppBid)) {
287 double additional = 0.0;
288 bei = new BidElementIterator(oppBid);
289 sum = 0;
290 while (bei.hasNext())
291 sum += myBidElementHistory
292 .getWeightedAppearanceCount(bei.next());
293 additional += (sum / basedBE);
294 pbei = new PairBidElementIterator(oppBid);
295 sum = 0;
296 while (pbei.hasNext())
297 sum += myPairBidElementHistory
298 .getWeightedAppearanceCount(pbei.next());
299 additional += (sum / basedPBE);
300 additional *= (Math.pow((now / df), 2) > 1 ? 1
301 : Math.pow((now / df), 2));
302 if (2 * additional > allowedAdditional) {
303 allowedAdditional = 2 * additional;
304 }
305 }
306
307 if (oppMaxBid == null) {
308 oppMaxBid = new Bid(oppBid);
309 } else if (utilitySpace.getUtility(oppMaxBid) < utilitySpace
310 .getUtility(oppBid)) {
311 oppMaxBid = new Bid(oppBid);
312 }
313 bei = new BidElementIterator(oppMaxBid);
314 sum = 0;
315 while (bei.hasNext())
316 sum += opponentBidElementHistory
317 .getWeightedAppearanceCount(bei.next());
318 baseBE = sum;
319 pbei = new PairBidElementIterator(oppMaxBid);
320 sum = 0;
321 while (pbei.hasNext())
322 sum += opponentPairBidElementHistory
323 .getWeightedAppearanceCount(pbei.next());
324 basePBE = sum;
325 }
326
327 private void myBidManager(Bid myBid) throws Exception {
328 BidElementIterator bei = new BidElementIterator(myBid);
329 PairBidElementIterator pbei = new PairBidElementIterator(myBid);
330
331 if (!myBidPool.contains(myBid.toString())) {
332 myBidPool.add(myBid.toString());
333 if (myHistory.isEmpty()) {
334 myHistory.add(new BidDetails(myBid,
335 utilitySpace.getUtility(myBid), now));
336 while (bei.hasNext())
337 myBidElementHistory
338 .add(new BidElementDetails(bei.next(), now));
339 while (pbei.hasNext())
340 myPairBidElementHistory
341 .add(new PairBidElementDetails(pbei.next(), now));
342 } else {
343 myAverage = 0.0;
344 for (BidDetails bd : myHistory.getHistory()) {
345 myAverage += bd.getMyUndiscountedUtil();
346 }
347 myAverage /= myHistory.size();
348
349 myHistory.add(new BidDetails(myBid,
350 utilitySpace.getUtility(myBid), now));
351 while (bei.hasNext())
352 myBidElementHistory
353 .add(new BidElementDetails(bei.next(), now));
354 while (pbei.hasNext())
355 myPairBidElementHistory
356 .add(new PairBidElementDetails(pbei.next(), now));
357 }
358 }
359
360 if (utilitySpace.getUtility(myMinBid) > utilitySpace
361 .getUtility(myBid)) {
362 myMinBid = new Bid(myBid);
363 }
364
365 basedBE = basedPBE = 0;
366 for (BidDetails bd : myHistory.getHistory()) {
367 bei = new BidElementIterator(bd.getBid());
368 while (bei.hasNext())
369 basedBE += myBidElementHistory
370 .getWeightedAppearanceCount(bei.next());
371 pbei = new PairBidElementIterator(bd.getBid());
372 while (pbei.hasNext())
373 basedPBE += myPairBidElementHistory
374 .getWeightedAppearanceCount(pbei.next());
375 }
376 basedBE /= myHistory.size();
377 basedPBE /= myHistory.size();
378 }
379
380 private void updateLeftTime() {
381 if (timeQue.size() < linkedNum) {
382 timeQue.offer(now - preTime);
383 preTime = now;
384 } else {
385 timeQue.poll();
386 timeQue.offer(now - preTime);
387 preTime = now;
388 double avgTime = 0.0;
389 for (int i = 1; i < linkedNum; i++) {
390 avgTime += timeQue.get(i);
391 }
392 avgTime /= (linkedNum - 1);
393 leftTime = (int) ((1 - now) / avgTime);
394 totalTime = (int) (1 / avgTime);
395 }
396 }
397
398 private boolean isAccept(Bid offeredBid) throws Exception {
399 double offeredUtil = utilitySpace.getUtility(offeredBid);
400
401 if (offeredUtil >= utilitySpace.getUtility(myMinBid) * 0.999)
402 return true;
403
404 return false;
405 }
406
407 private Bid getHillClimbBid(int stopNr) throws Exception {
408 Bid bid = getRandomBid();
409 int c = 0;
410 double maxEval = evaluateBid(bid);
411
412 while (c++ < stopNr && timeline.getTime() - now < 0.01
413 && timeline.getTime() < 1.0) {
414 double r = Math.random();
415 int changeNum = 1;
416 for (int i = issueNrs.size(); i >= 1; i--) {
417 if (r < 1.0 / (i * i)) {
418 changeNum = i;
419 break;
420 }
421 }
422
423 Bid nextBid = nextBid(bid, changeNum);
424
425 double eval = evaluateBid(nextBid);
426 if (eval > maxEval) {
427 maxEval = eval;
428 bid = new Bid(nextBid);
429 }
430 }
431
432 return bid;
433 }
434
435 private Bid nextBid(Bid bid, int changeNum) {
436 List<Integer> changeIssueNrs = new ArrayList<Integer>();
437 Random randomnr = new Random();
438 Bid nextBid = new Bid(bid);
439
440 for (int i = 0; i < changeNum; i++) {
441 changeIssueNrs.add(issueNrs.get(randomnr.nextInt(issueNrs.size())));
442 }
443
444 Domain domain = utilitySpace.getDomain();
445 for (Integer issueNumber : changeIssueNrs) {
446 int issueN = 0;
447 int i = 0;
448 for (Issue issue : domain.getIssues()) {
449 if (issueNumber == issue.getNumber()) {
450 issueN = i;
451 break;
452 }
453 i++;
454 }
455 switch (domain.getIssues().get(issueN).getType()) {
456 case DISCRETE:
457 IssueDiscrete lIssueDiscrete = (IssueDiscrete) domain
458 .getIssues().get(issueN);
459 int optionIndex = randomnr
460 .nextInt(lIssueDiscrete.getNumberOfValues());
461 nextBid = nextBid.putValue(issueNumber,
462 lIssueDiscrete.getValue(optionIndex));
463 break;
464 case REAL:
465 IssueReal lIssueReal = (IssueReal) domain.getIssues()
466 .get(issueN);
467 int optionInd = randomnr.nextInt(
468 lIssueReal.getNumberOfDiscretizationSteps() - 1);
469 nextBid = nextBid.putValue(issueNumber,
470 new ValueReal(lIssueReal.getLowerBound() + (lIssueReal
471 .getUpperBound() - lIssueReal.getLowerBound())
472 * (optionInd) / (lIssueReal
473 .getNumberOfDiscretizationSteps())));
474 break;
475 case INTEGER:
476 IssueInteger lIssueInteger = (IssueInteger) domain.getIssues()
477 .get(issueN);
478 int optionIndex2 = lIssueInteger.getLowerBound()
479 + randomnr.nextInt(lIssueInteger.getUpperBound()
480 - lIssueInteger.getLowerBound());
481 nextBid = nextBid.putValue(issueNumber,
482 new ValueInteger(optionIndex2));
483 break;
484 default:
485 }
486 }
487
488 return nextBid;
489 }
490
491 private double evaluateBid(Bid bid) throws Exception {
492 double eval = 0.0;
493 eval = utilitySpace.getUtility(bid);
494
495 if (baseBE > 0 && basePBE > 0) {
496 double additional = 0.0;
497 BidElementIterator bei = new BidElementIterator(bid);
498 double sum = 0;
499 while (bei.hasNext())
500 sum += opponentBidElementHistory
501 .getWeightedAppearanceCount(bei.next());
502 additional += (sum / baseBE);
503 PairBidElementIterator pbei = new PairBidElementIterator(bid);
504 sum = 0;
505 while (pbei.hasNext())
506 sum += opponentPairBidElementHistory
507 .getWeightedAppearanceCount(pbei.next());
508 additional += (sum / basePBE);
509 additional *= ((now / df) > 1 ? 1 : (now / df));
510 if (additional > (1 + (double) (3 - FaceOfBuddha) / 8) * eval)
511 additional = (1 + (double) (3 - FaceOfBuddha) / 8) * eval;
512 if (MINIMUM_BID_UTILITY <= eval && eval < noValueUtil)
513 eval += (allowedAdditional < additional ? allowedAdditional
514 : additional);
515 }
516
517 if (timeBonus && !myBidPool.contains(bid.toString())
518 && myBidPool.size() <= opponentBidPool.size())
519 eval += now;
520
521 return eval;
522 }
523
524 private Bid getRandomBid() throws Exception {
525 HashMap<Integer, Value> values = new HashMap<Integer, Value>();
526 List<Issue> issues = utilitySpace.getDomain().getIssues();
527 Random randomnr = new Random();
528
529 for (Issue lIssue : issues) {
530 switch (lIssue.getType()) {
531 case DISCRETE:
532 IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
533 int optionIndex = randomnr
534 .nextInt(lIssueDiscrete.getNumberOfValues());
535 values.put(lIssue.getNumber(),
536 lIssueDiscrete.getValue(optionIndex));
537 break;
538 case REAL:
539 IssueReal lIssueReal = (IssueReal) lIssue;
540 int optionInd = randomnr.nextInt(
541 lIssueReal.getNumberOfDiscretizationSteps() - 1);
542 values.put(lIssueReal.getNumber(),
543 new ValueReal(lIssueReal.getLowerBound() + (lIssueReal
544 .getUpperBound() - lIssueReal.getLowerBound())
545 * (optionInd) / (lIssueReal
546 .getNumberOfDiscretizationSteps())));
547 break;
548 case INTEGER:
549 IssueInteger lIssueInteger = (IssueInteger) lIssue;
550 int optionIndex2 = lIssueInteger.getLowerBound()
551 + randomnr.nextInt(lIssueInteger.getUpperBound()
552 - lIssueInteger.getLowerBound());
553 values.put(lIssueInteger.getNumber(),
554 new ValueInteger(optionIndex2));
555 break;
556 default:
557 throw new Exception(
558 "issue type " + lIssue.getType() + " not supported.");
559 }
560 }
561 return new Bid(utilitySpace.getDomain(), values);
562 }
563
564 private long getTotalCombinationNumber() {
565 long c = 1;
566 try {
567 List<Issue> issues = utilitySpace.getDomain().getIssues();
568 for (Issue lIssue : issues) {
569 switch (lIssue.getType()) {
570 case DISCRETE:
571 IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
572 c *= lIssueDiscrete.getValues().size();
573 break;
574 case REAL:
575 IssueReal lIssueReal = (IssueReal) lIssue;
576 c *= lIssueReal.getNumberOfDiscretizationSteps();
577 break;
578 case INTEGER:
579 IssueInteger lIssueInteger = (IssueInteger) lIssue;
580 c *= (lIssueInteger.getUpperBound()
581 - lIssueInteger.getLowerBound() + 1);
582 break;
583 default:
584 break;
585 }
586 }
587 return c;
588 } catch (Exception e) {
589 System.out.println(e.getMessage());
590 }
591 return -1;
592 }
593
594 @Override
595 public String getDescription() {
596 return "ANAC2014 compatible with non-linear utility spaces";
597 }
598}
Note: See TracBrowser for help on using the repository browser.