1 | package onetomany.bargainingchipsgame;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import java.util.ArrayList;
|
---|
6 |
|
---|
7 | import onetomany.bargainingchipsgame.interactions.Offer;
|
---|
8 | import onetomany.bargainingchipsgame.players.Agent;
|
---|
9 | import onetomany.bargainingchipsgame.players.utilityfunction.UtilityFunction;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * This class represents 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 a test outcome space.
|
---|
23 | */
|
---|
24 | public OutcomeSpace()
|
---|
25 | {
|
---|
26 | generateAllBids();
|
---|
27 | }
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Generates all the possible bids in the domain
|
---|
31 | *
|
---|
32 | * @param utilSpace
|
---|
33 | */
|
---|
34 | private void generateAllBids()
|
---|
35 | {
|
---|
36 | for (int g = 1; g < 10; g++)
|
---|
37 | allBids.add(Offer.getSampleOffer("Green", g).getBundle());
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * gets a {@link Bundle} which is closest to the given utility
|
---|
42 | *
|
---|
43 | * @param utility to which the found bid must be closest.
|
---|
44 | */
|
---|
45 | public Bundle getBidNearUtility(double utility, UtilityFunction u, Agent a)
|
---|
46 | {
|
---|
47 | int index = getIndexOfBidNearUtility(utility, u);
|
---|
48 | Bundle bundle = allBids.get(index);
|
---|
49 | System.out.println(a + ": the bid closest to target util " + utility + " is " + bundle + " (#" + index + ") with util " + u.getUtility(bundle));
|
---|
50 | return bundle;
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * @return best bid in the domain.
|
---|
55 | */
|
---|
56 | private Bundle getMaxBidPossible(UtilityFunction u)
|
---|
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 | */
|
---|
70 | private Bundle getMinBidPossible(UtilityFunction u) {
|
---|
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 | */
|
---|
85 | private int getIndexOfBidNearUtility(double utility, UtilityFunction u)
|
---|
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 | } |
---|