source: src/main/java/agents/anac/y2017/mosateam/Mosa.java

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

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

File size: 42.5 KB
RevLine 
[1]1package agents.anac.y2017.mosateam;
2
[46]3import java.util.List;
4
[1]5import java.util.ArrayList;
6import java.util.HashMap;
7import java.util.LinkedHashMap;
8import java.util.LinkedList;
9import java.util.Map;
10import java.util.Map.Entry;
11import java.util.Set;
12
13import genius.core.AgentID;
14import genius.core.Bid;
15import genius.core.actions.Accept;
16import genius.core.actions.Action;
17import genius.core.actions.Offer;
18import genius.core.issue.Issue;
19import genius.core.issue.IssueDiscrete;
20import genius.core.issue.IssueInteger;
21import genius.core.issue.Objective;
22import genius.core.issue.Value;
23import genius.core.issue.ValueDiscrete;
24import genius.core.issue.ValueInteger;
25import genius.core.list.Tuple;
26import genius.core.parties.AbstractNegotiationParty;
27import genius.core.parties.NegotiationInfo;
28import genius.core.persistent.PersistentDataType;
29import genius.core.persistent.StandardInfo;
30import genius.core.persistent.StandardInfoList;
31import genius.core.utility.AbstractUtilitySpace;
32import genius.core.utility.Evaluator;
33import genius.core.utility.EvaluatorDiscrete;
34import genius.core.utility.EvaluatorInteger;
35import genius.core.utility.UtilitySpace;
36import genius.core.xml.SimpleElement;
37
38/**
39 * Sample party that accepts the Nth offer, where N is the number of sessions
40 * this [agent-profile] already did.
41 *
42 * Members: Lichun Yuan (Southwest University), Jiancheng Wu (Southwest
43 * University). Affiliation: Southwest University
44 */
45
46public class Mosa extends AbstractNegotiationParty {
47
48 private Bid lastReceivedBid = null;
49 private int nrChosenActions;
50 private StandardInfoList history;
51 private int numOfIssues;
52 private int numOfParties;
53 private double reservationValue;
54 private double initialUpper;
55 private double initialExponent;
56 private double upper;
57 private double lower;
58 private double exponent;
59 private double acceptUtil;
60 private double utility;
61 private List<Bid>[] proposedBids;
62 private int[] numOfBids;
63 private Map<Integer, Map<Value, Integer>>[] valueFrequences;
64 private Map<Integer, Integer>[] issueChangeFrequences;
65 private double[] maxUtils;
66 private double[] minUtils;
67 private LinkedList<Bid>[] proposedMaxBid;
68 private LinkedList<Bid>[] proposedMinBid;
69 private List<Bid>[] topBidsInOppsSpace;
70 private LinkedList<Bid>[] topBidsInMySpace;
71 private Bid myBid;
72 private List<Bid> fullUtilBids;
73 private int myIndex;
74 private String oppName;
75 private int oppIndex;
76 private boolean[] acceptAction;
77 private PreferModel[] pModel;
78 private Bid lastConcessionPoint;
79 private List<Bid>[] acceptableBidsWithOppValues;
80 private List<Bid> candidateBids;
81 private List<Bid> lastPeriodBidList;
82 private int myNextPartyIndex;
83 private int myLastPartyIndex;
84 private List<Bid>[] maxProductBids;
85 private List<Bid> maxUtilAdditionBidByOpps;
86 private double maxUtilAdditionByOpps;
87 private double time1;
88 private double time2;
89 private double time3;
90 private boolean hasHistory;
91 private boolean hasHistWithRightPosition;
92 private boolean hasInitParamByHist;
93 private List<Tuple<Bid, Double>> agreements;
94 private boolean hasAgreementHist;
95 private List<Bid> maxAgreedBids;
96 private Map<String, List<Double>>[] histUtils;
97 private Map<String, Double>[] maxUtilsInHist;
98 private List<Bid>[] maxUtilBidsByHist;
99 private List<Bid> sameBidsInOpps;
100
101 @Override
102 public void init(NegotiationInfo info) {
103
104 super.init(info);
105
106 if (getData().getPersistentDataType() != PersistentDataType.STANDARD) {
107 throw new IllegalStateException("need standard persistent data");
108 }
109 history = (StandardInfoList) getData().get();
110
111 if (!history.isEmpty()) {
112 Map<String, Double> maxutils = new HashMap<String, Double>();
113 StandardInfo lastinfo = history.get(history.size() - 1);
114 for (Tuple<String, Double> offered : lastinfo.getUtilities()) {
115 String party = offered.get1();
116 Double util = offered.get2();
117 maxutils.put(party, maxutils.containsKey(party)
118 ? Math.max(maxutils.get(party), util) : util);
119 }
120 }
121 hasAgreementHist = false;
122 if (!history.isEmpty()) {
123 hasHistory = true;
124 hasInitParamByHist = false;
125 agreements = new ArrayList<>();
126 maxAgreedBids = new ArrayList<>();
127 int numOfAgent = history.get(0).getAgentProfiles().size();
128 histUtils = new HashMap[numOfAgent];
129 maxUtilsInHist = new HashMap[numOfAgent];
130 maxUtilBidsByHist = new ArrayList[numOfAgent];
131 for (int i = 0; i < numOfAgent; i++) {
132 histUtils[i] = new HashMap<>();
133 maxUtilsInHist[i] = new HashMap<>();
134 maxUtilBidsByHist[i] = new ArrayList<>();
135 }
136 for (StandardInfo sessionInfo : history) {
137 if (sessionInfo.getAgreement().get1() != null) {
138 agreements.add(sessionInfo.getAgreement());
139 hasAgreementHist = true;
140 }
141 for (Tuple<String, Double> offered : sessionInfo
142 .getUtilities()) {
143 String party = offered.get1();
144 double util = offered.get2();
145 int index = Integer.parseInt(party.split("@")[1])
146 % numOfAgent;
147 if (!histUtils[index].containsKey(party)) {
148 List<Double> utils = new ArrayList<>();
149 utils.add(util);
150 histUtils[index].put(party, utils);
151 } else {
152 histUtils[index].get(party).add(util);
153 }
154 }
155 }
156 maxAgreedBids = getMaxBidsInAgreements();
157 for (int partyIndex = 0; partyIndex < numOfAgent; partyIndex++) {
158 for (String party : histUtils[partyIndex].keySet()) {
159 String partyName = party.split("@")[0];
160 List<Double> utils = histUtils[partyIndex].get(party);
161 double maxUtil = getMaxInArray(utils);
162 if (!maxUtilsInHist[partyIndex].containsKey(partyName)) {
163 maxUtilsInHist[partyIndex].put(partyName, maxUtil);
164 } else if (maxUtilsInHist[partyIndex]
165 .get(partyName) < maxUtil) {
166 maxUtilsInHist[partyIndex].put(partyName, maxUtil);
167 }
168 }
169 }
170 } else {
171 hasHistory = false;
172 hasHistWithRightPosition = false;
173 }
174
175 nrChosenActions = 0;
[46]176 numOfIssues = getUtilitySpace().getDomain().getIssues().size();
[1]177 fullUtilBids = new ArrayList<>();
[46]178 fullUtilBids = getFullUtilBid(getUtilitySpace());
[1]179 reservationValue = getUtilitySpace().getReservationValue();
180 sameBidsInOpps = new ArrayList<>();
181 maxUtilAdditionBidByOpps = new ArrayList<>();
182 lastPeriodBidList = new ArrayList<>();
183 maxUtilAdditionByOpps = 0.0;
184 initialUpper = 0.95;
185 initialExponent = 50;
186 upper = initialUpper;
187 lower = reservationValue;
188 exponent = initialExponent;
189 time1 = 0.9;
190 time2 = 0.99;
191 time3 = 0.996;
192 hasInitParamByHist = false;
193 }
194
195 @Override
196 public Action chooseAction(List<Class<? extends Action>> validActions) {
197 nrChosenActions++;
198 Action action = null;
199 Bid bid = null;
200 acceptUtil = getAccept();
201 if (lastReceivedBid != null) {
202 if (utility >= acceptUtil
203 && !hasProposedMyLastRound(lastReceivedBid)) {
204 bid = new Bid(lastReceivedBid);
205 action = new Accept(getPartyId(), bid);
206 } else {
207 if (timeline.getTime() > time3) {
208 if (hasHistory) {
209 if (!maxAgreedBids.isEmpty()) {
210 bid = maxAgreedBids.get(
211 nrChosenActions % maxAgreedBids.size());
212 action = new Offer(getPartyId(), bid);
213 } else {
214 if (!lastPeriodBidList.isEmpty()) {
215 bid = lastPeriodBidList.get(nrChosenActions
216 % lastPeriodBidList.size());
217 action = new Offer(getPartyId(), bid);
218 } else {
219 if (utility > reservationValue) {
220 bid = lastReceivedBid;
221 action = new Accept(getPartyId(), bid);
222 return action;
223 } else {
224 bid = proposedBids[myIndex]
225 .get(nrChosenActions
226 % numOfBids[myIndex]);
227 }
228 }
229 }
230 } else {
231 if (!lastPeriodBidList.isEmpty()) {
232 bid = lastPeriodBidList.get(
233 nrChosenActions % lastPeriodBidList.size());
234 } else {
235 if (utility > reservationValue) {
236 bid = lastReceivedBid;
237 action = new Accept(getPartyId(), bid);
238 myBid = bid;
239 return action;
240 } else {
241 bid = proposedBids[myIndex].get(
242 nrChosenActions % numOfBids[myIndex]);
243 }
244 }
245 }
246 action = new Offer(getPartyId(), bid);
247 myBid = bid;
248 } else if (timeline.getTime() > time2) {
249 if (nrChosenActions % 3 == 1) {
250 if (candidateBids.isEmpty()) {
251 if (!acceptAction[myNextPartyIndex]
252 && !acceptAction[myLastPartyIndex]) {
253 generateCandidateFromMyTopBids(acceptUtil,
254 candidateBids);
255 generateCandidateFromOppTopBids(acceptUtil,
256 candidateBids);
257 } else if (!acceptAction[myNextPartyIndex]) {
258 generateCandidateFromMyFullAndOppTopBids(
259 acceptUtil, myNextPartyIndex,
260 candidateBids);
261 generateCandidateFromOppTopAndMyTopBids(
262 myNextPartyIndex, acceptUtil,
263 candidateBids);
264 } else {
265 generateCandidateFromMyFullAndOppTopBids(
266 acceptUtil, myLastPartyIndex,
267 candidateBids);
268 generateCandidateFromOppTopAndMyTopBids(
269 myLastPartyIndex, acceptUtil,
270 candidateBids);
271 }
272 }
273 if (!candidateBids.isEmpty()) {
274 bid = candidateBids.remove(0);
275 if (hasProposedMyLastRound(bid)) {
276 bid = proposedBids[myIndex].get(
277 nrChosenActions % numOfBids[myIndex]);
278 }
279 } else {
280 bid = proposedBids[myIndex]
281 .get(nrChosenActions % numOfBids[myIndex]);
282 }
283 action = new Offer(getPartyId(), bid);
284 } else if (nrChosenActions % 3 == 0) {
285 if (candidateBids.isEmpty()) {
286 generateCandidateFromMyFullBids(
287 proposedBids[myIndex]);
288 generateCandidateFromMyTopBids(acceptUtil,
289 proposedBids[myIndex]);
290 }
291 if (!candidateBids.isEmpty()) {
292 bid = candidateBids.remove(0);
293 } else {
294 bid = proposedBids[myIndex]
295 .get(nrChosenActions % numOfBids[myIndex]);
296 }
297 action = new Offer(getPartyId(), bid);
298 } else {
299 bid = proposedBids[myIndex]
300 .get(nrChosenActions % numOfBids[myIndex]);
301 action = new Offer(getPartyId(), bid);
302 }
303 if (lastPeriodBidList.isEmpty()) {
304 lastPeriodBidList = getMaxUtilBidsInList(
305 sameBidsInOpps);
306 }
307 if (lastPeriodBidList.isEmpty()) {
308 mergeBids(proposedBids[myNextPartyIndex].get(0),
309 proposedBids[myLastPartyIndex].get(0), lower,
310 lastPeriodBidList, lastPeriodBidList);
311 }
312 } else if (timeline.getTime() > time1) {
313 if (candidateBids.isEmpty()) {
314 if (nrChosenActions % 3 == 0) {
315 if (!acceptAction[myNextPartyIndex]) {
316 generateCandidateFromMyFullAndOppTopBids(
317 acceptUtil, myNextPartyIndex,
318 candidateBids);
319 generateCandidateFromMyFullAndMyTopBids(
320 acceptUtil, myNextPartyIndex,
321 candidateBids);
322 generateCandidateFromOppTopAndMyTopBids(
323 myNextPartyIndex, acceptUtil,
324 candidateBids);
325 }
326 if (!acceptAction[myLastPartyIndex]) {
327 generateCandidateFromMyFullAndOppTopBids(
328 acceptUtil, myLastPartyIndex,
329 candidateBids);
330 generateCandidateFromMyFullAndMyTopBids(
331 acceptUtil, myLastPartyIndex,
332 candidateBids);
333 generateCandidateFromOppTopAndMyTopBids(
334 myLastPartyIndex, acceptUtil,
335 candidateBids);
336 }
337 } else if (nrChosenActions % 3 == 1) {
338 generateCandidateFromOppTopBids(acceptUtil,
339 candidateBids);
340 generateCandidateFromMyTopBids(acceptUtil,
341 candidateBids);
342 } else {
343 generateCandidateFromMyFullBids(
344 proposedBids[myIndex]);
345 generateCandidateFromMyTopBids(acceptUtil,
346 proposedBids[myIndex]);
347 }
348 }
349 if (!candidateBids.isEmpty()) {
350 bid = candidateBids.remove(0);
351 if (hasProposedMyLastRound(bid)) {
352 bid = proposedBids[myIndex]
353 .get(nrChosenActions % numOfBids[myIndex]);
354 }
355 } else {
356 bid = proposedBids[myIndex]
357 .get(nrChosenActions % numOfBids[myIndex]);
358 }
359 } else {
360 if (candidateBids.isEmpty()) {
361 generateCandidateFromMyFullBids(proposedBids[myIndex]);
362 generateCandidateFromMyTopBids(acceptUtil,
363 proposedBids[myIndex]);
364 generateCandidateFromMyFullAndMyTopBids(acceptUtil,
365 myNextPartyIndex, proposedBids[myIndex]);
366 generateCandidateFromMyFullAndMyTopBids(acceptUtil,
367 myLastPartyIndex, proposedBids[myIndex]);
368 }
369 if (!candidateBids.isEmpty()) {
370 bid = candidateBids.remove(0);
371 } else {
372 Bid nBid = fullUtilBids
373 .get(nrChosenActions % fullUtilBids.size());
374 if (proposedBids[myIndex].contains(nBid)) {
375 bid = proposedBids[myIndex]
376 .get(nrChosenActions % numOfBids[myIndex]);
377 } else {
378 bid = nBid;
379 }
380 }
381 }
382 if (bid == null) {
383 lastConcessionPoint = getBidWithLessBoundedUtil(
384 lastConcessionPoint, acceptUtil);
385 bid = lastConcessionPoint;
386 if (hasProposedMyLastRound(bid)) {
387 bid = proposedBids[myIndex]
388 .get(nrChosenActions % numOfBids[myIndex]);
389 }
390 }
391 if (bid == null) {
392 bid = myBid;
393 }
394 action = new Offer(getPartyId(), bid);
395 }
396 } else {
397 bid = new Bid(
398 fullUtilBids.get(nrChosenActions % fullUtilBids.size()));
399 action = new Offer(getPartyId(), bid);
400 }
401 myBid = bid;
402 numOfBids[myIndex]++;
403 proposedBids[myIndex].add(bid);
404 return action;
405 }
406
407 @SuppressWarnings("unchecked")
408 @Override
409 public void receiveMessage(AgentID sender, Action action) {
410 super.receiveMessage(sender, action);
411 if (sender == null) {
412 numOfParties = getNumberOfParties();
413 myIndex = Integer.parseInt(getPartyId().toString().split("@")[1])
414 % numOfParties;
415 myNextPartyIndex = (myIndex + 1) % numOfParties;
416 myLastPartyIndex = (myIndex + numOfParties - 1) % numOfParties;
417 proposedBids = new ArrayList[numOfParties];
418 numOfBids = new int[numOfParties];
419 valueFrequences = new HashMap[numOfParties];
420 issueChangeFrequences = new HashMap[numOfParties];
421 acceptAction = new boolean[numOfParties];
422 maxUtils = new double[numOfParties];
423 minUtils = new double[numOfParties];
424 proposedMaxBid = new LinkedList[numOfParties];
425 proposedMinBid = new LinkedList[numOfParties];
426 pModel = new PreferModel[numOfParties];
427 acceptableBidsWithOppValues = new ArrayList[numOfParties];
428 candidateBids = new ArrayList<>();
429 maxProductBids = new ArrayList[numOfParties];
430 topBidsInOppsSpace = new ArrayList[numOfParties];
431 topBidsInMySpace = new LinkedList[numOfParties];
432 for (int i = 0; i < numOfParties; i++) {
433 proposedBids[i] = new ArrayList<>();
434 valueFrequences[i] = new HashMap<>();
435 issueChangeFrequences[i] = new HashMap<>();
436 proposedMaxBid[i] = new LinkedList<>();
437 proposedMinBid[i] = new LinkedList<>();
438 pModel[i] = new PreferModel(getUtilitySpace(), numOfIssues);
439 acceptableBidsWithOppValues[i] = new ArrayList<>();
440 maxProductBids[i] = new ArrayList<>();
441 topBidsInOppsSpace[i] = new ArrayList<>();
442 topBidsInMySpace[i] = new LinkedList<>();
443 }
444 } else {
445 oppName = sender.toString().split("@")[0];
446 oppIndex = Integer.parseInt(sender.toString().split("@")[1])
447 % numOfParties;
448 if (action instanceof Offer) {
449 acceptAction[oppIndex] = false;
450 lastReceivedBid = ((Offer) action).getBid();
451 }
452 if (action instanceof Accept) {
453 acceptAction[oppIndex] = true;
454 lastReceivedBid = ((Accept) action).getBid();
455 }
456 utility = getUtility(lastReceivedBid);
457 proposedBids[oppIndex].add(lastReceivedBid);
458 numOfBids[oppIndex]++;
459 updateMaxOrMinBids(oppIndex);
460 if (isDifferentBid(oppIndex)) {
461 updateSameBidsInOpps(oppIndex, lastReceivedBid);
462 updateTopBidsInOppsSpace(oppIndex);
463 updateTopBidsInMySpace(oppIndex);
464 }
465 if (hasHistory) {
466 if (!hasInitParamByHist) {
467 hasHistWithRightPosition = false;
468 for (String name : maxUtilsInHist[oppIndex].keySet()) {
469 if (oppName.equals(name)) {
470 hasHistWithRightPosition = true;
471 }
472 }
473 if (hasHistWithRightPosition) {
474 double[] arrs = new double[3];
475 for (int i = 0; i < getNumberOfParties(); i++) {
476 if (i == myIndex) {
477 continue;
478 }
479 for (String name : maxUtilsInHist[i].keySet()) {
480 if (oppIndex == i) {
481 arrs[i] = maxUtilsInHist[oppIndex]
482 .get(oppName);
483 } else {
484 if (!name.equals(oppName)) {
485 arrs[i] = maxUtilsInHist[i].get(name);
486 }
487 }
488 }
489 }
490 double max = 0.0;
491 double min = 1.0;
492 for (int i = 0; i < 3; i++) {
493 if (i == myIndex) {
494 continue;
495 }
496 if (max < arrs[i]) {
497 max = arrs[i];
498 }
499 if (min > arrs[i]) {
500 min = arrs[i];
501 }
502 }
503 if (maxAgreedBids.isEmpty()) {
504 if (min - 0.1 > reservationValue) {
505 lower = min - 0.1;
506 } else {
507 lower = reservationValue;
508 }
509 } else {
510 double agreedUtil = getUtility(
511 maxAgreedBids.get(0));
512 double[] utilArrs = { max, min, agreedUtil };
513 upper = getMaxInArray(utilArrs);
514 for (int i = 0; i < utilArrs.length; i++) {
515 if (utilArrs[i] == upper) {
516 utilArrs[i] = 0.0;
517 }
518 }
519 lower = getMinInArray(utilArrs) - 0.05;
520 if (lower <= 0) {
521 lower = reservationValue;
522 }
523 }
524 exponent = initialExponent * (upper - lower);
525 } else {
526 double[] arrs = new double[3];
527 for (int i = 0; i < getNumberOfParties(); i++) {
528 if (i == myIndex) {
529 continue;
530 }
531 for (String name : maxUtilsInHist[i].keySet()) {
532 arrs[i] = maxUtilsInHist[i].get(name);
533 }
534 }
535 double max = 0.0;
536 double min = 1.0;
537 for (int i = 0; i < 3; i++) {
538 if (i == myIndex) {
539 continue;
540 }
541 if (max < arrs[i]) {
542 max = arrs[i];
543 }
544 if (min > arrs[i]) {
545 min = arrs[i];
546 }
547 }
548 if (maxAgreedBids.isEmpty()) {
549 if (min - 0.1 > reservationValue) {
550 lower = min - 0.1;
551 } else {
552 lower = reservationValue;
553 }
554 } else {
555 double agreedUtil = getUtility(
556 maxAgreedBids.get(0));
557 double[] utilArrs = { max, min, agreedUtil, 0.9 };
558 upper = getMaxInArray(utilArrs);
559 for (int i = 0; i < utilArrs.length; i++) {
560 if (utilArrs[i] == upper) {
561 utilArrs[i] = 0.0;
562 }
563 }
564 lower = getMinInArray(utilArrs) - 0.1;
565 if (lower <= 0) {
566 lower = reservationValue;
567 }
568 }
569 exponent = initialExponent * (upper - lower);
570 }
571 hasInitParamByHist = true;
572 }
573 if (hasInitParamByHist) {
574 if (hasHistWithRightPosition) {
575
576 } else {
577
578 }
579 }
580 } else {
581 if (isDifferentBid(oppIndex)) {
582 pModel[oppIndex].updateEvaluators(lastReceivedBid);
583 if (timeline.getTime() > 0.5 && timeline.getTime() < 0.9) {
584 updateMaxUtilAddition(oppIndex, lastReceivedBid);
585 }
586 }
587 updateConcessionParam();
588 }
589 }
590 }
591
592 @Override
593 public String getDescription() {
594 return "ANAC2017";
595 }
596
597 private double getAccept() {
598 double accept = 0.9;
599 accept = upper - (upper + 0.001 - lower)
600 * Math.pow(timeline.getTime(), exponent);
601 return accept;
602 }
603
604 private List<Bid> getFullUtilBid(AbstractUtilitySpace utilitySpace) {
605 Bid bid = null;
606 List<Bid> bidList = new ArrayList<>();
607 try {
608 bid = utilitySpace.getMaxUtilityBid();
609 } catch (Exception e) {
610 e.printStackTrace();
611 System.err.println(e.toString());
612 }
613 if (!bidList.contains(bid)) {
614 bidList.add(bid);
615 }
616 List<Issue> issues = getUtilitySpace().getDomain().getIssues();
617 for (Issue item : issues) {
618 Bid newBid = null;
619 switch (item.getType()) {
620 case DISCRETE:
621 IssueDiscrete discrete = (IssueDiscrete) item;
622 for (Value value : discrete.getValues()) {
623 newBid = bid.putValue(discrete.getNumber(), value);
624 if (getUtility(newBid) == 1.0
625 && !bidList.contains(newBid)) {
626 bidList.add(newBid);
627 }
628 }
629 break;
630 case INTEGER:
631 break;
632 default:
633 break;
634 }
635 }
636 return bidList;
637 }
638
639 @SuppressWarnings("unused")
640 private Bid getBidWithLessUtil(Bid bid) {
641 Bid srcBid = null;
642 Bid nBid = null;
643 if (bid == null) {
644 srcBid = fullUtilBids.get(0);
645 } else {
646 srcBid = new Bid(bid);
647 }
648 double oldU = getUtility(srcBid);
649 List<Bid> bidList = new ArrayList<>();
650 for (int i = 0; i < numOfIssues; i++) {
651 Issue issue = srcBid.getIssues().get(i);
652 switch (issue.getType()) {
653 case DISCRETE:
654 IssueDiscrete issueDiscrete = (IssueDiscrete) issue;
655 for (Value value : issueDiscrete.getValues()) {
656 nBid = srcBid.putValue(i + 1, value);
657 if (oldU > getUtility(nBid)) {
658 bidList.add(nBid);
659 }
660 }
661 break;
662 case INTEGER:
663 IssueInteger issueInteger = (IssueInteger) issue;
664 for (int j = issueInteger.getLowerBound(); j <= issueInteger
665 .getUpperBound(); j++) {
666 nBid = srcBid.putValue(i + 1, new ValueInteger(j));
667 if (oldU > getUtility(nBid)) {
668 bidList.add(nBid);
669 }
670 }
671 break;
672 default:
673 break;
674 }
675 }
676 if (bidList.isEmpty()) {
677 nBid = new Bid(srcBid);
678 } else {
679 double distance = oldU - 0.0;
680 for (Bid item : bidList) {
681 double distance2 = oldU - getUtility(item);
682 if (distance2 < distance) {
683 nBid = new Bid(item);
684 distance = distance2;
685 }
686 }
687 }
688 return nBid;
689 }
690
691 private Bid getBidWithLessBoundedUtil(Bid bid, double min) {
692 Bid srcBid = null;
693 Bid nBid = null;
694 if (bid == null) {
695 srcBid = fullUtilBids.get(0);
696 } else {
697 srcBid = new Bid(bid);
698 }
699 double oldU = getUtility(srcBid);
700 List<Bid> bidList = new ArrayList<>();
701 for (int i = 0; i < numOfIssues; i++) {
702 Issue issue = srcBid.getIssues().get(i);
703 double newU = 0.0;
704 switch (issue.getType()) {
705 case DISCRETE:
706 IssueDiscrete issueDiscrete = (IssueDiscrete) issue;
707 for (Value value : issueDiscrete.getValues()) {
708 nBid = srcBid.putValue(i + 1, value);
709 newU = getUtility(nBid);
710 if (oldU > newU && newU >= min) {
711 bidList.add(nBid);
712 }
713 }
714 break;
715 case INTEGER:
716 IssueInteger issueInteger = (IssueInteger) issue;
717 for (int j = issueInteger.getLowerBound(); j <= issueInteger
718 .getUpperBound(); j++) {
719 nBid = srcBid.putValue(i + 1, new ValueInteger(j));
720 newU = getUtility(nBid);
721 if (oldU > newU && newU >= min) {
722 bidList.add(nBid);
723 }
724 }
725 break;
726 default:
727 break;
728 }
729 }
730 if (bidList.isEmpty()) {
731 nBid = new Bid(srcBid);
732 } else {
733 double distance = oldU - 0.0;
734 for (Bid item : bidList) {
735 double distance2 = oldU - getUtility(item);
736 if (distance2 < distance) {
737 nBid = new Bid(item);
738 distance = distance2;
739 }
740 }
741 }
742 return nBid;
743 }
744
745 private void updateProposedMaxBid(int partyId) {
746 if (proposedMaxBid[partyId].isEmpty()) {
747 if (utility > maxUtils[partyId]) {
748 maxUtils[partyId] = utility;
749 proposedMaxBid[partyId].add(lastReceivedBid);
750 }
751 } else {
752 if (maxUtils[partyId] > utility) {
753 return;
754 }
755 if (maxUtils[partyId] == utility
756 && !proposedMaxBid[partyId].contains(lastReceivedBid)) {
757 proposedMaxBid[partyId].add(lastReceivedBid);
758 }
759 if (maxUtils[partyId] < utility) {
760 proposedMaxBid[partyId].clear();
761 proposedMaxBid[partyId].add(lastReceivedBid);
762 maxUtils[partyId] = utility;
763 }
764 }
765 }
766
767 private void updateProposedMinBid(int partyId) {
768 if (proposedMinBid[partyId].isEmpty()) {
769 proposedMinBid[partyId].add(lastReceivedBid);
770 if (0 >= minUtils[partyId]) {
771 minUtils[partyId] = utility;
772 }
773 } else {
774 if (minUtils[partyId] < utility) {
775 return;
776 }
777 if (minUtils[partyId] == utility
778 && !proposedMinBid[partyId].contains(lastReceivedBid)) {
779 proposedMinBid[partyId].add(lastReceivedBid);
780 }
781 if (minUtils[partyId] > utility) {
782 proposedMinBid[partyId].clear();
783 proposedMinBid[partyId].add(lastReceivedBid);
784 minUtils[partyId] = utility;
785 }
786 }
787 }
788
789 private void updateMaxOrMinBids(int partyId) {
790 updateProposedMaxBid(partyId);
791 updateProposedMinBid(partyId);
792 }
793
794 private void updateTopBidsInOppsSpace(int partyId) {
795 if (topBidsInOppsSpace[partyId].size() < 5
796 && !topBidsInOppsSpace[partyId].contains(lastReceivedBid)) {
797 topBidsInOppsSpace[partyId].add(lastReceivedBid);
798 }
799 }
800
801 private void updateTopBidsInMySpace(int partyId) {
802 if (topBidsInMySpace[partyId].contains(lastReceivedBid)) {
803 return;
804 } else {
805 topBidsInMySpace[partyId] = proposedMaxBid[partyId];
806 }
807 }
808
809 private void updateConcessionParam() {
810 if (!hasHistory) {
811 // ����Ϊ�����������С��utility
812 double maxInMinUtil = 0.0;
813 double minInMinUtil = 1.0;
814 double minInMaxUtil = 1.0;
815 double[] avgUtil = new double[numOfParties];
816 for (int i = 0; i < numOfParties; i++) {
817 if (i == myIndex) {
818 avgUtil[i] = 1.0;
819 continue;
820 }
821 if (maxUtils[i] == 0.0 || minUtils[i] == 0.0) {
822 continue;
823 }
824 double utilInMin2 = minUtils[i];
825 if (utilInMin2 > maxInMinUtil) {
826 maxInMinUtil = utilInMin2;
827 }
828 if (utilInMin2 < minInMinUtil) {
829 minInMinUtil = utilInMin2;
830 }
831 double utilInMax = maxUtils[i];
832 if (minInMaxUtil > utilInMax) {
833 minInMaxUtil = utilInMax;
834 }
835 avgUtil[i] = (3 * maxUtils[i] + minUtils[i]) / 4;
836 }
837 double lowerbound = 0.0;
838 double minAvg = getMinInArray(avgUtil);
839 if (reservationValue > minAvg - 0.001) {
840 lowerbound = reservationValue;
841 } else {
842 lowerbound = minAvg - 0.001;
843 }
844 lower = lowerbound;
845 if (minInMaxUtil <= 0.0) {
846 exponent = initialExponent;
847 } else {
848 exponent = initialExponent * minInMaxUtil;
849 }
850 } else {
851
852 }
853 }
854
855 private double getMinInArray(double arr[]) {
856 double min = arr[0];
857 for (int i = 0; i < arr.length; i++) {
858 if (min > arr[i]) {
859 min = arr[i];
860 }
861 }
862 return min;
863 }
864
865 private double getMaxInArray(double[] arr) {
866 double max = 0.0;
867 for (int i = 0; i < arr.length; i++) {
868 if (max < arr[i]) {
869 max = arr[i];
870 }
871 }
872 return max;
873 }
874
875 private double getMaxInArray(List<Double> list) {
876 double max = 0.0;
877 for (double item : list) {
878 if (item > max) {
879 max = item;
880 }
881 }
882 return max;
883 }
884
885 private boolean isDifferentBid(int partyId) {
886 if (proposedBids[partyId].size() < 2) {
887 return true;
888 } else {
889 if (proposedBids[partyId].get(numOfBids[partyId] - 1).equals(
890 proposedBids[partyId].get(numOfBids[partyId] - 2))) {
891 return false;
892 } else {
893 return true;
894 }
895 }
896 }
897
898 private void alterBidValue(Bid srcBid, Map<Integer, Value> valueMap,
899 int issueNr, double acceptableUtil, List<Bid> bidList,
900 List<Bid> validationList) {
901 Bid nBid = new Bid(srcBid);
902 if (getUtility(nBid) >= acceptableUtil
903 && !validationList.contains(nBid)) {
904 bidList.add(nBid);
905 }
906 if (!valueMap.containsKey(issueNr)) {
907 return;
908 }
909 for (int j = issueNr; j <= valueMap.size(); j++) {
910 Value value = valueMap.get(j);
911 if (value.equals(srcBid.getValue(j))) {
912 continue;
913 }
914 nBid = srcBid.putValue(j, value);
915 int item = j + 1;
916 alterBidValue(nBid, valueMap, item, acceptableUtil, bidList,
917 validationList);
918 }
919 }
920
921 private void mergeBids(Bid fir, Bid sec, double acceptableUtil,
922 List<Bid> bidList, List<Bid> validationList) {
923 if (fir == null || sec == null) {
924 return;
925 }
926 if (fir.equals(sec)) {
927 if (!validationList.contains(fir)) {
928 bidList.add(fir);
929 }
930 return;
931 }
932 alterBidValue(fir, sec.getValues(), 1, acceptableUtil, bidList,
933 validationList);
934 }
935
936 private void generateCandidateFromMyFullBidsAndOppsTopBids(double minUtil,
937 int oppIndex, List<Bid> validationList) {
938 if (proposedBids[oppIndex].isEmpty()) {
939 return;
940 }
941 for (Bid item : fullUtilBids) {
942 for (Bid item2 : topBidsInOppsSpace[oppIndex]) {
943 if (item.equals(item2)) {
944 continue;
945 }
946 mergeBids(item, item2, minUtil, candidateBids, validationList);
947 }
948 }
949 }
950
951 private List<Bid> getMaxSocialWelfareBidsInList(List<Bid> bidList) {
952 List<Bid> maxAdditionBids = new ArrayList<>();
953 List<Bid> maxProductBids = new ArrayList<>();
954 List<Bid> maxSocialWelfareBids = new ArrayList<>();
955 double maxAddition = 0.0;
956 double maxProduct = 0.0;
957 for (Bid bid : bidList) {
958 double bidAddition = 0.0;
959 double bidProduct = 1.0;
960 bidAddition = getUtilAddition(bid);
961 bidProduct = getUtilProduct(bid);
962 if (bidAddition > maxAddition) {
963 maxAdditionBids.clear();
964 maxAdditionBids.add(bid);
965 maxAddition = bidAddition;
966 } else if (bidAddition == maxAddition
967 && !maxAdditionBids.contains(bid)) {
968 maxAdditionBids.add(bid);
969 }
970 if (bidProduct > maxProduct) {
971 maxProductBids.clear();
972 maxProductBids.add(bid);
973 maxProduct = bidProduct;
974 } else if (bidProduct == maxProduct
975 && !maxProductBids.contains(bid)) {
976 maxProductBids.add(bid);
977 }
978 }
979 maxSocialWelfareBids.addAll(maxAdditionBids);
980 maxSocialWelfareBids.addAll(maxProductBids);
981 return maxSocialWelfareBids;
982 }
983
984 private double getUtilProduct(Bid bid) {
985 double product = 1.0;
986 for (int i = 0; i < numOfParties; i++) {
987 if (i == myIndex) {
988 product *= getUtility(bid);
989 }
990 product *= pModel[i].getUtil(bid);
991 }
992 return product;
993 }
994
995 private double getUtilAddition(Bid bid) {
996 double addition = 0.0;
997 for (int i = 0; i < numOfParties; i++) {
998 if (i == myIndex) {
999 addition += getUtility(bid);
1000 }
1001 addition += pModel[i].getUtil(bid);
1002 }
1003 return addition;
1004 }
1005
1006 private List<Bid> getMaxUtilBidsInList(List<Bid> bidList) {
1007 List<Bid> bids = new ArrayList<>();
1008 double maxUtil = 0.0;
1009 for (Bid item : bidList) {
1010 double util = getUtility(item);
1011 if (util < maxUtil) {
1012 continue;
1013 }
1014 if (util > maxUtil) {
1015 bids.clear();
1016 bids.add(item);
1017 maxUtil = util;
1018 } else {
1019 bids.add(item);
1020 }
1021 }
1022 return bids;
1023 }
1024
1025 private void updateSameBidsInOpps(int proposedPartyId, Bid bid) {
1026 boolean bidHasProposedByAllOpps = true;
1027 for (int i = 0; i < numOfParties; i++) {
1028 if (i == myIndex || i == proposedPartyId) {
1029 continue;
1030 }
1031 if (!proposedBids[i].contains(bid)) {
1032 bidHasProposedByAllOpps = false;
1033 return;
1034 } else {
1035 bidHasProposedByAllOpps = true;
1036 }
1037 }
1038 if (bidHasProposedByAllOpps) {
1039 sameBidsInOpps.add(bid);
1040 }
1041 }
1042
1043 private void updateMaxUtilAddition(int partyId, Bid bid) {
1044 double utilAddition = 0.0;
1045 for (int i = 0; i < numOfParties; i++) {
1046 if (partyId == myIndex) {
1047 utilAddition += getUtility(bid);
1048 } else {
1049 utilAddition += pModel[i].getUtil(bid);
1050 }
1051 }
1052 if (utilAddition < maxUtilAdditionByOpps) {
1053 return;
1054 }
1055 if (utilAddition > maxUtilAdditionByOpps) {
1056 maxUtilAdditionBidByOpps.clear();
1057 maxUtilAdditionBidByOpps.add(bid);
1058 maxUtilAdditionByOpps = utilAddition;
1059 } else {
1060 maxUtilAdditionBidByOpps.add(bid);
1061 }
1062 }
1063
1064 private void generateCandidateFromOppTopAndMyTopBids(int partyId,
1065 double minUtil, List<Bid> validationList) {
1066 if (proposedBids[partyId].isEmpty()) {
1067 return;
1068 }
1069 for (Bid item : topBidsInOppsSpace[partyId]) {
1070 for (Bid maxBid : topBidsInMySpace[partyId]) {
1071 mergeBids(maxBid, item, minUtil, candidateBids, candidateBids);
1072 }
1073 }
1074 }
1075
1076 private List<Bid> getMaxBidsInAgreements() {
1077 double maxUtil = 0.0;
1078 List<Bid> maxBids = new ArrayList<>();
1079 for (Tuple<Bid, Double> item : agreements) {
1080 if (item.get2() < maxUtil) {
1081 continue;
1082 }
1083 if (item.get2() > maxUtil) {
1084 maxBids.clear();
1085 maxBids.add(item.get1());
1086 maxUtil = item.get2();
1087 } else {
1088 if (!maxBids.contains(item.get1())) {
1089 maxBids.add(item.get1());
1090 }
1091 }
1092 }
1093 return maxBids;
1094 }
1095
1096 private boolean hasProposedMyLastRound(Bid bid) {
1097 if (numOfBids[myIndex] <= 0) {
1098 return false;
1099 }
1100 Bid lastRoundBid = proposedBids[myIndex].get(numOfBids[myIndex] - 1);
1101 return lastRoundBid.equals(bid);
1102 }
1103
1104 private void generateCandidateFromMyFullBids(List<Bid> validationList) {
1105 for (Bid item1 : fullUtilBids) {
1106 for (Bid item2 : fullUtilBids) {
1107 if (item1.equals(item2)) {
1108 if (validationList.contains(item1)) {
1109 continue;
1110 } else {
1111 candidateBids.add(item1);
1112 continue;
1113 }
1114 } else {
1115 mergeBids(item1, item2, 0.9, candidateBids, validationList);
1116 }
1117 }
1118 }
1119 }
1120
1121 private void generateCandidateFromMyFullAndMyTopBids(double accept,
1122 int partyId, List<Bid> validationList) {
1123 for (Bid item1 : fullUtilBids) {
1124 for (Bid item2 : topBidsInMySpace[partyId]) {
1125 mergeBids(item1, item2, accept, candidateBids, validationList);
1126 }
1127 }
1128 }
1129
1130 private void generateCandidateFromMyFullAndOppTopBids(double accept,
1131 int partyId, List<Bid> validationList) {
1132 for (Bid item1 : fullUtilBids) {
1133 for (Bid item2 : topBidsInOppsSpace[partyId]) {
1134 mergeBids(item1, item2, accept, candidateBids, validationList);
1135 }
1136 }
1137 }
1138
1139 private void generateCandidateFromMyTopBids(double acceptableUtil,
1140 List<Bid> validationList) {
1141 for (Bid fBid : topBidsInMySpace[myNextPartyIndex]) {
1142 for (Bid sBid : topBidsInMySpace[myLastPartyIndex]) {
1143 alterBidValue(fBid, sBid.getValues(), 1, acceptableUtil,
1144 candidateBids, validationList);
1145 }
1146 }
1147 }
1148
1149 private void generateCandidateFromOppTopBids(double acceptableUtil,
1150 List<Bid> validationList) {
1151 if (numOfBids[myLastPartyIndex] == 0
1152 || numOfBids[myNextPartyIndex] == 0) {
1153 return;
1154 }
1155 for (Bid item1 : topBidsInOppsSpace[myNextPartyIndex]) {
1156 for (Bid item2 : topBidsInOppsSpace[myLastPartyIndex]) {
1157 if (item1.equals(item2)) {
1158 continue;
1159 }
1160 mergeBids(item1, item2, acceptableUtil, candidateBids,
1161 validationList);
1162 }
1163 }
1164 }
1165
1166 public class PreferModel {
1167
1168 private UtilitySpace utilitySpace;
1169 private Set<Entry<Objective, Evaluator>> evaluators;
1170 private int numOfIssues;
1171 private double[] weights;
1172 private int[] indexOfMaxFreq;
1173 private int numOfValues;
1174 private int learnRate;
1175
1176 public PreferModel(UtilitySpace utilitySpace, int num) {
1177 this.utilitySpace = utilitySpace;
1178 numOfIssues = num;
1179 weights = new double[num];
1180 initWeights();
1181 initEvaluators();
1182 setNumOfValues();
1183 learnRate = 1;
1184 }
1185
1186 public Set<Entry<Objective, Evaluator>> getEvaluators() {
1187 return evaluators;
1188 }
1189
1190 private void setEvaluators(
1191 Set<Entry<Objective, Evaluator>> evaluators) {
1192 this.evaluators = evaluators;
1193 }
1194
1195 public double[] getWeights() {
1196 return weights;
1197 }
1198
1199 public void setWeights(double weights[]) {
1200 this.weights = weights;
1201 }
1202
1203 public int[] getIndexOfMaxFreq() {
1204 return indexOfMaxFreq;
1205 }
1206
1207 public void setIndexOfMaxFreq(int[] indexOfMaxFreq) {
1208 this.indexOfMaxFreq = indexOfMaxFreq;
1209 }
1210
1211 public int getNumOfValues() {
1212 return numOfValues;
1213 }
1214
1215 public void setNumOfValues() {
1216 int num = 0;
1217 List<Issue> issues = utilitySpace.getDomain().getIssues();
1218 for (Issue issue : issues) {
1219 switch (issue.getType()) {
1220 case DISCRETE:
1221 IssueDiscrete discrete = (IssueDiscrete) issue;
1222 num += discrete.getNumberOfValues();
1223 break;
1224 case INTEGER:
1225 IssueInteger integer = (IssueInteger) issue;
1226 int distance = integer.getUpperBound()
1227 - integer.getLowerBound();
1228 num += (distance + 1);
1229 break;
1230 default:
1231 break;
1232 }
1233 }
1234 numOfValues = num;
1235 }
1236
1237 private void initEvaluators() {
1238 try {
1239 Object[] issuesXML = ((SimpleElement) (utilitySpace.toXML()
1240 .getChildByTagName("objective")[0]))
1241 .getChildByTagName("issue").clone();
1242 Map<Objective, Evaluator> map = new LinkedHashMap<>();
1243 for (int i = 0; i < numOfIssues; i++) {
1244 Issue issue = utilitySpace.getDomain().getIssues().get(i);
1245 switch (issue.getType()) {
1246 case DISCRETE:
1247 List<ValueDiscrete> values = ((IssueDiscrete) utilitySpace
1248 .getDomain().getIssues().get(i)).getValues();
1249 EvaluatorDiscrete ed = new EvaluatorDiscrete();
1250 ed.loadFromXML((SimpleElement) issuesXML[i]);
1251 ed.setWeight(weights[i]);
1252 for (ValueDiscrete item : values) {
1253 ed.addEvaluation(item, 1);
1254 }
1255 map.put(issue, ed);
1256 break;
1257 case INTEGER:
1258 EvaluatorInteger et = new EvaluatorInteger();
1259 et.loadFromXML((SimpleElement) issuesXML[i]);
1260 et.setWeight(weights[i]);
1261 et.setLinearFunction(0.0, 1.0);
1262 map.put(issue, et);
1263 break;
1264 default:
1265 System.err.println(
1266 "Unsuported issue type " + issue.getType());
1267 break;
1268 }
1269 setEvaluators((map.entrySet()));
1270 }
1271 } catch (Exception e) {
1272 // TODO Auto-generated catch block
1273 e.printStackTrace();
1274 }
1275 }
1276
1277 public void loadEvaluatorFromMyPrefer() {
1278 try {
1279 Object[] issuesXML = ((SimpleElement) (utilitySpace.toXML()
1280 .getChildByTagName("objective")[0]))
1281 .getChildByTagName("issue").clone();
1282 Object[] weightXML = ((SimpleElement) (utilitySpace.toXML()
1283 .getChildByTagName("objective")[0]))
1284 .getChildByTagName("weight");
1285 Map<Objective, Evaluator> map = new LinkedHashMap<>();
1286 for (int i = 0; i < numOfIssues; i++) {
1287 Issue issue = utilitySpace.getDomain().getIssues().get(i);
1288 switch (issue.getType()) {
1289 case DISCRETE:
1290 EvaluatorDiscrete ed = new EvaluatorDiscrete();
1291 ed.loadFromXML((SimpleElement) issuesXML[i]);
1292 ed.setWeight(Double
1293 .parseDouble(((SimpleElement) weightXML[i])
1294 .getAttribute("value")));
1295 map.put(issue, ed);
1296 break;
1297 case INTEGER:
1298 EvaluatorInteger et = new EvaluatorInteger();
1299 et.loadFromXML((SimpleElement) issuesXML[i]);
1300 et.setWeight(Double
1301 .parseDouble(((SimpleElement) weightXML[i])
1302 .getAttribute("value")));
1303 map.put(issue, et);
1304 break;
1305 default:
1306 System.err.println(
1307 "Unsuported issue type " + issue.getType());
1308 break;
1309 }
1310 setEvaluators((map.entrySet()));
1311 }
1312 } catch (Exception e) {
1313 // TODO Auto-generated catch block
1314 e.printStackTrace();
1315 }
1316 }
1317
1318 public void updateEvaluators(Bid bid, int learnRate) {
1319 HashMap<Integer, Value> values = bid.getValues();
1320 List<Issue> issues = bid.getIssues();
1321 int i = 0;
1322 for (Entry<Objective, Evaluator> evaluator : evaluators) {
1323 evaluator.getValue().setWeight(weights[i]);
1324 try {
1325 Value value = values.get(i + 1);
1326 Issue issue = issues.get(i);
1327 for (int j = 0; j < numOfIssues; j++) {
1328 if (evaluator.getKey().toString()
1329 .equals(issues.get(j).toString())) {
1330 value = values.get(j + 1);
1331 issue = issues.get(j);
1332 break;
1333 }
1334 }
1335 switch (value.getType()) {
1336 case DISCRETE:
1337 ValueDiscrete valueDiscrete = (ValueDiscrete) values
1338 .get(i + 1);
1339 int old = ((EvaluatorDiscrete) evaluator.getValue())
1340 .getEvaluationNotNormalized(valueDiscrete);
1341 ((EvaluatorDiscrete) evaluator.getValue())
1342 .setEvaluation(value, old + learnRate);
1343 break;
1344 case INTEGER:
1345 EvaluatorInteger ei = ((EvaluatorInteger) evaluator
1346 .getValue());
1347 if (ei.weightLocked()) {
1348 continue;
1349 }
1350 IssueInteger issueInteger = (IssueInteger) issue;
1351 ValueInteger valueInteger = (ValueInteger) value;
1352 int iValue = valueInteger.getValue();
1353 int distanceToUpper = Math
1354 .abs(issueInteger.getUpperBound() - iValue);
1355 int distanceToLower = Math
1356 .abs(issueInteger.getLowerBound() - iValue);
1357 if (distanceToUpper < distanceToLower) {
1358 ei.setLinearFunction(0.0, 1.0);
1359 } else {
1360 ei.setLinearFunction(1.0, 0.0);
1361 }
1362 ei.lockWeight();
1363 break;
1364 default:
1365 break;
1366 }
1367
1368 } catch (Exception e) {
1369 // TODO Auto-generated catch block
1370 e.printStackTrace();
1371 }
1372 i++;
1373 }
1374
1375 }
1376
1377 private void initWeights() {
1378 for (int i = 0; i < weights.length; i++) {
1379 weights[i] = 1D / numOfIssues;
1380 }
1381 }
1382
1383 public void updateEvaluators(Bid bid) {
1384 boolean hasUpadated = false;
1385 HashMap<Integer, Value> values = bid.getValues();
1386 List<Issue> issues = bid.getIssues();
1387 int i = 0;
1388 for (Entry<Objective, Evaluator> evaluator : evaluators) {
1389 try {
1390 Value value = values.get(i + 1);
1391 Issue issue = issues.get(i);
1392 for (int j = 0; j < numOfIssues; j++) {
1393 if (evaluator.getKey().toString()
1394 .equals(issues.get(j).toString())) {
1395 value = values.get(j + 1);
1396 issue = issues.get(j);
1397 break;
1398 }
1399 }
1400 switch (value.getType()) {
1401 case DISCRETE:
1402 ValueDiscrete valueDiscrete = (ValueDiscrete) values
1403 .get(i + 1);
1404 int old = ((EvaluatorDiscrete) evaluator.getValue())
1405 .getEvaluationNotNormalized(valueDiscrete);
1406 ((EvaluatorDiscrete) evaluator.getValue())
1407 .setEvaluation(value, old + 1);
1408 hasUpadated = true;
1409 break;
1410 case INTEGER:
1411 EvaluatorInteger ei = ((EvaluatorInteger) evaluator
1412 .getValue());
1413 if (ei.weightLocked()) {
1414 continue;
1415 }
1416 IssueInteger issueInteger = (IssueInteger) issue;
1417 ValueInteger valueInteger = (ValueInteger) value;
1418 int iValue = valueInteger.getValue();
1419 int distanceToUpper = Math
1420 .abs(issueInteger.getUpperBound() - iValue);
1421 int distanceToLower = Math
1422 .abs(issueInteger.getLowerBound() - iValue);
1423 if (distanceToUpper < distanceToLower) {
1424 ei.setLinearFunction(0.0, 1.0);
1425 } else {
1426 ei.setLinearFunction(1.0, 0.0);
1427 }
1428 hasUpadated = true;
1429 ei.lockWeight();
1430 break;
1431 default:
1432 break;
1433 }
1434 } catch (Exception e) {
1435 // TODO Auto-generated catch block
1436 e.printStackTrace();
1437 }
1438 i++;
1439 }
1440 if (hasUpadated) {
1441 learnRate--;
1442 }
1443 }
1444
1445 public double getUtil(Bid bid) {
1446 double util = 0D;
1447 Map<Integer, Value> values = bid.getValues();
1448 int issueNr = 1;
1449 for (Entry<Objective, Evaluator> evaluator : evaluators) {
1450 try {
1451 Value value = values.get(issueNr);
1452 double weight = evaluator.getValue().getWeight();
1453 double valueEvaluation = 0.0;
1454 switch (value.getType()) {
1455 case DISCRETE:
1456 EvaluatorDiscrete dEvaluator = (EvaluatorDiscrete) evaluator
1457 .getValue();
1458 valueEvaluation = dEvaluator
1459 .getEvaluation((ValueDiscrete) value);
1460 util += weight * valueEvaluation;
1461 break;
1462 case INTEGER:
1463 EvaluatorInteger iEvaluator = (EvaluatorInteger) evaluator
1464 .getValue();
1465 valueEvaluation = iEvaluator.getEvaluation(
1466 Integer.parseInt(value.toString()));
1467 util += weight * valueEvaluation;
1468 break;
1469 default:
1470 break;
1471 }
1472 } catch (Exception e) {
1473 // TODO Auto-generated catch block
1474 e.printStackTrace();
1475 }
1476 issueNr++;
1477 }
1478 return util;
1479 }
1480
1481 public double getValueEvaluation(int issueNr, Value value) {
1482 double evaluation = 0.0;
1483 int i = 1;
1484 for (Entry<Objective, Evaluator> evaluator : evaluators) {
1485 if (i != issueNr) {
1486 i++;
1487 continue;
1488 } else {
1489 switch (value.getType()) {
1490 case DISCRETE:
1491 ValueDiscrete dValue = (ValueDiscrete) value;
1492 EvaluatorDiscrete dEvaluator = (EvaluatorDiscrete) evaluator
1493 .getValue();
1494 try {
1495 evaluation = dEvaluator.getWeight()
1496 * dEvaluator.getEvaluation(dValue);
1497 } catch (Exception e) {
1498 // TODO Auto-generated catch block
1499 e.printStackTrace();
1500 }
1501 break;
1502 case INTEGER:
1503 ValueInteger vInteger = (ValueInteger) value;
1504 EvaluatorInteger iEvaluator = (EvaluatorInteger) evaluator
1505 .getValue();
1506 evaluation = iEvaluator.getWeight()
1507 * iEvaluator.getEvaluation(vInteger.getValue());
1508 break;
1509 default:
1510 System.err.println(
1511 "Unsupported value type: " + value.getType());
1512 break;
1513 }
1514 i++;
1515 }
1516 }
1517 return evaluation;
1518 }
1519 }
1520}
Note: See TracBrowser for help on using the repository browser.