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