source: src/main/java/agents/anac/y2013/MetaAgent/portfolio/AgentMR/AgentMR.java

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

Initial import : Genius 9.0.0

File size: 14.3 KB
Line 
1package agents.anac.y2013.MetaAgent.portfolio.AgentMR;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.Comparator;
6import java.util.HashMap;
7import java.util.List;
8import java.util.Random;
9
10import genius.core.Agent;
11import genius.core.Bid;
12import genius.core.actions.Accept;
13import genius.core.actions.Action;
14import genius.core.actions.ActionWithBid;
15import genius.core.actions.Offer;
16import genius.core.issue.Issue;
17import genius.core.issue.IssueDiscrete;
18import genius.core.issue.IssueInteger;
19import genius.core.issue.IssueReal;
20import genius.core.issue.Value;
21import genius.core.issue.ValueDiscrete;
22import genius.core.issue.ValueInteger;
23import genius.core.issue.ValueReal;
24
25/**
26 * @author W.Pasman Some improvements over the standard SimpleAgent.
27 */
28public class AgentMR extends Agent {
29
30 private boolean EQUIVALENCE_TEST = true;
31 private Random random100;
32
33 private Action actionOfPartner = null;
34 private ArrayList<Bid> bidRunk = new ArrayList<Bid>();
35 private ArrayList<Double> observationUtility = new ArrayList<Double>();
36 private HashMap<Bid, Double> bidTables = new HashMap<Bid, Double>();
37 private static final double MINIMUM_ACCEPT_P = 0.965;
38 private static boolean firstOffer;
39 private static boolean forecastTime = true;
40 private static boolean discountFactor;
41 private static double minimumBidUtility;
42 private static double minimumOffereDutil;
43 private static Bid offereMaxBid = null;
44 private static double offereMaxUtility;
45 private static double firstOffereUtility;
46 private int currentBidNumber = 0;
47 private int lastBidNumber = 1;
48 private double sigmoidGain;
49 private double sigmoidX;
50 private double reservation = 0.0;
51 private double alpha;
52 private double percent;
53 private double p = 0.90;
54
55 /**
56 * init is called when a next session starts with the same opponent.
57 */
58 public void init() {
59 try {
60 firstOffer = true;
61 getDiscountFactor();
62 getReservationFactor();
63 updateMinimumBidUtility(0);
64 // System.out.println("Original minimumBidUtility1: " +
65 // minimumBidUtility);
66 Bid b = utilitySpace.getMaxUtilityBid();
67 bidTables.put(b, getUtility(b));
68 bidRunk.add(b);
69 if (discountFactor) {
70 sigmoidGain = -3;
71 percent = 0.55;
72 } else {
73 sigmoidGain = -5;
74 percent = 0.70;
75 }
76 if (EQUIVALENCE_TEST) {
77 random100 = new Random(100);
78 } else {
79 random100 = new Random();
80 }
81 } catch (Exception e) {
82 e.printStackTrace();
83 }
84 }
85
86 @Override
87 public String getVersion() {
88 return "1.2";
89 }
90
91 @Override
92 public String getName() {
93 return "AgentMR";
94 }
95
96 public void ReceiveMessage(Action opponentAction) {
97 actionOfPartner = opponentAction;
98 }
99
100 public Action chooseAction() {
101 Action action = null;
102
103 try {
104 if (actionOfPartner == null) {
105 action = new Offer(getAgentID(),
106 utilitySpace.getMaxUtilityBid());
107 }
108 if (actionOfPartner instanceof Offer) {
109 Bid partnerBid = ((Offer) actionOfPartner).getBid();
110 // System.out.println("Original partnerBid: " + partnerBid);
111
112 // get current time
113 double time = timeline.getTime();
114
115 double offeredutil;
116 if (discountFactor) {
117 offeredutil = getUtility(partnerBid)
118 * (1 / Math.pow(utilitySpace.getDiscountFactor(),
119 time));
120 } else {
121 offeredutil = getUtility(partnerBid);
122 }
123
124 if (firstOffer) {
125 offereMaxBid = partnerBid;
126 offereMaxUtility = offeredutil;
127 firstOffereUtility = offeredutil;
128 // System.out.println("Original firstOffereUtility: " +
129 // firstOffereUtility);
130
131 observationUtility.add(offeredutil); // addObservation
132 if (offeredutil > 0.5) {
133 p = 0.90;
134 } else {
135 p = 0.80;
136 }
137 firstOffer = !firstOffer;
138 }
139
140 updateMinimumBidUtility(time);
141
142 if (offeredutil > offereMaxUtility) {
143 offereMaxBid = partnerBid;
144 offereMaxUtility = offeredutil;
145 // addObservation
146 observationUtility.add(offeredutil);
147 if ((time > 0.5) && !discountFactor) {
148 newupdateSigmoidFunction();
149 }
150 }
151
152 // forecasting
153 if ((time > 0.5) && forecastTime) {
154 updateSigmoidFunction();
155 forecastTime = !forecastTime;
156 }
157
158 double P = Paccept(offeredutil, time);
159 // System.out.println("Orig condition1: " + (P >
160 // MINIMUM_ACCEPT_P));
161 // System.out.println("Orig condition2: " + (offeredutil >
162 // minimumBidUtility));
163 // System.out.println("Orig condition3: " +
164 // bidRunk.contains(partnerBid));
165
166 if ((P > MINIMUM_ACCEPT_P) || (offeredutil > minimumBidUtility)
167 || bidRunk.contains(partnerBid)) {
168 action = new Accept(getAgentID(), partnerBid);
169 } else {
170 if (offereMaxUtility > minimumBidUtility) {
171 // System.out.println("Origional NextBid1: " +
172 // offereMaxBid);
173
174 action = new Offer(getAgentID(), offereMaxBid);
175 } else if (time > 0.985) {
176 if (offereMaxUtility > reservation) {
177 // System.out.println("Origional NextBid2: " +
178 // offereMaxBid);
179
180 action = new Offer(getAgentID(), offereMaxBid);
181 } else {
182 action = new Offer(getAgentID(),
183 bidRunk.get(bidRunk.size() - lastBidNumber));
184 // System.out.println("Origional NextBid3: " +
185 // bidRunk.get(bidRunk.size() - lastBidNumber));
186
187 lastBidNumber++;
188 }
189 } else {
190
191 // System.out.println("Original offeredutil: " +
192 // offeredutil);
193 // System.out.println("Original getMinimumOffereDutil: "
194 // + minimumOffereDutil);
195
196 if (offeredutil > minimumOffereDutil) {
197 HashMap<Bid, Double> getBids = getBidTable(1);
198
199 if (getBids.size() >= 1) {
200 currentBidNumber = 0;
201 bidRunk.clear();
202 bidTables = getBids;
203 sortBid(getBids); // Sort BidTable
204 } else {
205 getBids = getBidTable(2);
206 if (getBids.size() >= 1) {
207 sortBid(getBids); // Sort BidTable
208 Bid maxBid = getMaxBidUtility(getBids);
209 currentBidNumber = bidRunk.indexOf(maxBid);
210 }
211 }
212 action = new Offer(getAgentID(),
213 bidRunk.get(currentBidNumber));
214 // System.out.println("Origional NextBid4: " +
215 // bidRunk.get(currentBidNumber));
216
217 // System.out.println("Original Condition: " +
218 // (currentBidNumber + 1 < bidRunk.size()));
219
220 if (currentBidNumber + 1 < bidRunk.size()) {
221 // System.out.println("Original currentBidNumberChange1");
222
223 currentBidNumber++;
224 }
225 } else {
226 HashMap<Bid, Double> getBids = getBidTable(2);
227 // System.out.println("Original getBids.size(): " +
228 // getBids.size());
229
230 if (getBids.size() >= 1) {
231 sortBid(getBids); // Sort BidTable
232 Bid maxBid = getMaxBidUtility(getBids);
233 // System.out.println("Original maxBid: " +
234 // maxBid);
235
236 // System.out.println("Change2 currentBidNumber");
237
238 currentBidNumber = bidRunk.indexOf(maxBid);
239 // System.out.println("Original maxBid: " +
240 // maxBid);
241
242 }
243 // System.out.println("Origional currentBidNumber: "
244 // +currentBidNumber);
245
246 action = new Offer(getAgentID(),
247 bidRunk.get(currentBidNumber));
248 // System.out.println("Origional NextBid5: " +
249 // bidRunk.get(currentBidNumber));
250 if (currentBidNumber + 1 < bidRunk.size()) {
251 // System.out.println("Original currentBidNumberChange2");
252
253 currentBidNumber++;
254 } else {
255 currentBidNumber = 0;
256 }
257 }
258 }
259 }
260 }
261 } catch (Exception e) {
262 e.printStackTrace();
263 action = new Accept(getAgentID(),
264 ((ActionWithBid) actionOfPartner).getBid()); // best guess
265 // if things
266 // go wrong.
267 }
268 return action;
269 }
270
271 private void getReservationFactor() {
272 if (utilitySpace.getReservationValue() != null) {
273 reservation = utilitySpace.getReservationValue();
274 }
275 }
276
277 private void getDiscountFactor() {
278 if (utilitySpace.getDiscountFactor() > 0.0) {
279 discountFactor = true;
280 } else
281 discountFactor = false;
282 }
283
284 // forecasting
285 private void newupdateSigmoidFunction() {
286 double latestObservation = observationUtility.get(observationUtility
287 .size() - 1);
288 double concessionPercent = Math.abs(latestObservation
289 - firstOffereUtility)
290 / (1.0 - firstOffereUtility);
291 double modPercent = Math.abs(minimumOffereDutil - firstOffereUtility)
292 / (1.0 - firstOffereUtility);
293
294 if (modPercent < concessionPercent) {
295 percent = concessionPercent;
296 }
297 }
298
299 private void updateSigmoidFunction() {
300 int observationSize = observationUtility.size();
301 double latestObservation = observationUtility.get(observationSize - 1);
302 double concessionPercent = Math.abs(latestObservation
303 - firstOffereUtility)
304 / (1.0 - firstOffereUtility);
305
306 if (discountFactor) {
307 if ((concessionPercent < 0.20) || (observationSize < 3)) {
308 percent = 0.35;
309 sigmoidGain = -2;
310 } else {
311 percent = 0.45;
312 }
313 } else {
314 if ((concessionPercent < 0.20) || (observationSize < 3)) {
315 percent = 0.50;
316 sigmoidGain = -4;
317 } else if (concessionPercent > 0.60) { // Agent
318 percent = 0.80;
319 sigmoidGain = -6;
320 } else {
321 percent = 0.60;
322 }
323 }
324 }
325
326 private Bid getMaxBidUtility(HashMap<Bid, Double> bidTable) {
327 Double maxBidUtility = 0.0;
328 Bid maxBid = null;
329 for (Bid b : bidTable.keySet()) {
330 if (getUtility(b) > maxBidUtility) {
331 maxBidUtility = getUtility(b);
332 maxBid = b;
333 }
334 }
335 return maxBid;
336 }
337
338 private void updateMinimumBidUtility(double time) {
339 alpha = (1.0 - firstOffereUtility) * percent;
340 // System.out.println("Original percent: " + percent);
341 // System.out.println("Original alpha: " + alpha);
342 // System.out.println("Original firstOffereUtility: " +
343 // firstOffereUtility);
344
345 double mbuInfimum = firstOffereUtility + alpha;
346 // System.out.println("Original firstOffereUtility: " +
347 // firstOffereUtility);
348
349 if (mbuInfimum >= 1.0) {
350 mbuInfimum = 0.999;
351 } else if (mbuInfimum <= 0.70) {
352 mbuInfimum = 0.70;
353 }
354 sigmoidX = 1 - ((1 / sigmoidGain) * Math.log(mbuInfimum
355 / (1 - mbuInfimum)));
356
357 minimumBidUtility = 1 - (1 / (1 + Math.exp(sigmoidGain
358 * (time - sigmoidX))));
359 // System.out.println("Original sigmoidX: " + sigmoidX);
360 // System.out.println("Original sigmoidGain: " + sigmoidGain);
361
362 if (minimumBidUtility < reservation) {
363 minimumBidUtility = reservation;
364 }
365
366 minimumOffereDutil = minimumBidUtility * p;
367
368 }
369
370 /**
371 * BidTable
372 *
373 * @param bidTable
374 */
375 private void sortBid(final HashMap<Bid, Double> getBids) {
376
377 for (Bid bid : getBids.keySet()) {
378 bidTables.put(bid, getUtility(bid));
379 bidRunk.add(bid); // Add bidRunk
380 }
381 if (!EQUIVALENCE_TEST) {
382
383 Collections.sort(bidRunk, new Comparator<Bid>() {
384 @Override
385 public int compare(Bid o1, Bid o2) {
386 return (int) Math.ceil(-(bidTables.get(o1) - bidTables
387 .get(o2)));
388 }
389 });
390
391 }
392 }
393
394 private Bid clone(Bid source) throws Exception {
395 HashMap<Integer, Value> hash = new HashMap<Integer, Value>();
396 for (Issue i : utilitySpace.getDomain().getIssues()) {
397 hash.put(i.getNumber(), source.getValue(i.getNumber()));
398 }
399 return new Bid(utilitySpace.getDomain(), hash);
400 }
401
402 /**
403 * Bid
404 *
405 * @param maxBid
406 * @return hashmap of bid, utility
407 * @throws Exception
408 */
409 private HashMap<Bid, Double> getBidTable(int flag) throws Exception {
410 HashMap<Bid, Double> getBids = new HashMap<Bid, Double>();
411
412 // Random randomnr = new Random();
413 List<Issue> issues = utilitySpace.getDomain().getIssues();
414 Bid standardBid = null;
415
416 for (Issue lIssue : issues) {
417 switch (lIssue.getType()) {
418 case DISCRETE:
419 IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
420 for (ValueDiscrete value : lIssueDiscrete.getValues()) {
421 if (flag == 0) {
422 standardBid = utilitySpace.getMaxUtilityBid();
423 } else if (flag == 1) {
424 standardBid = ((Offer) actionOfPartner).getBid();
425 } else {
426 standardBid = bidRunk.get(currentBidNumber);
427 }
428 standardBid = clone(standardBid);
429 standardBid = standardBid.putValue(lIssue.getNumber(),
430 value);
431 double utility = getUtility(standardBid);
432 // System.out.println("Original minimumBidUtility: " +
433 // minimumBidUtility);
434
435 if ((utility > minimumBidUtility)
436 && (!bidRunk.contains(standardBid))) {
437 getBids.put(standardBid, utility);
438 }
439 }
440 break;
441 case REAL:
442 IssueReal lIssueReal = (IssueReal) lIssue;
443 int optionInd = random100.nextInt(lIssueReal
444 .getNumberOfDiscretizationSteps() - 1);
445 Value pValue = new ValueReal(
446 lIssueReal.getLowerBound()
447 + (lIssueReal.getUpperBound() - lIssueReal
448 .getLowerBound())
449 * (double) (optionInd)
450 / (double) (lIssueReal
451 .getNumberOfDiscretizationSteps()));
452 standardBid = standardBid.putValue(lIssueReal.getNumber(),
453 pValue);
454 double utility = getUtility(standardBid);
455 getBids.put(standardBid, utility);
456 break;
457 case INTEGER:
458 IssueInteger lIssueInteger = (IssueInteger) lIssue;
459 int optionIndex2 = lIssueInteger.getLowerBound()
460 + random100.nextInt(lIssueInteger.getUpperBound()
461 - lIssueInteger.getLowerBound());
462 Value pValue2 = new ValueInteger(optionIndex2);
463 standardBid = standardBid.putValue(lIssueInteger.getNumber(),
464 pValue2);
465 double utility2 = getUtility(standardBid);
466 getBids.put(standardBid, utility2);
467 break;
468 default:
469 throw new Exception("issue type " + lIssue.getType()
470 + " not supported by AgentMR");
471 }
472 }
473
474 return getBids;
475 }
476
477 /**
478 * This function determines the accept probability for an offer. At t=0 it
479 * will prefer high-utility offers. As t gets closer to 1, it will accept
480 * lower utility offers with increasing probability. it will never accept
481 * offers with utility 0.
482 *
483 * @param u
484 * is the utility
485 * @param t
486 * is the time as fraction of the total available time (t=0 at
487 * start, and t=1 at end time)
488 * @return the probability of an accept at time t
489 * @throws Exception
490 * if you use wrong values for u or t.
491 *
492 */
493 double Paccept(double u, double t1) throws Exception {
494 double t = t1 * t1 * t1; // steeper increase when deadline approaches.
495 if (u < 0 || u > 1.05)
496 throw new Exception("utility " + u + " outside [0,1]");
497 // normalization may be slightly off, therefore we have a broad boundary
498 // up to 1.05
499 if (t < 0 || t > 1)
500 throw new Exception("time " + t + " outside [0,1]");
501 if (u > 1.)
502 u = 1;
503 if (t == 0.5)
504 return u;
505 return (u - 2. * u * t + 2. * (-1. + t + Math.sqrt(sq(-1. + t) + u
506 * (-1. + 2 * t))))
507 / (-1. + 2 * t);
508 }
509
510 double sq(double x) {
511 return x * x;
512 }
513}
Note: See TracBrowser for help on using the repository browser.