source: anac2020/agentxx/src/main/java/geniusweb/exampleparties/agentgg/SimpleLinearOrdering.java

Last change on this file was 54, checked in by wouter, 3 years ago

#5 fix supported profile type

File size: 3.2 KB
Line 
1package geniusweb.exampleparties.agentgg;
2
3import java.math.BigDecimal;
4import java.math.RoundingMode;
5import java.util.Collections;
6import java.util.Comparator;
7import java.util.LinkedList;
8import java.util.List;
9
10import geniusweb.issuevalue.Bid;
11import geniusweb.issuevalue.Domain;
12import geniusweb.profile.DefaultPartialOrdering;
13import geniusweb.profile.Profile;
14import geniusweb.profile.utilityspace.UtilitySpace;
15
16/**
17 * A simple list of bids, but all bids are fully ordered (better or worse than
18 * other bids in the list).
19 */
20public class SimpleLinearOrdering implements UtilitySpace {
21
22 private final Domain domain;
23 private final List<Bid> bids; // worst bid first, best bid last.
24
25 SimpleLinearOrdering(Profile profile) {
26 this(profile.getDomain(), getSortedBids(profile));
27 }
28
29 /**
30 *
31 * @param domain
32 * @param bids a list of bids, ordered from lowest to highest util. The first
33 * bid will have utility 0, the last utility 1. If only 0 or 1 bid
34 * in the list, or if the bid is not known, it will have utility
35 * 0.
36 */
37 SimpleLinearOrdering(Domain domain, List<Bid> bids) {
38 this.domain = domain;
39 this.bids = bids;
40 }
41
42 @Override
43 public String getName() {
44 throw new UnsupportedOperationException();
45 }
46
47 @Override
48 public Domain getDomain() {
49 return domain;
50 }
51
52 @Override
53 public Bid getReservationBid() {
54 throw new UnsupportedOperationException();
55 }
56
57 @Override
58 public BigDecimal getUtility(Bid bid) {
59 if (bids.size() < 2 || !bids.contains(bid)) {
60 return BigDecimal.ZERO;
61 }
62 // using 8 decimals, we have to pick something here
63 return new BigDecimal(bids.indexOf(bid)).divide(new BigDecimal((bids.size() - 1)), 8, RoundingMode.HALF_UP);
64 }
65
66 /**
67 *
68 * @param bid
69 * @return true iff bid is contained in this ordering
70 */
71 public boolean contains(Bid bid) {
72 return bids.contains(bid);
73 }
74
75 /**
76 *
77 * @return list of all bids in the current ordering.
78 */
79 public List<Bid> getBids() {
80 return Collections.unmodifiableList(bids);
81 }
82
83 /**
84 *
85 * @param profile
86 * @return a list of bids in the profile sorted from low to high utility.
87 */
88 private static List<Bid> getSortedBids(Profile profile) {
89 if (!(profile instanceof DefaultPartialOrdering)) {
90 throw new UnsupportedOperationException("Only DefaultPartialOrdering supported");
91 }
92 DefaultPartialOrdering prof = (DefaultPartialOrdering) profile;
93 List<Bid> bidslist = prof.getBids();
94 // NOTE sort defaults to ascending order, this is missing in docs.
95 Collections.sort(bidslist, new Comparator<Bid>() {
96
97 @Override
98 public int compare(Bid b1, Bid b2) {
99 return prof.isPreferredOrEqual(b1, b2) ? 1 : -1;
100 }
101
102 });
103
104 return bidslist;
105 }
106
107 /**
108 * @param bid a new bid to be inserted
109 * @param worseBids all bids that are worse than this bid.
110 * @return a SimpleLinearOrdering, updated with the given comparison. Thee bid
111 * will be inserted after the first bid that is not worse than bid.
112 */
113 public SimpleLinearOrdering with(Bid bid, List<Bid> worseBids) {
114 int n = 0;
115 while (n < bids.size() && worseBids.contains(bids.get(n)))
116 n++;
117 LinkedList<Bid> newbids = new LinkedList<Bid>(bids);
118 newbids.add(n, bid);
119 return new SimpleLinearOrdering(domain, newbids);
120 }
121
122}
Note: See TracBrowser for help on using the repository browser.