source: src/main/java/bargainingchips/analysis/BundleUtilityPoint.java@ 339

Last change on this file since 339 was 339, checked in by Faria Nassiri Mofakham, 5 years ago

BargainingChips creates bob calling BargainingBuyer. OutcomeSpace is Iterable<Bundle>, also getAllBids, getRandom, getRandom(r), getAverageUtility(u), size(), checkReadyForNegotiation, getName(), getUtility(u, b), getReservationValue(u), and getDiscountFactor added. An update on Bid getSampleBid type. An update on Agent. A history' package of BidEvent, BidEventHistory, BidEventHisoryKeeper, BidEventSortedUtility, and BidEventStrictSortedUtility added. HistoryAgent added. An analysis' package of BundleUtilityPoint, BundleUtilitySpace, and ParetoFrontier added. BargainingAgent added. TitForTatNegotiationAgent. Now, Buyer extended to BargainingBuyer which is created using Boulware or TitForTatNegotiationAgent using one among 9 utility functions. Name of strategy added toDescription in BoulwareAgent. A few small updates on several utility function classes.

File size: 4.5 KB
Line 
1package bargainingchips.analysis;
2
3import java.io.Serializable;
4import java.util.Arrays;
5
6import bargainingchips.Bundle;
7
8/**
9 * This class adapts {@link genius.core.analysis.BidPoint}.
10 * A BundlePoint is a tuple which contains the utility of a particular bundle for each
11 * agent.
12 *
13 */
14public class BundleUtilityPoint implements Serializable {
15
16 /**
17 *
18 */
19 private static final long serialVersionUID = 1L;
20 /**
21 *
22 */
23 /** Bundle of which the utilities are shown. */
24 private Bundle bundle;
25 /** Array holding the utility of the bundle for each agent. */
26 private Double[] utility;
27
28 /**
29 * Create a BundlePoint by given the bundle and the tuple of utilities for that
30 * bundle.
31 *
32 * @param bundle
33 * from which the utilities are stored.
34 * @param utility
35 * tuple of utilities of the bundle.
36 */
37 public BundleUtilityPoint(Bundle bundle, Double... utility) {
38 this.bundle = bundle;
39 this.utility = utility.clone();
40 }
41
42 /**
43 * @return string representation of the object.
44 */
45 @Override
46 public String toString() {
47 String result = "BundlePoint [" + bundle + ",";
48 for (int i = 0; i < utility.length; i++) {
49 result += "util" + i + "[" + utility[i] + "],";
50 }
51 return result;
52 }
53
54 /**
55 * @param obj
56 * object to which this object is compared.
57 * @return true if this object is equal to the given object.
58 */
59 @Override
60 public boolean equals(Object obj) {
61 if (this == obj)
62 return true;
63 if (obj == null)
64 return false;
65 if (getClass() != obj.getClass())
66 return false;
67 BundleUtilityPoint other = (BundleUtilityPoint) obj;
68 if (bundle == null) {
69 if (other.bundle != null)
70 return false;
71 } else if (!bundle.equals(other.bundle))
72 return false;
73 if (this.utility.length != other.utility.length) {
74 return false;
75 }
76
77 for (int i = 0; i < this.utility.length; i++) {
78 if (!this.utility[i].equals(other.utility[i])) {
79 return false;
80 }
81 }
82 return true;
83 }
84
85 /**
86 * @return hashcode of this object.
87 */
88 @Override
89 public int hashCode() {
90 final int prime = 31;
91 int result = 1;
92 result = prime * result + ((bundle == null) ? 0 : bundle.hashCode());
93 result = prime * result + Arrays.hashCode(utility);
94 return result;
95 }
96
97 /**
98 * Bundle from which the utilities are represented. This bundle may be null to
99 * save memory.
100 *
101 * @return bundle which utilities are represented.
102 */
103 public Bundle getBundle() {
104 return bundle;
105 }
106
107 /**
108 * Returns the utility of the bundle for the i'th agent (agent A = 0, etc.).
109 *
110 * @param index
111 * of the agent of which the utility should be returned.
112 * @return utility of the bundle for the i'th agent.
113 */
114 public Double getUtility(int index) {
115 return utility[index];
116 }
117
118 /**
119 * Returns the utility of the bundle for agent A.
120 *
121 * @return utility for agent A.
122 */
123 public Double getUtilityA() {
124 return utility[0];
125 }
126
127 /**
128 * Returns the utility of the bundle for agent B.
129 *
130 * @return utility for agent B.
131 */
132 public Double getUtilityB() {
133 return utility[1];
134 }
135
136 /**
137 * Returns true if this BundlePoint is strictly dominated by another BundlePoint.
138 * A BundlePoint is dominated when the utility of the other bundle for at least
139 * one agent is higher and equal for the other agent.
140 *
141 * @param other
142 * BundlePoint.
143 * @return true if "other" dominates "this".
144 */
145 public boolean isStrictlyDominatedBy(BundleUtilityPoint other) {
146 if (this == other) {
147 return false;
148 }
149
150 boolean atleastOneBetter = false;
151
152 for (int i = 0; i < utility.length; i++) {
153 if (other.utility[i] >= this.utility[i]) {
154 if (other.utility[i] > this.utility[i]) {
155 atleastOneBetter = true;
156 }
157 } else {
158 return false;
159 }
160 }
161 return atleastOneBetter;
162 }
163
164 /**
165 * Returns the distance between this BundlePoint and another BundlePoint. sqrt((Tx
166 * - Ox) ^ 2 + (Ty - Oy) ^ 2 + ...).
167 *
168 * @param other
169 * bundlepoint to which the distance is calculated.
170 * @return distance to the given bundlepoint.
171 */
172 public double getDistance(BundleUtilityPoint other) {
173 double sum = 0;
174 for (int i = 0; i < utility.length; i++) {
175 sum += Math.pow(this.utility[i] - other.utility[i], 2);
176 }
177 return Math.sqrt(sum);
178 }
179
180 /**
181 *
182 * @return sum of utilities of all parties for this bundle
183 */
184 public double getSocialWelfare() {
185 double sum = 0;
186 for (double util : utility) {
187 sum += util;
188 }
189 return sum;
190 }
191}
Note: See TracBrowser for help on using the repository browser.