source: src/main/java/agents/anac/y2017/mamenchis/Mamenchis.java@ 345

Last change on this file since 345 was 46, checked in by Tim Baarslag, 6 years ago

agents get their getUtilitySpace() from the API, not the info object anymore

File size: 32.4 KB
Line 
1package agents.anac.y2017.mamenchis;
2
3import java.util.List;
4
5import java.util.ArrayList;
6import java.util.HashMap;
7import java.util.LinkedList;
8import java.util.Map;
9
10import genius.core.AgentID;
11import genius.core.Bid;
12import genius.core.actions.Accept;
13import genius.core.actions.Action;
14import genius.core.actions.Offer;
15import genius.core.issue.Issue;
16import genius.core.issue.IssueDiscrete;
17import genius.core.issue.IssueInteger;
18import genius.core.issue.Value;
19import genius.core.issue.ValueInteger;
20import genius.core.list.Tuple;
21import genius.core.parties.AbstractNegotiationParty;
22import genius.core.parties.NegotiationInfo;
23import genius.core.persistent.PersistentDataType;
24import genius.core.persistent.StandardInfo;
25import genius.core.persistent.StandardInfoList;
26import genius.core.utility.AbstractUtilitySpace;
27
28/**
29 * Sample party that accepts the Nth offer, where N is the number of sessions
30 * this [agent-profile] already did.
31 */
32
33public class Mamenchis extends AbstractNegotiationParty {
34
35 private Bid lastReceivedBid = null;
36 private int nrChosenActions;
37 private StandardInfoList history;
38 private int numOfIssues;
39 private int numOfParties;
40 private double reservationValue;
41 private double initialUpper;
42 private double initialExponent;
43 private double upper;
44 private double lower;
45 private double exponent;
46 private double acceptUtil;
47 private double utility;
48 private List<Bid>[] proposedBids;
49 private int[] numOfBids;
50 private Map<Integer, Map<Value, Integer>>[] valueFrequences;
51 private Map<Integer, Integer>[] issueChangeFrequences;
52 private double[] maxUtils;
53 private double[] minUtils;
54 private LinkedList<Bid>[] proposedMaxBid;
55 private LinkedList<Bid>[] proposedMinBid;
56 private List<Bid>[] topBidsInOppsSpace;
57 private LinkedList<Bid>[] topBidsInMySpace;
58 private Bid myBid;
59 private List<Bid> fullUtilBids;
60 private int myIndex;
61 private String oppName;
62 private int oppIndex;
63 private boolean[] acceptAction;
64 private PreferModel[] pModel;
65 private Bid lastConcessionPoint;
66 private List<Bid>[] acceptableBidsWithOppValues;
67 private List<Bid> candidateBids;
68 private List<Bid> lastPeriodBidList;
69 private int myNextPartyIndex;
70 private int myLastPartyIndex;
71 private List<Bid>[] maxProductBids;
72 private List<Bid> maxUtilAdditionBidByOpps;
73 private double maxUtilAdditionByOpps;
74 private double time1;
75 private double time2;
76 private double time3;
77 private boolean hasHistory;
78 private boolean hasHistWithRightPosition;
79 private boolean hasInitParamByHist;
80 private List<Tuple<Bid, Double>> agreements;
81 private boolean hasAgreementHist;
82 private List<Bid> maxAgreedBids;
83 private Map<String, List<Double>>[] histUtils;
84 private Map<String, Double>[] maxUtilsInHist;
85 private List<Bid>[] maxUtilBidsByHist;
86 private List<Bid> sameBidsInOpps;
87
88 @Override
89 public void init(NegotiationInfo info) {
90
91 super.init(info);
92
93 if (getData().getPersistentDataType() != PersistentDataType.STANDARD) {
94 throw new IllegalStateException("need standard persistent data");
95 }
96 history = (StandardInfoList) getData().get();
97
98 if (!history.isEmpty()) {
99 Map<String, Double> maxutils = new HashMap<String, Double>();
100 StandardInfo lastinfo = history.get(history.size() - 1);
101 for (Tuple<String, Double> offered : lastinfo.getUtilities()) {
102 String party = offered.get1();
103 Double util = offered.get2();
104 maxutils.put(party, maxutils.containsKey(party)
105 ? Math.max(maxutils.get(party), util) : util);
106 }
107 }
108 hasAgreementHist = false;
109 if (!history.isEmpty()) {
110 hasHistory = true;
111 hasInitParamByHist = false;
112 agreements = new ArrayList<>();
113 maxAgreedBids = new ArrayList<>();
114 int numOfAgent = history.get(0).getAgentProfiles().size();
115 histUtils = new HashMap[numOfAgent];
116 maxUtilsInHist = new HashMap[numOfAgent];
117 maxUtilBidsByHist = new ArrayList[numOfAgent];
118 for (int i = 0; i < numOfAgent; i++) {
119 histUtils[i] = new HashMap<>();
120 maxUtilsInHist[i] = new HashMap<>();
121 maxUtilBidsByHist[i] = new ArrayList<>();
122 }
123 for (StandardInfo sessionInfo : history) {
124 if (sessionInfo.getAgreement().get1() != null) {
125 agreements.add(sessionInfo.getAgreement());
126 hasAgreementHist = true;
127 }
128 for (Tuple<String, Double> offered : sessionInfo
129 .getUtilities()) {
130 String party = offered.get1();
131 double util = offered.get2();
132 int index = Integer.parseInt(party.split("@")[1])
133 % numOfAgent;
134 if (!histUtils[index].containsKey(party)) {
135 List<Double> utils = new ArrayList<>();
136 utils.add(util);
137 histUtils[index].put(party, utils);
138 } else {
139 histUtils[index].get(party).add(util);
140 }
141 }
142 }
143 maxAgreedBids = getMaxBidsInAgreements();
144 for (int partyIndex = 0; partyIndex < numOfAgent; partyIndex++) {
145 for (String party : histUtils[partyIndex].keySet()) {
146 String partyName = party.split("@")[0];
147 List<Double> utils = histUtils[partyIndex].get(party);
148 double maxUtil = getMaxInArray(utils);
149 if (!maxUtilsInHist[partyIndex].containsKey(partyName)) {
150 maxUtilsInHist[partyIndex].put(partyName, maxUtil);
151 } else if (maxUtilsInHist[partyIndex]
152 .get(partyName) < maxUtil) {
153 maxUtilsInHist[partyIndex].put(partyName, maxUtil);
154 }
155 }
156 }
157 } else {
158 hasHistory = false;
159 hasHistWithRightPosition = false;
160 }
161
162 nrChosenActions = 0;
163 numOfIssues = getUtilitySpace().getDomain().getIssues().size();
164 fullUtilBids = new ArrayList<>();
165 fullUtilBids = getFullUtilBid(info.getUtilitySpace());
166 reservationValue = getUtilitySpace().getReservationValue();
167 sameBidsInOpps = new ArrayList<>();
168 maxUtilAdditionBidByOpps = new ArrayList<>();
169 lastPeriodBidList = new ArrayList<>();
170 maxUtilAdditionByOpps = 0.0;
171 initialUpper = 0.9;
172 initialExponent = 20;
173 upper = initialUpper;
174 lower = reservationValue;
175 exponent = initialExponent;
176 time1 = 0.9;
177 time2 = 0.99;
178 time3 = 0.995;
179 hasInitParamByHist = false;
180 }
181
182 @Override
183 public Action chooseAction(List<Class<? extends Action>> validActions) {
184 nrChosenActions++;
185 Action action = null;
186 Bid bid = null;
187 acceptUtil = getAccept();
188 if (lastReceivedBid != null) {
189 if (utility >= acceptUtil
190 && !hasProposedMyLastRound(lastReceivedBid)) {
191 bid = new Bid(lastReceivedBid);
192 action = new Accept(getPartyId(), bid);
193 } else {
194 if (timeline.getTime() > time3) {
195 if (hasHistory) {
196 if (!maxAgreedBids.isEmpty()) {
197 bid = maxAgreedBids.get(
198 nrChosenActions % maxAgreedBids.size());
199 action = new Offer(getPartyId(), bid);
200 } else {
201 if (!lastPeriodBidList.isEmpty()) {
202 bid = lastPeriodBidList.get(nrChosenActions
203 % lastPeriodBidList.size());
204 action = new Offer(getPartyId(), bid);
205 } else {
206 bid = lastReceivedBid;
207 action = new Accept(getPartyId(), bid);
208 return action;
209 }
210 }
211 } else {
212 if (!lastPeriodBidList.isEmpty()) {
213 bid = lastPeriodBidList.get(
214 nrChosenActions % lastPeriodBidList.size());
215 } else {
216 bid = lastReceivedBid;
217 action = new Accept(getPartyId(), bid);
218 myBid = bid;
219 return action;
220 }
221 }
222 action = new Offer(getPartyId(), bid);
223 myBid = bid;
224 } else if (timeline.getTime() > time2) {
225 if (nrChosenActions % 2 == 1) {
226 if (maxProductBids[myIndex].size() <= 0) {
227 maxProductBids[myIndex] = getMaxSocialWelfareBidsInList(
228 proposedBids[myIndex]);
229 }
230 if (maxProductBids[myIndex].size() > 0) {
231 Bid nBid = maxProductBids[myIndex]
232 .get(nrChosenActions
233 % maxProductBids[myIndex].size());
234 if (hasProposedMyLastRound(nBid)) {
235 bid = proposedBids[myIndex].get(
236 nrChosenActions % numOfBids[myIndex]);
237 } else {
238 bid = nBid;
239 }
240 } else {
241 bid = proposedBids[myIndex]
242 .get(nrChosenActions % numOfBids[myIndex]);
243 }
244 } else {
245 if (candidateBids.isEmpty()) {
246 if (!acceptAction[myNextPartyIndex]
247 && !acceptAction[myLastPartyIndex]) {
248 generateCandidateFromOppTopBids(acceptUtil,
249 candidateBids);
250 generateCandidateFromMyTopBids(acceptUtil,
251 candidateBids);
252 } else if (!acceptAction[myNextPartyIndex]
253 && acceptAction[myLastPartyIndex]) {
254 generateCandidateFromOppTopAndMyTopBids(
255 myNextPartyIndex, acceptUtil,
256 candidateBids);
257 generateCandidateFromMyFullAndOppTopBids(
258 acceptUtil, myNextPartyIndex,
259 candidateBids);
260 } else {
261 generateCandidateFromOppTopAndMyTopBids(
262 myLastPartyIndex, acceptUtil,
263 candidateBids);
264 generateCandidateFromMyFullAndOppTopBids(
265 acceptUtil, myLastPartyIndex,
266 candidateBids);
267 }
268 }
269 if (!candidateBids.isEmpty()) {
270 bid = candidateBids.remove(0);
271 if (hasProposedMyLastRound(bid)) {
272 bid = proposedBids[myIndex].get(
273 nrChosenActions % numOfBids[myIndex]);
274 }
275 } else {
276 bid = proposedBids[myIndex]
277 .get(nrChosenActions % numOfBids[myIndex]);
278 }
279 action = new Offer(getPartyId(), bid);
280 }
281 if (lastPeriodBidList.isEmpty()) {
282 lastPeriodBidList = getMaxUtilBidsInList(
283 sameBidsInOpps);
284 }
285 if (lastPeriodBidList.isEmpty()) {
286 mergeBids(proposedBids[myNextPartyIndex].get(0),
287 proposedBids[myLastPartyIndex].get(0), lower,
288 lastPeriodBidList, lastPeriodBidList);
289 }
290 } else if (timeline.getTime() > time1) {
291 if (candidateBids.isEmpty()) {
292 if (nrChosenActions % 3 == 0) {
293 generateCandidateFromOppTopBids(acceptUtil,
294 candidateBids);
295 generateCandidateFromMyTopBids(acceptUtil,
296 candidateBids);
297 } else {
298 if (!acceptAction[myNextPartyIndex]) {
299 generateCandidateFromMyFullAndOppTopBids(
300 acceptUtil, myNextPartyIndex,
301 candidateBids);
302 generateCandidateFromMyFullAndMyTopBids(
303 acceptUtil, myNextPartyIndex,
304 candidateBids);
305 generateCandidateFromOppTopAndMyTopBids(
306 myNextPartyIndex, acceptUtil,
307 candidateBids);
308 }
309 if (!acceptAction[myLastPartyIndex]) {
310 generateCandidateFromMyFullAndOppTopBids(
311 acceptUtil, myLastPartyIndex,
312 candidateBids);
313 generateCandidateFromMyFullAndMyTopBids(
314 acceptUtil, myLastPartyIndex,
315 candidateBids);
316 generateCandidateFromOppTopAndMyTopBids(
317 myLastPartyIndex, acceptUtil,
318 candidateBids);
319 }
320 }
321 }
322 if (!candidateBids.isEmpty()) {
323 bid = candidateBids.remove(0);
324 if (hasProposedMyLastRound(bid)) {
325 bid = proposedBids[myIndex]
326 .get(nrChosenActions % numOfBids[myIndex]);
327 }
328 } else {
329 bid = proposedBids[myIndex]
330 .get(nrChosenActions % numOfBids[myIndex]);
331 }
332 } else {
333 if (candidateBids.isEmpty()) {
334 generateCandidateFromMyFullBids(proposedBids[myIndex]);
335 generateCandidateFromMyTopBids(acceptUtil,
336 proposedBids[myIndex]);
337 generateCandidateFromMyFullAndMyTopBids(acceptUtil,
338 myNextPartyIndex, proposedBids[myIndex]);
339 generateCandidateFromMyFullAndMyTopBids(acceptUtil,
340 myLastPartyIndex, proposedBids[myIndex]);
341 }
342 if (!candidateBids.isEmpty()) {
343 bid = candidateBids.remove(0);
344 } else {
345 Bid nBid = fullUtilBids
346 .get(nrChosenActions % fullUtilBids.size());
347 if (proposedBids[myIndex].contains(nBid)) {
348 bid = proposedBids[myIndex]
349 .get(nrChosenActions % numOfBids[myIndex]);
350 } else {
351 bid = nBid;
352 }
353 }
354 }
355 if (bid == null) {
356 lastConcessionPoint = getBidWithLessBoundedUtil(
357 lastConcessionPoint, acceptUtil);
358 bid = lastConcessionPoint;
359 if (hasProposedMyLastRound(bid)) {
360 bid = proposedBids[myIndex]
361 .get(nrChosenActions % numOfBids[myIndex]);
362 }
363 }
364 if (bid == null) {
365 bid = myBid;
366 }
367 action = new Offer(getPartyId(), bid);
368 }
369 } else {
370 bid = new Bid(
371 fullUtilBids.get(nrChosenActions % fullUtilBids.size()));
372 action = new Offer(getPartyId(), bid);
373 }
374 myBid = bid;
375 numOfBids[myIndex]++;
376 proposedBids[myIndex].add(bid);
377 return action;
378 }
379
380 @SuppressWarnings("unchecked")
381 @Override
382 public void receiveMessage(AgentID sender, Action action) {
383 super.receiveMessage(sender, action);
384 if (sender == null) {
385 numOfParties = getNumberOfParties();
386 myIndex = Integer.parseInt(getPartyId().toString().split("@")[1])
387 % numOfParties;
388 myNextPartyIndex = (myIndex + 1) % numOfParties;
389 myLastPartyIndex = (myIndex + numOfParties - 1) % numOfParties;
390 proposedBids = new ArrayList[numOfParties];
391 numOfBids = new int[numOfParties];
392 valueFrequences = new HashMap[numOfParties];
393 issueChangeFrequences = new HashMap[numOfParties];
394 acceptAction = new boolean[numOfParties];
395 maxUtils = new double[numOfParties];
396 minUtils = new double[numOfParties];
397 proposedMaxBid = new LinkedList[numOfParties];
398 proposedMinBid = new LinkedList[numOfParties];
399 pModel = new PreferModel[numOfParties];
400 acceptableBidsWithOppValues = new ArrayList[numOfParties];
401 candidateBids = new ArrayList<>();
402 maxProductBids = new ArrayList[numOfParties];
403 topBidsInOppsSpace = new ArrayList[numOfParties];
404 topBidsInMySpace = new LinkedList[numOfParties];
405 for (int i = 0; i < numOfParties; i++) {
406 proposedBids[i] = new ArrayList<>();
407 valueFrequences[i] = new HashMap<>();
408 issueChangeFrequences[i] = new HashMap<>();
409 proposedMaxBid[i] = new LinkedList<>();
410 proposedMinBid[i] = new LinkedList<>();
411 pModel[i] = new PreferModel(getUtilitySpace(), numOfIssues);
412 acceptableBidsWithOppValues[i] = new ArrayList<>();
413 maxProductBids[i] = new ArrayList<>();
414 topBidsInOppsSpace[i] = new ArrayList<>();
415 topBidsInMySpace[i] = new LinkedList<>();
416 }
417 } else {
418 oppName = sender.toString().split("@")[0];
419 oppIndex = Integer.parseInt(sender.toString().split("@")[1])
420 % numOfParties;
421 if (action instanceof Offer) {
422 acceptAction[oppIndex] = false;
423 lastReceivedBid = ((Offer) action).getBid();
424 }
425 if (action instanceof Accept) {
426 acceptAction[oppIndex] = true;
427 lastReceivedBid = ((Accept) action).getBid();
428 }
429 utility = getUtility(lastReceivedBid);
430 proposedBids[oppIndex].add(lastReceivedBid);
431 numOfBids[oppIndex]++;
432 updateMaxOrMinBids(oppIndex);
433 if (isDifferentBid(oppIndex)) {
434 updateSameBidsInOpps(oppIndex, lastReceivedBid);
435 updateTopBidsInOppsSpace(oppIndex);
436 updateTopBidsInMySpace(oppIndex);
437 }
438 if (hasHistory) {
439 if (!hasInitParamByHist) {
440 hasHistWithRightPosition = false;
441 for (String name : maxUtilsInHist[oppIndex].keySet()) {
442 if (oppName.equals(name)) {
443 hasHistWithRightPosition = true;
444 }
445 }
446 if (hasHistWithRightPosition) {
447 double[] arrs = new double[3];
448 for (int i = 0; i < getNumberOfParties(); i++) {
449 if (i == myIndex) {
450 continue;
451 }
452 for (String name : maxUtilsInHist[i].keySet()) {
453 if (oppIndex == i) {
454 arrs[i] = maxUtilsInHist[oppIndex]
455 .get(oppName);
456 } else {
457 if (!name.equals(oppName)) {
458 arrs[i] = maxUtilsInHist[i].get(name);
459 }
460 }
461 }
462 }
463 double max = 0.0;
464 double min = 1.0;
465 for (int i = 0; i < 3; i++) {
466 if (i == myIndex) {
467 continue;
468 }
469 if (max < arrs[i]) {
470 max = arrs[i];
471 }
472 if (min > arrs[i]) {
473 min = arrs[i];
474 }
475 }
476 if (maxAgreedBids.isEmpty()) {
477 if (min - 0.3 > reservationValue) {
478 lower = min - 0.3;
479 } else {
480 lower = reservationValue;
481 }
482 } else {
483 double agreedUtil = getUtility(
484 maxAgreedBids.get(0));
485 double[] utilArrs = { max, min, agreedUtil };
486 upper = getMaxInArray(utilArrs);
487 for (int i = 0; i < utilArrs.length; i++) {
488 if (utilArrs[i] == upper) {
489 utilArrs[i] = 0.0;
490 }
491 }
492 lower = getMinInArray(utilArrs) - 0.1;
493 if (lower <= 0) {
494 lower = reservationValue;
495 }
496 }
497 exponent = initialExponent * (upper - lower) / 2;
498 } else {
499 double[] arrs = new double[3];
500 for (int i = 0; i < getNumberOfParties(); i++) {
501 if (i == myIndex) {
502 continue;
503 }
504 for (String name : maxUtilsInHist[i].keySet()) {
505 arrs[i] = maxUtilsInHist[i].get(name);
506 }
507 }
508 double max = 0.0;
509 double min = 1.0;
510 for (int i = 0; i < 3; i++) {
511 if (i == myIndex) {
512 continue;
513 }
514 if (max < arrs[i]) {
515 max = arrs[i];
516 }
517 if (min > arrs[i]) {
518 min = arrs[i];
519 }
520 }
521 if (maxAgreedBids.isEmpty()) {
522 if (min - 0.3 > reservationValue) {
523 lower = min - 0.3;
524 } else {
525 lower = reservationValue;
526 }
527 } else {
528 double agreedUtil = getUtility(
529 maxAgreedBids.get(0));
530 double[] utilArrs = { max, min, agreedUtil, 0.9 };
531 upper = getMaxInArray(utilArrs);
532 for (int i = 0; i < utilArrs.length; i++) {
533 if (utilArrs[i] == upper) {
534 utilArrs[i] = 0.0;
535 }
536 }
537 lower = getMinInArray(utilArrs) - 0.2;
538 if (lower <= 0) {
539 lower = reservationValue;
540 }
541 }
542 exponent = initialExponent * (upper - lower) / 2;
543 }
544 hasInitParamByHist = true;
545 }
546 if (hasInitParamByHist) {
547 if (hasHistWithRightPosition) {
548
549 } else {
550
551 }
552 }
553 } else {
554 if (isDifferentBid(oppIndex)) {
555 pModel[oppIndex].updateEvaluators(lastReceivedBid);
556 if (timeline.getTime() > 0.5 && timeline.getTime() < 0.9) {
557 updateMaxUtilAddition(oppIndex, lastReceivedBid);
558 }
559 }
560 updateConcessionParam();
561 }
562 }
563 }
564
565 @Override
566 public String getDescription() {
567 return "ANAC2017";
568 }
569
570 private double getAccept() {
571 double accept = 1.0;
572 accept = upper - (upper + 0.001 - lower)
573 * Math.pow(timeline.getTime(), exponent);
574 return accept;
575 }
576
577 private List<Bid> getFullUtilBid(AbstractUtilitySpace utilitySpace) {
578 Bid bid = null;
579 List<Bid> bidList = new ArrayList<>();
580 try {
581 bid = utilitySpace.getMaxUtilityBid();
582 } catch (Exception e) {
583 e.printStackTrace();
584 System.err.println(e.toString());
585 }
586 if (!bidList.contains(bid)) {
587 bidList.add(bid);
588 }
589 List<Issue> issues = getUtilitySpace().getDomain().getIssues();
590 for (Issue item : issues) {
591 Bid newBid = null;
592 switch (item.getType()) {
593 case DISCRETE:
594 IssueDiscrete discrete = (IssueDiscrete) item;
595 for (Value value : discrete.getValues()) {
596 newBid = bid.putValue(discrete.getNumber(), value);
597 if (getUtility(newBid) == 1.0
598 && !bidList.contains(newBid)) {
599 bidList.add(newBid);
600 }
601 }
602 break;
603 case INTEGER:
604 break;
605 default:
606 break;
607 }
608 }
609 return bidList;
610 }
611
612 @SuppressWarnings("unused")
613 private Bid getBidWithLessUtil(Bid bid) {
614 Bid srcBid = null;
615 Bid nBid = null;
616 if (bid == null) {
617 srcBid = fullUtilBids.get(0);
618 } else {
619 srcBid = new Bid(bid);
620 }
621 double oldU = getUtility(srcBid);
622 List<Bid> bidList = new ArrayList<>();
623 for (int i = 0; i < numOfIssues; i++) {
624 Issue issue = srcBid.getIssues().get(i);
625 switch (issue.getType()) {
626 case DISCRETE:
627 IssueDiscrete issueDiscrete = (IssueDiscrete) issue;
628 for (Value value : issueDiscrete.getValues()) {
629 nBid = srcBid.putValue(i + 1, value);
630 if (oldU > getUtility(nBid)) {
631 bidList.add(nBid);
632 }
633 }
634 break;
635 case INTEGER:
636 IssueInteger issueInteger = (IssueInteger) issue;
637 for (int j = issueInteger.getLowerBound(); j <= issueInteger
638 .getUpperBound(); j++) {
639 nBid = srcBid.putValue(i + 1, new ValueInteger(j));
640 if (oldU > getUtility(nBid)) {
641 bidList.add(nBid);
642 }
643 }
644 break;
645 default:
646 break;
647 }
648 }
649 if (bidList.isEmpty()) {
650 nBid = new Bid(srcBid);
651 } else {
652 double distance = oldU - 0.0;
653 for (Bid item : bidList) {
654 double distance2 = oldU - getUtility(item);
655 if (distance2 < distance) {
656 nBid = new Bid(item);
657 distance = distance2;
658 }
659 }
660 }
661 return nBid;
662 }
663
664 private Bid getBidWithLessBoundedUtil(Bid bid, double min) {
665 Bid srcBid = null;
666 Bid nBid = null;
667 if (bid == null) {
668 srcBid = fullUtilBids.get(0);
669 } else {
670 srcBid = new Bid(bid);
671 }
672 double oldU = getUtility(srcBid);
673 List<Bid> bidList = new ArrayList<>();
674 for (int i = 0; i < numOfIssues; i++) {
675 Issue issue = srcBid.getIssues().get(i);
676 double newU = 0.0;
677 switch (issue.getType()) {
678 case DISCRETE:
679 IssueDiscrete issueDiscrete = (IssueDiscrete) issue;
680 for (Value value : issueDiscrete.getValues()) {
681 nBid = srcBid.putValue(i + 1, value);
682 newU = getUtility(nBid);
683 if (oldU > newU && newU >= min) {
684 bidList.add(nBid);
685 }
686 }
687 break;
688 case INTEGER:
689 IssueInteger issueInteger = (IssueInteger) issue;
690 for (int j = issueInteger.getLowerBound(); j <= issueInteger
691 .getUpperBound(); j++) {
692 nBid = srcBid.putValue(i + 1, new ValueInteger(j));
693 newU = getUtility(nBid);
694 if (oldU > newU && newU >= min) {
695 bidList.add(nBid);
696 }
697 }
698 break;
699 default:
700 break;
701 }
702 }
703 if (bidList.isEmpty()) {
704 nBid = new Bid(srcBid);
705 } else {
706 double distance = oldU - 0.0;
707 for (Bid item : bidList) {
708 double distance2 = oldU - getUtility(item);
709 if (distance2 < distance) {
710 nBid = new Bid(item);
711 distance = distance2;
712 }
713 }
714 }
715 return nBid;
716 }
717
718 private void updateProposedMaxBid(int partyId) {
719 if (proposedMaxBid[partyId].isEmpty()) {
720 if (utility > maxUtils[partyId]) {
721 maxUtils[partyId] = utility;
722 proposedMaxBid[partyId].add(lastReceivedBid);
723 }
724 } else {
725 if (maxUtils[partyId] > utility) {
726 return;
727 }
728 if (maxUtils[partyId] == utility
729 && !proposedMaxBid[partyId].contains(lastReceivedBid)) {
730 proposedMaxBid[partyId].add(lastReceivedBid);
731 }
732 if (maxUtils[partyId] < utility) {
733 proposedMaxBid[partyId].clear();
734 proposedMaxBid[partyId].add(lastReceivedBid);
735 maxUtils[partyId] = utility;
736 }
737 }
738 }
739
740 private void updateProposedMinBid(int partyId) {
741 if (proposedMinBid[partyId].isEmpty()) {
742 proposedMinBid[partyId].add(lastReceivedBid);
743 if (0 >= minUtils[partyId]) {
744 minUtils[partyId] = utility;
745 }
746 } else {
747 if (minUtils[partyId] < utility) {
748 return;
749 }
750 if (minUtils[partyId] == utility
751 && !proposedMinBid[partyId].contains(lastReceivedBid)) {
752 proposedMinBid[partyId].add(lastReceivedBid);
753 }
754 if (minUtils[partyId] > utility) {
755 proposedMinBid[partyId].clear();
756 proposedMinBid[partyId].add(lastReceivedBid);
757 minUtils[partyId] = utility;
758 }
759 }
760 }
761
762 private void updateMaxOrMinBids(int partyId) {
763 updateProposedMaxBid(partyId);
764 updateProposedMinBid(partyId);
765 }
766
767 private void updateTopBidsInOppsSpace(int partyId) {
768 if (topBidsInOppsSpace[partyId].size() < 5
769 && !topBidsInOppsSpace[partyId].contains(lastReceivedBid)) {
770 topBidsInOppsSpace[partyId].add(lastReceivedBid);
771 }
772 }
773
774 private void updateTopBidsInMySpace(int partyId) {
775 if (topBidsInMySpace[partyId].contains(lastReceivedBid)) {
776 return;
777 } else {
778 if (topBidsInMySpace[partyId].size() < 5) {
779 topBidsInMySpace[partyId].add(lastReceivedBid);
780 } else {
781 int removeIndex = -1;
782 double maxDistance = 0.0;
783 for (int i = 0; i < topBidsInMySpace[partyId].size(); i++) {
784 double distance = utility
785 - getUtility(topBidsInMySpace[partyId].get(i));
786 if (distance > maxDistance) {
787 maxDistance = distance;
788 removeIndex = i;
789 }
790 }
791 if (removeIndex != -1) {
792 topBidsInMySpace[partyId].set(removeIndex, lastReceivedBid);
793 }
794 }
795 }
796 }
797
798 private void updateConcessionParam() {
799 if (!hasHistory) {
800 // ����Ϊ�����������С��utility
801 double maxInMinUtil = 0.0;
802 double minInMinUtil = 1.0;
803 double minInMaxUtil = 1.0;
804 // double[] avgUtil = new double[numOfParties];
805 for (int i = 0; i < numOfParties; i++) {
806 if (i == myIndex) {
807 // avgUtil[i] = 1.0;
808 continue;
809 }
810 if (maxUtils[i] == 0.0 || minUtils[i] == 0.0) {
811 continue;
812 }
813 double utilInMin2 = minUtils[i];
814 if (utilInMin2 > maxInMinUtil) {
815 maxInMinUtil = utilInMin2;
816 }
817 if (utilInMin2 < minInMinUtil) {
818 minInMinUtil = utilInMin2;
819 }
820 double utilInMax = maxUtils[i];
821 if (minInMaxUtil > utilInMax) {
822 minInMaxUtil = utilInMax;
823 }
824 // avgUtil[i] = (maxUtils[i]+minUtils[i])/2;
825 }
826 double lowerbound = 0.0;
827 if (reservationValue > minInMinUtil - 0.01) {
828 lowerbound = reservationValue;
829 } else {
830 lowerbound = minInMinUtil - 0.01;
831 }
832 lower = lowerbound;
833 if (minInMaxUtil <= 0.0) {
834 exponent = 1.0;
835 } else {
836 exponent = initialExponent * minInMaxUtil;
837 }
838 } else {
839
840 }
841 }
842
843 private double getMinInArray(double arr[]) {
844 double min = arr[0];
845 for (int i = 0; i < arr.length; i++) {
846 if (min > arr[i]) {
847 min = arr[i];
848 }
849 }
850 return min;
851 }
852
853 private double getMaxInArray(double[] arr) {
854 double max = 0.0;
855 for (int i = 0; i < arr.length; i++) {
856 if (max < arr[i]) {
857 max = arr[i];
858 }
859 }
860 return max;
861 }
862
863 private double getMaxInArray(List<Double> list) {
864 double max = 0.0;
865 for (double item : list) {
866 if (item > max) {
867 max = item;
868 }
869 }
870 return max;
871 }
872
873 private boolean isDifferentBid(int partyId) {
874 if (proposedBids[partyId].size() < 2) {
875 return true;
876 } else {
877 if (proposedBids[partyId].get(numOfBids[partyId] - 1).equals(
878 proposedBids[partyId].get(numOfBids[partyId] - 2))) {
879 return false;
880 } else {
881 return true;
882 }
883 }
884 }
885
886 private void alterBidValue(Bid srcBid, Map<Integer, Value> valueMap,
887 int issueNr, double acceptableUtil, List<Bid> bidList,
888 List<Bid> validationList) {
889 Bid nBid = new Bid(srcBid);
890 if (getUtility(nBid) >= acceptableUtil
891 && !validationList.contains(nBid)) {
892 bidList.add(nBid);
893 }
894 if (!valueMap.containsKey(issueNr)) {
895 return;
896 }
897 for (int j = issueNr; j <= valueMap.size(); j++) {
898 Value value = valueMap.get(j);
899 if (value.equals(srcBid.getValue(j))) {
900 continue;
901 }
902 nBid = srcBid.putValue(j, value);
903 int item = j + 1;
904 alterBidValue(nBid, valueMap, item, acceptableUtil, bidList,
905 validationList);
906 }
907 }
908
909 private void mergeBids(Bid fir, Bid sec, double acceptableUtil,
910 List<Bid> bidList, List<Bid> validationList) {
911 if (fir == null || sec == null) {
912 return;
913 }
914 if (fir.equals(sec)) {
915 if (!validationList.contains(fir)) {
916 bidList.add(fir);
917 }
918 return;
919 }
920 alterBidValue(fir, sec.getValues(), 1, acceptableUtil, bidList,
921 validationList);
922 }
923
924 private void generateCandidateFromMyFullBidsAndOppsTopBids(double minUtil,
925 int oppIndex, List<Bid> validationList) {
926 if (proposedBids[oppIndex].isEmpty()) {
927 return;
928 }
929 for (Bid item : fullUtilBids) {
930 for (Bid item2 : topBidsInOppsSpace[oppIndex]) {
931 if (item.equals(item2)) {
932 continue;
933 }
934 mergeBids(item, item2, minUtil, candidateBids, validationList);
935 }
936 }
937 }
938
939 private void generateCandidate(double acceptableUtil,
940 List<Bid> validationList) {
941 generateCandidateFromMyTopBids(acceptableUtil, validationList);
942 generateCandidateFromOppTopBids(acceptableUtil, validationList);
943 }
944
945 private List<Bid> getMaxSocialWelfareBidsInList(List<Bid> bidList) {
946 List<Bid> maxAdditionBids = new ArrayList<>();
947 List<Bid> maxProductBids = new ArrayList<>();
948 List<Bid> maxSocialWelfareBids = new ArrayList<>();
949 double maxAddition = 0.0;
950 double maxProduct = 0.0;
951 for (Bid bid : bidList) {
952 double bidAddition = 0.0;
953 double bidProduct = 1.0;
954 bidAddition = getUtilAddition(bid);
955 bidProduct = getUtilProduct(bid);
956 if (bidAddition > maxAddition) {
957 maxAdditionBids.clear();
958 maxAdditionBids.add(bid);
959 maxAddition = bidAddition;
960 } else if (bidAddition == maxAddition
961 && !maxAdditionBids.contains(bid)) {
962 maxAdditionBids.add(bid);
963 }
964 if (bidProduct > maxProduct) {
965 maxProductBids.clear();
966 maxProductBids.add(bid);
967 maxProduct = bidProduct;
968 } else if (bidProduct == maxProduct
969 && !maxProductBids.contains(bid)) {
970 maxProductBids.add(bid);
971 }
972 }
973 maxSocialWelfareBids.addAll(maxAdditionBids);
974 maxSocialWelfareBids.addAll(maxProductBids);
975 return maxSocialWelfareBids;
976 }
977
978 private double getUtilProduct(Bid bid) {
979 double product = 1.0;
980 for (int i = 0; i < numOfParties; i++) {
981 if (i == myIndex) {
982 product *= getUtility(bid);
983 }
984 product *= pModel[i].getUtil(bid);
985 }
986 return product;
987 }
988
989 private double getUtilAddition(Bid bid) {
990 double addition = 0.0;
991 for (int i = 0; i < numOfParties; i++) {
992 if (i == myIndex) {
993 addition += getUtility(bid);
994 }
995 addition += pModel[i].getUtil(bid);
996 }
997 return addition;
998 }
999
1000 private List<Bid> getMaxUtilBidsInList(List<Bid> bidList) {
1001 List<Bid> bids = new ArrayList<>();
1002 double maxUtil = 0.0;
1003 for (Bid item : bidList) {
1004 double util = getUtility(item);
1005 if (util < maxUtil) {
1006 continue;
1007 }
1008 if (util > maxUtil) {
1009 bids.clear();
1010 bids.add(item);
1011 maxUtil = util;
1012 } else {
1013 bids.add(item);
1014 }
1015 }
1016 return bids;
1017 }
1018
1019 private void updateSameBidsInOpps(int proposedPartyId, Bid bid) {
1020 boolean bidHasProposedByAllOpps = true;
1021 for (int i = 0; i < numOfParties; i++) {
1022 if (i == myIndex || i == proposedPartyId) {
1023 continue;
1024 }
1025 if (!proposedBids[i].contains(bid)) {
1026 bidHasProposedByAllOpps = false;
1027 return;
1028 } else {
1029 bidHasProposedByAllOpps = true;
1030 }
1031 }
1032 if (bidHasProposedByAllOpps) {
1033 sameBidsInOpps.add(bid);
1034 }
1035 }
1036
1037 private void updateMaxUtilAddition(int partyId, Bid bid) {
1038 double utilAddition = 0.0;
1039 for (int i = 0; i < numOfParties; i++) {
1040 if (partyId == myIndex) {
1041 utilAddition += getUtility(bid);
1042 } else {
1043 utilAddition += pModel[i].getUtil(bid);
1044 }
1045 }
1046 if (utilAddition < maxUtilAdditionByOpps) {
1047 return;
1048 }
1049 if (utilAddition > maxUtilAdditionByOpps) {
1050 maxUtilAdditionBidByOpps.clear();
1051 maxUtilAdditionBidByOpps.add(bid);
1052 maxUtilAdditionByOpps = utilAddition;
1053 } else {
1054 maxUtilAdditionBidByOpps.add(bid);
1055 }
1056 }
1057
1058 private void generateCandidateFromOppTopAndMyTopBids(int partyId,
1059 double minUtil, List<Bid> validationList) {
1060 if (proposedBids[partyId].isEmpty()) {
1061 return;
1062 }
1063 for (Bid item : topBidsInOppsSpace[partyId]) {
1064 for (Bid maxBid : topBidsInMySpace[partyId]) {
1065 mergeBids(maxBid, item, minUtil, candidateBids, validationList);
1066 }
1067 }
1068 }
1069
1070 private List<Bid> getMaxBidsInAgreements() {
1071 double maxUtil = 0.0;
1072 List<Bid> maxBids = new ArrayList<>();
1073 for (Tuple<Bid, Double> item : agreements) {
1074 if (item.get2() < maxUtil) {
1075 continue;
1076 }
1077 if (item.get2() > maxUtil) {
1078 maxBids.clear();
1079 maxBids.add(item.get1());
1080 maxUtil = item.get2();
1081 } else {
1082 if (!maxBids.contains(item.get1())) {
1083 maxBids.add(item.get1());
1084 }
1085 }
1086 }
1087 return maxBids;
1088 }
1089
1090 private boolean hasProposedMyLastRound(Bid bid) {
1091 if (numOfBids[myIndex] <= 0) {
1092 return false;
1093 }
1094 Bid lastRoundBid = proposedBids[myIndex].get(numOfBids[myIndex] - 1);
1095 return lastRoundBid.equals(bid);
1096 }
1097
1098 private void generateCandidateFromMyFullBids(List<Bid> validationList) {
1099 for (Bid item1 : fullUtilBids) {
1100 for (Bid item2 : fullUtilBids) {
1101 if (item1.equals(item2)) {
1102 if (validationList.contains(item1)) {
1103 continue;
1104 } else {
1105 candidateBids.add(item1);
1106 continue;
1107 }
1108 } else {
1109 mergeBids(item1, item2, 0.9, candidateBids, validationList);
1110 }
1111 }
1112 }
1113 }
1114
1115 private void generateCandidateFromMyFullAndMyTopBids(double accept,
1116 int partyId, List<Bid> validationList) {
1117 for (Bid item1 : fullUtilBids) {
1118 for (Bid item2 : topBidsInMySpace[partyId]) {
1119 mergeBids(item1, item2, accept, candidateBids, validationList);
1120 }
1121 }
1122 }
1123
1124 private void generateCandidateFromMyFullAndOppTopBids(double accept,
1125 int partyId, List<Bid> validationList) {
1126 for (Bid item1 : fullUtilBids) {
1127 for (Bid item2 : topBidsInOppsSpace[partyId]) {
1128 mergeBids(item1, item2, accept, candidateBids, validationList);
1129 }
1130 }
1131 }
1132
1133 private void generateCandidateFromMyTopBids(double acceptableUtil,
1134 List<Bid> validationList) {
1135 for (Bid fBid : topBidsInMySpace[myNextPartyIndex]) {
1136 for (Bid sBid : topBidsInMySpace[myLastPartyIndex]) {
1137 alterBidValue(fBid, sBid.getValues(), 1, acceptableUtil,
1138 candidateBids, validationList);
1139 }
1140 }
1141 }
1142
1143 private void generateCandidateFromOppTopBids(double acceptableUtil,
1144 List<Bid> validationList) {
1145 if (numOfBids[myLastPartyIndex] == 0
1146 || numOfBids[myNextPartyIndex] == 0) {
1147 return;
1148 }
1149 for (Bid item1 : topBidsInOppsSpace[myNextPartyIndex]) {
1150 for (Bid item2 : topBidsInOppsSpace[myLastPartyIndex]) {
1151 if (item1.equals(item2)) {
1152 continue;
1153 }
1154 mergeBids(item1, item2, acceptableUtil, candidateBids,
1155 validationList);
1156 }
1157 }
1158 }
1159}
Note: See TracBrowser for help on using the repository browser.