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