[1] | 1 | package geniusweb.exampleparties.anaconda;
|
---|
| 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 | public class SimpleLinearOrdering implements UtilitySpace {
|
---|
| 17 |
|
---|
| 18 | private final Domain domain;
|
---|
| 19 | private final List<Bid> bids; // worst bid first, best bid last.
|
---|
| 20 |
|
---|
| 21 | SimpleLinearOrdering(Profile profile) {
|
---|
| 22 | this(profile.getDomain(), getSortedBids(profile));
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | SimpleLinearOrdering(Domain domain, List<Bid> bids) {
|
---|
| 26 | this.domain = domain;
|
---|
| 27 | this.bids = bids;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | @Override
|
---|
| 31 | public String getName() {
|
---|
| 32 | throw new UnsupportedOperationException();
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | @Override
|
---|
| 36 | public Domain getDomain() {
|
---|
| 37 | return domain;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | @Override
|
---|
| 41 | public Bid getReservationBid() {
|
---|
| 42 | throw new UnsupportedOperationException();
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | @Override
|
---|
| 46 | public BigDecimal getUtility(Bid bid) {
|
---|
| 47 | if (bids.size() < 2 || !bids.contains(bid)) {
|
---|
| 48 | return BigDecimal.ZERO;
|
---|
| 49 | }
|
---|
| 50 | // using 8 decimals, we have to pick something here
|
---|
| 51 | return new BigDecimal(bids.indexOf(bid)).divide(
|
---|
| 52 | new BigDecimal((bids.size() - 1)), 8, RoundingMode.HALF_UP);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 |
|
---|
| 56 | public boolean contains(Bid bid) {
|
---|
| 57 | return bids.contains(bid);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | public List<Bid> getBids() {
|
---|
| 61 | return Collections.unmodifiableList(bids);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | private static List<Bid> getSortedBids(Profile profile) {
|
---|
| 65 | if (!(profile instanceof DefaultPartialOrdering)) {
|
---|
| 66 | throw new UnsupportedOperationException(
|
---|
| 67 | "Only DefaultPartialOrdering supported");
|
---|
| 68 | }
|
---|
| 69 | DefaultPartialOrdering prof = (DefaultPartialOrdering) profile;
|
---|
| 70 | List<Bid> bidslist = prof.getBids();
|
---|
| 71 | // NOTE sort defaults to ascending order, this is missing in docs.
|
---|
| 72 | Collections.sort(bidslist, new Comparator<Bid>() {
|
---|
| 73 |
|
---|
| 74 | @Override
|
---|
| 75 | public int compare(Bid b1, Bid b2) {
|
---|
| 76 | return prof.isPreferredOrEqual(b1, b2) ? 1 : -1;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | });
|
---|
| 80 |
|
---|
| 81 | return bidslist;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 | public SimpleLinearOrdering with(Bid bid, List<Bid> worseBids) {
|
---|
| 86 | int n = 0;
|
---|
| 87 | while (n < bids.size() && worseBids.contains(bids.get(n)))
|
---|
| 88 | n++;
|
---|
| 89 | LinkedList<Bid> newbids = new LinkedList<Bid>(bids);
|
---|
| 90 | newbids.add(n, bid);
|
---|
| 91 | return new SimpleLinearOrdering(domain, newbids);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | }
|
---|