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