source: exampleparties/timedependentparty/test/TimeDependentPartyMOPACTest.java@ 88

Last change on this file since 88 was 88, checked in by Bart Vastenhouw, 3 years ago

Added python SimpleRunner GUI

File size: 5.8 KB
Line 
1package geniusweb.exampleparties.timedependentparty;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertTrue;
5import static org.mockito.Mockito.mock;
6
7import java.io.IOException;
8import java.math.BigDecimal;
9import java.net.URI;
10import java.net.URISyntaxException;
11import java.nio.charset.StandardCharsets;
12import java.nio.file.Files;
13import java.nio.file.Paths;
14import java.util.Arrays;
15import java.util.Collections;
16import java.util.HashMap;
17import java.util.Map;
18
19import org.junit.Before;
20import org.junit.Test;
21
22import com.fasterxml.jackson.core.JsonParseException;
23import com.fasterxml.jackson.databind.JsonMappingException;
24import com.fasterxml.jackson.databind.ObjectMapper;
25
26import geniusweb.actions.Action;
27import geniusweb.actions.Offer;
28import geniusweb.actions.PartyId;
29import geniusweb.actions.Vote;
30import geniusweb.actions.Votes;
31import geniusweb.inform.OptIn;
32import geniusweb.inform.Settings;
33import geniusweb.inform.Voting;
34import geniusweb.inform.YourTurn;
35import geniusweb.issuevalue.Bid;
36import geniusweb.issuevalue.DiscreteValue;
37import geniusweb.issuevalue.NumberValue;
38import geniusweb.issuevalue.Value;
39import geniusweb.profile.Profile;
40import geniusweb.profile.utilityspace.LinearAdditiveUtilitySpace;
41import geniusweb.progress.ProgressRounds;
42import geniusweb.references.Parameters;
43import geniusweb.references.ProfileRef;
44import geniusweb.references.ProtocolRef;
45
46public class TimeDependentPartyMOPACTest {
47
48 private static final PartyId me = new PartyId("me");
49 private static final PartyId otherparty = new PartyId("other");
50 private static final String MOPAC = "MOPAC";
51 private static final String PROFILE = "src/test/resources/testprofile.json";
52 private final static ObjectMapper jackson = new ObjectMapper();
53
54 private TimeDependentParty party;
55 private TestConnection connection = new TestConnection();
56 private ProtocolRef protocol = new ProtocolRef("MOPAC");
57 private ProgressRounds progress = mock(ProgressRounds.class);
58 private Parameters parameters = new Parameters();
59 private Settings settings;
60 private LinearAdditiveUtilitySpace profile;
61 private Map<PartyId, Integer> powers = new HashMap<>();
62 private Bid bestBid;
63
64 @Before
65 public void before() throws JsonParseException, JsonMappingException,
66 IOException, URISyntaxException {
67 powers.put(me, 1);
68 powers.put(otherparty, 1);
69
70 party = new TimeDependentParty() {
71
72 @Override
73 public String getDescription() {
74 return "test";
75 }
76
77 @Override
78 public double getE() {
79 return 2; // conceder-like
80 }
81 };
82 settings = new Settings(me, new ProfileRef(new URI("file:" + PROFILE)),
83 protocol, progress, parameters);
84
85 String serialized = new String(Files.readAllBytes(Paths.get(PROFILE)),
86 StandardCharsets.UTF_8);
87 profile = (LinearAdditiveUtilitySpace) jackson.readValue(serialized,
88 Profile.class);
89
90 // hard coded best bid for this domain
91 Map<String, Value> vals = new HashMap<>();
92 vals.put("issue2", new NumberValue(new BigDecimal("18")));
93 vals.put("issue1", new DiscreteValue("issue1value2"));
94 bestBid = new Bid(vals);
95
96 }
97
98 @Test
99 public void testPartyFirstOffers() {
100 party.connect(connection);
101 party.notifyChange(settings);
102
103 party.notifyChange(new YourTurn());
104 assertEquals(1, connection.getActions().size());
105 assertTrue(connection.getActions().get(0) instanceof Offer);
106 }
107
108 @Test
109 public void checkPartyFirstBid() {
110 party.connect(connection);
111 party.notifyChange(settings);
112
113 // let party make an offer and go to the voting phase
114 party.notifyChange(new YourTurn());
115 Offer offer = (Offer) connection.getActions().get(0);
116
117 // it's either the best or one-but-best bid,
118 // because we set up the tolerance like that.
119 Bid bid = offer.getBid();
120 assertEquals("issue1value2",
121 ((DiscreteValue) bid.getValue("issue1")).getValue());
122 assertTrue(((NumberValue) bid.getValue("issue2")).getValue()
123 .compareTo(new BigDecimal("17")) >= 0);
124 }
125
126 @Test
127 public void testPartyVotesForBest() {
128 party.connect(connection);
129 party.notifyChange(settings);
130
131 // let party make an offer and go to the voting phase
132 party.notifyChange(new YourTurn());
133 assertEquals(1, connection.getActions().size());
134
135 // send bestBid bid as the voting option
136 // this is better than party's firstBid
137
138 party.notifyChange(new Voting(
139 Arrays.asList(new Offer(otherparty, bestBid)), powers));
140 assertEquals(2, connection.getActions().size());
141
142 Action lastAction = connection.getActions().get(1);
143 assertTrue(lastAction instanceof Votes);
144 assertEquals(
145 new Votes(me,
146 Collections.singleton(
147 new Vote(me, bestBid, 1, Integer.MAX_VALUE))),
148 lastAction);
149 }
150
151 @Test
152 public void testPartyOptsInBest() {
153 party.connect(connection);
154 party.notifyChange(settings);
155
156 // let party make an offer and go to the voting phase
157 party.notifyChange(new YourTurn());
158 assertEquals(1, connection.getActions().size());
159
160 // send bestBid bid as the voting option
161 // this is better than party's firstBid
162
163 party.notifyChange(new Voting(Arrays.asList(new Offer(me, bestBid),
164 new Offer(otherparty, bestBid)), powers));
165 assertEquals(2, connection.getActions().size());
166
167 // we already checked the party votes for bestBid
168 Votes votes = (Votes) connection.getActions().get(1);
169
170 party.notifyChange(new OptIn(Arrays.asList(votes)));
171 assertEquals(3, connection.getActions().size());
172
173 Action lastAction = connection.getActions().get(2);
174 assertTrue(lastAction instanceof Votes);
175 assertTrue(((Votes) lastAction).isExtending(votes));
176 }
177
178 @Test
179 public void testGetCapabilities() {
180 assertTrue(party.getCapabilities().getBehaviours().contains(MOPAC));
181 }
182
183// private Bid findBestBid() {
184// BigDecimal bestvalue = BigDecimal.ZERO;
185// Bid best = null;
186// for (Bid bid : new AllBidsList(profile.getDomain())) {
187// BigDecimal util = profile.getUtility(bid);
188// if (util.compareTo(bestvalue) > 0) {
189// best = bid;
190// bestvalue = util;
191// }
192// }
193// return best;
194// }
195
196}
Note: See TracBrowser for help on using the repository browser.