source: anac2020/AzarAgent/src/test/java/geniusweb/exampleparties/simpleshaop/ShaopPartyTest.java

Last change on this file was 31, checked in by wouter, 3 years ago

#3

File size: 10.5 KB
Line 
1package geniusweb.exampleparties.simpleshaop;
2
3import static org.junit.Assert.assertNotNull;
4import static org.mockito.Mockito.mock;
5
6import java.io.IOException;
7import java.net.URI;
8import java.net.URISyntaxException;
9import java.nio.charset.StandardCharsets;
10import java.nio.file.Files;
11import java.nio.file.Paths;
12import java.util.ArrayList;
13import java.util.HashMap;
14import java.util.LinkedList;
15import java.util.List;
16import java.util.Map;
17
18import javax.websocket.DeploymentException;
19
20import org.junit.Before;
21import org.junit.Test;
22
23import com.fasterxml.jackson.core.JsonParseException;
24import com.fasterxml.jackson.databind.JsonMappingException;
25import com.fasterxml.jackson.databind.ObjectMapper;
26
27import geniusweb.actions.Action;
28import geniusweb.actions.PartyId;
29import geniusweb.bidspace.AllBidsList;
30import geniusweb.connection.ConnectionEnd;
31import geniusweb.inform.Inform;
32import geniusweb.inform.Settings;
33import geniusweb.issuevalue.Bid;
34import geniusweb.issuevalue.DiscreteValue;
35import geniusweb.issuevalue.Value;
36import geniusweb.profile.Profile;
37import geniusweb.profileconnection.ProfileConnectionFactory;
38import geniusweb.profileconnection.ProfileInterface;
39import geniusweb.progress.ProgressRounds;
40import geniusweb.references.Parameters;
41import geniusweb.references.ProfileRef;
42import geniusweb.references.ProtocolRef;
43import geniusweb.references.Reference;
44import tudelft.utilities.listener.DefaultListenable;
45import tudelft.utilities.logging.ReportToLogger;
46import tudelft.utilities.logging.Reporter;
47
48public class ShaopPartyTest {
49
50 private static final String SHAOP = "SHAOP";
51 private static final PartyId otherparty = new PartyId("other");
52 // private static final String PROFILE = "src/test/resources/testprofile.json";
53
54 private static final String PROFILE = "src/test/resources/jobs1.json";
55
56 private final static ObjectMapper jackson = new ObjectMapper();
57
58 private ShaopParty party;
59 private final TestConnection connection = new TestConnection();
60 private final ProtocolRef protocol = mock(ProtocolRef.class);
61 private final ProgressRounds progress = mock(ProgressRounds.class);
62 private Settings settings;
63 private Profile profile;
64 private final Parameters parameters = new Parameters();
65
66 @Before
67 public void before() throws JsonParseException, JsonMappingException, IOException, URISyntaxException {
68 party = new ShaopParty();
69 settings = new Settings(new PartyId("party1"), new ProfileRef(new URI("file:" + PROFILE)), protocol, progress,
70 parameters);
71
72 String serialized = new String(Files.readAllBytes(Paths.get(PROFILE)), StandardCharsets.UTF_8);
73 profile = jackson.readValue(serialized, Profile.class);
74
75 }
76
77 @Test
78 public void smokeTest() {
79 }
80
81 @Test
82 public void getDescriptionTest() {
83 assertNotNull(party.getDescription());
84 }
85
86 /*
87 *
88 * @Test public void getCapabilitiesTest() { Capabilities capabilities =
89 * party.getCapabilities(); assertFalse("party does not define protocols",
90 * capabilities.getBehaviours().isEmpty()); }
91 *
92 * @Test public void testInformConnection() { party.connect(connection); //
93 * agent should not start acting just after an inform assertEquals(0,
94 * connection.getActions().size()); }
95 *
96 * @Test public void testInformSettings() { party.connect(connection);
97 * connection.notifyListeners(settings); assertEquals(0,
98 * connection.getActions().size()); }
99 *
100 * @Test public void testInformAndConnection() { party.connect(connection);
101 * party.notifyChange(settings); assertEquals(0,
102 * connection.getActions().size()); }
103 *
104 * @Test public void testOtherWalksAway() { party.connect(connection);
105 * party.notifyChange(settings);
106 *
107 * party.notifyChange(new ActionDone(new EndNegotiation(otherparty)));
108 *
109 * // party should not act at this point assertEquals(0,
110 * connection.getActions().size()); }
111 *
112 * @Test public void testAgentHasFirstTurn() { party.connect(connection);
113 * party.notifyChange(settings);
114 *
115 * party.notifyChange(new YourTurn()); assertEquals(1,
116 * connection.getActions().size()); assertTrue(connection.getActions().get(0)
117 * instanceof Offer); }
118 *
119 * @Test public void testAgentElicitsComparison() { party.connect(connection);
120 * party.notifyChange(settings);
121 *
122 * Bid bid = makeBid("3", "3"); // not yet in profile party.notifyChange(new
123 * ActionDone(new Offer(otherparty, bid))); party.notifyChange(new YourTurn());
124 * assertEquals(1, connection.getActions().size());
125 * //assertTrue(connection.getActions().get(0) instanceof ElicitComparison);
126 *
127 * }
128 *
129 * @Ignore
130 *
131 * @Test public void testAgentAccepts() { // to make agent accept, the offered
132 * bid must score >0.9 // we would need to mock a lot t make that happen // as
133 * we need to place >10 comparable bids for that. party.connect(connection);
134 * party.notifyChange(settings);
135 *
136 * Bid bid = makeBid("1", "1");// best pssible party.notifyChange(new
137 * ActionDone(new Offer(otherparty, bid))); party.notifyChange(new YourTurn());
138 * assertEquals(1, connection.getActions().size());
139 * assertTrue(connection.getActions().get(0) instanceof Accept);
140 *
141 * }
142 *
143 * @Test public void testAgentLogsFinal() { // this log output is optional, this
144 * is to show how to check log Reporter reporter = mock(Reporter.class); party =
145 * new ShaopParty(reporter); party.connect(connection);
146 * party.notifyChange(settings); party.notifyChange(new Finished(null));
147 *
148 * verify(reporter).log(eq(Level.INFO), eq("Final ourcome:Finished[null]")); }
149 *
150 * @Test public void testAgentsUpdatesProgress() { party.connect(connection);
151 * party.notifyChange(settings);
152 *
153 * party.notifyChange(new ActionDone(new Offer(null, mock(Bid.class))));
154 *
155 * //party.notifyChange(new YourTurn()); //verify(progress).advance(); }
156 *
157 * @Test public void testGetCapabilities() {
158 * assertTrue(party.getCapabilities().getBehaviours().contains(SHAOP)); }
159 *
160 *
161 */
162
163 private Bid myMakeBid(String val1, String val2, String val3, String val4, String val5, String val6) {
164 Map<String, Value> map = new HashMap<>();
165 map.put("lease car", new DiscreteValue(val1));
166 map.put("permanent contract", new DiscreteValue(val2));
167 map.put("career development opportunities", new DiscreteValue(val3));
168 map.put("fte", new DiscreteValue(val4));
169 map.put("salary", new DiscreteValue(val5));
170 map.put("work from home", new DiscreteValue(val6));
171 return new Bid(map);
172
173 }
174
175 private Bid makeBid(String val1, String val2) {
176 Map<String, Value> map = new HashMap<>();
177 map.put("iss1", new DiscreteValue(val1));
178 map.put("iss2", new DiscreteValue(val2));
179 return new Bid(map);
180
181 }
182
183 @Test
184 public void WinkyA2UpdateTest() {
185
186 List<Bid> bidOrder = new ArrayList<Bid>();
187
188 String[] iss1 = { "yes", "no" };
189 String[] iss2 = { "yes", "no" };
190 String[] iss3 = { "low", "medium", "high" };
191 String[] iss4 = { "0.6", "0.8", "1.0" };
192 String[] iss5 = { "2000", "2500", "3000", "3500", "4000" };
193 String[] iss6 = { "0", "1", "2" };
194
195 for (int i = 0; i < 2; i++) {
196 for (int j = 0; j < 1; j++) {
197 for (int j2 = 0; j2 < 1; j2++) {
198 for (int k = 0; k < 1; k++) {
199 for (int k2 = 0; k2 < 3; k2++) {
200 for (int l = 0; l < 1; l++) {
201 bidOrder.add(myMakeBid(iss1[i], iss2[j], iss3[j2], iss4[k], iss5[k2], iss6[l]));
202 }
203 }
204 }
205 }
206
207 }
208 }
209
210 AllBidsList allBids = new AllBidsList(profile.getDomain());
211
212 Reporter reporter = new ReportToLogger("");
213
214 ProfileInterface profileint = null;
215 try {
216 profileint = ProfileConnectionFactory.create(settings.getProfile().getURI(), reporter);
217 } catch (IOException | DeploymentException e) {
218 // TODO Auto-generated catch block
219 e.printStackTrace();
220 }
221
222 GravityEs g = new GravityEs(profile.getDomain(), bidOrder);
223
224 Bid b = myMakeBid(iss1[1], iss2[1], iss3[0], iss4[0], iss5[0], iss6[0]);
225
226 log2(g.getUtilityForBid(bidOrder.get(bidOrder.size() - 1)));
227
228 /*
229 * WinkyA2 winky = new WinkyA2(profile.getDomain(), bidOrder, profileint);
230 *
231 *
232 * Bid b = myMakeBid(iss1[1], iss2[1], iss3[0], iss4[0], iss5[0], iss6[0]);
233 * bidOrder.add(3, b);
234 *
235 *
236 * log2("New bid => "+b+"\n\n");
237 *
238 * WinkyA2 winky1 = new WinkyA2(profile.getDomain(), bidOrder, profileint);
239 *
240 *
241 * bidOrder.remove(3); bidOrder.add(4, b);
242 *
243 * WinkyA2 winky2 = new WinkyA2(profile.getDomain(), bidOrder, profileint);
244 */
245
246 }
247
248 private void log2(Object o) {
249
250 System.out.println(o);
251
252 }
253
254 @Test
255 public void getBestBidInKnownSetTest() {
256 /*
257 * party.connect(connection); party.notifyChange(settings);
258 *
259 * //Bid myBid = makeBid("1", "1");// best pssible //party.myBids.add(myBid);
260 *
261 * Bid oppBid = makeBid("4", "4");// worst pssible party.oppBids.add(oppBid);
262 *
263 *
264 * //assertEquals(0.0, party.getAgreementProbability(myBid, true),0.0000001 );
265 * assertEquals(1.0, party.getAgreementProbability(oppBid, true),0.0000001);
266 *
267 *
268 * MyBidDetails b1 = party.getBestBidInKnownSet(0.1);
269 * System.out.println(b1.getBid()+" => "+b1.getEu());
270 * party.myBids.add(b1.getBid());
271 *
272 * MyBidDetails b2 = party.getBestBidInKnownSet(0.1);
273 * System.out.println(b2.getBid()+" => "+b2.getEu());
274 * party.myBids.add(b2.getBid());
275 *
276 * MyBidDetails b3 = party.getBestBidInKnownSet(0.1);
277 * System.out.println(b3.getBid()+" => "+b3.getEu());
278 * party.myBids.add(b3.getBid());
279 *
280 * MyBidDetails b4 = party.getBestBidInKnownSet(0.1);
281 * System.out.println(b4.getBid()+" => "+b4.getEu());
282 */
283
284 }
285
286 @Test
287 public void testSendOffer() {
288 /*
289 * party.connect(connection); party.notifyChange(settings);
290 *
291 * party.notifyChange(new ActionDone(new Offer(null, makeBid("4", "4"))));
292 * party.notifyChange( new YourTurn());
293 *
294 * System.out.println("oppBids => " + party.oppBids );
295 * System.out.println("myBids => " + party.myBids );
296 */
297
298 // party.notifyChange(new ActionDone(new Offer(null, makeBid("3", "4"))));
299 // party.notifyChange( new YourTurn() );
300
301 // System.out.println("oppBids => " + party.oppBids );
302 // System.out.println("myBids => " + party.myBids );
303
304 }
305
306}
307
308/**
309 * A "real" connection object, because the party is going to subscribe etc, and
310 * without a real connection we would have to do a lot of mocks that would make
311 * the test very hard to read.
312 *
313 */
314class TestConnection extends DefaultListenable<Inform> implements ConnectionEnd<Inform, Action> {
315 private List<Action> actions = new LinkedList<>();
316
317 @Override
318 public void send(Action action) throws IOException {
319 actions.add(action);
320 }
321
322 @Override
323 public Reference getReference() {
324 return null;
325 }
326
327 @Override
328 public URI getRemoteURI() {
329 return null;
330 }
331
332 @Override
333 public void close() {
334
335 }
336
337 @Override
338 public Error getError() {
339 return null;
340 }
341
342 public List<Action> getActions() {
343 return actions;
344 }
345
346}
Note: See TracBrowser for help on using the repository browser.