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