[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 | /**
|
---|
[310] | 12 | * This class represents the complete outcome space and is therefore useful if
|
---|
[284] | 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 | /**
|
---|
[310] | 22 | * Creates a test outcome space.
|
---|
[284] | 23 | */
|
---|
[310] | 24 | public OutcomeSpace()
|
---|
[284] | 25 | {
|
---|
| 26 | generateAllBids();
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | /**
|
---|
| 30 | * Generates all the possible bids in the domain
|
---|
| 31 | *
|
---|
| 32 | * @param utilSpace
|
---|
| 33 | */
|
---|
[302] | 34 | private void generateAllBids()
|
---|
[284] | 35 | {
|
---|
[310] | 36 | for (int g = 1; g < 10; g++)
|
---|
| 37 | allBids.add(Offer.getSampleOffer("Green", g).getBundle());
|
---|
[284] | 38 | }
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
[302] | 41 | * gets a {@link Bundle} which is closest to the given utility
|
---|
[284] | 42 | *
|
---|
[302] | 43 | * @param utility to which the found bid must be closest.
|
---|
[284] | 44 | */
|
---|
[302] | 45 | public Bundle getBidNearUtility(double utility, UtilityFunction u, Agent a)
|
---|
[284] | 46 | {
|
---|
[300] | 47 | int index = getIndexOfBidNearUtility(utility, u);
|
---|
| 48 | Bundle bundle = allBids.get(index);
|
---|
[302] | 49 | System.out.println(a + ": the bid closest to target util " + utility + " is " + bundle + " (#" + index + ") with util " + u.getUtility(bundle));
|
---|
[300] | 50 | return bundle;
|
---|
[284] | 51 | }
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
| 54 | * @return best bid in the domain.
|
---|
| 55 | */
|
---|
[302] | 56 | private Bundle getMaxBidPossible(UtilityFunction u)
|
---|
[284] | 57 | {
|
---|
| 58 | Bundle maxBid = allBids.get(0);
|
---|
| 59 | for (Bundle bid : allBids) {
|
---|
| 60 | if (u.getUtility(bid) > u.getUtility(maxBid)) {
|
---|
| 61 | maxBid = bid;
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | return maxBid;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * @return worst bid in the domain.
|
---|
| 69 | */
|
---|
[302] | 70 | private Bundle getMinBidPossible(UtilityFunction u) {
|
---|
[284] | 71 | Bundle minBid = allBids.get(0);
|
---|
| 72 | for (Bundle bid : allBids) {
|
---|
| 73 | if (u.getUtility(bid) < u.getUtility(minBid)) {
|
---|
| 74 | minBid = bid;
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | return minBid;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * @param utility
|
---|
| 82 | * to which the found bid must be closest.
|
---|
| 83 | * @return index of the bid with the utility closest to the given utilty.
|
---|
| 84 | */
|
---|
[302] | 85 | private int getIndexOfBidNearUtility(double utility, UtilityFunction u)
|
---|
[284] | 86 | {
|
---|
| 87 | double closesDistance = 1;
|
---|
| 88 | int best = 0;
|
---|
| 89 | for (int i = 0; i < allBids.size(); i++) {
|
---|
| 90 | if (Math.abs(u.getUtility(allBids.get(i)) - utility) < closesDistance) {
|
---|
| 91 | closesDistance = Math.abs(u.getUtility(allBids.get(i)) - utility);
|
---|
| 92 | best = i;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | return best;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | @Override
|
---|
| 99 | public String toString() {
|
---|
| 100 | String all = "";
|
---|
| 101 | for (Bundle b : allBids) {
|
---|
| 102 | all += b.toString() + "\n,";
|
---|
| 103 | }
|
---|
| 104 | return "OutcomeSpace[" + all + "]";
|
---|
| 105 | }
|
---|
| 106 | } |
---|