[96] | 1 | package geniusweb.exampleparties.timedependentparty;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.assertEquals;
|
---|
| 4 | import static org.junit.Assert.assertTrue;
|
---|
| 5 | import static org.mockito.Mockito.mock;
|
---|
| 6 |
|
---|
| 7 | import java.io.IOException;
|
---|
| 8 | import java.math.BigDecimal;
|
---|
| 9 | import java.net.URI;
|
---|
| 10 | import java.net.URISyntaxException;
|
---|
| 11 | import java.nio.charset.StandardCharsets;
|
---|
| 12 | import java.nio.file.Files;
|
---|
| 13 | import java.nio.file.Paths;
|
---|
| 14 | import java.util.Arrays;
|
---|
| 15 | import java.util.Collections;
|
---|
| 16 | import java.util.HashMap;
|
---|
| 17 | import java.util.Map;
|
---|
| 18 |
|
---|
| 19 | import org.junit.Before;
|
---|
| 20 | import org.junit.Test;
|
---|
| 21 |
|
---|
| 22 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
| 23 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
| 24 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 25 |
|
---|
| 26 | import geniusweb.actions.Action;
|
---|
| 27 | import geniusweb.actions.Offer;
|
---|
| 28 | import geniusweb.actions.PartyId;
|
---|
| 29 | import geniusweb.actions.Vote;
|
---|
| 30 | import geniusweb.actions.Votes;
|
---|
| 31 | import geniusweb.inform.OptIn;
|
---|
| 32 | import geniusweb.inform.Settings;
|
---|
| 33 | import geniusweb.inform.Voting;
|
---|
| 34 | import geniusweb.inform.YourTurn;
|
---|
| 35 | import geniusweb.issuevalue.Bid;
|
---|
| 36 | import geniusweb.issuevalue.DiscreteValue;
|
---|
| 37 | import geniusweb.issuevalue.NumberValue;
|
---|
| 38 | import geniusweb.issuevalue.Value;
|
---|
| 39 | import geniusweb.profile.Profile;
|
---|
| 40 | import geniusweb.profile.utilityspace.LinearAdditiveUtilitySpace;
|
---|
| 41 | import geniusweb.progress.ProgressRounds;
|
---|
| 42 | import geniusweb.references.Parameters;
|
---|
| 43 | import geniusweb.references.ProfileRef;
|
---|
| 44 | import geniusweb.references.ProtocolRef;
|
---|
| 45 |
|
---|
| 46 | public 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 | }
|
---|