1 | package geniusweb.bidspace.pareto;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.mockito.Matchers.any;
|
---|
5 | import static org.mockito.Matchers.eq;
|
---|
6 | import static org.mockito.Mockito.mock;
|
---|
7 | import static org.mockito.Mockito.when;
|
---|
8 |
|
---|
9 | import java.math.BigDecimal;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.LinkedList;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Map;
|
---|
16 |
|
---|
17 | import org.junit.Test;
|
---|
18 |
|
---|
19 | import geniusweb.bidspace.pareto.GenericPareto;
|
---|
20 | import geniusweb.issuevalue.Bid;
|
---|
21 | import geniusweb.issuevalue.DiscreteValue;
|
---|
22 | import geniusweb.issuevalue.DiscreteValueSet;
|
---|
23 | import geniusweb.issuevalue.Domain;
|
---|
24 | import geniusweb.issuevalue.NumberValue;
|
---|
25 | import geniusweb.issuevalue.NumberValueSet;
|
---|
26 | import geniusweb.issuevalue.Value;
|
---|
27 | import geniusweb.issuevalue.ValueSet;
|
---|
28 | import geniusweb.profile.PartialOrdering;
|
---|
29 |
|
---|
30 | public class GenericParetoTest {
|
---|
31 | private static final DiscreteValue I1V2 = new DiscreteValue("i1v2");
|
---|
32 | private static final DiscreteValue I1V1 = new DiscreteValue("i1v1");
|
---|
33 | private static final NumberValue I2V1 = new NumberValue("2.00");
|
---|
34 | private static final NumberValue I2V2 = new NumberValue("2.45");
|
---|
35 | private static final NumberValue I2V3 = new NumberValue("2.90");
|
---|
36 | private static final String DOMAINNAME = "testdomain";
|
---|
37 | private static final String ISSUE1 = "issue1";
|
---|
38 | private static final String ISSUE2 = "issue2";
|
---|
39 | private static final Map<String, ValueSet> issues = new HashMap<>();
|
---|
40 |
|
---|
41 | private static final ValueSet values1;
|
---|
42 | private static final ValueSet values2;
|
---|
43 | private static final BigDecimal TWO = new BigDecimal("2");
|
---|
44 | private static final BigDecimal THREE = new BigDecimal("3");
|
---|
45 |
|
---|
46 | private static final Domain domain;
|
---|
47 | private static Bid bid1, bid2, bid3;
|
---|
48 |
|
---|
49 | static {
|
---|
50 | Collection<DiscreteValue> discretevalues1 = new LinkedList<>();
|
---|
51 | discretevalues1.add(I1V1);
|
---|
52 | discretevalues1.add(I1V2);
|
---|
53 | values1 = new DiscreteValueSet(discretevalues1);
|
---|
54 | issues.put(ISSUE1, values1);
|
---|
55 |
|
---|
56 | values2 = new NumberValueSet(TWO, THREE, new BigDecimal("0.45"));
|
---|
57 | issues.put(ISSUE2, values2);
|
---|
58 |
|
---|
59 | domain = new Domain(DOMAINNAME, issues);
|
---|
60 |
|
---|
61 | Map<String, Value> issuevalues = new HashMap<>();
|
---|
62 | issuevalues.put(ISSUE1, I1V1);
|
---|
63 | issuevalues.put(ISSUE2, I2V1);
|
---|
64 | bid1 = new Bid(issuevalues);
|
---|
65 | issuevalues.put(ISSUE1, I1V1);
|
---|
66 | issuevalues.put(ISSUE2, I2V2);
|
---|
67 | bid2 = new Bid(issuevalues);
|
---|
68 | issuevalues.put(ISSUE1, I1V2);
|
---|
69 | issuevalues.put(ISSUE2, I2V1);
|
---|
70 | bid3 = new Bid(issuevalues);
|
---|
71 |
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Test
|
---|
75 | public void genericParetoTest() {
|
---|
76 | PartialOrdering profile1 = mock(PartialOrdering.class);
|
---|
77 | PartialOrdering profile2 = mock(PartialOrdering.class);
|
---|
78 |
|
---|
79 | when(profile1.getDomain()).thenReturn(domain);
|
---|
80 | when(profile2.getDomain()).thenReturn(domain);
|
---|
81 |
|
---|
82 | // fefault: there is no preference at all, all isPreferredOrEqual
|
---|
83 | // returns false and we can't remove pareto points.
|
---|
84 |
|
---|
85 | List<PartialOrdering> profiles = Arrays.asList(profile1, profile2);
|
---|
86 | GenericPareto pareto = new GenericPareto(profiles);
|
---|
87 |
|
---|
88 | Collection<Bid> points = pareto.getPoints();
|
---|
89 | assertEquals(6, points.size());
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Test
|
---|
93 | public void genericParetoTest1() {
|
---|
94 | PartialOrdering profile1 = mock(PartialOrdering.class);
|
---|
95 | PartialOrdering profile2 = mock(PartialOrdering.class);
|
---|
96 |
|
---|
97 | when(profile1.getDomain()).thenReturn(domain);
|
---|
98 | when(profile2.getDomain()).thenReturn(domain);
|
---|
99 |
|
---|
100 | // both prefer bid1 over any other bid.
|
---|
101 | when(profile1.isPreferredOrEqual(eq(bid1), any(Bid.class)))
|
---|
102 | .thenReturn(true);
|
---|
103 | when(profile2.isPreferredOrEqual(eq(bid1), any(Bid.class)))
|
---|
104 | .thenReturn(true);
|
---|
105 |
|
---|
106 | List<PartialOrdering> profiles = Arrays.asList(profile1, profile2);
|
---|
107 | GenericPareto pareto = new GenericPareto(profiles);
|
---|
108 |
|
---|
109 | Collection<Bid> points = pareto.getPoints();
|
---|
110 | assertEquals(1, points.size());
|
---|
111 | assertEquals(bid1, points.iterator().next());
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Test
|
---|
115 | public void genericParetoTest2() {
|
---|
116 | PartialOrdering profile1 = mock(PartialOrdering.class);
|
---|
117 | PartialOrdering profile2 = mock(PartialOrdering.class);
|
---|
118 |
|
---|
119 | when(profile1.getDomain()).thenReturn(domain);
|
---|
120 | when(profile2.getDomain()).thenReturn(domain);
|
---|
121 |
|
---|
122 | // both prefer bid2 over any other bid.
|
---|
123 | when(profile1.isPreferredOrEqual(eq(bid2), any(Bid.class)))
|
---|
124 | .thenReturn(true);
|
---|
125 | when(profile2.isPreferredOrEqual(eq(bid2), any(Bid.class)))
|
---|
126 | .thenReturn(true);
|
---|
127 | List<PartialOrdering> profiles = Arrays.asList(profile1, profile2);
|
---|
128 | GenericPareto pareto = new GenericPareto(profiles);
|
---|
129 |
|
---|
130 | Collection<Bid> points = pareto.getPoints();
|
---|
131 | // since both prefer bid2, bid is pareto point.
|
---|
132 | assertEquals(1, points.size());
|
---|
133 | assertEquals(bid2, points.iterator().next());
|
---|
134 | }
|
---|
135 |
|
---|
136 | @Test
|
---|
137 | public void genericParetoTest3() {
|
---|
138 | PartialOrdering profile1 = mock(PartialOrdering.class);
|
---|
139 | PartialOrdering profile2 = mock(PartialOrdering.class);
|
---|
140 |
|
---|
141 | when(profile1.getDomain()).thenReturn(domain);
|
---|
142 | when(profile2.getDomain()).thenReturn(domain);
|
---|
143 |
|
---|
144 | // profile1 prefers bid1, profile2 prefers bid2.
|
---|
145 | // now neither bid1 nor bid2 are dominating each other
|
---|
146 | // and nor does bid1 or bid2 dominate anything else.
|
---|
147 | when(profile1.isPreferredOrEqual(eq(bid1), any(Bid.class)))
|
---|
148 | .thenReturn(true);
|
---|
149 | when(profile2.isPreferredOrEqual(eq(bid2), any(Bid.class)))
|
---|
150 | .thenReturn(true);
|
---|
151 | List<PartialOrdering> profiles = Arrays.asList(profile1, profile2);
|
---|
152 | GenericPareto pareto = new GenericPareto(profiles);
|
---|
153 |
|
---|
154 | Collection<Bid> points = pareto.getPoints();
|
---|
155 | // since both prefer bid2, bid is pareto point.
|
---|
156 | assertEquals(6, points.size());
|
---|
157 | }
|
---|
158 |
|
---|
159 | @Test
|
---|
160 | public void genericParetoTest4() {
|
---|
161 | PartialOrdering profile1 = mock(PartialOrdering.class);
|
---|
162 | PartialOrdering profile2 = mock(PartialOrdering.class);
|
---|
163 |
|
---|
164 | when(profile1.getDomain()).thenReturn(domain);
|
---|
165 | when(profile2.getDomain()).thenReturn(domain);
|
---|
166 |
|
---|
167 | // profile1 prefers bid1, profile2 prefers bid2.
|
---|
168 | // but they both hate bid3.
|
---|
169 | when(profile1.isPreferredOrEqual(eq(bid1), any(Bid.class)))
|
---|
170 | .thenReturn(true);
|
---|
171 | when(profile1.isPreferredOrEqual(any(Bid.class), eq(bid3)))
|
---|
172 | .thenReturn(true);
|
---|
173 | when(profile2.isPreferredOrEqual(any(Bid.class), eq(bid3)))
|
---|
174 | .thenReturn(true);
|
---|
175 | when(profile2.isPreferredOrEqual(eq(bid2), any(Bid.class)))
|
---|
176 | .thenReturn(true);
|
---|
177 | List<PartialOrdering> profiles = Arrays.asList(profile1, profile2);
|
---|
178 | GenericPareto pareto = new GenericPareto(profiles);
|
---|
179 |
|
---|
180 | Collection<Bid> points = pareto.getPoints();
|
---|
181 | // both hate bid3 but don't agree on bid1/2. bid3 is ruled out.
|
---|
182 | assertEquals(5, points.size());
|
---|
183 | }
|
---|
184 |
|
---|
185 | }
|
---|