[1] | 1 | package geniusweb.exampleparties.newagentgg;
|
---|
| 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 |
|
---|
[54] | 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 |
|
---|
[1] | 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
|
---|
[54] | 32 | * @param bids a list of bids, ordered from lowest to highest util. The first
|
---|
| 33 | * bid will have utility 0, the last utility 1. If only 0 or 1 bid
|
---|
| 34 | * in the list, or if the bid is not known, it will have utility
|
---|
| 35 | * 0.
|
---|
[1] | 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
|
---|
[54] | 63 | return new BigDecimal(bids.indexOf(bid)).divide(new BigDecimal((bids.size() - 1)), 8, RoundingMode.HALF_UP);
|
---|
[1] | 64 | }
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | *
|
---|
| 68 | * @param bid
|
---|
| 69 | * @return true iff bid is contained in this ordering
|
---|
| 70 | */
|
---|
| 71 | public boolean contains(Bid bid) {
|
---|
| 72 | return bids.contains(bid);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | /**
|
---|
| 76 | *
|
---|
| 77 | * @return list of all bids in the current ordering.
|
---|
| 78 | */
|
---|
| 79 | public List<Bid> getBids() {
|
---|
| 80 | return Collections.unmodifiableList(bids);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
| 84 | *
|
---|
| 85 | * @param profile
|
---|
| 86 | * @return a list of bids in the profile sorted from low to high utility.
|
---|
| 87 | */
|
---|
| 88 | private static List<Bid> getSortedBids(Profile profile) {
|
---|
| 89 | if (!(profile instanceof DefaultPartialOrdering)) {
|
---|
[54] | 90 | throw new UnsupportedOperationException("Only DefaultPartialOrdering supported");
|
---|
[1] | 91 | }
|
---|
| 92 | DefaultPartialOrdering prof = (DefaultPartialOrdering) profile;
|
---|
| 93 | List<Bid> bidslist = prof.getBids();
|
---|
| 94 | // NOTE sort defaults to ascending order, this is missing in docs.
|
---|
| 95 | Collections.sort(bidslist, new Comparator<Bid>() {
|
---|
| 96 |
|
---|
| 97 | @Override
|
---|
| 98 | public int compare(Bid b1, Bid b2) {
|
---|
| 99 | return prof.isPreferredOrEqual(b1, b2) ? 1 : -1;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | });
|
---|
| 103 |
|
---|
| 104 | return bidslist;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
| 108 | * @param bid a new bid to be inserted
|
---|
| 109 | * @param worseBids all bids that are worse than this bid.
|
---|
[54] | 110 | * @return a SimpleLinearOrdering, updated with the given comparison. Thee bid
|
---|
| 111 | * will be inserted after the first bid that is not worse than bid.
|
---|
[1] | 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 | }
|
---|