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