1 | package geniusweb.profile.utilityspace;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertFalse;
|
---|
5 | import static org.junit.Assert.assertTrue;
|
---|
6 | import static org.mockito.Mockito.mock;
|
---|
7 | import static org.mockito.Mockito.when;
|
---|
8 |
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.math.BigDecimal;
|
---|
11 | import java.nio.charset.StandardCharsets;
|
---|
12 | import java.nio.file.Files;
|
---|
13 | import java.nio.file.Paths;
|
---|
14 | import java.util.Arrays;
|
---|
15 | import java.util.Collections;
|
---|
16 | import java.util.HashMap;
|
---|
17 | import java.util.HashSet;
|
---|
18 | import java.util.List;
|
---|
19 | import java.util.Map;
|
---|
20 |
|
---|
21 | import org.junit.Test;
|
---|
22 |
|
---|
23 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
24 |
|
---|
25 | import geniusweb.issuevalue.Bid;
|
---|
26 | import geniusweb.issuevalue.DiscreteValue;
|
---|
27 | import geniusweb.issuevalue.DiscreteValueSet;
|
---|
28 | import geniusweb.issuevalue.Domain;
|
---|
29 | import geniusweb.issuevalue.NumberValue;
|
---|
30 | import geniusweb.issuevalue.Value;
|
---|
31 | import geniusweb.issuevalue.ValueSet;
|
---|
32 | import tudelft.utilities.junit.GeneralTests;
|
---|
33 |
|
---|
34 | public class LinearAdditiveTest
|
---|
35 | extends GeneralTests<LinearAdditiveUtilitySpace> {
|
---|
36 | private static final String NAME = "test";
|
---|
37 | private static LinearAdditiveUtilitySpace utilspace1, utilspace1a,
|
---|
38 | utilspace2a, utilspace2b, utilspace3;
|
---|
39 | private static Bid reservationBid;
|
---|
40 | private static Bid reservationBid2;
|
---|
41 | private static final BigDecimal I1V1UTIL = new BigDecimal("0.3");
|
---|
42 | private static final BigDecimal I1V2UTIL = new BigDecimal("0.2");
|
---|
43 | private static final BigDecimal I2V1UTIL = new BigDecimal("0.6");
|
---|
44 | private static final BigDecimal I2V2UTIL = new BigDecimal("0.8");
|
---|
45 | private static final BigDecimal WEIGHT1 = new BigDecimal("0.4");
|
---|
46 | private static final BigDecimal WEIGHT2 = new BigDecimal("0.6");
|
---|
47 | private static final Map<String, ValueSetUtilities> utils;
|
---|
48 | private static final Map<String, BigDecimal> weights;
|
---|
49 | private static final String ISS1 = "issue1";
|
---|
50 | private static final String ISS2 = "issue2";
|
---|
51 | private static final String ISS3 = "issue3";
|
---|
52 | private static final DiscreteValue i1v1 = new DiscreteValue("issue1value1");
|
---|
53 | private static final DiscreteValue i1v2 = new DiscreteValue("issue1value2");
|
---|
54 | private static final DiscreteValue i2v1 = new DiscreteValue("issue2value1");
|
---|
55 | private static final DiscreteValue i2v2 = new DiscreteValue("issue2value2");
|
---|
56 | private static final DiscreteValue i3v1 = new DiscreteValue("issue3value1");
|
---|
57 | private static final DiscreteValue i3v2 = new DiscreteValue("issue3value2");
|
---|
58 |
|
---|
59 | private static final Domain smalldomain, domain;
|
---|
60 |
|
---|
61 | private static final ValueSetUtilities value1Utils;
|
---|
62 | private static final ObjectMapper jackson = new ObjectMapper();
|
---|
63 |
|
---|
64 | static {
|
---|
65 | BigDecimal WEIGHT1a = new BigDecimal("0.3");
|
---|
66 | BigDecimal WEIGHT2a = new BigDecimal("0.7");
|
---|
67 |
|
---|
68 | Map<String, ValueSet> values = new HashMap<>();
|
---|
69 | values.put(ISS1, new DiscreteValueSet(Arrays.asList(i1v1, i1v2)));
|
---|
70 | smalldomain = new Domain(NAME, values);
|
---|
71 | values.put(ISS2, new DiscreteValueSet(Arrays.asList(i2v1, i2v2)));
|
---|
72 | domain = new Domain(NAME, values);
|
---|
73 |
|
---|
74 | // build utilspace for string and equals testing.
|
---|
75 | utils = new HashMap<>();
|
---|
76 | Map<DiscreteValue, BigDecimal> valueUtils = new HashMap<>();
|
---|
77 | valueUtils.put(i1v1, I1V1UTIL);
|
---|
78 | valueUtils.put(i1v2, I1V2UTIL);
|
---|
79 | value1Utils = new DiscreteValueSetUtilities(valueUtils);
|
---|
80 | utils.put(ISS1, value1Utils);
|
---|
81 | valueUtils = new HashMap<>();
|
---|
82 | valueUtils.put(i2v1, I2V1UTIL);
|
---|
83 | valueUtils.put(i2v2, I2V2UTIL);
|
---|
84 | ValueSetUtilities value2Utils = new DiscreteValueSetUtilities(
|
---|
85 | valueUtils);
|
---|
86 | utils.put(ISS2, value2Utils);
|
---|
87 |
|
---|
88 | // build utilspaceb, mix up the utilities a bit.
|
---|
89 | Map<String, ValueSetUtilities> utilsb = new HashMap<>();
|
---|
90 | valueUtils = new HashMap<>();
|
---|
91 | valueUtils.put(i1v1, I2V1UTIL);
|
---|
92 | valueUtils.put(i1v2, I2V2UTIL);
|
---|
93 | DiscreteValueSetUtilities value1UtilsB = new DiscreteValueSetUtilities(
|
---|
94 | valueUtils);
|
---|
95 | utilsb.put(ISS1, value1UtilsB);
|
---|
96 | valueUtils = new HashMap<>();
|
---|
97 | valueUtils.put(i2v1, I1V1UTIL);
|
---|
98 | valueUtils.put(i2v2, I1V2UTIL);
|
---|
99 | ValueSetUtilities value2UtilsB = new DiscreteValueSetUtilities(
|
---|
100 | valueUtils);
|
---|
101 | utilsb.put(ISS2, value2UtilsB);
|
---|
102 |
|
---|
103 | // weight map
|
---|
104 | weights = new HashMap<>();
|
---|
105 | weights.put(ISS1, WEIGHT1);
|
---|
106 | weights.put(ISS2, WEIGHT2);
|
---|
107 |
|
---|
108 | // weight map 2
|
---|
109 | Map<String, BigDecimal> weightsb = new HashMap<>();
|
---|
110 | weightsb.put(ISS1, WEIGHT1a);
|
---|
111 | weightsb.put(ISS2, WEIGHT2a);
|
---|
112 |
|
---|
113 | // bid with lowest utility
|
---|
114 | Map<String, Value> issuevalues = new HashMap<>();
|
---|
115 | issuevalues.put(ISS1, i1v2);
|
---|
116 | issuevalues.put(ISS2, i2v1);
|
---|
117 | reservationBid = new Bid(issuevalues);
|
---|
118 |
|
---|
119 | issuevalues = new HashMap<>();
|
---|
120 | issuevalues.put(ISS1, i1v1);
|
---|
121 | issuevalues.put(ISS2, i2v1);
|
---|
122 | reservationBid2 = new Bid(issuevalues);
|
---|
123 |
|
---|
124 | // make the utilspaces
|
---|
125 | utilspace1 = new LinearAdditiveUtilitySpace(domain, NAME, utils,
|
---|
126 | weights, reservationBid);
|
---|
127 | utilspace1a = new LinearAdditiveUtilitySpace(domain, NAME, utils,
|
---|
128 | weights, reservationBid);
|
---|
129 |
|
---|
130 | utilspace2a = new LinearAdditiveUtilitySpace(domain, NAME, utils,
|
---|
131 | weightsb, reservationBid);
|
---|
132 | utilspace2b = new LinearAdditiveUtilitySpace(domain, NAME, utilsb,
|
---|
133 | weights, reservationBid);
|
---|
134 | utilspace3 = new LinearAdditiveUtilitySpace(domain, NAME, utilsb,
|
---|
135 | weights, reservationBid2);
|
---|
136 |
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Override
|
---|
140 | public List<List<LinearAdditiveUtilitySpace>> getGeneralTestData() {
|
---|
141 | return Arrays.asList(Arrays.asList(utilspace1, utilspace1a),
|
---|
142 | Arrays.asList(utilspace2a), Arrays.asList(utilspace2b),
|
---|
143 | Arrays.asList(utilspace3));
|
---|
144 | }
|
---|
145 |
|
---|
146 | @Override
|
---|
147 | public List<String> getGeneralTestStrings() {
|
---|
148 | return Arrays.asList(
|
---|
149 | "LinearAdditive\\[\\{issue2=DiscreteValueSetUtilities\\{\"issue2value2\"=0.8, \"issue2value1\"=0.6\\}, issue1=DiscreteValueSetUtilities\\{\"issue1value1\"=0.3, \"issue1value2\"=0.2\\}\\},\\{issue2=0.6, issue1=0.4\\},Bid\\{issue2=\"issue2value1\", issue1=\"issue1value2\"\\}\\]",
|
---|
150 | "LinearAdditive\\[\\{issue2=DiscreteValueSetUtilities\\{\"issue2value2\"=0.8, \"issue2value1\"=0.6\\}, issue1=DiscreteValueSetUtilities\\{\"issue1value1\"=0.3, \"issue1value2\"=0.2\\}\\},\\{issue2=0.7, issue1=0.3\\},Bid\\{issue2=\"issue2value1\", issue1=\"issue1value2\"\\}\\]",
|
---|
151 | "LinearAdditive\\[\\{issue2=DiscreteValueSetUtilities\\{\"issue2value2\"=0.2, \"issue2value1\"=0.3\\}, issue1=DiscreteValueSetUtilities\\{\"issue1value1\"=0.6, \"issue1value2\"=0.8\\}\\},\\{issue2=0.6, issue1=0.4\\},Bid\\{issue2=\"issue2value1\", issue1=\"issue1value2\"\\}\\]",
|
---|
152 | "LinearAdditive\\[\\{issue2=DiscreteValueSetUtilities\\{\"issue2value2\"=0.2, \"issue2value1\"=0.3\\}, issue1=DiscreteValueSetUtilities\\{\"issue1value1\"=0.6, \"issue1value2\"=0.8\\}\\},\\{issue2=0.6, issue1=0.4\\},Bid\\{issue2=\"issue2value1\", issue1=\"issue1value1\"\\}\\]");
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Test(expected = NullPointerException.class)
|
---|
156 | public void constructorNullIssuesTest() {
|
---|
157 | new LinearAdditiveUtilitySpace(domain, NAME, null, null,
|
---|
158 | reservationBid);
|
---|
159 | }
|
---|
160 |
|
---|
161 | @Test(expected = NullPointerException.class)
|
---|
162 | public void constructorNullDomainTest() {
|
---|
163 | new LinearAdditiveUtilitySpace(null, NAME, utils, weights,
|
---|
164 | reservationBid);
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Empty profile is not allowed since the weights then don't sum up to 1
|
---|
169 | */
|
---|
170 | @Test(expected = IllegalArgumentException.class)
|
---|
171 | public void constructorEmptyTest() {
|
---|
172 | new LinearAdditiveUtilitySpace(domain, NAME, new HashMap<>(), weights,
|
---|
173 | reservationBid);
|
---|
174 | }
|
---|
175 |
|
---|
176 | @Test
|
---|
177 | public void constructorOneIssueTest() {
|
---|
178 | Map<String, ValueSetUtilities> utilset = new HashMap<>();
|
---|
179 | Map<String, BigDecimal> weightset = new HashMap<>();
|
---|
180 | utilset.put(ISS1, value1Utils);
|
---|
181 | weightset.put(ISS1, BigDecimal.ONE);
|
---|
182 | new LinearAdditiveUtilitySpace(smalldomain, NAME, utilset, weightset,
|
---|
183 | null);
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Empty profile is not allowed since the weights then don't sum up to 1
|
---|
188 | */
|
---|
189 | @Test(expected = IllegalArgumentException.class)
|
---|
190 | public void constructoroneIssueTestWrongWeight() {
|
---|
191 | Map<String, ValueSetUtilities> utilset = new HashMap<>();
|
---|
192 | Map<String, BigDecimal> weightset = new HashMap<>();
|
---|
193 | utilset.put(ISS1, value1Utils);
|
---|
194 | weightset.put(ISS1, WEIGHT1);
|
---|
195 | new LinearAdditiveUtilitySpace(smalldomain, NAME, utilset, weightset,
|
---|
196 | reservationBid);
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Try creating a domain and check isFitting
|
---|
201 | */
|
---|
202 | @Test
|
---|
203 | public void checkCoversDomain() {
|
---|
204 | Map<String, ValueSetUtilities> utilset = new HashMap<>();
|
---|
205 | Map<String, BigDecimal> weightset = new HashMap<>();
|
---|
206 |
|
---|
207 | utilset.put(ISS1, value1Utils);
|
---|
208 | weightset.put(ISS1, BigDecimal.ONE);
|
---|
209 | LinearAdditiveUtilitySpace space = new LinearAdditiveUtilitySpace(
|
---|
210 | smalldomain, NAME, utilset, weightset, null);
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Try creating a domain and check isFitting but there is more issues in our
|
---|
215 | * map than in the actual domain
|
---|
216 | */
|
---|
217 | @Test(expected = IllegalArgumentException.class)
|
---|
218 | public void checkCoversWrongDomain() {
|
---|
219 | Map<String, ValueSetUtilities> utilset = new HashMap<>();
|
---|
220 | Map<String, BigDecimal> weightset = new HashMap<>();
|
---|
221 |
|
---|
222 | utilset.put(ISS1, value1Utils);
|
---|
223 | weightset.put(ISS1, BigDecimal.ONE);
|
---|
224 | LinearAdditiveUtilitySpace space = new LinearAdditiveUtilitySpace(
|
---|
225 | domain, NAME, utilset, weightset, reservationBid);
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Empty profile is not allowed since the weights then don't sum up to 1
|
---|
230 | */
|
---|
231 | @Test
|
---|
232 | public void utilityTest() {
|
---|
233 | LinearAdditiveUtilitySpace space = new LinearAdditiveUtilitySpace(
|
---|
234 | domain, NAME, utils, weights, reservationBid);
|
---|
235 |
|
---|
236 | Bid bid = mock(Bid.class);
|
---|
237 | HashSet<String> issues = new HashSet<String>();
|
---|
238 | issues.add(ISS1);
|
---|
239 | when(bid.getIssues()).thenReturn(issues);
|
---|
240 | when(bid.getValue(ISS1)).thenReturn(i1v1);
|
---|
241 | assertEquals(0,
|
---|
242 | WEIGHT1.multiply(I1V1UTIL).compareTo(space.getUtility(bid)));
|
---|
243 | }
|
---|
244 |
|
---|
245 | @Test
|
---|
246 | public void preferredTest() {
|
---|
247 | Map<String, Value> issuevalues = new HashMap<>();
|
---|
248 | issuevalues.put(ISS1, i1v1);
|
---|
249 | issuevalues.put(ISS2, i2v2);
|
---|
250 | Bid bid1 = new Bid(issuevalues);
|
---|
251 | Map<String, Value> issuevalues1 = new HashMap<>();
|
---|
252 | issuevalues1.put(ISS1, i1v2);
|
---|
253 | issuevalues1.put(ISS2, i2v1);
|
---|
254 | Bid bid2 = new Bid(issuevalues1);
|
---|
255 | assertTrue(utilspace1.isPreferredOrEqual(bid1, bid2));
|
---|
256 | assertTrue(utilspace1.isPreferredOrEqual(bid1, bid1));
|
---|
257 | assertTrue(utilspace1.isPreferredOrEqual(bid2, bid2));
|
---|
258 | assertFalse(utilspace1.isPreferredOrEqual(bid2, bid1));
|
---|
259 | assertFalse(utilspace1
|
---|
260 | .isPreferredOrEqual(new Bid(Collections.emptyMap()), bid1));
|
---|
261 | }
|
---|
262 |
|
---|
263 | @Test
|
---|
264 | public void partialBidUtilityTest() {
|
---|
265 | LinearAdditiveUtilitySpace space = new LinearAdditiveUtilitySpace(
|
---|
266 | domain, NAME, utils, weights, reservationBid);
|
---|
267 | Map<String, Value> issuevalues = new HashMap<>();
|
---|
268 | issuevalues.put(ISS1, i1v1);
|
---|
269 | Bid bid = new Bid(issuevalues);
|
---|
270 | assertEquals(0,
|
---|
271 | WEIGHT1.multiply(I1V1UTIL).compareTo(space.getUtility(bid)));
|
---|
272 | }
|
---|
273 |
|
---|
274 | @Test
|
---|
275 | public void loadFullWithJson() throws IOException {
|
---|
276 | String profile = new String(
|
---|
277 | Files.readAllBytes(Paths.get("src/test/resources/party1.json")),
|
---|
278 | StandardCharsets.UTF_8);
|
---|
279 | UtilitySpace space = jackson.readValue(profile, UtilitySpace.class);
|
---|
280 |
|
---|
281 | }
|
---|
282 |
|
---|
283 | @Test
|
---|
284 | public void loadFullWithJsonNumber() throws IOException {
|
---|
285 | String profile = new String(
|
---|
286 | Files.readAllBytes(
|
---|
287 | Paths.get("src/test/resources/japantrip1.json")),
|
---|
288 | StandardCharsets.UTF_8);
|
---|
289 | UtilitySpace space = jackson.readValue(profile, UtilitySpace.class);
|
---|
290 |
|
---|
291 | }
|
---|
292 |
|
---|
293 | @Test(expected = IllegalArgumentException.class)
|
---|
294 | public void testResBidWithNonsenseIssue() {
|
---|
295 | Bid resBid = new Bid("nonsense", i1v1);
|
---|
296 | new LinearAdditiveUtilitySpace(domain, NAME, utils, weights, resBid);
|
---|
297 | }
|
---|
298 |
|
---|
299 | @Test(expected = IllegalArgumentException.class)
|
---|
300 | public void testResBidWithWrongValueType() {
|
---|
301 | Bid resBid = new Bid(ISS1, new NumberValue(BigDecimal.ZERO));
|
---|
302 | new LinearAdditiveUtilitySpace(domain, NAME, utils, weights, resBid);
|
---|
303 | }
|
---|
304 |
|
---|
305 | @Test(expected = IllegalArgumentException.class)
|
---|
306 | public void testResBidWithNonsenseValue() {
|
---|
307 | Bid resBid = new Bid(ISS1, new DiscreteValue("nonsense"));
|
---|
308 | new LinearAdditiveUtilitySpace(domain, NAME, utils, weights, resBid);
|
---|
309 | }
|
---|
310 |
|
---|
311 | }
|
---|