source: src/main/java/agents/anac/y2012/AgentMR/AgentMR.java@ 346

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

Initial import : Genius 9.0.0

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