1 | package geniusweb.opponentmodel;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertNotNull;
|
---|
5 | import static org.junit.Assert.assertTrue;
|
---|
6 | import static org.mockito.Mockito.mock;
|
---|
7 |
|
---|
8 | import java.math.BigDecimal;
|
---|
9 | import java.math.BigInteger;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.Collections;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.LinkedList;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Map;
|
---|
17 |
|
---|
18 | import org.junit.Test;
|
---|
19 |
|
---|
20 | import geniusweb.actions.Offer;
|
---|
21 | import geniusweb.actions.PartyId;
|
---|
22 | import geniusweb.issuevalue.Bid;
|
---|
23 | import geniusweb.issuevalue.DiscreteValue;
|
---|
24 | import geniusweb.issuevalue.DiscreteValueSet;
|
---|
25 | import geniusweb.issuevalue.Domain;
|
---|
26 | import geniusweb.issuevalue.NumberValue;
|
---|
27 | import geniusweb.issuevalue.NumberValueSet;
|
---|
28 | import geniusweb.issuevalue.Value;
|
---|
29 | import geniusweb.issuevalue.ValueSet;
|
---|
30 | import geniusweb.progress.Progress;
|
---|
31 | import tudelft.utilities.junit.GeneralTests;
|
---|
32 |
|
---|
33 | public class FrequencyOppModelTest
|
---|
34 | extends GeneralTests<FrequencyOpponentModel> {
|
---|
35 | private final static String ISS1 = "issue1";
|
---|
36 | private final static String ISS2 = "issue2";
|
---|
37 | private static final DiscreteValue I1V1 = new DiscreteValue("i1v1");
|
---|
38 | private static final DiscreteValue I1V2 = new DiscreteValue("i1v2");
|
---|
39 | private static final DiscreteValue I2V1 = new DiscreteValue("i2v1");
|
---|
40 | private static final DiscreteValue I2V2 = new DiscreteValue("i2v2");
|
---|
41 | private static final DiscreteValue I1V2b = new DiscreteValue("i1v2b");
|
---|
42 | private static final Progress progress = mock(Progress.class);
|
---|
43 | private static final PartyId other = new PartyId("other");
|
---|
44 |
|
---|
45 | private static Domain domain, domain2, domain3;
|
---|
46 | private static FrequencyOpponentModel oppModel1, oppModel1b, oppModel2,
|
---|
47 | oppModel3, oppModel4;
|
---|
48 |
|
---|
49 | private final static List<List<Bid>> list = new LinkedList<>();
|
---|
50 |
|
---|
51 | private final static Bid bid1, bid2, bid3;
|
---|
52 | private static final BigDecimal HALF = new BigDecimal("0.5");
|
---|
53 |
|
---|
54 | static {
|
---|
55 | Map<String, ValueSet> issues = new HashMap<>();
|
---|
56 | Collection<DiscreteValue> discretevalues1 = new LinkedList<>();
|
---|
57 | discretevalues1.add(I1V1);
|
---|
58 | discretevalues1.add(I1V2);
|
---|
59 | DiscreteValueSet values1 = new DiscreteValueSet(discretevalues1);
|
---|
60 | issues.put(ISS1, values1);
|
---|
61 | NumberValueSet values2 = new NumberValueSet(BigDecimal.ZERO,
|
---|
62 | BigDecimal.TEN, new BigDecimal("0.3"));
|
---|
63 | issues.put(ISS2, values2);
|
---|
64 | domain = new Domain("test", issues);
|
---|
65 | domain2 = new Domain("test2", issues);
|
---|
66 |
|
---|
67 | // slightly different issue1
|
---|
68 | issues = new HashMap<>();
|
---|
69 | discretevalues1 = new LinkedList<>();
|
---|
70 | discretevalues1.add(I1V1);
|
---|
71 | discretevalues1.add(I1V2b);
|
---|
72 | values1 = new DiscreteValueSet(discretevalues1);
|
---|
73 | issues.put(ISS1, values1);
|
---|
74 | values2 = new NumberValueSet(BigDecimal.ZERO, BigDecimal.TEN,
|
---|
75 | new BigDecimal("0.3"));
|
---|
76 | issues.put(ISS2, values2);
|
---|
77 | domain3 = new Domain("test", issues);
|
---|
78 |
|
---|
79 | // all bids are for domain
|
---|
80 | Map<String, Value> issuevalues = new HashMap<>();
|
---|
81 | issuevalues.put(ISS1, I1V1);
|
---|
82 | issuevalues.put(ISS2, new NumberValue(new BigDecimal("1.2")));
|
---|
83 | bid1 = new Bid(issuevalues);
|
---|
84 |
|
---|
85 | issuevalues.put(ISS1, I1V1);
|
---|
86 | issuevalues.put(ISS2, new NumberValue(new BigDecimal("1.5")));
|
---|
87 | bid2 = new Bid(issuevalues);
|
---|
88 |
|
---|
89 | issuevalues.put(ISS1, I1V2);
|
---|
90 | issuevalues.put(ISS2, new NumberValue(new BigDecimal("1.5")));
|
---|
91 | bid3 = new Bid(issuevalues);
|
---|
92 |
|
---|
93 | oppModel1 = new FrequencyOpponentModel().with(domain, null);
|
---|
94 | oppModel1b = new FrequencyOpponentModel().with(domain, null);
|
---|
95 | oppModel2 = new FrequencyOpponentModel().with(domain2, null);
|
---|
96 | oppModel3 = new FrequencyOpponentModel().with(domain3, null);
|
---|
97 | oppModel4 = oppModel3.with(new Offer(other, bid1), progress);
|
---|
98 |
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public List<List<FrequencyOpponentModel>> getGeneralTestData() {
|
---|
103 | return Arrays.asList(Arrays.asList(oppModel1, oppModel1b),
|
---|
104 | Arrays.asList(oppModel2), Arrays.asList(oppModel3),
|
---|
105 | Arrays.asList(oppModel4));
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public List<String> getGeneralTestStrings() {
|
---|
110 | return Arrays.asList(
|
---|
111 | "FrequencyOpponentModel\\[0,\\{issue2=\\{\\}, issue1=\\{\\}\\}\\]",
|
---|
112 | "FrequencyOpponentModel\\[0,\\{issue2=\\{\\}, issue1=\\{\\}\\}\\]",
|
---|
113 | "FrequencyOpponentModel\\[0,\\{issue2=\\{\\}, issue1=\\{\\}\\}\\]",
|
---|
114 | "FrequencyOpponentModel\\[1,\\{issue2=\\{1.2=1\\}, issue1=\\{\"i1v1\"=1\\}\\}\\]");
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Test(expected = NullPointerException.class)
|
---|
118 | public void smokeTestNull() {
|
---|
119 | new FrequencyOpponentModel().with((Domain) null, null);
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Test
|
---|
123 | public void smokeTest() {
|
---|
124 | new FrequencyOpponentModel().with(domain, null);
|
---|
125 | }
|
---|
126 |
|
---|
127 | @Test
|
---|
128 | public void testEmptyModel() {
|
---|
129 | FrequencyOpponentModel oppModel = new FrequencyOpponentModel()
|
---|
130 | .with(domain, null);
|
---|
131 | assertEquals(BigDecimal.ONE, oppModel.getUtility(bid1));
|
---|
132 | assertEquals(BigDecimal.ONE, oppModel.getUtility(bid2));
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Test
|
---|
136 | public void testEmptyModelPartialBid() {
|
---|
137 | FrequencyOpponentModel oppModel = new FrequencyOpponentModel()
|
---|
138 | .with(domain, null);
|
---|
139 | Bid bid = new Bid(Collections.emptyMap());
|
---|
140 | assertEquals(BigDecimal.ONE, oppModel.getUtility(bid));
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Test
|
---|
144 | public void testPartialUtility() {
|
---|
145 | Map<String, Map<Value, Integer>> freqs = new HashMap<>();
|
---|
146 | Map<Value, Integer> freqs1 = new HashMap<>();
|
---|
147 | Map<Value, Integer> freqs2 = new HashMap<>();
|
---|
148 |
|
---|
149 | freqs1.put(I1V1, 2);
|
---|
150 | freqs1.put(I1V2, 0);
|
---|
151 | freqs2.put(I2V1, 1);
|
---|
152 | freqs2.put(I2V2, 1);
|
---|
153 |
|
---|
154 | freqs.put(ISS1, freqs1);
|
---|
155 | freqs.put(ISS2, freqs2);
|
---|
156 | BigInteger total = new BigInteger("2");
|
---|
157 |
|
---|
158 | FrequencyOpponentModel oppModel = new FrequencyOpponentModel(domain,
|
---|
159 | freqs, total, (Bid) null);
|
---|
160 |
|
---|
161 | double E = 0.0000001;
|
---|
162 | assertEquals(0.5, oppModel.getUtility(new Bid(ISS1, I1V1)).floatValue(),
|
---|
163 | E);
|
---|
164 | assertEquals(0, oppModel.getUtility(new Bid(ISS1, I1V2)).floatValue(),
|
---|
165 | E);
|
---|
166 | assertEquals(0.25,
|
---|
167 | oppModel.getUtility(new Bid(ISS2, I2V1)).floatValue(), E);
|
---|
168 | assertEquals(0.25,
|
---|
169 | oppModel.getUtility(new Bid(ISS2, I2V2)).floatValue(), E);
|
---|
170 |
|
---|
171 | }
|
---|
172 |
|
---|
173 | @Test
|
---|
174 | public void testUpdate() {
|
---|
175 | FrequencyOpponentModel oppModel = oppModel1.with(new Offer(other, bid1),
|
---|
176 | progress);
|
---|
177 | assertTrue(BigDecimal.ONE.compareTo(oppModel.getUtility(bid1)) == 0);
|
---|
178 | assertTrue(BigDecimal.ZERO.compareTo(oppModel.getUtility(bid3)) == 0);
|
---|
179 | // bid2 has 1 of 2 issue values same as bid1.
|
---|
180 | assertTrue(HALF.compareTo(oppModel.getUtility(bid2)) == 0);
|
---|
181 | }
|
---|
182 |
|
---|
183 | @Test
|
---|
184 | public void testUpdate2() {
|
---|
185 | // bid1 and bid2 both want I1V1. They differ on the number value.
|
---|
186 | // bid3 wants I1V2 but does have the number value from bid2
|
---|
187 | FrequencyOpponentModel oppModel = oppModel1
|
---|
188 | .with(new Offer(other, bid1), progress)
|
---|
189 | .with(new Offer(other, bid2), progress);
|
---|
190 | assertTrue(new BigDecimal("0.75")
|
---|
191 | .compareTo(oppModel.getUtility(bid1)) == 0);
|
---|
192 | assertTrue(new BigDecimal("0.75")
|
---|
193 | .compareTo(oppModel.getUtility(bid2)) == 0);
|
---|
194 | assertTrue(new BigDecimal("0.25")
|
---|
195 | .compareTo(oppModel.getUtility(bid3)) == 0);
|
---|
196 | }
|
---|
197 |
|
---|
198 | @Test
|
---|
199 | public void testPartialBidUpdate() {
|
---|
200 | FrequencyOpponentModel oppModel = oppModel1.with(new Offer(other, bid1),
|
---|
201 | progress);
|
---|
202 | Bid partialbid = new Bid(ISS1, I1V1);
|
---|
203 | oppModel.with(new Offer(other, partialbid), progress);
|
---|
204 | }
|
---|
205 |
|
---|
206 | @Test
|
---|
207 | public void testGetCounts() {
|
---|
208 | assertEquals(Collections.emptyMap(), oppModel1.getCounts(ISS1));
|
---|
209 | assertEquals(Collections.emptyMap(), oppModel1.getCounts(ISS2));
|
---|
210 |
|
---|
211 | Map<String, Value> values1 = new HashMap<>();
|
---|
212 | values1.put(ISS1, I1V1);
|
---|
213 | values1.put(ISS2, I2V1);
|
---|
214 | Offer offer1 = new Offer(other, new Bid(values1));
|
---|
215 |
|
---|
216 | FrequencyOpponentModel oppmod = oppModel1.with(offer1, progress);
|
---|
217 |
|
---|
218 | assertEquals(1, oppmod.getCounts(ISS1).get(I1V1).intValue());
|
---|
219 | assertEquals(null, oppmod.getCounts(ISS1).get(I1V2));
|
---|
220 | assertEquals(1, oppmod.getCounts(ISS2).get(I2V1).intValue());
|
---|
221 | assertEquals(null, oppmod.getCounts(ISS2).get(I2V2));
|
---|
222 |
|
---|
223 | // same identical bid. Counts should go to 2
|
---|
224 | oppmod = oppmod.with(offer1, progress);
|
---|
225 |
|
---|
226 | assertEquals(2, oppmod.getCounts(ISS1).get(I1V1).intValue());
|
---|
227 | assertEquals(2, oppmod.getCounts(ISS2).get(I2V1).intValue());
|
---|
228 |
|
---|
229 | Map<String, Value> values2 = new HashMap<>();
|
---|
230 | values2.put(ISS1, I1V2);
|
---|
231 | values2.put(ISS2, I2V2);
|
---|
232 | Offer offer2 = new Offer(other, new Bid(values2));
|
---|
233 |
|
---|
234 | // Other bid. with value2 for both issues
|
---|
235 | oppmod = oppmod.with(offer2, progress);
|
---|
236 |
|
---|
237 | assertEquals(2, oppmod.getCounts(ISS1).get(I1V1).intValue());
|
---|
238 | assertEquals(2, oppmod.getCounts(ISS2).get(I2V1).intValue());
|
---|
239 | assertEquals(1, oppmod.getCounts(ISS1).get(I1V2).intValue());
|
---|
240 | assertEquals(1, oppmod.getCounts(ISS2).get(I2V2).intValue());
|
---|
241 |
|
---|
242 | }
|
---|
243 |
|
---|
244 | @Test
|
---|
245 | public void testStableName() {
|
---|
246 | String name = oppModel1.getName();
|
---|
247 | assertNotNull(name);
|
---|
248 | assertEquals(name, oppModel1.getName());
|
---|
249 | }
|
---|
250 | }
|
---|