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