[1] | 1 | package geniusweb.exampleparties.simpleshaop;
|
---|
| 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 | *
|
---|
| 32 | * @param domain
|
---|
| 33 | * @param bids a list of bids, ordered from lowest to highest util. The
|
---|
| 34 | * first bid will have utility 0, the last utility 1. If only
|
---|
| 35 | * 0 or 1 bid in the list, or if the bid is not known, it will
|
---|
| 36 | * have utility 0.
|
---|
| 37 | */
|
---|
| 38 | SimpleLinearOrdering(Domain domain, List<Bid> bids) {
|
---|
| 39 | this.domain = domain;
|
---|
| 40 | this.bids = bids;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | @Override
|
---|
| 44 | public String getName() {
|
---|
| 45 | throw new UnsupportedOperationException();
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | @Override
|
---|
| 49 | public Domain getDomain() {
|
---|
| 50 | return domain;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | @Override
|
---|
| 54 | public Bid getReservationBid() {
|
---|
| 55 | throw new UnsupportedOperationException();
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | @Override
|
---|
| 59 | public BigDecimal getUtility(Bid bid) {
|
---|
| 60 | if (bids.size() < 2 || !bids.contains(bid)) {
|
---|
| 61 | return BigDecimal.ZERO;
|
---|
| 62 | }
|
---|
| 63 | // using 8 decimals, we have to pick something here
|
---|
| 64 | return new BigDecimal(bids.indexOf(bid)).divide(
|
---|
| 65 | new BigDecimal((bids.size() - 1)), 8, RoundingMode.HALF_UP);
|
---|
| 66 |
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | *
|
---|
| 71 | * @param 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
|
---|
| 89 | * @return a list of bids in the profile sorted from low to high utility.
|
---|
| 90 | */
|
---|
| 91 | private static List<Bid> getSortedBids(Profile profile) {
|
---|
| 92 | if (!(profile instanceof DefaultPartialOrdering)) {
|
---|
| 93 | throw new UnsupportedOperationException(
|
---|
| 94 | "Only DefaultPartialOrdering supported");
|
---|
| 95 | }
|
---|
| 96 | DefaultPartialOrdering prof = (DefaultPartialOrdering) profile;
|
---|
| 97 | List<Bid> bidslist = prof.getBids();
|
---|
| 98 | // NOTE sort defaults to ascending order, this is missing in docs.
|
---|
| 99 | Collections.sort(bidslist, new Comparator<Bid>() {
|
---|
| 100 |
|
---|
| 101 | @Override
|
---|
| 102 | public int compare(Bid b1, Bid b2) {
|
---|
| 103 | return prof.isPreferredOrEqual(b1, b2) ? 1 : -1;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | });
|
---|
| 107 |
|
---|
| 108 | return bidslist;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /**
|
---|
| 112 | * @param bid a new bid to be inserted
|
---|
| 113 | * @param worseBids all bids that are worse than this bid.
|
---|
| 114 | * @return a SimpleLinearOrdering, updated with the given comparison. Thee
|
---|
| 115 | * bid will be inserted after the first bid that is not worse than
|
---|
| 116 | * bid.
|
---|
| 117 | */
|
---|
| 118 | public SimpleLinearOrdering with(Bid bid, List<Bid> worseBids) {
|
---|
| 119 | int n = 0;
|
---|
| 120 | while (n < bids.size() && worseBids.contains(bids.get(n)))
|
---|
| 121 | n++;
|
---|
| 122 | LinkedList<Bid> newbids = new LinkedList<Bid>(bids);
|
---|
| 123 | newbids.add(n, bid);
|
---|
| 124 | return new SimpleLinearOrdering(domain, newbids);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /**
|
---|
| 128 | *
|
---|
| 129 | * @return the bid that gives maximum utility
|
---|
| 130 | */
|
---|
| 131 | public Bid maxBid() {
|
---|
| 132 | return bids.get(bids.size() - 1);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /**
|
---|
| 136 | *
|
---|
| 137 | * @return the bid that gives the minimum
|
---|
| 138 | */
|
---|
| 139 | public Bid minBid() {
|
---|
| 140 | return bids.get(0);
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | }
|
---|