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

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

Initial import : Genius 9.0.0

File size: 10.9 KB
Line 
1package agents.anac.y2014.ArisawaYaki;
2
3import java.io.Serializable;
4import java.util.List;
5import java.util.Random;
6
7import genius.core.Agent;
8import genius.core.Bid;
9import genius.core.BidHistory;
10import genius.core.actions.Accept;
11import genius.core.actions.Action;
12import genius.core.actions.DefaultAction;
13import genius.core.actions.Offer;
14import genius.core.bidding.BidDetails;
15import genius.core.issue.Issue;
16import genius.core.issue.IssueInteger;
17import genius.core.issue.ValueInteger;
18
19public class ArisawaYaki extends Agent {
20
21 /** The minimum utility a bid should have to be accepted or offered. */
22 private double MINIMUM_BID_UTILITY;
23 /** The opponent's last action. */
24 private Bid opponentLastBid;
25 /** Bid with the highest possible utility. */
26 // private Bid maxBid;
27 /** ‘ŠŽè‚ª�o‚µ‚½ƒrƒbƒh‚Ì“à�A�Å‚à�‚‚¢Œø—p’l‚̃rƒbƒh‚ð•Û‘¶ */
28 private Bid opponentBestBid;
29 /** ‘ŠŽè‚ÌBid‚ð•Û‘¶‚·‚éArrayList */
30 // private ArrayList<Bid> opponentBids;
31 /** sigmoid‚ÌŒX‚« */
32 private double a = 40.0;
33 /** �¡‰ñ‚ÌBidHistory */
34 private BidHistory currBidHistory;
35 /** Accept‚·‚éŒø—p’l */
36 private double acceptUtility;
37 /** ‚ ‚é‹æŠÔ‚ÌŒø—p’l‚Ì•½‹Ï */
38 private double ave;
39 /** ‘O‰ñ‚Ì‚ ‚é‹æŠÔ‚ÌŒø—p’l‚Ì•½‹Ï */
40 private double pre_ave;
41 /** ŽžŠÔ‚ð‹L˜^ */
42 private double laptime;
43 /** �s“®‰ñ�”‚ð‹L˜^ */
44 private int count;
45 /** �I—¹ŠÔ�Û‚Ì�s“®‰ñ�” */
46 private int countFinal;
47
48 public ArisawaYaki() {
49 }
50
51 /**
52 * Initialize the target utility to MAX(rv, max). Where rv is the
53 * reservation value of the preference profile and max is the highest
54 * utility received on the current preference profile.
55 */
56 @Override
57 public void init() {
58 MINIMUM_BID_UTILITY = 1.0;
59 opponentBestBid = null;
60 opponentLastBid = null;
61 currBidHistory = new BidHistory();
62 acceptUtility = 0.85;
63 pre_ave = 0.0;
64 ave = 0.0;
65 laptime = 0.0;
66 count = 1;
67 countFinal = -1;
68
69 int num = sessionNr;
70 Serializable prev = this.loadSessionData();
71 // if (prev != null) {
72 // double previousOutcome = (Double) prev;
73 // //�@‘O‰ñŠl“¾Œø—p’l
74 // //BidHistory history = (BidHistory) prev;
75 // MINIMUM_BID_UTILITY =
76 // Math.max(Math.max(utilitySpace.getReservationValueUndiscounted(),
77 // previousOutcome), 0.5);
78 // }
79 // else{
80 // MINIMUM_BID_UTILITY =
81 // Math.max(utilitySpace.getReservationValueUndiscounted(), 0.5);
82 // }
83 }
84
85 @Override
86 public String getVersion() {
87 return "1.0";
88 }
89
90 @Override
91 public String getName() {
92 return "ArisawaYaki";
93 }
94
95 /**
96 * Set the target utility for the next match on the same preference profile.
97 * If the received utility is higher than the current target, save the
98 * received utility as the new target utility.
99 */
100 // public void endSession(NegotiationResult result) {
101 // if (result.getMyDiscountedUtility() > MINIMUM_BID_UTILITY) {
102 // saveSessionData(new Double(result.getMyDiscountedUtility()));
103 // }
104 // //System.out.println(result);
105 // }
106
107 /**
108 * Retrieve the bid from the opponent's last action.
109 */
110 @Override
111 public void ReceiveMessage(Action opponentAction) {
112 count++;
113 if (opponentAction != null) {
114 opponentLastBid = DefaultAction.getBidFromAction(opponentAction); // ‘ŠŽè‚ÌlastBid
115 currBidHistory.add(new BidDetails(opponentLastBid,
116 getUtility(opponentLastBid), timeline.getTime()));
117
118 // opponentBestBid‚Ì�X�V
119 if (opponentBestBid != null) {
120 if (getUtility(opponentLastBid) > getUtility(opponentBestBid)) {
121 opponentBestBid = new Bid(opponentLastBid);
122 }
123 } else {
124 opponentBestBid = new Bid(opponentLastBid);
125 }
126 }
127 // System.out.println("debug: opponentBestBid"+opponentBestBid);
128 }
129
130 /**
131 * Accept if the utility of the opponent's is higher than the target
132 * utility; else return a random bid with a utility at least equal to the
133 * target utility.
134 */
135 @Override
136 public Action chooseAction() {
137 double time = timeline.getTime();
138 if (time >= 0.99) {
139 return finalPhase();
140 }
141 calculateMinimumBidUtil(time);
142
143 if (opponentLastBid != null
144 && getUtility(opponentLastBid) >= acceptUtility) {
145 return new Accept(getAgentID(), opponentLastBid);
146 }
147
148 Bid bid = null;
149 try {
150 if (opponentLastBid != null) {
151 bid = getBidBySA2(MINIMUM_BID_UTILITY);
152 } else {
153 bid = getBidBySA(MINIMUM_BID_UTILITY);
154 }
155 } catch (Exception e) {
156 e.printStackTrace();
157 }
158 return new Offer(getAgentID(), bid);
159 }
160
161 /**
162 * MINIMUM_BID_UTILITY‚ÆacceptUtility‚Ì’l‚ðŒˆ‚ß‚é
163 *
164 * @param time
165 */
166 private void calculateMinimumBidUtil(double time) {
167 double reservationValue = utilitySpace
168 .getReservationValueWithDiscount(time);
169 double nowUtility = sigmoid(time);
170
171 if (reservationValue > nowUtility) {
172 acceptUtility = reservationValue;
173 } else {
174 acceptUtility = nowUtility;
175 }
176
177 if (acceptUtility > 0.87) {
178 acceptUtility = 0.87;
179 }
180
181 if (count % 50 == 0) {
182 setUtilityByAve();
183 }
184
185 if (opponentBestBid != null) {
186 double minUtility = getUtility(opponentBestBid);
187 if (acceptUtility < minUtility) {
188 acceptUtility = minUtility;
189 }
190 if (MINIMUM_BID_UTILITY < minUtility) {
191 MINIMUM_BID_UTILITY = minUtility;
192 }
193 }
194
195 if (MINIMUM_BID_UTILITY >= 0.95) {
196 MINIMUM_BID_UTILITY = 0.95;
197 }
198 // System.out.println("debug MINIMUM_BID_UTILITY "+MINIMUM_BID_UTILITY);
199 // System.out.println("debug acceptUtility "+acceptUtility);
200 }
201
202 /**
203 * ‘ŠŽè‚ÌBid‚Ì•½‹Ï‚ð—p‚¢‚ÄOffer‚·‚éBid‚ÌŒø—p’l‚ðŒˆ‚ß‚é
204 *
205 * @param time
206 */
207 private void setUtilityByAve() {
208 double time = timeline.getTime();
209 // System.out.println("debug size "+currBidHistory.size());
210 BidHistory bids = currBidHistory.filterBetweenTime(laptime, time);
211 // System.out.println("debug size "+bids.size());
212
213 List<BidDetails> list = bids.getHistory();
214 int size = list.size();
215 double sum = 0.0;
216
217 // System.out.println("debug list "+list.toString());
218 // System.out.println("debug laptime "+laptime);
219 // System.out.println("debug time "+time);
220
221 for (int i = 0; i < size; i++) {
222 sum += getUtility(list.get(i).getBid());
223 }
224
225 ave = sum / size;
226
227 if (ave - pre_ave > 0) {
228 MINIMUM_BID_UTILITY -= 0.03;
229 } else if (ave - pre_ave < 0) {
230 MINIMUM_BID_UTILITY += 0.03;
231 }
232
233 laptime = time;
234 pre_ave = ave;
235 System.out.println("debug ave " + ave);
236 System.out.println("debug pre_ave " + pre_ave);
237 }
238
239 private Action finalPhase() {
240 countFinal++;
241 List<BidDetails> list = currBidHistory.getNBestBids(7);
242 return new Offer(getAgentID(), list.get(countFinal).getBid());
243 }
244
245 private double sigmoid(double x) {
246 return (Math.exp(a * (1 - x)) - 1) / (Math.exp(a * (1 - x)) + 1)
247 * Math.pow(utilitySpace.getDiscountFactor(), x);
248 }
249
250 /**
251 * �Ä‚«‚È‚Ü‚µ–@‚ð—p‚¢‚ÄtargetˆÈ�ã‚ÌBid‚ð‹�‚ß‚é
252 *
253 * @param target
254 * @return
255 * @throws Exception
256 */
257 private Bid getBidBySA(double target) throws Exception {
258 double t = 1000000;
259 double cool = 0.999;
260 List<Issue> issues = utilitySpace.getDomain().getRandomBid(null)
261 .getIssues();
262 Random rnd = new Random();
263 Bid nowBid;
264
265 nowBid = utilitySpace.getDomain().getRandomBid(null);
266 Bid nextBid = null;
267
268 // ƒ‰ƒ“ƒ_ƒ€‚Å‚Æ‚Á‚½’l‚ªtarget‚ð’´‚¦‚Ä‚¢‚½‚炱‚ê‚ð•Ô‚·
269 if (getUtility(nowBid) >= target) {
270 return nowBid;
271 }
272
273 while (t > 0.001) {
274 int index = rnd.nextInt(issues.size());
275 IssueInteger isInt = (IssueInteger) issues.get(index);
276 ValueInteger vlInt = (ValueInteger) nowBid.getValue(index + 1);
277
278 // nowBid‚ðƒ‰ƒ“ƒ_ƒ€‚ɂЂƂ‚¸‚炵‚½nextBid‚ð�¶�¬
279 if (isInt.getUpperBound() == vlInt.getValue()) { // issue‚Ì�Å‘å’l‚Ì‚Æ‚«
280 vlInt = new ValueInteger(vlInt.getValue() - 1);
281 nextBid = new Bid(nowBid);
282 nextBid = nextBid.putValue(index + 1, vlInt);
283 } else if (isInt.getLowerBound() == vlInt.getValue()) { // issue‚Ì�Å�¬’l‚Ì‚Æ‚«
284 vlInt = new ValueInteger(vlInt.getValue() + 1);
285 nextBid = new Bid(nowBid);
286 nextBid = nextBid.putValue(index + 1, vlInt);
287 } else {
288 int updown = rnd.nextInt(2);
289 if (updown == 0) {
290 nextBid = new Bid(nowBid);
291 vlInt = new ValueInteger(vlInt.getValue() - 1);
292 nextBid = nextBid.putValue(index + 1, vlInt);
293 } else {
294 nextBid = new Bid(nowBid);
295 vlInt = new ValueInteger(vlInt.getValue() + 1);
296 nextBid = nextBid.putValue(index + 1, vlInt);
297 }
298 }
299
300 // ‘JˆÚ�æ‚̃rƒbƒh‚ªtarget‚ð’´‚¦‚½‚炱‚ê‚ð•Ô‚·
301 if (getUtility(nextBid) > target) {
302 return nextBid;
303 }
304 double utility = getUtility(nextBid) - getUtility(nowBid);
305
306 // utility‚ª‘�‰Á‚µ‚Ä‚¢‚é‚©�A‚ ‚éŠm—¦‚Å‚»‚Ì•ûŒü‚ÖˆÚ“®
307 double p = Math.pow(Math.E, -Math.abs(utility) / t);
308 if (utility >= 0 || Math.random() < p) {
309 nowBid = new Bid(nextBid);
310 }
311 t *= cool;
312 }
313 return nextBid;
314 }
315
316 /**
317 * �Ä‚«‚È‚Ü‚µ–@‚ð—p‚¢‚ÄtargetˆÈ�ã‚ÌBid‚ð‹�‚ß‚é
318 *
319 * @param target
320 * @return
321 * @throws Exception
322 */
323 private Bid getBidBySA2(double target) throws Exception {
324 double t = 1000;
325 double cool = 0.99;
326 List<Issue> issues = utilitySpace.getDomain().getRandomBid(null)
327 .getIssues();
328 Bid nowBid = new Bid(opponentLastBid);
329 Bid nextBid = new Bid(opponentLastBid);
330
331 while (t > 0.001) {
332 // Bid‚Ìissue‚̂ЂƂ‚ð•Ï‚¦‚Ä‚Ý‚é
333 Bid tempBid = null;
334 double tempUtility = -1.0;
335 // nextBid‚ð�X�V
336 for (int i = 0; i < issues.size(); i++) {
337 IssueInteger isInt = (IssueInteger) issues.get(i);
338 int min = isInt.getLowerBound();
339 int max = isInt.getUpperBound();
340 for (int j = min; j < max; j++) {
341 tempBid = new Bid(nowBid);
342 tempBid = tempBid.putValue(i + 1, new ValueInteger(j));
343 if (getUtility(tempBid) > tempUtility) {
344 tempUtility = getUtility(tempBid);
345 // ‘JˆÚ�æ‚̃rƒbƒh‚ªtarget‚ð’´‚¦‚½‚炱‚ê‚ð•Ô‚·
346 if (tempUtility > target) {
347 return tempBid;
348 }
349 nextBid = new Bid(tempBid);
350 }
351 }
352 }
353
354 // ‘JˆÚ�æ‚̃rƒbƒh‚ªtarget‚ð’´‚¦‚½‚炱‚ê‚ð•Ô‚·
355 if (getUtility(nextBid) > target) {
356 return nextBid;
357 }
358 double utility = getUtility(nextBid) - getUtility(nowBid);
359
360 // utility‚ª‘�‰Á‚µ‚Ä‚¢‚é‚©�A‚ ‚éŠm—¦‚Å‚»‚Ì•ûŒü‚ÖˆÚ“®
361 double p = Math.pow(Math.E, -Math.abs(utility) / t);
362 if (utility >= 0 || Math.random() < p) {
363 nowBid = new Bid(nextBid);
364 }
365 t *= cool;
366 }
367 return nextBid;
368 }
369
370 @Override
371 public String getDescription() {
372 return "ANAC2014 compatible with non-linear utility spaces";
373 }
374}
375
376// class MySessionData implements Serializable{
377// BidHistory history;
378// Bid lastBid;
379//
380// public MySessionData(){
381//
382// }
383// }
Note: See TracBrowser for help on using the repository browser.