1 | package agents.anac.y2011.BramAgent;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Collections;
|
---|
5 | import java.util.Comparator;
|
---|
6 | import java.util.Date;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Random;
|
---|
10 |
|
---|
11 | import genius.core.Agent;
|
---|
12 | import genius.core.Bid;
|
---|
13 | import genius.core.BidIterator;
|
---|
14 | import genius.core.SupportedNegotiationSetting;
|
---|
15 | import genius.core.actions.Accept;
|
---|
16 | import genius.core.actions.Action;
|
---|
17 | import genius.core.actions.ActionWithBid;
|
---|
18 | import genius.core.actions.Offer;
|
---|
19 | import genius.core.issue.Issue;
|
---|
20 | import genius.core.issue.IssueDiscrete;
|
---|
21 | import genius.core.issue.IssueInteger;
|
---|
22 | import genius.core.issue.IssueReal;
|
---|
23 | import genius.core.issue.Value;
|
---|
24 | import genius.core.issue.ValueInteger;
|
---|
25 | import genius.core.issue.ValueReal;
|
---|
26 |
|
---|
27 | public class BRAMAgent extends Agent {
|
---|
28 | /* FINAL VARIABLES */
|
---|
29 | private final double TIME_TO_CREATE_BIDS_ARRAY = 2000;// The time that we
|
---|
30 | // allocate to
|
---|
31 | // creating the bids
|
---|
32 | // array
|
---|
33 | private final double FREQUENCY_OF_PROPOSAL = 0.2;// If the frequency of the
|
---|
34 | // proposal is larger
|
---|
35 | // than this variable
|
---|
36 | // than we won't propose
|
---|
37 | // it
|
---|
38 | // The threshold will be calculated as percentage of the required utility
|
---|
39 | // depending of the elapsed time
|
---|
40 |
|
---|
41 | private final double THRESHOLD_PERC_FLEXIBILITY_1 = 0.07;
|
---|
42 | private final double THRESHOLD_PERC_FLEXIBILITY_2 = 0.15;
|
---|
43 | private final double THRESHOLD_PERC_FLEXIBILITY_3 = 0.3;
|
---|
44 | private final double THRESHOLD_PERC_FLEXIBILITY_4 = 0.8;
|
---|
45 |
|
---|
46 | // The number of opponent's bids that we save in order to learn its
|
---|
47 | // preferences
|
---|
48 | private final int OPPONENT_ARRAY_SIZE = 10;
|
---|
49 | /* MEMBERS */
|
---|
50 | private Action actionOfPartner;// The action of the opponent
|
---|
51 | private Bid bestBid;// The best bid that our agent offered
|
---|
52 | private double maxUtility;// The maximum utility that our agent can get
|
---|
53 | private ArrayList<Bid> ourBidsArray;// An Array that contains all the bids
|
---|
54 | // that our agent can offer
|
---|
55 | private ArrayList<Bid> opponentBidsArray;// An Array that contains the last
|
---|
56 | // 10 bids that the opponent
|
---|
57 | // agent offered
|
---|
58 | private int lastPositionInBidArray;// The position in the bid array of the
|
---|
59 | // our agent last offer
|
---|
60 | private int[] bidsCountProposalArray;// An array that saves the number of
|
---|
61 | // offers that were made per each
|
---|
62 | // bid
|
---|
63 | private int numOfProposalsFromOurBidsArray;// The number of proposals that
|
---|
64 | // were made - NOT including the
|
---|
65 | // proposals that were made in
|
---|
66 | // the TIME_TO_OFFER_MAX_BID
|
---|
67 | // time
|
---|
68 | private double offeredUtility;// The utility of the current bid that the
|
---|
69 | // opponent had offered
|
---|
70 | private double threshold;// The threshold - we will accept any offer that
|
---|
71 | // its utility is larger than the threshold
|
---|
72 | private int randomInterval;
|
---|
73 | private int randomOffset;
|
---|
74 | /* Data Structures for any type of issue */
|
---|
75 | private ArrayList<ArrayList<Integer>> opponentBidsStatisticsForReal;
|
---|
76 | private ArrayList<HashMap<Value, Integer>> opponentBidsStatisticsDiscrete;
|
---|
77 | private ArrayList<ArrayList<Integer>> opponentBidsStatisticsForInteger;
|
---|
78 | private Random random100;
|
---|
79 | private Random random200;
|
---|
80 | private Random random300;
|
---|
81 | private Bid previousOfferedBid;
|
---|
82 | private final boolean TEST_EQUIVALENCE = false;
|
---|
83 | int round = 0;
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public void init() {
|
---|
87 | actionOfPartner = null;
|
---|
88 | ourBidsArray = new ArrayList<Bid>();
|
---|
89 | bidsCountProposalArray = null;
|
---|
90 | lastPositionInBidArray = 0;
|
---|
91 | numOfProposalsFromOurBidsArray = 0;
|
---|
92 | randomInterval = 8;
|
---|
93 | randomOffset = 4;
|
---|
94 | opponentBidsArray = new ArrayList<Bid>();
|
---|
95 | initializeDataStructures();
|
---|
96 | if (TEST_EQUIVALENCE) {
|
---|
97 | random100 = new Random(100);
|
---|
98 | random200 = new Random(200);
|
---|
99 | random300 = new Random(300);
|
---|
100 | } else {
|
---|
101 | random100 = new Random();
|
---|
102 | random200 = new Random();
|
---|
103 | random300 = new Random();
|
---|
104 | }
|
---|
105 | try {
|
---|
106 | bestBid = this.utilitySpace.getMaxUtilityBid();
|
---|
107 | maxUtility = this.utilitySpace.getUtilityWithDiscount(bestBid,
|
---|
108 | timeline);
|
---|
109 | ourBidsArray.add(bestBid);// The offer with the maximum utility will
|
---|
110 | // be offered at the beginning
|
---|
111 | threshold = maxUtility;
|
---|
112 | previousOfferedBid = bestBid;
|
---|
113 |
|
---|
114 | } catch (Exception e) {
|
---|
115 | e.printStackTrace();
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Override
|
---|
120 | public void ReceiveMessage(Action opponentAction) {
|
---|
121 | actionOfPartner = opponentAction;
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Override
|
---|
125 | public String getName() {
|
---|
126 | return "BRAMAgent";
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Override
|
---|
130 | public Action chooseAction() {
|
---|
131 | round++;
|
---|
132 | Action action = null;
|
---|
133 | Bid bid2offer = new Bid(utilitySpace.getDomain());
|
---|
134 | threshold = getNewThreshold();// Update the threshold according to the
|
---|
135 | // discount factor
|
---|
136 |
|
---|
137 | try {
|
---|
138 | // If we start the negotiation, we will offer the bid with
|
---|
139 | // the maximum utility for us
|
---|
140 | if (actionOfPartner == null) {
|
---|
141 | bid2offer = this.utilitySpace.getMaxUtilityBid();
|
---|
142 | action = new Offer(this.getAgentID(), bid2offer);
|
---|
143 | } else if (actionOfPartner instanceof Offer) {
|
---|
144 | offeredUtility = this.utilitySpace.getUtilityWithDiscount(
|
---|
145 | ((Offer) actionOfPartner).getBid(), timeline);
|
---|
146 |
|
---|
147 | if (offeredUtility >= threshold)// If the utility of the bid
|
---|
148 | // that we received from the
|
---|
149 | // opponent
|
---|
150 | // is larger than the threshold
|
---|
151 | // that we ready to accept,
|
---|
152 | // we will accept the offer
|
---|
153 | action = new Accept(this.getAgentID(),
|
---|
154 | ((Offer) actionOfPartner).getBid());
|
---|
155 | else {
|
---|
156 | Bid bidToRemove = null;
|
---|
157 | Bid opponentBid = ((Offer) actionOfPartner).getBid();
|
---|
158 | Bid bidToOffer = null;
|
---|
159 | if (opponentBidsArray.size() < OPPONENT_ARRAY_SIZE) {// In
|
---|
160 | // this
|
---|
161 | // phase
|
---|
162 | // we
|
---|
163 | // are
|
---|
164 | // gathering
|
---|
165 | // information
|
---|
166 | // about
|
---|
167 | // the
|
---|
168 | // bids
|
---|
169 | // that
|
---|
170 | // the
|
---|
171 | // opponent
|
---|
172 | // is
|
---|
173 | // offering
|
---|
174 | opponentBidsArray.add(opponentBid);
|
---|
175 | updateStatistics(opponentBid, false);
|
---|
176 | bidToOffer = bestBid;
|
---|
177 | } else {
|
---|
178 | // Remove the oldest bid and receiveMessage the
|
---|
179 | // statistics
|
---|
180 | bidToRemove = opponentBidsArray.get(0);
|
---|
181 | updateStatistics(bidToRemove, true);
|
---|
182 | opponentBidsArray.remove(0);
|
---|
183 | // Add the new bid of the opponent and receiveMessage
|
---|
184 | // the
|
---|
185 | // statistics
|
---|
186 | opponentBidsArray.add(opponentBid);
|
---|
187 | updateStatistics(opponentBid, false);
|
---|
188 | // Calculate the bid that the agent will offer
|
---|
189 | bidToOffer = getBidToOffer();
|
---|
190 | }
|
---|
191 |
|
---|
192 | if (offeredUtility >= this.utilitySpace
|
---|
193 | .getUtilityWithDiscount(bidToOffer, timeline)) {
|
---|
194 | action = new Accept(this.getAgentID(),
|
---|
195 | ((Offer) actionOfPartner).getBid());
|
---|
196 | } else {
|
---|
197 | action = new Offer(this.getAgentID(), bidToOffer);
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | }
|
---|
202 | } catch (Exception e) {
|
---|
203 | e.printStackTrace();
|
---|
204 | action = new Accept(this.getAgentID(),
|
---|
205 | ((ActionWithBid) actionOfPartner).getBid());
|
---|
206 | }
|
---|
207 |
|
---|
208 | return action;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * This function updates the statistics of the bids that were received from
|
---|
213 | * the opponent
|
---|
214 | *
|
---|
215 | * @param bidToUpdate
|
---|
216 | * - the bid that we want to receiveMessage its statistics
|
---|
217 | * @param toRemove
|
---|
218 | * - flag that indicates if we removing (or adding) a bid to (or
|
---|
219 | * from) the statistics
|
---|
220 | */
|
---|
221 | private void updateStatistics(Bid bidToUpdate, boolean toRemove) {
|
---|
222 | try {
|
---|
223 | List<Issue> issues = utilitySpace.getDomain().getIssues();
|
---|
224 |
|
---|
225 | // counters for each type of issue
|
---|
226 | int realIndex = 0;
|
---|
227 | int discreteIndex = 0;
|
---|
228 | int integerIndex = 0;
|
---|
229 |
|
---|
230 | for (Issue lIssue : issues) {
|
---|
231 | int issueNum = lIssue.getNumber();
|
---|
232 | Value v = bidToUpdate.getValue(issueNum);
|
---|
233 | switch (lIssue.getType()) {
|
---|
234 | case DISCRETE:
|
---|
235 | if (opponentBidsStatisticsDiscrete != null
|
---|
236 | && opponentBidsStatisticsDiscrete
|
---|
237 | .get(discreteIndex) != null) {
|
---|
238 | int counterPerValue = opponentBidsStatisticsDiscrete
|
---|
239 | .get(discreteIndex).get(v);
|
---|
240 | if (toRemove)
|
---|
241 | counterPerValue--;
|
---|
242 | else
|
---|
243 | counterPerValue++;
|
---|
244 | opponentBidsStatisticsDiscrete.get(discreteIndex).put(v,
|
---|
245 | counterPerValue);
|
---|
246 | }
|
---|
247 | discreteIndex++;
|
---|
248 | break;
|
---|
249 |
|
---|
250 | case REAL:
|
---|
251 |
|
---|
252 | IssueReal lIssueReal = (IssueReal) lIssue;
|
---|
253 | int lNumOfPossibleRealValues = lIssueReal
|
---|
254 | .getNumberOfDiscretizationSteps();
|
---|
255 | double lOneStep = (lIssueReal.getUpperBound()
|
---|
256 | - lIssueReal.getLowerBound())
|
---|
257 | / lNumOfPossibleRealValues;
|
---|
258 | double first = lIssueReal.getLowerBound();
|
---|
259 | double last = lIssueReal.getLowerBound() + lOneStep;
|
---|
260 | double valueReal = ((ValueReal) v).getValue();
|
---|
261 | boolean found = false;
|
---|
262 |
|
---|
263 | for (int i = 0; !found && i < opponentBidsStatisticsForReal
|
---|
264 | .get(realIndex).size(); i++) {
|
---|
265 | if (valueReal >= first && valueReal <= last) {
|
---|
266 | int countPerValue = opponentBidsStatisticsForReal
|
---|
267 | .get(realIndex).get(i);
|
---|
268 | if (toRemove)
|
---|
269 | countPerValue--;
|
---|
270 | else
|
---|
271 | countPerValue++;
|
---|
272 |
|
---|
273 | opponentBidsStatisticsForReal.get(realIndex).set(i,
|
---|
274 | countPerValue);
|
---|
275 | found = true;
|
---|
276 | }
|
---|
277 | first = last;
|
---|
278 | last = last + lOneStep;
|
---|
279 | }
|
---|
280 | // If no matching value was found, receiveMessage the last
|
---|
281 | // cell
|
---|
282 | if (found == false) {
|
---|
283 | int i = opponentBidsStatisticsForReal.get(realIndex)
|
---|
284 | .size() - 1;
|
---|
285 | int countPerValue = opponentBidsStatisticsForReal
|
---|
286 | .get(realIndex).get(i);
|
---|
287 | if (toRemove)
|
---|
288 | countPerValue--;
|
---|
289 | else
|
---|
290 | countPerValue++;
|
---|
291 |
|
---|
292 | opponentBidsStatisticsForReal.get(realIndex).set(i,
|
---|
293 | countPerValue);
|
---|
294 | }
|
---|
295 | realIndex++;
|
---|
296 | break;
|
---|
297 |
|
---|
298 | case INTEGER:
|
---|
299 |
|
---|
300 | IssueInteger lIssueInteger = (IssueInteger) lIssue;
|
---|
301 | int valueInteger = ((ValueInteger) v).getValue();
|
---|
302 |
|
---|
303 | int valueIndex = valueInteger
|
---|
304 | - lIssueInteger.getLowerBound(); // For
|
---|
305 | // ex.
|
---|
306 | // LowerBound
|
---|
307 | // index
|
---|
308 | // is
|
---|
309 | // 0,
|
---|
310 | // and
|
---|
311 | // the
|
---|
312 | // lower
|
---|
313 | // bound
|
---|
314 | // is
|
---|
315 | // 2,
|
---|
316 | // the
|
---|
317 | // value
|
---|
318 | // is
|
---|
319 | // 4,
|
---|
320 | // so
|
---|
321 | // the
|
---|
322 | // index
|
---|
323 | // of
|
---|
324 | // 4
|
---|
325 | // would
|
---|
326 | // be
|
---|
327 | // 2
|
---|
328 | // which
|
---|
329 | // is
|
---|
330 | // exactly
|
---|
331 | // 4-2
|
---|
332 | int countPerValue = opponentBidsStatisticsForInteger
|
---|
333 | .get(integerIndex).get(valueIndex);
|
---|
334 | if (toRemove)
|
---|
335 | countPerValue--;
|
---|
336 | else
|
---|
337 | countPerValue++;
|
---|
338 |
|
---|
339 | opponentBidsStatisticsForInteger.get(integerIndex)
|
---|
340 | .set(valueIndex, countPerValue);
|
---|
341 | integerIndex++;
|
---|
342 | break;
|
---|
343 | }
|
---|
344 | }
|
---|
345 | } catch (Exception ex) {
|
---|
346 | ex.printStackTrace();
|
---|
347 | }
|
---|
348 |
|
---|
349 | }
|
---|
350 |
|
---|
351 | /**
|
---|
352 | * This function calculates the threshold. It takes into consideration the
|
---|
353 | * time that passed from the beginning of the game. As time goes by, the
|
---|
354 | * agent becoming more flexible to the offers that it is willing to accept.
|
---|
355 | *
|
---|
356 | * @return - the threshold
|
---|
357 | */
|
---|
358 | private double getNewThreshold() {
|
---|
359 | double minUtil = utilitySpace.getUtilityWithDiscount(
|
---|
360 | ourBidsArray.get(ourBidsArray.size() - 1), timeline);
|
---|
361 | double maxUtil = utilitySpace.getUtilityWithDiscount(bestBid, timeline);
|
---|
362 | double tresholdBestBidDiscount = 0.0;
|
---|
363 |
|
---|
364 | if (timeline.getTime() < 60.0 / 180.0) {
|
---|
365 | tresholdBestBidDiscount = maxUtil
|
---|
366 | - (maxUtil - minUtil) * THRESHOLD_PERC_FLEXIBILITY_1;
|
---|
367 | } else if (timeline.getTime() < 150.0 / 180.0) {
|
---|
368 | tresholdBestBidDiscount = maxUtil
|
---|
369 | - (maxUtil - minUtil) * THRESHOLD_PERC_FLEXIBILITY_2;
|
---|
370 | } else if (timeline.getTime() < 175.0 / 180.0)
|
---|
371 | tresholdBestBidDiscount = maxUtil
|
---|
372 | - (maxUtil - minUtil) * THRESHOLD_PERC_FLEXIBILITY_3;
|
---|
373 | else
|
---|
374 | tresholdBestBidDiscount = maxUtil
|
---|
375 | - (maxUtil - minUtil) * THRESHOLD_PERC_FLEXIBILITY_4;
|
---|
376 | return tresholdBestBidDiscount;
|
---|
377 |
|
---|
378 | }
|
---|
379 |
|
---|
380 | /**
|
---|
381 | * This function calculates the bid that the agent offers. If a calculated
|
---|
382 | * bid is close enough to the preferences of the opponent, and its utility
|
---|
383 | * is acceptable by our agent, our agent will offer it. Otherwise, we will
|
---|
384 | * offer a bid from
|
---|
385 | *
|
---|
386 | * @return
|
---|
387 | */
|
---|
388 | private Bid getBidToOffer() {
|
---|
389 | Bid bidWithMaxUtility = null;
|
---|
390 | try {
|
---|
391 | double maxUt = threshold;
|
---|
392 |
|
---|
393 | for (int i = 0; i < 10; i++) {
|
---|
394 | Bid currBid = createBidByOpponentModeling();
|
---|
395 | if (currBid != null) {
|
---|
396 | double currUtility = utilitySpace
|
---|
397 | .getUtilityWithDiscount(currBid, timeline);
|
---|
398 |
|
---|
399 | if (currUtility > maxUt) {
|
---|
400 | maxUt = currUtility;
|
---|
401 | bidWithMaxUtility = currBid;
|
---|
402 | }
|
---|
403 | }
|
---|
404 |
|
---|
405 | }
|
---|
406 | if (bidWithMaxUtility == null) {
|
---|
407 | return getBidFromBidsArray();
|
---|
408 |
|
---|
409 | } else {
|
---|
410 | return bidWithMaxUtility;
|
---|
411 | }
|
---|
412 | } catch (Exception e) {
|
---|
413 | e.printStackTrace();
|
---|
414 | }
|
---|
415 | return bidWithMaxUtility;
|
---|
416 | }
|
---|
417 |
|
---|
418 | /**
|
---|
419 | * This function creates random bids that the agent can offer and sorts it
|
---|
420 | * in a descending order.
|
---|
421 | *
|
---|
422 | * @return
|
---|
423 | */
|
---|
424 | private Bid getBidFromBidsArray() {
|
---|
425 |
|
---|
426 | if (ourBidsArray.size() == 1) {
|
---|
427 | // We get here only at the first time - when we want to build the
|
---|
428 | // array
|
---|
429 | fillBidsArray(new Date().getTime());
|
---|
430 | if (ourBidsArray.size() <= 50) {
|
---|
431 | randomInterval = 3;
|
---|
432 | randomOffset = 1;
|
---|
433 | }
|
---|
434 | initializeBidsFrequencyArray();
|
---|
435 | Collections.sort(ourBidsArray, new Comparator<Bid>() {
|
---|
436 | // @Override
|
---|
437 | @Override
|
---|
438 | public int compare(Bid bid1, Bid bid2) {
|
---|
439 | // We will sort the array in a descending order
|
---|
440 | double utility1 = 0.0;
|
---|
441 | double utility2 = 0.0;
|
---|
442 | try {
|
---|
443 | utility1 = utilitySpace.getUtility(bid1);
|
---|
444 | utility2 = utilitySpace.getUtility(bid2);
|
---|
445 | if (utility1 > utility2)
|
---|
446 | return -1;
|
---|
447 | else if (utility1 < utility2)
|
---|
448 | return 1;
|
---|
449 | } catch (Exception e) {
|
---|
450 | e.printStackTrace();
|
---|
451 | }
|
---|
452 | return 0;
|
---|
453 | }
|
---|
454 | });
|
---|
455 | }
|
---|
456 | // We will make an offer
|
---|
457 | numOfProposalsFromOurBidsArray++;
|
---|
458 | Bid bidToOffer = selectCurrentBidFromOurBidsArray();
|
---|
459 | return bidToOffer;
|
---|
460 | }
|
---|
461 |
|
---|
462 | /**
|
---|
463 | * This function creates a bid according to the preferences of the opponent.
|
---|
464 | * Meaning, we assume that if the opponent insisted on some value of an
|
---|
465 | * issue, it's probably important to it.
|
---|
466 | *
|
---|
467 | * @return
|
---|
468 | */
|
---|
469 | private Bid createBidByOpponentModeling() {
|
---|
470 | Bid bid = new Bid(utilitySpace.getDomain());
|
---|
471 | try {
|
---|
472 | HashMap<Integer, Value> valuesToOfferPerIssue = new HashMap<Integer, Value>();
|
---|
473 | List<Issue> issues = utilitySpace.getDomain().getIssues();
|
---|
474 |
|
---|
475 | // counters for each type of issue
|
---|
476 | int discreteIndex = 0;
|
---|
477 | int realIndex = 0;
|
---|
478 | int integerIndex = 0;
|
---|
479 |
|
---|
480 | for (Issue lIssue : issues) {
|
---|
481 |
|
---|
482 | int issueNum = lIssue.getNumber();
|
---|
483 | int indx = random100.nextInt(OPPONENT_ARRAY_SIZE);
|
---|
484 |
|
---|
485 | int first = 0;
|
---|
486 | int last = 0;
|
---|
487 |
|
---|
488 | switch (lIssue.getType()) {
|
---|
489 |
|
---|
490 | case DISCRETE:
|
---|
491 | HashMap<Value, Integer> valuesHash = new HashMap<Value, Integer>();
|
---|
492 | valuesHash = opponentBidsStatisticsDiscrete
|
---|
493 | .get(discreteIndex);
|
---|
494 |
|
---|
495 | // The keySet is the value that was proposed
|
---|
496 | for (Value v : valuesHash.keySet()) {
|
---|
497 |
|
---|
498 | first = last;
|
---|
499 | last = first + valuesHash.get(v);
|
---|
500 |
|
---|
501 | if (indx >= first && indx < last)
|
---|
502 | valuesToOfferPerIssue.put(issueNum, v);
|
---|
503 | }
|
---|
504 | discreteIndex++;
|
---|
505 |
|
---|
506 | break;
|
---|
507 |
|
---|
508 | case REAL:
|
---|
509 | IssueReal lIssueReal = (IssueReal) lIssue;
|
---|
510 | ArrayList<Integer> valueList = opponentBidsStatisticsForReal
|
---|
511 | .get(realIndex);
|
---|
512 |
|
---|
513 | for (int i = 0; i < valueList.size(); i++) {
|
---|
514 |
|
---|
515 | first = last;
|
---|
516 | last = first + valueList.get(i);
|
---|
517 |
|
---|
518 | if (indx >= first && indx <= last) {
|
---|
519 | int lNrOfOptions = lIssueReal
|
---|
520 | .getNumberOfDiscretizationSteps();
|
---|
521 | double lOneStep = (lIssueReal.getUpperBound()
|
---|
522 | - lIssueReal.getLowerBound())
|
---|
523 | / lNrOfOptions;
|
---|
524 | double lowerBound = lIssueReal.getLowerBound();
|
---|
525 | double realValueForBid = lowerBound
|
---|
526 | + lOneStep * indx
|
---|
527 | + random100.nextDouble() * lOneStep;
|
---|
528 | ValueReal valueForBid = new ValueReal(
|
---|
529 | realValueForBid);
|
---|
530 | valuesToOfferPerIssue.put(issueNum, valueForBid);
|
---|
531 | }
|
---|
532 | }
|
---|
533 | realIndex++;
|
---|
534 | break;
|
---|
535 |
|
---|
536 | case INTEGER:
|
---|
537 | IssueInteger lIssueInteger = (IssueInteger) lIssue;
|
---|
538 | ArrayList<Integer> integerValueList = opponentBidsStatisticsForInteger
|
---|
539 | .get(integerIndex);
|
---|
540 |
|
---|
541 | for (int i = 0; i < integerValueList.size(); i++) {
|
---|
542 | first = last;
|
---|
543 | last = first + integerValueList.get(i);
|
---|
544 |
|
---|
545 | if (indx >= first && indx <= last) {
|
---|
546 | int valuesLowerBound = lIssueInteger
|
---|
547 | .getLowerBound();
|
---|
548 | ValueInteger valueIntegerForBid = new ValueInteger(
|
---|
549 | valuesLowerBound + i);
|
---|
550 | valuesToOfferPerIssue.put(issueNum,
|
---|
551 | valueIntegerForBid);
|
---|
552 | }
|
---|
553 | }
|
---|
554 | integerIndex++;
|
---|
555 | break;
|
---|
556 | }
|
---|
557 |
|
---|
558 | bid = new Bid(utilitySpace.getDomain(), valuesToOfferPerIssue);
|
---|
559 | }
|
---|
560 | } catch (Exception e) {
|
---|
561 | e.printStackTrace();
|
---|
562 | }
|
---|
563 |
|
---|
564 | return bid;
|
---|
565 | }
|
---|
566 |
|
---|
567 | /**
|
---|
568 | * This function initializes the data structures that will be later used for
|
---|
569 | * the calculations of the statistics.
|
---|
570 | */
|
---|
571 | private void initializeDataStructures() {
|
---|
572 | try {
|
---|
573 | opponentBidsStatisticsForReal = new ArrayList<ArrayList<Integer>>();
|
---|
574 | opponentBidsStatisticsDiscrete = new ArrayList<HashMap<Value, Integer>>();
|
---|
575 | opponentBidsStatisticsForInteger = new ArrayList<ArrayList<Integer>>();
|
---|
576 |
|
---|
577 | List<Issue> issues = utilitySpace.getDomain().getIssues();
|
---|
578 |
|
---|
579 | for (Issue lIssue : issues) {
|
---|
580 |
|
---|
581 | switch (lIssue.getType()) {
|
---|
582 |
|
---|
583 | case DISCRETE:
|
---|
584 | IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
|
---|
585 | HashMap<Value, Integer> discreteIssueValuesMap = new HashMap<Value, Integer>();
|
---|
586 | for (int j = 0; j < lIssueDiscrete
|
---|
587 | .getNumberOfValues(); j++) {
|
---|
588 | Value v = lIssueDiscrete.getValue(j);
|
---|
589 | discreteIssueValuesMap.put(v, 0);
|
---|
590 | }
|
---|
591 |
|
---|
592 | opponentBidsStatisticsDiscrete.add(discreteIssueValuesMap);
|
---|
593 | break;
|
---|
594 |
|
---|
595 | case REAL:
|
---|
596 | IssueReal lIssueReal = (IssueReal) lIssue;
|
---|
597 | ArrayList<Integer> numProposalsPerValue = new ArrayList<Integer>();
|
---|
598 | int lNumOfPossibleValuesInThisIssue = lIssueReal
|
---|
599 | .getNumberOfDiscretizationSteps();
|
---|
600 | for (int i = 0; i < lNumOfPossibleValuesInThisIssue; i++) {
|
---|
601 | numProposalsPerValue.add(0);
|
---|
602 | }
|
---|
603 | opponentBidsStatisticsForReal.add(numProposalsPerValue);
|
---|
604 | break;
|
---|
605 |
|
---|
606 | case INTEGER:
|
---|
607 | IssueInteger lIssueInteger = (IssueInteger) lIssue;
|
---|
608 | ArrayList<Integer> numOfValueProposals = new ArrayList<Integer>();
|
---|
609 |
|
---|
610 | // number of possible value when issue is integer (we should
|
---|
611 | // add 1 in order to include all values)
|
---|
612 | int lNumOfPossibleValuesForThisIssue = lIssueInteger
|
---|
613 | .getUpperBound() - lIssueInteger.getLowerBound()
|
---|
614 | + 1;
|
---|
615 | for (int i = 0; i < lNumOfPossibleValuesForThisIssue; i++) {
|
---|
616 | numOfValueProposals.add(0);
|
---|
617 | }
|
---|
618 | opponentBidsStatisticsForInteger.add(numOfValueProposals);
|
---|
619 | break;
|
---|
620 | }
|
---|
621 | }
|
---|
622 | } catch (Exception e) {
|
---|
623 | e.printStackTrace();
|
---|
624 | }
|
---|
625 | }
|
---|
626 |
|
---|
627 | /**
|
---|
628 | * fillBidsArray filling the array with random bids The maximum time that
|
---|
629 | * this function can run is TIME_TO_CREATE_BIDS_ARRAY seconds However, it
|
---|
630 | * will stop if all the possible bids were created
|
---|
631 | *
|
---|
632 | * @param startTime
|
---|
633 | * - the time when this function was called (in seconds, from the
|
---|
634 | * beginning of the negotiation)
|
---|
635 | */
|
---|
636 | private void fillBidsArray(double startTime) {
|
---|
637 | int bidsMaxAmount = getBidMaxAmount();
|
---|
638 | int countNewBids = 0;
|
---|
639 |
|
---|
640 | if (TEST_EQUIVALENCE) {
|
---|
641 | BidIterator iterator = new BidIterator(
|
---|
642 | this.utilitySpace.getDomain());
|
---|
643 | while (iterator.hasNext()) {
|
---|
644 | ourBidsArray.add(iterator.next());
|
---|
645 | }
|
---|
646 | } else {
|
---|
647 | while (new Date().getTime() - startTime < TIME_TO_CREATE_BIDS_ARRAY
|
---|
648 | && countNewBids < bidsMaxAmount) {
|
---|
649 | try {
|
---|
650 | Bid newBid = getRandomBid();
|
---|
651 | if (!ourBidsArray.contains(newBid)) {
|
---|
652 | countNewBids++;
|
---|
653 | ourBidsArray.add(newBid);
|
---|
654 | }
|
---|
655 | } catch (Exception e) {
|
---|
656 | e.printStackTrace();
|
---|
657 | }
|
---|
658 | }
|
---|
659 | }
|
---|
660 | }
|
---|
661 |
|
---|
662 | /**
|
---|
663 | * getBidMaxAmount counts how many possible bids exists in the given domain
|
---|
664 | *
|
---|
665 | * @return the number of options
|
---|
666 | */
|
---|
667 | private int getBidMaxAmount() {
|
---|
668 | int count = 1;
|
---|
669 | List<Issue> issues = utilitySpace.getDomain().getIssues();
|
---|
670 | for (Issue lIssue : issues) {
|
---|
671 | switch (lIssue.getType()) {
|
---|
672 |
|
---|
673 | case DISCRETE:
|
---|
674 | IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
|
---|
675 | int numOfValues = lIssueDiscrete.getNumberOfValues();
|
---|
676 | count = count * numOfValues;
|
---|
677 | break;
|
---|
678 |
|
---|
679 | case REAL:
|
---|
680 | IssueReal lIssueReal = (IssueReal) lIssue;
|
---|
681 | count = count * lIssueReal.getNumberOfDiscretizationSteps();
|
---|
682 | break;
|
---|
683 |
|
---|
684 | case INTEGER:
|
---|
685 | IssueInteger lIssueInteger = (IssueInteger) lIssue;
|
---|
686 | // number of possible value when issue is integer (we should add
|
---|
687 | // 1 in order to include all values)
|
---|
688 | count = count * (lIssueInteger.getUpperBound()
|
---|
689 | - lIssueInteger.getLowerBound() + 1);
|
---|
690 | break;
|
---|
691 | }
|
---|
692 | }
|
---|
693 | return count;
|
---|
694 | }
|
---|
695 |
|
---|
696 | /**
|
---|
697 | * @return a random bid
|
---|
698 | * @throws Exception
|
---|
699 | * if we can't compute the utility (no evaluators have been set)
|
---|
700 | * or when other evaluators than a DiscreteEvaluator are present
|
---|
701 | * in the utility space.
|
---|
702 | */
|
---|
703 | private Bid getRandomBid() {
|
---|
704 | Bid bid = null;
|
---|
705 | try {
|
---|
706 | HashMap<Integer, Value> values = new HashMap<Integer, Value>(); // pairs
|
---|
707 | // <issuenumber,chosen
|
---|
708 | // value
|
---|
709 | // string>
|
---|
710 | List<Issue> issues = utilitySpace.getDomain().getIssues();
|
---|
711 |
|
---|
712 | for (Issue lIssue : issues) {
|
---|
713 | switch (lIssue.getType()) {
|
---|
714 |
|
---|
715 | case DISCRETE:
|
---|
716 |
|
---|
717 | IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
|
---|
718 | int optionIndex = random200
|
---|
719 | .nextInt(lIssueDiscrete.getNumberOfValues());
|
---|
720 | values.put(lIssue.getNumber(),
|
---|
721 | lIssueDiscrete.getValue(optionIndex));
|
---|
722 | break;
|
---|
723 |
|
---|
724 | case REAL:
|
---|
725 |
|
---|
726 | IssueReal lIssueReal = (IssueReal) lIssue;
|
---|
727 | int lNrOfOptions = lIssueReal
|
---|
728 | .getNumberOfDiscretizationSteps();
|
---|
729 | double lOneStep = (lIssueReal.getUpperBound()
|
---|
730 | - lIssueReal.getLowerBound()) / lNrOfOptions;
|
---|
731 | int lOptionIndex = random200.nextInt(lNrOfOptions);
|
---|
732 | if (lOptionIndex >= lNrOfOptions)
|
---|
733 | lOptionIndex = lNrOfOptions - 1;
|
---|
734 | ValueReal value = new ValueReal(
|
---|
735 | lIssueReal.getLowerBound() + lOneStep * lOptionIndex
|
---|
736 | + random200.nextDouble() * lOneStep);
|
---|
737 | values.put(lIssueReal.getNumber(), value);
|
---|
738 | break;
|
---|
739 |
|
---|
740 | case INTEGER:
|
---|
741 |
|
---|
742 | IssueInteger lIssueInteger = (IssueInteger) lIssue;
|
---|
743 | // number of possible value when issue is integer
|
---|
744 | int numOfPossibleIntVals = lIssueInteger.getUpperBound()
|
---|
745 | - lIssueInteger.getLowerBound();
|
---|
746 | int randomIndex = random200.nextInt(numOfPossibleIntVals)
|
---|
747 | + lIssueInteger.getLowerBound();
|
---|
748 | ValueInteger randomValueInteger = new ValueInteger(
|
---|
749 | randomIndex);
|
---|
750 | values.put(lIssue.getNumber(), randomValueInteger);
|
---|
751 | break;
|
---|
752 | }
|
---|
753 | }
|
---|
754 | bid = new Bid(utilitySpace.getDomain(), values);
|
---|
755 | } catch (Exception ex) {
|
---|
756 | ex.printStackTrace();
|
---|
757 | }
|
---|
758 |
|
---|
759 | return bid;
|
---|
760 | }
|
---|
761 |
|
---|
762 | /**
|
---|
763 | * selectCurrentBid - This function selects the next bid to offer to the
|
---|
764 | * opponent. The bid is selected randomly, with skips up and down.
|
---|
765 | *
|
---|
766 | * @return
|
---|
767 | */
|
---|
768 | private Bid selectCurrentBidFromOurBidsArray() {
|
---|
769 | int value = random300.nextInt(randomInterval);
|
---|
770 | int rndNum = value - randomOffset;
|
---|
771 | int arraySize = ourBidsArray.size();
|
---|
772 | int newIndex = 0;
|
---|
773 | if (lastPositionInBidArray + rndNum < 0)// If the index is smaller than
|
---|
774 | // the lower bound of the array
|
---|
775 | // receiveMessage it to the
|
---|
776 | // first cell
|
---|
777 | newIndex = 0;
|
---|
778 | else if (lastPositionInBidArray + rndNum > (arraySize - 1))// If the
|
---|
779 | // index is
|
---|
780 | // larger
|
---|
781 | // than the
|
---|
782 | // upper
|
---|
783 | // bound of
|
---|
784 | // the array
|
---|
785 | // receiveMessage
|
---|
786 | // it
|
---|
787 | // to the
|
---|
788 | // last cell
|
---|
789 | newIndex = arraySize - 1;
|
---|
790 | else
|
---|
791 | newIndex = lastPositionInBidArray + rndNum;
|
---|
792 | while ((bidsCountProposalArray[newIndex]
|
---|
793 | / numOfProposalsFromOurBidsArray) > FREQUENCY_OF_PROPOSAL) {// If
|
---|
794 | // this
|
---|
795 | // bid
|
---|
796 | // was
|
---|
797 | // proposed
|
---|
798 | // too
|
---|
799 | // much
|
---|
800 | // than
|
---|
801 | // choose
|
---|
802 | // the
|
---|
803 | // next(neighbor)
|
---|
804 | // bid
|
---|
805 | newIndex++;
|
---|
806 | }
|
---|
807 | Bid toSend = ourBidsArray.get(newIndex);
|
---|
808 | // ADDED *********************************//
|
---|
809 | if (this.utilitySpace.getUtilityWithDiscount(toSend,
|
---|
810 | timeline) < threshold) {
|
---|
811 | toSend = previousOfferedBid;
|
---|
812 | bidsCountProposalArray[lastPositionInBidArray]++;// receiveMessage
|
---|
813 | // the
|
---|
814 | // number of
|
---|
815 | // times that
|
---|
816 | // this bid was
|
---|
817 | // offered
|
---|
818 | } else {
|
---|
819 | previousOfferedBid = toSend;
|
---|
820 | lastPositionInBidArray = newIndex;// receiveMessage the last
|
---|
821 | // position - this
|
---|
822 | // is an indication to the last
|
---|
823 | // bid that was offered
|
---|
824 | bidsCountProposalArray[newIndex]++;// receiveMessage the number of
|
---|
825 | // times
|
---|
826 | // that this bid was offered
|
---|
827 | }
|
---|
828 |
|
---|
829 | return toSend;
|
---|
830 | }
|
---|
831 |
|
---|
832 | /**
|
---|
833 | * initializeBidsFrequencyArray initializes all of the cells of the
|
---|
834 | * bidsCountProposalArray to 0
|
---|
835 | */
|
---|
836 | private void initializeBidsFrequencyArray() {
|
---|
837 |
|
---|
838 | bidsCountProposalArray = new int[ourBidsArray.size()];
|
---|
839 | for (int i = 0; i < bidsCountProposalArray.length; i++) {
|
---|
840 | bidsCountProposalArray[i] = 0;
|
---|
841 | }
|
---|
842 | }
|
---|
843 |
|
---|
844 | @Override
|
---|
845 | public SupportedNegotiationSetting getSupportedNegotiationSetting() {
|
---|
846 | return SupportedNegotiationSetting.getLinearUtilitySpaceInstance();
|
---|
847 | }
|
---|
848 |
|
---|
849 | @Override
|
---|
850 | public String getDescription() {
|
---|
851 | return "ANAC2011t";
|
---|
852 | }
|
---|
853 |
|
---|
854 | }
|
---|