source: exampleparties/simpleshaop/src/main/java/geniusweb/exampleparties/simpleshaop/SimpleLinearOrdering.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

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