[519] | 1 | package geniusweb.profile;
|
---|
| 2 |
|
---|
| 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.Arrays;
|
---|
| 5 | import java.util.HashMap;
|
---|
| 6 | import java.util.HashSet;
|
---|
| 7 | import java.util.LinkedList;
|
---|
| 8 | import java.util.List;
|
---|
| 9 | import java.util.Map;
|
---|
| 10 | import java.util.Set;
|
---|
| 11 |
|
---|
| 12 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
| 13 | import com.fasterxml.jackson.annotation.JsonGetter;
|
---|
| 14 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
| 15 |
|
---|
| 16 | import geniusweb.issuevalue.Bid;
|
---|
| 17 | import geniusweb.issuevalue.Domain;
|
---|
| 18 |
|
---|
| 19 | /**
|
---|
| 20 | * Default implementation of partial ordering that stores all is-better
|
---|
| 21 | * relations explicitly in a map.
|
---|
| 22 | *
|
---|
| 23 | * NOTICE this can handle profiles of max size Integer. This is because various
|
---|
| 24 | * functions used here rely on basic java functions that can handle only int.
|
---|
| 25 | * Besides, the size of partial maps grows very rapidly so this approach is
|
---|
| 26 | * limited anyway.
|
---|
| 27 | */
|
---|
| 28 | public class DefaultPartialOrdering extends DefaultProfile
|
---|
| 29 | implements PartialOrdering {
|
---|
| 30 |
|
---|
| 31 | /**
|
---|
| 32 | * Set is sparsely filled. If a Bid is not a key, it is not better than any
|
---|
| 33 | * other bid.
|
---|
| 34 | */
|
---|
| 35 | private final Map<Bid, Set<Bid>> isBetter;
|
---|
| 36 |
|
---|
| 37 | /**
|
---|
| 38 | *
|
---|
| 39 | * @param name name of the profile
|
---|
| 40 | * @param domain the {@link Domain}
|
---|
| 41 | * @param reservationbid the reservation bid, this is the minimum acceptable
|
---|
| 42 | * bid
|
---|
| 43 | * @param bids a list of bids that this ordering contains
|
---|
| 44 | * comparisons for
|
---|
| 45 | * @param isbetterList a list of tuples [bid1index, bid2index]. It
|
---|
| 46 | * indicates that bids[bid1index] isbetterthan
|
---|
| 47 | * bids[bid2index].
|
---|
| 48 | */
|
---|
| 49 | @JsonCreator
|
---|
| 50 | public DefaultPartialOrdering(@JsonProperty("name") String name,
|
---|
| 51 | @JsonProperty("domain") Domain domain,
|
---|
| 52 | @JsonProperty("reservationBid") Bid reservationbid,
|
---|
| 53 | @JsonProperty("bids") List<Bid> bids,
|
---|
| 54 | @JsonProperty("better") List<List<Integer>> isbetterList) {
|
---|
| 55 | this(name, domain, reservationbid, makeBidMap(bids, isbetterList));
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | /**
|
---|
| 59 | * @param name the name for the profile
|
---|
| 60 | * @param domain the {@link Domain} description
|
---|
| 61 | * @param reservationbid the reservation {@link Bid}
|
---|
| 62 | * @param isBetterMap a map with keys = a better bid and value=a less
|
---|
| 63 | * good bid.
|
---|
| 64 | */
|
---|
| 65 | public DefaultPartialOrdering(String name, Domain domain,
|
---|
| 66 | Bid reservationbid, Map<Bid, Set<Bid>> isBetterMap) {
|
---|
| 67 | super(name, domain, reservationbid);
|
---|
| 68 | this.isBetter = isBetterMap;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | @Override
|
---|
| 72 | public boolean isPreferredOrEqual(Bid bid1, Bid bid2) {
|
---|
| 73 | if (!isBetter.containsKey(bid1))
|
---|
| 74 | return false;
|
---|
| 75 | return isBetter.get(bid1).contains(bid2);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | *
|
---|
| 80 | * @return a list with all the bids that are referred to, either as better
|
---|
| 81 | * or as worse than another bid
|
---|
| 82 | */
|
---|
| 83 | @JsonGetter
|
---|
| 84 | public List<Bid> getBids() {
|
---|
| 85 | // FIXME the iteration order may not be guaranteed!
|
---|
| 86 | Set<Bid> bids = new HashSet<>();
|
---|
| 87 | for (Bid bid : isBetter.keySet()) {
|
---|
| 88 | bids.add(bid);
|
---|
| 89 | bids.addAll(isBetter.get(bid));
|
---|
| 90 | }
|
---|
| 91 | return new ArrayList<Bid>(bids);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | @JsonGetter
|
---|
| 95 | /**
|
---|
| 96 | *
|
---|
| 97 | * @return a list of tuples [bid1index, bid2index]. It indicates that
|
---|
| 98 | * bids[bid1index] isbetterthan bids[bid2index].
|
---|
| 99 | */
|
---|
| 100 | public List<List<Integer>> getBetter() {
|
---|
| 101 | List<List<Integer>> betterlist = new LinkedList<>();
|
---|
| 102 | List<Bid> bidslist = getBids();
|
---|
| 103 |
|
---|
| 104 | for (Bid bid : bidslist) {
|
---|
| 105 | if (isBetter.containsKey(bid)) {
|
---|
| 106 | for (Bid worsebid : isBetter.get(bid)) {
|
---|
| 107 | betterlist.add(Arrays.asList(bidslist.indexOf(bid),
|
---|
| 108 | bidslist.indexOf(worsebid)));
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | return betterlist;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | @Override
|
---|
| 117 | public String toString() {
|
---|
| 118 | return "DefaultPartialOrdering[" + getValuesString() + "," + isBetter
|
---|
| 119 | + "]";
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | private static Map<Bid, Set<Bid>> makeBidMap(List<Bid> bids,
|
---|
| 123 | List<List<Integer>> isBetterList) {
|
---|
| 124 | Map<Bid, Set<Bid>> betterMap = new HashMap<>();
|
---|
| 125 |
|
---|
| 126 | for (List<Integer> tuple : isBetterList) {
|
---|
| 127 | if (tuple.size() != 2) {
|
---|
| 128 | throw new IllegalArgumentException("Expected tuple but found "
|
---|
| 129 | + tuple + "in " + isBetterList);
|
---|
| 130 | }
|
---|
| 131 | Bid betterbid = bids.get(tuple.get(0));
|
---|
| 132 | Set<Bid> map = betterMap.get(betterbid);
|
---|
| 133 | if (map == null) {
|
---|
| 134 | map = new HashSet<>();
|
---|
| 135 | betterMap.put(betterbid, map);
|
---|
| 136 | }
|
---|
| 137 | map.add(bids.get(tuple.get(1)));
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | return betterMap;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | @Override
|
---|
| 144 | public int hashCode() {
|
---|
| 145 | final int prime = 31;
|
---|
| 146 | int result = super.hashCode();
|
---|
| 147 | result = prime * result
|
---|
| 148 | + ((isBetter == null) ? 0 : isBetter.hashCode());
|
---|
| 149 | return result;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | @Override
|
---|
| 153 | public boolean equals(Object obj) {
|
---|
| 154 | if (this == obj)
|
---|
| 155 | return true;
|
---|
| 156 | if (!super.equals(obj))
|
---|
| 157 | return false;
|
---|
| 158 | if (getClass() != obj.getClass())
|
---|
| 159 | return false;
|
---|
| 160 | DefaultPartialOrdering other = (DefaultPartialOrdering) obj;
|
---|
| 161 | if (isBetter == null) {
|
---|
| 162 | if (other.isBetter != null)
|
---|
| 163 | return false;
|
---|
| 164 | } else if (!isBetter.equals(other.isBetter))
|
---|
| 165 | return false;
|
---|
| 166 | return true;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | }
|
---|