[316] | 1 | package bargainingchips;
|
---|
[315] | 2 |
|
---|
| 3 | import java.util.List;
|
---|
[339] | 4 | import java.util.Random;
|
---|
[315] | 5 | import java.util.ArrayList;
|
---|
[339] | 6 | import java.util.Iterator;
|
---|
[315] | 7 |
|
---|
[316] | 8 | import bargainingchips.actions.Offer;
|
---|
| 9 | import bargainingchips.players.Agent;
|
---|
| 10 | import bargainingchips.utilityfunctions.UtilityFunction;
|
---|
[315] | 11 |
|
---|
[339] | 12 |
|
---|
[315] | 13 | /**
|
---|
[339] | 14 | *
|
---|
[315] | 15 | * This class represents the complete outcome space and is therefore useful if
|
---|
| 16 | * someone wants to quickly implement an agent. Note that working with a sorted
|
---|
| 17 | * outcomespace class would be faster during the negotiation.
|
---|
[339] | 18 | *
|
---|
| 19 | *
|
---|
| 20 | * @author Tim Baarslag and Faria Nassiri-Mofakham
|
---|
| 21 | *
|
---|
[315] | 22 | */
|
---|
[339] | 23 | public class OutcomeSpace implements Iterable<Bundle>
|
---|
[315] | 24 | {
|
---|
| 25 | /** List of all possible bids in the domain */
|
---|
| 26 | protected List<Bundle> allBids = new ArrayList<Bundle>();
|
---|
[339] | 27 | protected String fileName;
|
---|
| 28 |
|
---|
| 29 | private double discountFactor = 1;
|
---|
[315] | 30 |
|
---|
[339] | 31 |
|
---|
[315] | 32 | /**
|
---|
| 33 | * Creates a test outcome space.
|
---|
| 34 | */
|
---|
| 35 | public OutcomeSpace()
|
---|
| 36 | {
|
---|
| 37 | generateAllBids();
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | * Generates all the possible bids in the domain
|
---|
| 42 | *
|
---|
| 43 | * @param utilSpace
|
---|
| 44 | */
|
---|
| 45 | private void generateAllBids()
|
---|
| 46 | {
|
---|
| 47 | for (int g = 1; g < 10; g++)
|
---|
| 48 | allBids.add(Offer.getSampleOffer("Green", g).getBundle());
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[339] | 51 | public List<Bundle> getAllBids()
|
---|
| 52 | {
|
---|
| 53 | return allBids;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 |
|
---|
[315] | 57 | /**
|
---|
| 58 | * gets a {@link Bundle} which is closest to the given utility
|
---|
| 59 | *
|
---|
| 60 | * @param utility to which the found bid must be closest.
|
---|
| 61 | */
|
---|
| 62 | public Bundle getBidNearUtility(double utility, UtilityFunction u, Agent a)
|
---|
| 63 | {
|
---|
| 64 | int index = getIndexOfBidNearUtility(utility, u);
|
---|
| 65 | Bundle bundle = allBids.get(index);
|
---|
| 66 | System.out.println(a + ": the bid closest to target util " + utility + " is " + bundle + " (#" + index + ") with util " + u.getUtility(bundle));
|
---|
| 67 | return bundle;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /**
|
---|
| 71 | * @return best bid in the domain.
|
---|
| 72 | */
|
---|
[337] | 73 | public Bundle getMaxBidPossible(UtilityFunction u)
|
---|
[315] | 74 | {
|
---|
| 75 | Bundle maxBid = allBids.get(0);
|
---|
| 76 | for (Bundle bid : allBids) {
|
---|
| 77 | if (u.getUtility(bid) > u.getUtility(maxBid)) {
|
---|
| 78 | maxBid = bid;
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | return maxBid;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * @return worst bid in the domain.
|
---|
| 86 | */
|
---|
[339] | 87 | public Bundle getMinBidPossible(UtilityFunction u) {
|
---|
[315] | 88 | Bundle minBid = allBids.get(0);
|
---|
| 89 | for (Bundle bid : allBids) {
|
---|
| 90 | if (u.getUtility(bid) < u.getUtility(minBid)) {
|
---|
| 91 | minBid = bid;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | return minBid;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /**
|
---|
| 98 | * @param utility
|
---|
| 99 | * to which the found bid must be closest.
|
---|
| 100 | * @return index of the bid with the utility closest to the given utilty.
|
---|
| 101 | */
|
---|
| 102 | private int getIndexOfBidNearUtility(double utility, UtilityFunction u)
|
---|
| 103 | {
|
---|
| 104 | double closesDistance = 1;
|
---|
| 105 | int best = 0;
|
---|
| 106 | for (int i = 0; i < allBids.size(); i++) {
|
---|
| 107 | if (Math.abs(u.getUtility(allBids.get(i)) - utility) < closesDistance) {
|
---|
| 108 | closesDistance = Math.abs(u.getUtility(allBids.get(i)) - utility);
|
---|
| 109 | best = i;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | return best;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | @Override
|
---|
[339] | 116 | public String toString()
|
---|
| 117 | {
|
---|
[315] | 118 | String all = "";
|
---|
| 119 | for (Bundle b : allBids) {
|
---|
| 120 | all += b.toString() + "\n,";
|
---|
| 121 | }
|
---|
| 122 | return "OutcomeSpace[" + all + "]";
|
---|
| 123 | }
|
---|
[339] | 124 |
|
---|
| 125 | @Override
|
---|
| 126 | public Iterator<Bundle> iterator() {
|
---|
| 127 | return getAllBids().iterator();
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | public Bundle getRandom()
|
---|
| 131 | {
|
---|
| 132 | int size = size();
|
---|
| 133 | if (size == 0)
|
---|
| 134 | return null;
|
---|
| 135 | int index = (new Random()).nextInt(size);
|
---|
| 136 | return allBids.get(index);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | public Bundle getRandom(Random r)
|
---|
| 140 | {
|
---|
| 141 | int size = size();
|
---|
| 142 | if (size == 0)
|
---|
| 143 | return null;
|
---|
| 144 | int index = r.nextInt(size);
|
---|
| 145 | return allBids.get(index);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | public double getAverageUtility(UtilityFunction u)
|
---|
| 149 | {
|
---|
| 150 | int size = size();
|
---|
| 151 | if (size == 0)
|
---|
| 152 | return 0;
|
---|
| 153 | double totalUtil = 0;
|
---|
| 154 | for (Bundle b : allBids)
|
---|
| 155 | totalUtil += u.getUtility(b);
|
---|
| 156 | return totalUtil / size;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | public int size()
|
---|
| 160 | {
|
---|
| 161 | return allBids.size();
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | /**
|
---|
| 165 | * Check if this utility space is ready for negotiation. To be so, the
|
---|
| 166 | * domain must match the given domain and the space must be complete.
|
---|
| 167 | *
|
---|
| 168 | * @param dom
|
---|
| 169 | * is the domain in which nego is taking place
|
---|
| 170 | * @throws Exception
|
---|
| 171 | * if utility space is incomplete (@see isComplete())
|
---|
| 172 | */
|
---|
| 173 | public void checkReadyForNegotiation(List<Bundle> domain) throws Exception
|
---|
| 174 | {
|
---|
| 175 | if (!(domain.equals(getAllBids())))
|
---|
| 176 | throw new IllegalStateException(
|
---|
| 177 | "domain does not match the negotiation domain");
|
---|
| 178 | else
|
---|
| 179 | throw new IllegalStateException(
|
---|
| 180 | "utility space is incomplete:");
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | public String getName()
|
---|
| 184 | {
|
---|
| 185 | return getClass().getSimpleName();
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | public double getUtility(UtilityFunction u, Bundle b)
|
---|
| 189 | {
|
---|
| 190 | return u.getUtility(b);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | public double getReservationValue(UtilityFunction u)
|
---|
| 194 | {
|
---|
| 195 | try {
|
---|
| 196 | double fReservationValue = u.getUtility(getMinBidPossible(u));
|
---|
| 197 | return fReservationValue;
|
---|
| 198 | }
|
---|
| 199 | catch (Exception e) {
|
---|
| 200 | e.printStackTrace();
|
---|
| 201 | return 0;
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | public final double getDiscountFactor() {
|
---|
| 206 | return discountFactor;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | // public void setDiscount(double newDiscount) {
|
---|
| 210 | // discountFactor = validateDiscount(newDiscount);
|
---|
| 211 | // }
|
---|
| 212 | //
|
---|
| 213 | // protected double validateDiscount(double df) {
|
---|
| 214 | // if (df < 0 || df > 1) {
|
---|
| 215 | // System.err.println(
|
---|
| 216 | // "Warning: discount factor = " + df + " was discarded.");
|
---|
| 217 | // }
|
---|
| 218 | //
|
---|
| 219 | // if (df <= 0 || df > 1) {
|
---|
| 220 | // df = 1;
|
---|
| 221 | // }
|
---|
| 222 | // return df;
|
---|
| 223 | // }
|
---|
[315] | 224 | } |
---|