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