[284] | 1 | package onetomany.bargainingchipsgame;
|
---|
| 2 |
|
---|
| 3 | import java.util.List;
|
---|
| 4 |
|
---|
| 5 | import java.util.ArrayList;
|
---|
| 6 |
|
---|
[300] | 7 | import onetomany.bargainingchipsgame.interactions.Offer;
|
---|
[302] | 8 | import onetomany.bargainingchipsgame.players.Agent;
|
---|
[284] | 9 | import onetomany.bargainingchipsgame.players.utilityfunction.UtilityFunction;
|
---|
| 10 |
|
---|
| 11 | /**
|
---|
| 12 | * This class generates the complete outcome space and is therefore useful if
|
---|
| 13 | * someone wants to quickly implement an agent. Note that working with a sorted
|
---|
| 14 | * outcomespace class would be faster during the negotiation.
|
---|
| 15 | */
|
---|
| 16 | public class OutcomeSpace
|
---|
| 17 | {
|
---|
| 18 | /** List of all possible bids in the domain */
|
---|
| 19 | protected List<Bundle> allBids = new ArrayList<Bundle>();
|
---|
| 20 |
|
---|
| 21 | /**
|
---|
| 22 | * Creates an unsorted outcome space. Warning: this call iterates over ALL
|
---|
| 23 | * possible bids.
|
---|
| 24 | *
|
---|
| 25 | * @param utilSpace
|
---|
| 26 | */
|
---|
[285] | 27 | public OutcomeSpace(Domain domain)
|
---|
[284] | 28 | {
|
---|
| 29 | generateAllBids();
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * Generates all the possible bids in the domain
|
---|
| 34 | *
|
---|
| 35 | * @param utilSpace
|
---|
| 36 | */
|
---|
[302] | 37 | private void generateAllBids()
|
---|
[284] | 38 | {
|
---|
[300] | 39 | allBids.add(Offer.getSampleOffer("Green", 1).getBundle());
|
---|
| 40 | allBids.add(Offer.getSampleOffer("Green", 2).getBundle());
|
---|
| 41 | allBids.add(Offer.getSampleOffer("Green", 3).getBundle());
|
---|
[302] | 42 | allBids.add(Offer.getSampleOffer("Green", 4).getBundle());
|
---|
| 43 | allBids.add(Offer.getSampleOffer("Green", 5).getBundle());
|
---|
| 44 | allBids.add(Offer.getSampleOffer("Green", 6).getBundle());
|
---|
| 45 | allBids.add(Offer.getSampleOffer("Green", 7).getBundle());
|
---|
| 46 | allBids.add(Offer.getSampleOffer("Green", 8).getBundle());
|
---|
[284] | 47 | }
|
---|
| 48 |
|
---|
| 49 | /**
|
---|
[302] | 50 | * gets a {@link Bundle} which is closest to the given utility
|
---|
[284] | 51 | *
|
---|
[302] | 52 | * @param utility to which the found bid must be closest.
|
---|
[284] | 53 | */
|
---|
[302] | 54 | public Bundle getBidNearUtility(double utility, UtilityFunction u, Agent a)
|
---|
[284] | 55 | {
|
---|
[300] | 56 | int index = getIndexOfBidNearUtility(utility, u);
|
---|
| 57 | Bundle bundle = allBids.get(index);
|
---|
[302] | 58 | System.out.println(a + ": the bid closest to target util " + utility + " is " + bundle + " (#" + index + ") with util " + u.getUtility(bundle));
|
---|
[300] | 59 | return bundle;
|
---|
[284] | 60 | }
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | * @return best bid in the domain.
|
---|
| 64 | */
|
---|
[302] | 65 | private Bundle getMaxBidPossible(UtilityFunction u)
|
---|
[284] | 66 | {
|
---|
| 67 | Bundle maxBid = allBids.get(0);
|
---|
| 68 | for (Bundle bid : allBids) {
|
---|
| 69 | if (u.getUtility(bid) > u.getUtility(maxBid)) {
|
---|
| 70 | maxBid = bid;
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | return maxBid;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | * @return worst bid in the domain.
|
---|
| 78 | */
|
---|
[302] | 79 | private Bundle getMinBidPossible(UtilityFunction u) {
|
---|
[284] | 80 | Bundle minBid = allBids.get(0);
|
---|
| 81 | for (Bundle bid : allBids) {
|
---|
| 82 | if (u.getUtility(bid) < u.getUtility(minBid)) {
|
---|
| 83 | minBid = bid;
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | return minBid;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /**
|
---|
| 90 | * @param utility
|
---|
| 91 | * to which the found bid must be closest.
|
---|
| 92 | * @return index of the bid with the utility closest to the given utilty.
|
---|
| 93 | */
|
---|
[302] | 94 | private int getIndexOfBidNearUtility(double utility, UtilityFunction u)
|
---|
[284] | 95 | {
|
---|
| 96 | double closesDistance = 1;
|
---|
| 97 | int best = 0;
|
---|
| 98 | for (int i = 0; i < allBids.size(); i++) {
|
---|
| 99 | if (Math.abs(u.getUtility(allBids.get(i)) - utility) < closesDistance) {
|
---|
| 100 | closesDistance = Math.abs(u.getUtility(allBids.get(i)) - utility);
|
---|
| 101 | best = i;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | return best;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | @Override
|
---|
| 108 | public String toString() {
|
---|
| 109 | String all = "";
|
---|
| 110 | for (Bundle b : allBids) {
|
---|
| 111 | all += b.toString() + "\n,";
|
---|
| 112 | }
|
---|
| 113 | return "OutcomeSpace[" + all + "]";
|
---|
| 114 | }
|
---|
| 115 | } |
---|