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