1 | package agents.anac.y2014.AgentM;
|
---|
2 |
|
---|
3 | import java.io.Serializable;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Random;
|
---|
8 |
|
---|
9 | import agents.SimpleAgentSavingBidHistory;
|
---|
10 | import genius.core.Agent;
|
---|
11 | import genius.core.Bid;
|
---|
12 | import genius.core.BidHistory;
|
---|
13 | import genius.core.NegotiationResult;
|
---|
14 | import genius.core.actions.Accept;
|
---|
15 | import genius.core.actions.Action;
|
---|
16 | import genius.core.actions.ActionWithBid;
|
---|
17 | import genius.core.actions.EndNegotiation;
|
---|
18 | import genius.core.actions.Offer;
|
---|
19 | import genius.core.bidding.BidDetails;
|
---|
20 | import genius.core.issue.Issue;
|
---|
21 | import genius.core.issue.IssueInteger;
|
---|
22 | import genius.core.issue.Value;
|
---|
23 | import genius.core.issue.ValueInteger;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * @author S. Hourmann Some improvements over the standard Agent. Saving Bid
|
---|
27 | * History for the session.
|
---|
28 | *
|
---|
29 | * Random Walker, Zero Intelligence Agent
|
---|
30 | */
|
---|
31 | public class AgentM extends Agent {
|
---|
32 | // "state" represents "where am I in the code".
|
---|
33 | // I want to print "state" when I print a message about saving data.
|
---|
34 | private String state;
|
---|
35 | private Action actionOfPartner = null;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Note: {@link SimpleAgentSavingBidHistory} does not account for the
|
---|
39 | * discount factor in its computations
|
---|
40 | */
|
---|
41 | private static double MINIMUM_BID_UTILITY = 0.0;
|
---|
42 | private static int NUMBER_ITERATIONS = 1000;
|
---|
43 | private BidHistory currSessOppBidHistory;
|
---|
44 | private BidHistory prevSessOppBidHistory;
|
---|
45 | private BidHistory mySessBidHistory;
|
---|
46 | private Bid bidmax = null;
|
---|
47 | private Bid endbid = null;
|
---|
48 | private double concessionRate = 0.0;
|
---|
49 | private ArrayList issueAllay = new ArrayList();
|
---|
50 | private ArrayList myissue = new ArrayList();
|
---|
51 | private ArrayList<Integer> movement = new ArrayList<Integer>();
|
---|
52 | private ArrayList<Integer> maxissue = new ArrayList<Integer>();
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * init is called when a next session starts with the same opponent.
|
---|
56 | */
|
---|
57 | @Override
|
---|
58 | public void init() {
|
---|
59 | MINIMUM_BID_UTILITY = utilitySpace.getReservationValueUndiscounted();
|
---|
60 | myBeginSession();
|
---|
61 | currSessOppBidHistory = new BidHistory();
|
---|
62 | prevSessOppBidHistory = new BidHistory();
|
---|
63 | mySessBidHistory = new BidHistory();
|
---|
64 |
|
---|
65 | int[] numAllay = new int[10];
|
---|
66 | for (int j = 0; j < 10; j++) {
|
---|
67 | numAllay[j] = 0;
|
---|
68 | }
|
---|
69 | Integer num = 0;
|
---|
70 | for (int i = 0; i < this.utilitySpace.getDomain().getIssues()
|
---|
71 | .size(); i++) {
|
---|
72 | issueAllay.add(numAllay.clone());
|
---|
73 | movement.add(num);
|
---|
74 | maxissue.add(num);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | public void myBeginSession() {
|
---|
79 | System.out.println("Starting match num: " + sessionNr);
|
---|
80 |
|
---|
81 | // ---- Code for trying save and load functionality
|
---|
82 | // First try to load saved data
|
---|
83 | // ---- Loading from agent's function "loadSessionData"
|
---|
84 | Serializable prev = this.loadSessionData();
|
---|
85 | AgentMData a = (AgentMData) prev;
|
---|
86 | if (a != null) {
|
---|
87 | this.endbid = a.getBid();
|
---|
88 | System.out.println("load complete");
|
---|
89 | // System.out
|
---|
90 | // .println("---------/////////// NEW NEW NEW
|
---|
91 | // /////////////----------");
|
---|
92 | // System.out.println("The size of the previous BidHistory is: "
|
---|
93 | // + prevSessOppBidHistory.size());
|
---|
94 | } else {
|
---|
95 | // If didn't succeed, it means there is no data for this preference
|
---|
96 | // profile
|
---|
97 | // in this domain.
|
---|
98 | System.out.println("There is no history yet.");
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | @Override
|
---|
103 | public String getVersion() {
|
---|
104 | return "3.1";
|
---|
105 | }
|
---|
106 |
|
---|
107 | @Override
|
---|
108 | public String getName() {
|
---|
109 | return "AgentM";
|
---|
110 | }
|
---|
111 |
|
---|
112 | @Override
|
---|
113 | public void ReceiveMessage(Action opponentAction) {
|
---|
114 | actionOfPartner = opponentAction;
|
---|
115 | if (opponentAction instanceof Offer) {
|
---|
116 | Bid bid = ((Offer) opponentAction).getBid();
|
---|
117 | // 2. store the opponent's trace
|
---|
118 | try {
|
---|
119 | BidDetails opponentBid = new BidDetails(bid,
|
---|
120 | utilitySpace.getUtility(bid), timeline.getTime());
|
---|
121 | if (currSessOppBidHistory.getLastBidDetails() != null) {
|
---|
122 | this.prevSessOppBidHistory.add(
|
---|
123 | this.currSessOppBidHistory.getLastBidDetails());
|
---|
124 | }
|
---|
125 | currSessOppBidHistory.add(opponentBid);
|
---|
126 | } catch (Exception e) {
|
---|
127 | e.printStackTrace();
|
---|
128 | }
|
---|
129 | }
|
---|
130 | try {
|
---|
131 | if (this.prevSessOppBidHistory.getLastBid() != null) {
|
---|
132 | if (getUtility(this.prevSessOppBidHistory.getBestBidDetails()
|
---|
133 | .getBid()) < getUtility(
|
---|
134 | this.currSessOppBidHistory.getLastBid())) {
|
---|
135 | this.concessionRate = Math.pow(
|
---|
136 | getUtility(this.prevSessOppBidHistory
|
---|
137 | .getWorstBidDetails().getBid())
|
---|
138 | - getUtility(this.currSessOppBidHistory
|
---|
139 | .getBestBidDetails().getBid()),
|
---|
140 | 2.0);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | } catch (Exception e) {
|
---|
144 | e.printStackTrace();
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | @Override
|
---|
149 | public Action chooseAction() {
|
---|
150 | Action action = null;
|
---|
151 | try {
|
---|
152 | if (actionOfPartner == null)
|
---|
153 | action = chooseRandomBidAction(null); // original code but will
|
---|
154 | // throw NPE
|
---|
155 | if (actionOfPartner instanceof Offer) {
|
---|
156 | Bid partnerBid = ((Offer) actionOfPartner).getBid();
|
---|
157 | double offeredUtilFromOpponent = getUtility(partnerBid);
|
---|
158 | // get current time
|
---|
159 | double time = timeline.getTime();
|
---|
160 | action = chooseRandomBidAction(partnerBid);
|
---|
161 |
|
---|
162 | Bid myBid = ((Offer) action).getBid();
|
---|
163 | double myOfferedUtil = getUtility(myBid);
|
---|
164 |
|
---|
165 | // accept under certain circumstances
|
---|
166 | if (isAcceptable(offeredUtilFromOpponent, myOfferedUtil,
|
---|
167 | time)) {
|
---|
168 | action = new Accept(getAgentID(), partnerBid);
|
---|
169 | // ---- Code for trying save and load functionality
|
---|
170 | // /////////////////////////////////
|
---|
171 | state = "I accepted so I'm trying to save. ";
|
---|
172 | tryToSaveAndPrintState();
|
---|
173 | // /////////////////////////////////
|
---|
174 | }
|
---|
175 | }
|
---|
176 | if (actionOfPartner instanceof EndNegotiation) {
|
---|
177 | action = new Accept(getAgentID(),
|
---|
178 | ((ActionWithBid) actionOfPartner).getBid());
|
---|
179 | // ---- Code for trying save and load functionality
|
---|
180 | // /////////////////////////////////
|
---|
181 | state = "Got EndNegotiation from opponent. ";
|
---|
182 | tryToSaveAndPrintState();
|
---|
183 | // /////////////////////////////////
|
---|
184 | }
|
---|
185 | // sleep(0.001); // just for fun
|
---|
186 | } catch (Exception e) {
|
---|
187 | System.out.println("Exception in ChooseAction:" + e.getMessage());
|
---|
188 |
|
---|
189 | // ---- Code for trying save and load functionality
|
---|
190 | // /////////////////////////////////
|
---|
191 | state = "Got Exception. ";
|
---|
192 | tryToSaveAndPrintState();
|
---|
193 | // /////////////////////////////////
|
---|
194 | // best guess if things go wrong.
|
---|
195 | action = new Accept(getAgentID(),
|
---|
196 | ((ActionWithBid) actionOfPartner).getBid());
|
---|
197 | }
|
---|
198 | return action;
|
---|
199 | }
|
---|
200 |
|
---|
201 | // ---- Code for trying save and load functionality
|
---|
202 | private void tryToSaveAndPrintState() {
|
---|
203 |
|
---|
204 | // ---- Saving from agent's function "saveSessionData"
|
---|
205 |
|
---|
206 | // Bid lastBid = this.currSessOppBidHistory.getLastBid();
|
---|
207 | // System.out.println("testtettttttt");
|
---|
208 | // AgentMData data = new AgentMData(lastBid);
|
---|
209 | // System.out.println(data.getBid());
|
---|
210 | // this.saveSessionData(data);
|
---|
211 | // System.out.println(state +
|
---|
212 | // "The size of the BidHistory I'm saving is: "
|
---|
213 | // + currSessOppBidHistory.size());
|
---|
214 | }
|
---|
215 |
|
---|
216 | private boolean isAcceptable(double offeredUtilFromOpponent,
|
---|
217 | double myOfferedUtil, double time) throws Exception {
|
---|
218 | // double P = Paccept(offeredUtilFromOpponent, time);
|
---|
219 | // offeredUtilFromOpponent ‘ŠŽè‚Ì’ñˆÄ myOfferedUtil
|
---|
220 | // Ž©•ª‚Ì’ñˆÄ
|
---|
221 | // double offerOk =
|
---|
222 | // (getUtility(mySessBidHistory.getWorstBidDetails().getBid()) +
|
---|
223 | // getUtility(mySessBidHistory.getBestBidDetails().getBid()))/2;
|
---|
224 | double offerOk = getUtility(
|
---|
225 | mySessBidHistory.getWorstBidDetails().getBid());
|
---|
226 | List<Issue> issues = utilitySpace.getDomain().getIssues();
|
---|
227 |
|
---|
228 | // ‘ŠŽè‚Ìlastbid‚©‚çissueAllay‚É‚»‚Ì’l‚ð’ljÁ
|
---|
229 | int opfirstoplast = 0;
|
---|
230 | for (int i = 1; i < issues.size() + 1; i++) {
|
---|
231 | ValueInteger opLast = (ValueInteger) this.currSessOppBidHistory
|
---|
232 | .getLastBid().getValue(i);
|
---|
233 | int oplast = Integer.valueOf(opLast.toString());
|
---|
234 | int[] tempAllay = (int[]) issueAllay.get(i - 1);
|
---|
235 | tempAllay[oplast] += 1;
|
---|
236 | this.issueAllay.set(i - 1, tempAllay.clone());
|
---|
237 | }
|
---|
238 |
|
---|
239 | // System.out.println("check");
|
---|
240 |
|
---|
241 | // ’ñˆÄ‚³‚ꂽbid‚©‚ç‚»‚Ìissue‚Ì’†‚Å�Å‚à‰ñ�”‚Ì‘½‚¢‚à‚Ì‚ðmaxissue‚É�Ý’è
|
---|
242 | for (int i = 0; i < issues.size(); i++) {
|
---|
243 | // issue‚ÌŽæ‚é’l‚ðŽæ‚è�o‚·
|
---|
244 | IssueInteger lIssueInteger = (IssueInteger) issues.get(i);
|
---|
245 | int issueNumber = lIssueInteger.getNumber();
|
---|
246 | int issueIndexMin = lIssueInteger.getLowerBound();
|
---|
247 | int issueIndexMax = lIssueInteger.getUpperBound();
|
---|
248 | // issueAllay‚Å‚Íissue‚Ìbid‚³‚ꂽ‰ñ�”‚ð•Û‘¶
|
---|
249 | int[] temp = (int[]) issueAllay.get(i);
|
---|
250 | // num‚ÍissueIndexMin,issueIndexMax‚ÌŠÔ‚Ì’l
|
---|
251 | int num = 0;
|
---|
252 | // maxissuenum‚͌ĂÑ�o‚³‚ꂽ‰ñ�”‚Ì’l‚Ìmax
|
---|
253 | int maxissuenum = 0;
|
---|
254 | for (int j = issueIndexMin; j < issueIndexMax; j++) {
|
---|
255 | if (maxissuenum < temp[j]) {
|
---|
256 | // System.out.println(j);
|
---|
257 | num = j;
|
---|
258 | maxissuenum = temp[j];
|
---|
259 | }
|
---|
260 | }
|
---|
261 | Integer a = num;
|
---|
262 | this.maxissue.set(i, a);
|
---|
263 | }
|
---|
264 |
|
---|
265 | // ‘ŠŽè—\‘ª’l:w
|
---|
266 | if (endbid != null) {
|
---|
267 | // System.out.println(getUtility(endbid));
|
---|
268 | if (offeredUtilFromOpponent > offerOk
|
---|
269 | || offeredUtilFromOpponent >= getUtility(endbid)) {
|
---|
270 | return true;
|
---|
271 | }
|
---|
272 | }
|
---|
273 | if (offeredUtilFromOpponent > offerOk) {
|
---|
274 | return true;
|
---|
275 | }
|
---|
276 | return false;
|
---|
277 | }
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * Wrapper for getRandomBid, for convenience.
|
---|
281 | *
|
---|
282 | * @return new Action(Bid(..)), with bid utility > MINIMUM_BID_UTIL. If a
|
---|
283 | * problem occurs, it returns an Accept() action.
|
---|
284 | */
|
---|
285 | private Action chooseRandomBidAction(Bid opponentBid) {
|
---|
286 | Bid nextBid = null;
|
---|
287 | try {
|
---|
288 | nextBid = getRandomBid();
|
---|
289 | } catch (Exception e) {
|
---|
290 | System.out.println("Problem with received bid:" + e.getMessage()
|
---|
291 | + ". cancelling bidding");
|
---|
292 | }
|
---|
293 | if (nextBid == null)
|
---|
294 | return (new Accept(getAgentID(), opponentBid));
|
---|
295 | return (new Offer(getAgentID(), nextBid));
|
---|
296 | }
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * @param value
|
---|
300 | * @return a random bid with high enough utility value.
|
---|
301 | * @throws Exception
|
---|
302 | * if we can't compute the utility (eg no evaluators have been
|
---|
303 | * set) or when other evaluators than a DiscreteEvaluator are
|
---|
304 | * present in the util space.
|
---|
305 | */
|
---|
306 | @Override
|
---|
307 | public void endSession(NegotiationResult result) {
|
---|
308 | // System.out.println(result);
|
---|
309 | // System.out.println(result.isAgreement());
|
---|
310 | if (result.isAgreement()) {
|
---|
311 | Bid lastBid = result.getLastBid();
|
---|
312 | AgentMData data = new AgentMData(lastBid);
|
---|
313 | // System.out.println(data.getBid());
|
---|
314 | this.saveSessionData(data);
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | private Bid getRandomBid() throws Exception {
|
---|
319 | HashMap<Integer, Value> values = new HashMap<Integer, Value>(); // pairs
|
---|
320 | // <issuenumber,chosen
|
---|
321 | // value
|
---|
322 | // string>
|
---|
323 | List<Issue> issues = utilitySpace.getDomain().getIssues();
|
---|
324 | Random randomnr = new Random();
|
---|
325 |
|
---|
326 | // create a random bid with utility>MINIMUM_BID_UTIL.
|
---|
327 | // note that this may never succeed if you set MINIMUM too high!!!
|
---|
328 | // in that case we will search for a bid till the time is up (3 minutes)
|
---|
329 | // but this is just a simple agent.
|
---|
330 | Bid bid = null;
|
---|
331 | if (this.bidmax == null) {
|
---|
332 | int i = 0;
|
---|
333 |
|
---|
334 | double currentUtility, maxUtility = 0;
|
---|
335 | do {
|
---|
336 | bid = utilitySpace.getDomain().getRandomBid(null);
|
---|
337 | currentUtility = utilitySpace.getUtility(bid);
|
---|
338 | if (bidmax == null || getUtility(bidmax) < currentUtility) {
|
---|
339 | bidmax = bid;
|
---|
340 | maxUtility = getUtility(bidmax);
|
---|
341 | if (maxUtility > 0.98) {
|
---|
342 | break;
|
---|
343 | }
|
---|
344 | }
|
---|
345 | i++;
|
---|
346 | } while (i < NUMBER_ITERATIONS);
|
---|
347 | i = 0;
|
---|
348 | while ((i++ < NUMBER_ITERATIONS) && (maxUtility < 0.999)) {
|
---|
349 | Bid nextBid = nextBid(bid); // ‹ß–TBid‚ð’T�õ
|
---|
350 | double nextUtility = utilitySpace.getUtility(nextBid); // ‹ß–TBid‚ÌŒø—p’l
|
---|
351 | double temperature = calculateTemperature(i); // ‰·“x‚ÌŽZ�o
|
---|
352 | double probability = calculateProbability(currentUtility,
|
---|
353 | nextUtility, temperature); // ‘JˆÚŠm—¦‚ÌŽZ�o
|
---|
354 | if (probability > randomnr.nextDouble()) {
|
---|
355 | bid = nextBid; // Bid‚Ì�X�V
|
---|
356 | currentUtility = utilitySpace.getUtility(nextBid); // Œø—p’l‚Ì�X�V
|
---|
357 | // �Å“K‰ð‚Ì�X�V
|
---|
358 | if (nextUtility > maxUtility) {
|
---|
359 | this.bidmax = nextBid;
|
---|
360 | maxUtility = nextUtility;
|
---|
361 | }
|
---|
362 | }
|
---|
363 | }
|
---|
364 | } else {
|
---|
365 | int i = 0;
|
---|
366 |
|
---|
367 | double currentUtility, maxUtility = 0;
|
---|
368 | do {
|
---|
369 | bid = utilitySpace.getDomain().getRandomBid(null);
|
---|
370 | currentUtility = utilitySpace.getUtility(bid);
|
---|
371 | if (bidmax == null || getUtility(bidmax) < currentUtility) {
|
---|
372 | bidmax = bid;
|
---|
373 | maxUtility = getUtility(bidmax);
|
---|
374 | if (maxUtility > 0.98) {
|
---|
375 | break;
|
---|
376 | }
|
---|
377 | }
|
---|
378 | i++;
|
---|
379 | } while (i < NUMBER_ITERATIONS);
|
---|
380 | i = 0;
|
---|
381 | int j = 0;
|
---|
382 |
|
---|
383 | values = this.mySessBidHistory.getBestBidDetails().getBid()
|
---|
384 | .getValues();
|
---|
385 |
|
---|
386 | double endpoint;
|
---|
387 | this.concessionRate = Math
|
---|
388 | .pow(getUtility(this.currSessOppBidHistory
|
---|
389 | .getWorstBidDetails().getBid())
|
---|
390 | - getUtility(this.currSessOppBidHistory
|
---|
391 | .getBestBidDetails().getBid()),
|
---|
392 | 2.0)
|
---|
393 | + timeline.getTime() / 10;
|
---|
394 | if (this.endbid == null) {
|
---|
395 | endpoint = 0.999 - this.concessionRate;
|
---|
396 | } else {
|
---|
397 | endpoint = getUtility(this.endbid);
|
---|
398 | }
|
---|
399 | while (((i++ < NUMBER_ITERATIONS)
|
---|
400 | && (maxUtility < 0.999 - this.concessionRate))
|
---|
401 | || ((i < NUMBER_ITERATIONS) && maxUtility < endpoint)) {
|
---|
402 | Bid nextBid = nextBid(bid); // ‹ß–TBid‚ð’T�õ
|
---|
403 | // for(j = 0; j < (timeline.getTime()*issues.size()/5); j++){
|
---|
404 | for (j = 0; j < (issues.size() / 5); j++) {
|
---|
405 | int issueIndex = randomnr.nextInt(issues.size());
|
---|
406 | IssueInteger lIssueInteger = (IssueInteger) issues
|
---|
407 | .get(issueIndex);
|
---|
408 | int issueNumber = lIssueInteger.getNumber();
|
---|
409 | int issueIndexMin = lIssueInteger.getLowerBound();
|
---|
410 | int issueIndexMax = lIssueInteger.getUpperBound();
|
---|
411 | int optionIndex = 0;
|
---|
412 | ValueInteger lIssueValue = (ValueInteger) bid
|
---|
413 | .getValue(issueNumber);
|
---|
414 | int issueValue = Integer.valueOf(lIssueValue.toString())
|
---|
415 | .intValue();
|
---|
416 | optionIndex = nextOptionIndex(issueIndexMin, issueIndexMax,
|
---|
417 | issueValue);
|
---|
418 | lIssueInteger = (IssueInteger) issues.get(issueIndex);
|
---|
419 | issueNumber = lIssueInteger.getNumber();
|
---|
420 | int test_value = 0;
|
---|
421 | if (j % 2 == 0) {
|
---|
422 | test_value = this.maxissue.get(issueNumber - 1);
|
---|
423 | } else {
|
---|
424 | // ValueInteger aaa = (ValueInteger)
|
---|
425 | // this.currSessOppBidHistory.getBestBid().getValue(issueNumber);
|
---|
426 | ValueInteger aaa = (ValueInteger) this.currSessOppBidHistory
|
---|
427 | .getBestBidDetails().getBid()
|
---|
428 | .getValue(issueNumber);
|
---|
429 | test_value = Integer.valueOf(aaa.toString());
|
---|
430 | }
|
---|
431 | nextBid = nextBid.putValue(issueNumber,
|
---|
432 | new ValueInteger(test_value));
|
---|
433 | issueValue = Integer.valueOf(lIssueValue.toString())
|
---|
434 | .intValue();
|
---|
435 | issueIndex = randomnr.nextInt(issues.size());
|
---|
436 | lIssueInteger = (IssueInteger) issues.get(issueIndex);
|
---|
437 | issueNumber = lIssueInteger.getNumber();
|
---|
438 | ValueInteger bbb = (ValueInteger) this.currSessOppBidHistory
|
---|
439 | .getBestBidDetails().getBid().getValue(issueNumber);
|
---|
440 | test_value = Integer.valueOf(bbb.toString());
|
---|
441 | nextBid = nextBid.putValue(issueNumber,
|
---|
442 | new ValueInteger(test_value));
|
---|
443 | }
|
---|
444 | double nextUtility = utilitySpace.getUtility(nextBid); // ‹ß–TBid‚ÌŒø—p’l
|
---|
445 | double temperature = calculateTemperature(i); // ‰·“x‚ÌŽZ�o
|
---|
446 | double probability = calculateProbability(currentUtility,
|
---|
447 | nextUtility, temperature); // ‘JˆÚŠm—¦‚ÌŽZ�o
|
---|
448 | if (probability > randomnr.nextDouble()) {
|
---|
449 | bid = nextBid; // Bid‚Ì�X�V
|
---|
450 | currentUtility = utilitySpace.getUtility(nextBid); // Œø—p’l‚Ì�X�V
|
---|
451 | // �Å“K‰ð‚Ì�X�V
|
---|
452 | // if (nextUtility > maxUtility) {
|
---|
453 | if (nextUtility > (getUtility(this.bidmax)
|
---|
454 | - this.concessionRate)) {
|
---|
455 | this.bidmax = nextBid;
|
---|
456 | maxUtility = nextUtility;
|
---|
457 | // System.out.println(getUtility(this.bidmax)-this.concessionRate);
|
---|
458 | }
|
---|
459 | }
|
---|
460 | }
|
---|
461 |
|
---|
462 | }
|
---|
463 | BidDetails opponentBid = new BidDetails(bidmax,
|
---|
464 | utilitySpace.getUtility(bidmax), timeline.getTime());
|
---|
465 | mySessBidHistory.add(opponentBid);
|
---|
466 | return this.bidmax;
|
---|
467 | }
|
---|
468 |
|
---|
469 | private int randomOptionIndex(int issueIndexMin, int issueIndexMax) {
|
---|
470 | int optionIndex = 0;
|
---|
471 | Random randomnr = new Random();
|
---|
472 | if (issueIndexMin < issueIndexMax) {
|
---|
473 | optionIndex = issueIndexMin
|
---|
474 | + randomnr.nextInt(issueIndexMax - issueIndexMin);
|
---|
475 | } else {
|
---|
476 | optionIndex = issueIndexMin; // issueIndexMin ==
|
---|
477 | // issueIndexMax‚Ì�ê�‡
|
---|
478 | }
|
---|
479 |
|
---|
480 | return optionIndex;
|
---|
481 | }
|
---|
482 |
|
---|
483 | /**
|
---|
484 | * ‹ß–T’T�õ
|
---|
485 | *
|
---|
486 | * @param issueIndexMin
|
---|
487 | * ‰ºŒÀ’l
|
---|
488 | * @param issueIndexMax
|
---|
489 | * �ãŒÀ’l
|
---|
490 | * @param issueValue
|
---|
491 | * Œ»�Ý‚Ì’l
|
---|
492 | * @return ’l‚ð1‚¾‚¯‘�Œ¸‚³‚¹‚é
|
---|
493 | */
|
---|
494 | private int nextOptionIndex(int issueIndexMin, int issueIndexMax,
|
---|
495 | int issueValue) {
|
---|
496 | int step = 1; // ’l‚ð‘�Œ¸‚³‚¹‚é•�
|
---|
497 | Random randomnr = new Random();
|
---|
498 | int direction = randomnr.nextBoolean() ? 1 : -1; // ƒ‰ƒ“ƒ_ƒ€‚É‘�Œ¸‚ðŒˆ’è
|
---|
499 |
|
---|
500 | if (issueIndexMin < issueIndexMax) {
|
---|
501 | if ((issueValue + step) > issueIndexMax) { // +1‚·‚é‚Æ�ãŒÀ’l‚ð’´‚¦‚é�ê�‡
|
---|
502 | direction = -1;
|
---|
503 | } else if ((issueValue - step) < issueIndexMin) { // -1‚·‚é‚ƉºŒÀ’l‚ð‰º‰ñ‚é�ê�‡
|
---|
504 | direction = 1;
|
---|
505 | }
|
---|
506 | } else {
|
---|
507 | return issueValue; // issueIndexMin == issueIndexMax‚Ì�ê�‡
|
---|
508 | }
|
---|
509 |
|
---|
510 | return issueValue + step * direction;
|
---|
511 | }
|
---|
512 |
|
---|
513 | /**
|
---|
514 | * This function determines the accept probability for an offer. At t=0 it
|
---|
515 | * will prefer high-utility offers. As t gets closer to 1, it will accept
|
---|
516 | * lower utility offers with increasing probability. it will never accept
|
---|
517 | * offers with utility 0.
|
---|
518 | *
|
---|
519 | * @param u
|
---|
520 | * is the utility
|
---|
521 | * @param t
|
---|
522 | * is the time as fraction of the total available time (t=0 at
|
---|
523 | * start, and t=1 at end time)
|
---|
524 | * @return the probability of an accept at time t
|
---|
525 | * @throws Exception
|
---|
526 | * if you use wrong values for u or t.
|
---|
527 | *
|
---|
528 | */
|
---|
529 | double Paccept(double u, double t1) throws Exception {
|
---|
530 | double t = t1 * t1 * t1; // steeper increase when deadline approaches.
|
---|
531 | if (u < 0 || u > 1.05)
|
---|
532 | throw new Exception("utility " + u + " outside [0,1]");
|
---|
533 | // normalization may be slightly off, therefore we have a broad boundary
|
---|
534 | // up to 1.05
|
---|
535 | if (t < 0 || t > 1)
|
---|
536 | throw new Exception("time " + t + " outside [0,1]");
|
---|
537 | if (u > 1.)
|
---|
538 | u = 1;
|
---|
539 | if (t == 0.5)
|
---|
540 | return u;
|
---|
541 | return (u - 2. * u * t
|
---|
542 | + 2. * (-1. + t + Math.sqrt(sq(-1. + t) + u * (-1. + 2 * t))))
|
---|
543 | / (-1. + 2 * t);
|
---|
544 | }
|
---|
545 |
|
---|
546 | double sq(double x) {
|
---|
547 | return x * x;
|
---|
548 | }
|
---|
549 |
|
---|
550 | private double calculateProbability(double currentUtil, double nextUtil,
|
---|
551 | double temperature) {
|
---|
552 | double diff = currentUtil - nextUtil;
|
---|
553 | if (diff > 0.0) {
|
---|
554 | return Math.exp(-diff / temperature); // Œ»�Ý‚ÌŒø—p’l‚Ì•û‚ª�‚‚¢�ê�‡
|
---|
555 | } else {
|
---|
556 | return 1.0; // ‹ß–T‚ÌŒø—p’l‚Ì•û‚ª�‚‚¢�ê�‡
|
---|
557 | }
|
---|
558 | }
|
---|
559 |
|
---|
560 | private double calculateTemperature(int iteration) {
|
---|
561 | double t = 0.01;
|
---|
562 | return t * Math.pow(1.0 - ((double) iteration / NUMBER_ITERATIONS), 2);
|
---|
563 | }
|
---|
564 |
|
---|
565 | private Bid nextBid(Bid bid) throws Exception {
|
---|
566 | List<Issue> issues = utilitySpace.getDomain().getIssues(); // ‘Sissue‚̎擾
|
---|
567 | Bid nextBid = new Bid(bid); // Œ»�Ý‚ÌBid‚ðƒRƒs�[
|
---|
568 | Boolean optionFlag = false; // ’T�õŽè–@
|
---|
569 | int numberIndexes = utilitySpace.getDomain().getIssues().size() / 10;
|
---|
570 | Random randomnr = new Random();
|
---|
571 | for (int i = 0; i < numberIndexes; i++) {
|
---|
572 | int issueIndex = randomnr.nextInt(issues.size()); // Issue‚ðƒ‰ƒ“ƒ_ƒ€‚ÉŽw’è
|
---|
573 | IssueInteger lIssueInteger = (IssueInteger) issues.get(issueIndex); // Žw’肵‚½index‚Ìissue‚ðŽæ“¾
|
---|
574 | int issueNumber = lIssueInteger.getNumber(); // issue”Ô�†
|
---|
575 | int issueIndexMin = lIssueInteger.getLowerBound(); // issue‚̉ºŒÀ’l
|
---|
576 | int issueIndexMax = lIssueInteger.getUpperBound(); // issue‚Ì�ãŒÀ’l
|
---|
577 | int optionIndex = 0; // •Ï�X‚·‚éValue’l
|
---|
578 |
|
---|
579 | // ‹ß–T’T�õ
|
---|
580 | if (optionFlag) {
|
---|
581 | optionIndex = randomOptionIndex(issueIndexMin, issueIndexMax); // ƒ‰ƒ“ƒ_ƒ€‚É’l‚ð•Ï‚¦‚é
|
---|
582 | } else {
|
---|
583 | ValueInteger lIssueValue = (ValueInteger) bid
|
---|
584 | .getValue(issueNumber); // Žw’肵‚½issue‚ÌValue
|
---|
585 | int issueValue = Integer.valueOf(lIssueValue.toString())
|
---|
586 | .intValue();
|
---|
587 | optionIndex = nextOptionIndex(issueIndexMin, issueIndexMax,
|
---|
588 | issueValue); // ’l‚ð1‘�Œ¸‚³‚¹‚é
|
---|
589 | }
|
---|
590 |
|
---|
591 | nextBid = nextBid.putValue(issueNumber,
|
---|
592 | new ValueInteger(optionIndex)); // Œ»�Ý‚ÌBid‚©‚çIssue‚Ì’l‚ð“ü‚ê‘Ö‚¦‚é
|
---|
593 | }
|
---|
594 | return nextBid;
|
---|
595 | }
|
---|
596 |
|
---|
597 | @Override
|
---|
598 | public String getDescription() {
|
---|
599 | return "ANAC2014 compatible with non-linear utility spaces";
|
---|
600 | }
|
---|
601 | }
|
---|
602 |
|
---|
603 | class AgentMData implements Serializable {
|
---|
604 | Bid lastBid;
|
---|
605 | Boolean isAgreement;
|
---|
606 |
|
---|
607 | public AgentMData(Bid last) {
|
---|
608 | this.lastBid = last;
|
---|
609 | }
|
---|
610 |
|
---|
611 | public Bid getBid() {
|
---|
612 | return lastBid;
|
---|
613 | }
|
---|
614 | } |
---|