1 | package geniusweb.bagga.dicehaggler;
|
---|
2 |
|
---|
3 | import java.math.BigDecimal;
|
---|
4 | import java.math.RoundingMode;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.Comparator;
|
---|
7 | import java.util.LinkedList;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import geniusweb.issuevalue.Bid;
|
---|
11 | import geniusweb.issuevalue.Domain;
|
---|
12 | import geniusweb.profile.DefaultPartialOrdering;
|
---|
13 | import geniusweb.profile.Profile;
|
---|
14 | import 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 | */
|
---|
20 | public 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
|
---|
33 | * first bid will have utility 0, the last utility 1. If only
|
---|
34 | * 0 or 1 bid in the list, or if the bid is not known, it will
|
---|
35 | * have utility 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(
|
---|
64 | new BigDecimal((bids.size() - 1)), 8, RoundingMode.HALF_UP);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | *
|
---|
69 | * @param bid
|
---|
70 | * @return true iff bid is contained in this ordering
|
---|
71 | */
|
---|
72 | public boolean contains(Bid bid) {
|
---|
73 | return bids.contains(bid);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | *
|
---|
78 | * @return list of all bids in the current ordering.
|
---|
79 | */
|
---|
80 | public List<Bid> getBids() {
|
---|
81 | return Collections.unmodifiableList(bids);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | *
|
---|
86 | * @param profile
|
---|
87 | * @return a list of bids in the profile sorted from low to high utility.
|
---|
88 | */
|
---|
89 | private static List<Bid> getSortedBids(Profile profile) {
|
---|
90 | if (!(profile instanceof DefaultPartialOrdering)) {
|
---|
91 | throw new UnsupportedOperationException(
|
---|
92 | "Only DefaultPartialOrdering supported");
|
---|
93 | }
|
---|
94 | DefaultPartialOrdering prof = (DefaultPartialOrdering) profile;
|
---|
95 | List<Bid> bidslist = prof.getBids();
|
---|
96 | // NOTE sort defaults to ascending order, this is missing in docs.
|
---|
97 | Collections.sort(bidslist, new Comparator<Bid>() {
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public int compare(Bid b1, Bid b2) {
|
---|
101 | return prof.isPreferredOrEqual(b1, b2) ? 1 : -1;
|
---|
102 | }
|
---|
103 |
|
---|
104 | });
|
---|
105 |
|
---|
106 | return bidslist;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * @param bid a new bid to be inserted
|
---|
111 | * @param worseBids all bids that are worse than this bid.
|
---|
112 | * @return a SimpleLinearOrdering, updated with the given comparison. Thee
|
---|
113 | * bid will be inserted after the first bid that is not worse than
|
---|
114 | * bid.
|
---|
115 | */
|
---|
116 | public SimpleLinearOrdering with(Bid bid, List<Bid> worseBids) {
|
---|
117 | int n = 0;
|
---|
118 | while (n < bids.size() && worseBids.contains(bids.get(n)))
|
---|
119 | n++;
|
---|
120 | LinkedList<Bid> newbids = new LinkedList<Bid>(bids);
|
---|
121 | newbids.add(n, bid);
|
---|
122 | return new SimpleLinearOrdering(domain, newbids);
|
---|
123 | }
|
---|
124 |
|
---|
125 | }
|
---|