1 | package bargainingchips.analysis;
|
---|
2 |
|
---|
3 | import java.io.Serializable;
|
---|
4 | import java.util.Arrays;
|
---|
5 |
|
---|
6 | import 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 | */
|
---|
14 | public 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 | } |
---|