source: src/main/java/agents/anac/y2012/OMACagent/OMACagent.java

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

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 11.4 KB
Line 
1package agents.anac.y2012.OMACagent;
2
3import java.util.HashMap;
4import java.util.List;
5import java.util.Random;
6
7import genius.core.Agent;
8import genius.core.Bid;
9import genius.core.SupportedNegotiationSetting;
10import genius.core.actions.Accept;
11import genius.core.actions.Action;
12import genius.core.actions.ActionWithBid;
13import genius.core.actions.EndNegotiation;
14import genius.core.actions.Offer;
15import genius.core.issue.Issue;
16import genius.core.issue.IssueDiscrete;
17import genius.core.issue.IssueInteger;
18import genius.core.issue.IssueReal;
19import genius.core.issue.Value;
20import genius.core.issue.ValueInteger;
21import genius.core.issue.ValueReal;
22import genius.core.utility.AdditiveUtilitySpace;
23
24/**
25 * @author Siqi Chen/Maastricht University
26 *
27 */
28public class OMACagent extends Agent {
29 private Action actionOfPartner = null;
30 private double MINIMUM_UTILITY = 0.59; // 0.618
31 private double resU = 0.0;
32 private double EU = 0.95; // a threshold of expected utility for a session,
33 // in use now
34 private double est_t = 0;
35 private double est_u = 0;
36 private TimeBidHistory mBidHistory; // new
37 public int intervals = 100;
38 public double timeInt = 1.0 / intervals;
39
40 public double discount = 1.0;
41 private Bid maxBid = null; // the maxBid which can be made by itself
42 private double maxBidU = 0.0; // the uti of the bid above
43 public double cTime = 0.0;
44 private int tCount = 0;
45 private double nextUti = 0.96;
46 public int numberOfIssues = 0;
47 public double discountThreshold = 0.845D;
48 public int lenA = 0;
49
50 public double exma;
51 public double est_mu;
52 public double est_mt;
53 public boolean debug = false; // debug mode
54 public boolean detail = false; // record counter-offer or not
55 private double maxTime = 180.0;
56 private Random randomnr;
57 private final boolean TEST_EQUIVALENCE = true;
58
59 @Override
60 public void init() {
61 if (TEST_EQUIVALENCE) {
62 randomnr = new Random(100);
63 } else {
64 randomnr = new Random();
65 }
66 if (utilitySpace.getReservationValue() != null) {
67 resU = utilitySpace.getReservationValue();
68 if (MINIMUM_UTILITY < resU)
69 MINIMUM_UTILITY = resU * 1.06;
70 }
71
72 if (utilitySpace.getDiscountFactor() <= 1D
73 && utilitySpace.getDiscountFactor() > 0D)
74 discount = utilitySpace.getDiscountFactor();
75
76 numberOfIssues = utilitySpace.getDomain().getIssues().size();
77
78 try {
79 maxBid = utilitySpace.getMaxUtilityBid();
80 maxBidU = utilitySpace.getUtility(maxBid);
81 EU = EU * maxBidU;
82 } catch (Exception e) {
83 // System.out.println("Errors in ini process!");
84 }
85
86 mBidHistory = new TimeBidHistory(
87 (AdditiveUtilitySpace) this.utilitySpace, discount);
88 }
89
90 @Override
91 public String getVersion() {
92 return "1.06";
93 }
94
95 @Override
96 public String getName() {
97 return "OMAC_sp2012b";
98 }
99
100 @Override
101 public void ReceiveMessage(Action opponentAction) {
102 actionOfPartner = opponentAction;
103 }
104
105 @Override
106 public Action chooseAction() {
107 Action action = null;
108
109 try {
110 if (actionOfPartner == null) {
111 action = chooseBidAction();
112 }
113
114 if (actionOfPartner instanceof Offer) {
115 cTime = timeline.getTime();
116 Bid partnerBid = ((Offer) actionOfPartner).getBid();
117 double offeredUtilFromOpponent = getUtility(partnerBid);
118 mBidHistory.addOpponentBidnTime(offeredUtilFromOpponent,
119 partnerBid, cTime);
120
121 if (discount < discountThreshold) {
122 if (mBidHistory.isInsideMyBids(partnerBid)) {
123 action = new Accept(getAgentID(), partnerBid);
124 return action;
125 }
126 } else if (cTime > 0.97) {
127 if (mBidHistory.isInsideMyBids(partnerBid)) {
128 action = new Accept(getAgentID(), partnerBid);
129 return action;
130 }
131 }
132
133 action = chooseBidAction();
134 Bid myBid = ((Offer) action).getBid();
135 double myOfferedUtil = getUtility(myBid);
136
137 // accept under certain conditions
138 if (isAcceptable(offeredUtilFromOpponent, myOfferedUtil, cTime,
139 partnerBid))
140 action = new Accept(getAgentID(), partnerBid);
141 }
142 } catch (Exception e) {
143
144 if (resU != 0) {
145 action = new EndNegotiation(getAgentID());
146 } else {
147 action = new Accept(getAgentID(),
148 ((ActionWithBid) actionOfPartner).getBid());
149 }
150 }
151
152 return action;
153 }
154
155 private boolean isAcceptable(double offeredUtilFromOpponent,
156 double myOfferedUtil, double time, Bid oppBid) throws Exception {
157
158 if (offeredUtilFromOpponent >= myOfferedUtil) {
159 return true;
160 }
161
162 return false;
163 }
164
165 private Action chooseBidAction() {
166 Bid nextBid = null;
167 try {
168 if (cTime <= 0.02) {
169 nextBid = maxBid;
170 } else {
171 nextBid = getFinalBid();
172 }
173 } catch (Exception e) {
174 }
175
176 if (nextBid == null) {
177 // return (new Accept(getAgentID())); //need to break off
178 // negotiation now
179 return (new EndNegotiation(getAgentID()));
180 }
181
182 mBidHistory.addMyBid(nextBid);
183
184 return (new Offer(getAgentID(), nextBid));
185 }
186
187 private Bid getFinalBid() {
188 Bid bid = null;
189 double upper = 1.01;
190 double lower = 0.99;
191 double splitFactor = 3.0;
192 double val = 0.0;
193 double dval = 0.0;
194 int delay = 75;
195 double laTime = 0D;
196 double adp = 1.2;
197
198 if (discount >= discountThreshold) {
199 if (cTime <= delay / 100.0) {
200 if (resU <= 0.3) {
201 return maxBid;
202 } else {
203 val = EU;
204 }
205
206 dval = val * Math.pow(discount, cTime);
207 bid = genRanBid(val * lower, val * upper);
208
209 return bid;
210 } else if (cTime > 0.01 * (tCount + delay)) {
211 nextUti = getNextUti();
212 tCount++;
213 }
214 } else {
215 if (cTime <= discount / splitFactor) {
216 if (resU <= 0.3) {
217 return maxBid;
218 } else {
219 val = EU;
220 }
221
222 dval = val * Math.pow(discount, cTime);
223 bid = genRanBid(val * (1.0 - 0.02), val * (1.0 + 0.02));
224
225 return bid;
226 } else if (cTime > 0.01 * (tCount
227 + (int) Math.floor(discount / splitFactor * 100))) {
228 nextUti = getNextUti();
229 tCount++;
230 }
231 }
232
233 if (nextUti == -3.0) {
234 if (resU <= 0.3) {
235 return maxBid;
236 } else {
237 val = EU;
238 }
239 } else if (nextUti == -2.0) {
240 val = est_mu
241 + (cTime - est_mt) * (est_u - est_mu) / (est_t - est_mt);
242 } else if (nextUti == -1.0) {
243 val = getOriU(cTime);
244 }
245
246 laTime = mBidHistory.getMCtime() * maxTime;
247 if (cTime * maxTime - laTime > 1.5 || cTime > 0.995) {
248 dval = val * Math.pow(discount, cTime);
249 bid = genRanBid(dval * lower, dval * upper);
250 } else {
251 if (val * lower * adp >= maxBidU) {
252 bid = maxBid;
253 } else {
254 dval = adp * val * Math.pow(discount, cTime);
255 bid = genRanBid(dval * lower, dval * upper);
256 }
257 }
258
259 if (bid == null)
260 bid = mBidHistory.getMyLastBid();
261
262 if (getUtility(mBidHistory.bestOppBid) >= getUtility(bid))
263 return mBidHistory.bestOppBid;
264
265 if (cTime > 0.999
266 && getUtility(mBidHistory.bestOppBid) > MINIMUM_UTILITY
267 * Math.pow(discount, cTime)) {
268 return mBidHistory.bestOppBid;
269 }
270
271 return bid;
272 }
273
274 private double getOriU(double t) {
275 double exp = 1D;
276 double maxUtil = maxBidU;
277 double minUtil = 0.69; // 0.7
278 if (minUtil < MINIMUM_UTILITY)
279 minUtil = MINIMUM_UTILITY * 1.05;
280
281 double e1 = 0.033;
282 double e2 = 0.04;
283 double time = t;
284 double tMax = maxBidU;
285 double tMin = MINIMUM_UTILITY * 1.05; // Math.pow(discount, 0.33);
286
287 if (discount >= discountThreshold) {
288 exp = minUtil + (1 - Math.pow(time, 1D / e1)) * (maxUtil - minUtil);
289 } else {
290 tMax = Math.pow(discount, 0.2);
291 exp = tMin + (1 - Math.pow(time, 1D / e2)) * (tMax - tMin);
292 }
293
294 return exp;
295 }
296
297 private double getPre() {
298 int len = 3;
299 double[] pmaxList = mBidHistory.getTimeBlockList();
300 int lenA = (int) Math.floor(cTime * 100); // don't count the current
301 // second as it is not
302 // complete
303 if (lenA < len) {
304 return -1.0;
305 }
306
307 double[] maxList = new double[lenA];
308 double[] ma = new double[lenA];
309 double[] res = new double[lenA];
310 double exma = 0.0;
311
312 for (int i = 0; i < lenA; i++) {
313 maxList[i] = pmaxList[i];
314 }
315
316 for (int i = 0; i < len - 1; i++) {
317 ma[i] = 0;
318 }
319
320 for (int i = len - 1; i < lenA; i++) {
321 ma[i] = (maxList[i] + maxList[i - 1] + maxList[i - 2]) / 3.0;
322 }
323
324 for (int i = 0; i < lenA; i++) {
325 res[i] = maxList[i] - ma[i];
326 }
327
328 exma = ma[lenA - 1] + avg(res)
329 + std(res) * (1.0 - Math.pow(maxList[lenA - 1], 4))
330 * (1.3 + 0.66 * Math.pow(1 - cTime * cTime, 0.4));
331
332 return exma;
333 }
334
335 public static double sum(double[] arr) {
336 double sum = 0.0;
337 int len = arr.length;
338 for (int i = 0; i < len; i++) {
339 sum += arr[i];
340 }
341 return sum;
342 }
343
344 public static double avg(double[] arr) {
345 double avg = 0.0;
346 int len = arr.length;
347 avg = sum(arr) / len;
348 return avg;
349 }
350
351 public static double std(double[] arr) {
352 double std = 0.0;
353 int len = arr.length;
354 double ssum = 0.0;
355 for (int i = 0; i < len; i++) {
356 ssum += arr[i] * arr[i];
357 }
358
359 std = (len / (len - 1.0)) * (ssum / len - Math.pow(avg(arr), 2));
360 return Math.sqrt(std);
361 }
362
363 private double getNextUti() {
364 double utiO = getOriU(cTime + timeInt);
365 exma = getPre();
366
367 if (exma >= 1.0)
368 return -3.0;
369
370 if (exma > utiO) {
371 est_t = cTime + timeInt;
372 est_u = exma;
373 est_mu = getOriU(cTime);
374 est_mt = cTime;
375 return -2.0;
376 } else {
377 return -1.0;
378 }
379
380 }
381
382 private Bid genRanBid(double min, double max) {
383 HashMap<Integer, Value> values = new HashMap<Integer, Value>(); // pairs
384 // <issuenumber,chosen
385 // value
386 // string>
387 List<Issue> issues = utilitySpace.getDomain().getIssues();
388 int counter = 0;
389 int limit = 1000;
390 double fmax = max;
391
392 Bid bid = null;
393 do {
394 for (Issue lIssue : issues) {
395 switch (lIssue.getType()) {
396 case DISCRETE:
397 IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
398 int optionIndex = randomnr
399 .nextInt(lIssueDiscrete.getNumberOfValues());
400 values.put(lIssue.getNumber(),
401 lIssueDiscrete.getValue(optionIndex));
402 break;
403 case REAL:
404 IssueReal lIssueReal = (IssueReal) lIssue;
405 int optionInd = randomnr.nextInt(
406 lIssueReal.getNumberOfDiscretizationSteps() - 1);
407 values.put(lIssueReal.getNumber(), new ValueReal(lIssueReal
408 .getLowerBound()
409 + (lIssueReal.getUpperBound()
410 - lIssueReal.getLowerBound()) * (optionInd)
411 / (lIssueReal
412 .getNumberOfDiscretizationSteps())));
413 break;
414 case INTEGER:
415 IssueInteger lIssueInteger = (IssueInteger) lIssue;
416 int optionIndex2 = lIssueInteger.getLowerBound()
417 + randomnr.nextInt(lIssueInteger.getUpperBound()
418 - lIssueInteger.getLowerBound());
419 values.put(lIssueInteger.getNumber(),
420 new ValueInteger(optionIndex2));
421 break;
422 default:
423 //
424 }
425 }
426
427 try {
428 bid = new Bid(utilitySpace.getDomain(), values);
429 } catch (Exception e) {
430 // System.out.println("error in generating random bids");
431 }
432
433 counter++;
434 if (counter > limit) {
435 limit = limit + 500;
436 fmax += 0.005;
437 // return mBidHistory.getMyLastBid();
438 }
439
440 if (counter > 4000)
441 return mBidHistory.getMyLastBid();
442
443 } while (getUtility(bid) < min || getUtility(bid) > fmax);
444
445 return bid;
446 }
447
448 @Override
449 public SupportedNegotiationSetting getSupportedNegotiationSetting() {
450 return SupportedNegotiationSetting.getLinearUtilitySpaceInstance();
451 }
452
453 @Override
454 public String getDescription() {
455 return "ANAC2012";
456 }
457}
Note: See TracBrowser for help on using the repository browser.