source: voting/src/test/java/geniusweb/voting/CollectedVotesWithValueTest.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

File size: 13.2 KB
Line 
1package geniusweb.voting;
2
3import static org.junit.Assert.assertArrayEquals;
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertNull;
6import static org.mockito.Mockito.mock;
7import static org.mockito.Mockito.when;
8
9import java.util.Arrays;
10import java.util.Collections;
11import java.util.HashMap;
12import java.util.HashSet;
13import java.util.LinkedHashMap;
14import java.util.List;
15import java.util.Map;
16import java.util.Set;
17
18import org.junit.Before;
19import org.junit.Test;
20
21import geniusweb.actions.PartyId;
22import geniusweb.actions.VoteWithValue;
23import geniusweb.actions.VotesWithValue;
24import geniusweb.issuevalue.Bid;
25import tudelft.utilities.immutablelist.Tuple;
26import tudelft.utilities.junit.GeneralTests;
27
28public class CollectedVotesWithValueTest
29 extends GeneralTests<CollectedVotesWithValue> {
30
31 private final Bid bidA = mock(Bid.class), bidB = mock(Bid.class);
32 private final Bid a = mock(Bid.class), b = mock(Bid.class),
33 c = mock(Bid.class), d = mock(Bid.class);
34 private final PartyId party1 = new PartyId("party1");
35 private final PartyId party2 = new PartyId("party2");
36 private final PartyId party3 = new PartyId("party3");
37 private final PartyId party4 = new PartyId("party4");
38
39 private final PartyId partyP = new PartyId("partyP");
40 private final PartyId partyQ = new PartyId("partyQ");
41 private final PartyId partyR = new PartyId("partyR");
42 private final PartyId partyS = new PartyId("partyS");
43
44 private final VoteWithValue votePA2 = new VoteWithValue(partyP, bidA, 2, 9,
45 100);
46 private final VoteWithValue voteQA2 = new VoteWithValue(partyQ, bidA, 2, 9,
47 50);
48 private final VoteWithValue voteQB3 = new VoteWithValue(partyQ, bidB, 3, 9,
49 50);
50 private final VoteWithValue voteRA4 = new VoteWithValue(partyR, bidA, 4, 9,
51 50);
52
53 private Map<PartyId, Integer> allpowers = new HashMap<>();
54
55 private CollectedVotesWithValue cv1 = new CollectedVotesWithValue(
56 Collections.emptyMap(), Collections.emptyMap());
57 private CollectedVotesWithValue cv1a = new CollectedVotesWithValue(
58 Collections.emptyMap(), Collections.emptyMap());
59
60 private VotesWithValue votes = new VotesWithValue(partyP,
61 Collections.singleton(votePA2));
62 private CollectedVotesWithValue cv2 = new CollectedVotesWithValue(
63 Collections.singletonMap(partyP, votes),
64 Collections.singletonMap(partyP, 1));
65
66 @Override
67 public List<List<CollectedVotesWithValue>> getGeneralTestData() {
68 return Arrays.asList(Arrays.asList(cv1, cv1a), Arrays.asList(cv2));
69 }
70
71 @Override
72 public List<String> getGeneralTestStrings() {
73 return Arrays.asList("CollectedVotesWithValue.\\{\\}.*\\{\\}.*",
74 "CollectedVotesWithValue.\\{partyP=VotesWithValue.*partyP,.VoteWithValue.partyP,bidA.*,2.*\\}.*\\{partyP=1\\}.*");
75 }
76
77 @Before
78 public void before() {
79 allpowers.put(partyP, 1);
80 allpowers.put(partyQ, 1);
81 allpowers.put(partyR, 1);
82 allpowers.put(partyS, 1);
83
84 when(bidA.toString()).thenReturn("bidA");
85 when(bidB.toString()).thenReturn("bidB");
86
87 }
88
89 @Test
90 public void testGetAllBids() {
91 Map<PartyId, VotesWithValue> allvotes = new HashMap<>();
92 allvotes.put(partyP,
93 new VotesWithValue(partyP, Collections.singleton(votePA2)));
94 allvotes.put(partyQ, new VotesWithValue(partyQ,
95 new HashSet<>(Arrays.asList(voteQA2, voteQB3))));
96
97 Map<Bid, Set<VoteWithValue>> allbids = new CollectedVotesWithValue(
98 allvotes, allpowers).getAllBids();
99 assertEquals(2, allbids.size());
100 assertEquals(new HashSet<>(Arrays.asList(votePA2, voteQA2)),
101 allbids.get(bidA));
102 assertEquals(new HashSet<>(Arrays.asList(voteQB3)), allbids.get(bidB));
103 }
104
105 @Test
106 public void getBestCoalitionTest() {
107 // P votes for A, Q votes 50% on A 50% on B.
108 // Only deal is A, 150%
109 Map<PartyId, VotesWithValue> allvotes = new HashMap<>();
110 allvotes.put(partyP,
111 new VotesWithValue(partyP, Collections.singleton(votePA2)));
112 allvotes.put(partyQ, new VotesWithValue(partyQ,
113 new HashSet<>(Arrays.asList(voteQA2, voteQB3))));
114
115 CollectedVotesWithValue cv = new CollectedVotesWithValue(allvotes,
116 allpowers);
117 Tuple<Bid, Integer> best = cv
118 .getBestCoalition(new HashSet<>(Arrays.asList(partyP, partyQ)));
119 assertEquals(bidA, best.get1());
120 assertEquals((Integer) 150, best.get2());
121 }
122
123 @Test
124 public void getBestCoalitionTest2() {
125 // P votes 40% A 60$ B, Q votes 50% on A 50% on B.
126 // Only deal is B, 110%
127 Map<PartyId, VotesWithValue> allvotes = new HashMap<>();
128 allvotes.put(partyP,
129 new VotesWithValue(partyP,
130 new HashSet<>(Arrays.asList(
131 new VoteWithValue(partyP, bidA, 2, 9, 40),
132 new VoteWithValue(partyP, bidB, 2, 9, 60)))));
133 allvotes.put(partyQ,
134 new VotesWithValue(partyQ,
135 new HashSet<>(Arrays.asList(
136 new VoteWithValue(partyQ, bidA, 2, 9, 50),
137 new VoteWithValue(partyQ, bidB, 2, 9, 50)))));
138
139 CollectedVotesWithValue cv = new CollectedVotesWithValue(allvotes,
140 allpowers);
141 Tuple<Bid, Integer> best = cv
142 .getBestCoalition(new HashSet<>(Arrays.asList(partyP, partyQ)));
143 assertEquals(bidB, best.get1());
144 assertEquals((Integer) 110, best.get2());
145 }
146
147 @Test
148 public void getBestCoalitionTest3() {
149 // P votes 50% A 50$ B, Q votes 50% on A 50% on B.
150 // Both deals A and B are now equivalent so both A and B are good
151 Map<PartyId, VotesWithValue> allvotes = new HashMap<>();
152 allvotes.put(partyP,
153 new VotesWithValue(partyP,
154 new HashSet<>(Arrays.asList(
155 new VoteWithValue(partyP, bidA, 2, 9, 50),
156 new VoteWithValue(partyP, bidB, 2, 9, 50)))));
157 allvotes.put(partyQ,
158 new VotesWithValue(partyQ,
159 new HashSet<>(Arrays.asList(
160 new VoteWithValue(partyQ, bidA, 2, 9, 50),
161 new VoteWithValue(partyQ, bidB, 2, 9, 50)))));
162
163 CollectedVotesWithValue cv = new CollectedVotesWithValue(allvotes,
164 allpowers);
165 Tuple<Bid, Integer> best = cv
166 .getBestCoalition(new HashSet<>(Arrays.asList(partyP, partyQ)));
167 assertEquals((Integer) 100, best.get2());
168 }
169
170 @Test
171 public void getBestCoalitionTest4() {
172 // P votes 60% A 40% B, Q votes 50% on A 50% on B.
173 // R votes 70% A 30% B.
174 // But P does not want to be in a power-3 team.
175 // therefore there is no 3-team coalition.
176 Map<PartyId, VotesWithValue> allvotes = new HashMap<>();
177 allvotes.put(partyP,
178 new VotesWithValue(partyP,
179 new HashSet<>(Arrays.asList(
180 new VoteWithValue(partyP, bidA, 2, 2, 60),
181 new VoteWithValue(partyP, bidB, 2, 2, 40)))));
182 allvotes.put(partyQ,
183 new VotesWithValue(partyQ,
184 new HashSet<>(Arrays.asList(
185 new VoteWithValue(partyQ, bidA, 2, 9, 50),
186 new VoteWithValue(partyQ, bidB, 2, 9, 50)))));
187 allvotes.put(partyR,
188 new VotesWithValue(partyR,
189 new HashSet<>(Arrays.asList(
190 new VoteWithValue(partyR, bidA, 2, 9, 70),
191 new VoteWithValue(partyR, bidB, 2, 9, 30)))));
192
193 CollectedVotesWithValue cv = new CollectedVotesWithValue(allvotes,
194 allpowers);
195 Tuple<Bid, Integer> best = cv.getBestCoalition(
196 new HashSet<>(Arrays.asList(partyP, partyQ, partyR)));
197 assertNull(best);
198 }
199
200 @Test
201 public void getActiveParties() {
202 Map<PartyId, VotesWithValue> allvotes = new HashMap<>();
203 allvotes.put(partyP, new VotesWithValue(partyP, new HashSet<>(
204 Arrays.asList(new VoteWithValue(partyP, bidB, 2, 2, 100)))));
205 allvotes.put(partyQ, new VotesWithValue(partyQ, new HashSet<>(
206 Arrays.asList(new VoteWithValue(partyQ, bidB, 2, 9, 100)))));
207 allvotes.put(partyR, new VotesWithValue(partyR, new HashSet<>(
208 Arrays.asList(new VoteWithValue(partyR, bidB, 2, 9, 100)))));
209
210 CollectedVotesWithValue cv = new CollectedVotesWithValue(allvotes,
211 allpowers);
212
213 List<PartyId> parties = cv.getParties();
214 assertEquals(Collections.emptySet(), cv.getActiveParties(0));
215 assertEquals(new HashSet<>(Arrays.asList(parties.get(0))),
216 cv.getActiveParties(1));
217 assertEquals(new HashSet<>(Arrays.asList(parties.get(1))),
218 cv.getActiveParties(2));
219 assertEquals(new HashSet<>(Arrays.asList(parties.get(2))),
220 cv.getActiveParties(4));
221 assertEquals(
222 new HashSet<>(Arrays.asList(parties.get(2), parties.get(0))),
223 cv.getActiveParties(5));
224 }
225
226 @Test
227 public void getPartiesTest() {
228 Map<PartyId, VotesWithValue> allvotes = new HashMap<>();
229 allvotes.put(partyP, new VotesWithValue(partyP, new HashSet<>(
230 Arrays.asList(new VoteWithValue(partyP, bidB, 2, 2, 100)))));
231 allvotes.put(partyQ, new VotesWithValue(partyQ, new HashSet<>(
232 Arrays.asList(new VoteWithValue(partyQ, bidB, 2, 9, 100)))));
233 allvotes.put(partyR, new VotesWithValue(partyR, new HashSet<>(
234 Arrays.asList(new VoteWithValue(partyR, bidB, 2, 9, 100)))));
235
236 CollectedVotesWithValue cv = new CollectedVotesWithValue(allvotes,
237 allpowers);
238
239 List<PartyId> parties = cv.getParties();
240 assertEquals(
241 new HashSet<>(Arrays.asList(parties.get(2), parties.get(0))),
242 cv.getParties(new int[] { 1, 3 }));
243
244 }
245
246 @Test
247 public void getCoalitionValuesTest() {
248 // P votes 60% A 40% B, Q votes 50% on A 50% on B.
249 // R votes 70% A 30% B.
250 // But P does not want to be in a power-3 team.
251 // therefore there is no 3-team coalition.
252 // the max 2-team coalition is with P anyway: P+R with bid A, 130.
253
254 // use LinkedHashMap to ensure order of parties
255 // remains P,Q,R in the result.
256 Map<PartyId, VotesWithValue> allvotes = new LinkedHashMap<>();
257 allvotes.put(partyP,
258 new VotesWithValue(partyP,
259 new HashSet<>(Arrays.asList(
260 new VoteWithValue(partyP, bidA, 2, 2, 60),
261 new VoteWithValue(partyP, bidB, 2, 2, 40)))));
262 allvotes.put(partyQ,
263 new VotesWithValue(partyQ,
264 new HashSet<>(Arrays.asList(
265 new VoteWithValue(partyQ, bidA, 2, 9, 50),
266 new VoteWithValue(partyQ, bidB, 2, 9, 50)))));
267 allvotes.put(partyR,
268 new VotesWithValue(partyR,
269 new HashSet<>(Arrays.asList(
270 new VoteWithValue(partyR, bidA, 2, 9, 70),
271 new VoteWithValue(partyR, bidB, 2, 9, 30)))));
272
273 CollectedVotesWithValue cv = new CollectedVotesWithValue(allvotes,
274 allpowers);
275 assertArrayEquals(new double[] { 0d, 0d, 0d, 110d, 0d, 130, 120, 0d },
276 cv.getCoalitionValues(), .0001);
277
278 }
279
280 @Test
281 public void getMaxAgreementsTest() {
282 // P votes 60% A 40% B, Q votes 50% on A 50% on B.
283 // R votes 70% A 30% B.
284 // But P does not want to be in a power-3 team.
285 // therefore there is no 3-team coalition.
286 // the max 2-team coalition is with P anyway: P+R with bid A, 130.
287 Map<PartyId, VotesWithValue> allvotes = new HashMap<>();
288 allvotes.put(partyP,
289 new VotesWithValue(partyP,
290 new HashSet<>(Arrays.asList(
291 new VoteWithValue(partyP, bidA, 2, 2, 60),
292 new VoteWithValue(partyP, bidB, 2, 2, 40)))));
293 allvotes.put(partyQ,
294 new VotesWithValue(partyQ,
295 new HashSet<>(Arrays.asList(
296 new VoteWithValue(partyQ, bidA, 2, 9, 50),
297 new VoteWithValue(partyQ, bidB, 2, 9, 50)))));
298 allvotes.put(partyR,
299 new VotesWithValue(partyR,
300 new HashSet<>(Arrays.asList(
301 new VoteWithValue(partyR, bidA, 2, 9, 70),
302 new VoteWithValue(partyR, bidB, 2, 9, 30)))));
303
304 CollectedVotesWithValue cv = new CollectedVotesWithValue(allvotes,
305 allpowers);
306 Map<PartyId, Bid> best = cv.getMaxAgreements();
307
308 assertEquals(new HashSet<>(Arrays.asList(partyP, partyR)),
309 best.keySet());
310 assertEquals(bidA, best.get(partyP));
311 assertEquals(bidA, best.get(partyR));
312 }
313
314 @Test
315 public void getMaxAgreementsTest2() {
316 Map<PartyId, VotesWithValue> allvotes = new HashMap<>();
317 allvotes.put(partyP,
318 new VotesWithValue(partyP,
319 new HashSet<>(Arrays.asList(
320 new VoteWithValue(partyP, bidA, 2, 2, 30),
321 new VoteWithValue(partyP, bidB, 2, 3, 70)))));
322 allvotes.put(partyQ,
323 new VotesWithValue(partyQ,
324 new HashSet<>(Arrays.asList(
325 new VoteWithValue(partyQ, bidA, 2, 9, 30),
326 new VoteWithValue(partyQ, bidB, 2, 9, 70)))));
327 allvotes.put(partyR,
328 new VotesWithValue(partyR,
329 new HashSet<>(Arrays.asList(
330 new VoteWithValue(partyR, bidA, 2, 9, 30),
331 new VoteWithValue(partyR, bidB, 2, 9, 70)))));
332
333 allvotes.put(partyS, new VotesWithValue(partyS, new HashSet<>(
334 Arrays.asList(new VoteWithValue(partyS, bidA, 2, 9, 100)))));
335
336 CollectedVotesWithValue cv = new CollectedVotesWithValue(allvotes,
337 allpowers);
338 Map<PartyId, Bid> best = cv.getMaxAgreements();
339 assertEquals(
340 new HashSet<>(Arrays.asList(partyP, partyQ, partyR, partyS)),
341 best.keySet());
342 assertEquals(bidB, best.get(partyP));
343 assertEquals(bidB, best.get(partyQ));
344 assertEquals(bidA, best.get(partyR));
345 assertEquals(bidA, best.get(partyS));
346 }
347
348 @Test
349 public void getMaxAgreementsTest3() {
350 // all vote on bid A, but partyP wants no more than 2 in the coalition.
351 // this deal can still be reached, bu placing partyP,Q in one coalition
352 // and R and S in another coalition.
353 Map<PartyId, VotesWithValue> allvotes = new HashMap<>();
354 allvotes.put(partyP, new VotesWithValue(partyP, new HashSet<>(
355 Arrays.asList(new VoteWithValue(partyP, bidA, 2, 2, 100)))));
356 allvotes.put(partyQ, new VotesWithValue(partyQ, new HashSet<>(
357 Arrays.asList(new VoteWithValue(partyQ, bidA, 2, 9, 100)))));
358 allvotes.put(partyR, new VotesWithValue(partyR, new HashSet<>(
359 Arrays.asList(new VoteWithValue(partyR, bidA, 2, 3, 100)))));
360
361 allvotes.put(partyS, new VotesWithValue(partyS, new HashSet<>(
362 Arrays.asList(new VoteWithValue(partyS, bidA, 2, 9, 100)))));
363
364 CollectedVotesWithValue cv = new CollectedVotesWithValue(allvotes,
365 allpowers);
366 Map<PartyId, Bid> best = cv.getMaxAgreements();
367 assertEquals(
368 new HashSet<>(Arrays.asList(partyP, partyQ, partyR, partyS)),
369 best.keySet());
370 }
371
372}
Note: See TracBrowser for help on using the repository browser.