source: anac2020/Anaconda/src/main/java/geniusweb/exampleparties/anaconda/SimpleLinearOrdering.java@ 14

Last change on this file since 14 was 1, checked in by wouter, 4 years ago

#1910 added anac2020 parties

File size: 2.4 KB
Line 
1package geniusweb.exampleparties.anaconda;
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
16public class SimpleLinearOrdering implements UtilitySpace {
17
18 private final Domain domain;
19 private final List<Bid> bids; // worst bid first, best bid last.
20
21 SimpleLinearOrdering(Profile profile) {
22 this(profile.getDomain(), getSortedBids(profile));
23 }
24
25 SimpleLinearOrdering(Domain domain, List<Bid> bids) {
26 this.domain = domain;
27 this.bids = bids;
28 }
29
30 @Override
31 public String getName() {
32 throw new UnsupportedOperationException();
33 }
34
35 @Override
36 public Domain getDomain() {
37 return domain;
38 }
39
40 @Override
41 public Bid getReservationBid() {
42 throw new UnsupportedOperationException();
43 }
44
45 @Override
46 public BigDecimal getUtility(Bid bid) {
47 if (bids.size() < 2 || !bids.contains(bid)) {
48 return BigDecimal.ZERO;
49 }
50 // using 8 decimals, we have to pick something here
51 return new BigDecimal(bids.indexOf(bid)).divide(
52 new BigDecimal((bids.size() - 1)), 8, RoundingMode.HALF_UP);
53 }
54
55
56 public boolean contains(Bid bid) {
57 return bids.contains(bid);
58 }
59
60 public List<Bid> getBids() {
61 return Collections.unmodifiableList(bids);
62 }
63
64 private static List<Bid> getSortedBids(Profile profile) {
65 if (!(profile instanceof DefaultPartialOrdering)) {
66 throw new UnsupportedOperationException(
67 "Only DefaultPartialOrdering supported");
68 }
69 DefaultPartialOrdering prof = (DefaultPartialOrdering) profile;
70 List<Bid> bidslist = prof.getBids();
71 // NOTE sort defaults to ascending order, this is missing in docs.
72 Collections.sort(bidslist, new Comparator<Bid>() {
73
74 @Override
75 public int compare(Bid b1, Bid b2) {
76 return prof.isPreferredOrEqual(b1, b2) ? 1 : -1;
77 }
78
79 });
80
81 return bidslist;
82 }
83
84
85 public SimpleLinearOrdering with(Bid bid, List<Bid> worseBids) {
86 int n = 0;
87 while (n < bids.size() && worseBids.contains(bids.get(n)))
88 n++;
89 LinkedList<Bid> newbids = new LinkedList<Bid>(bids);
90 newbids.add(n, bid);
91 return new SimpleLinearOrdering(domain, newbids);
92 }
93
94}
Note: See TracBrowser for help on using the repository browser.