source: src/main/java/onetomany/bargainingchipsgame/OutcomeSpace.java@ 308

Last change on this file since 308 was 302, checked in by Tim Baarslag, 5 years ago

UF_CloseToQuantity

Accept(Offer agreement)

File size: 3.2 KB
Line 
1package onetomany.bargainingchipsgame;
2
3import java.util.List;
4
5import java.util.ArrayList;
6
7import onetomany.bargainingchipsgame.interactions.Offer;
8import onetomany.bargainingchipsgame.players.Agent;
9import 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 */
16public 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 */
27 public OutcomeSpace(Domain domain)
28 {
29 generateAllBids();
30 }
31
32 /**
33 * Generates all the possible bids in the domain
34 *
35 * @param utilSpace
36 */
37 private void generateAllBids()
38 {
39 allBids.add(Offer.getSampleOffer("Green", 1).getBundle());
40 allBids.add(Offer.getSampleOffer("Green", 2).getBundle());
41 allBids.add(Offer.getSampleOffer("Green", 3).getBundle());
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());
47 }
48
49 /**
50 * gets a {@link Bundle} which is closest to the given utility
51 *
52 * @param utility to which the found bid must be closest.
53 */
54 public Bundle getBidNearUtility(double utility, UtilityFunction u, Agent a)
55 {
56 int index = getIndexOfBidNearUtility(utility, u);
57 Bundle bundle = allBids.get(index);
58 System.out.println(a + ": the bid closest to target util " + utility + " is " + bundle + " (#" + index + ") with util " + u.getUtility(bundle));
59 return bundle;
60 }
61
62 /**
63 * @return best bid in the domain.
64 */
65 private Bundle getMaxBidPossible(UtilityFunction u)
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 */
79 private Bundle getMinBidPossible(UtilityFunction u) {
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 */
94 private int getIndexOfBidNearUtility(double utility, UtilityFunction u)
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}
Note: See TracBrowser for help on using the repository browser.