Changeset 21 for exampleparties/randomparty/src
- Timestamp:
- 09/22/20 08:52:04 (4 years ago)
- Location:
- exampleparties/randomparty/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
exampleparties/randomparty/src/main/java/geniusweb/exampleparties/randomparty/RandomParty.java
r10 r21 5 5 import java.util.Arrays; 6 6 import java.util.HashSet; 7 import java.util.List; 7 8 import java.util.Random; 8 9 import java.util.logging.Level; 10 import java.util.stream.Collectors; 9 11 10 12 import geniusweb.actions.Accept; … … 12 14 import geniusweb.actions.Offer; 13 15 import geniusweb.actions.PartyId; 16 import geniusweb.actions.Vote; 17 import geniusweb.actions.Votes; 14 18 import geniusweb.bidspace.AllPartialBidsList; 19 import geniusweb.inform.ActionDone; 20 import geniusweb.inform.Finished; 21 import geniusweb.inform.Inform; 22 import geniusweb.inform.OptIn; 23 import geniusweb.inform.Settings; 24 import geniusweb.inform.Voting; 25 import geniusweb.inform.YourTurn; 15 26 import geniusweb.issuevalue.Bid; 16 27 import geniusweb.party.Capabilities; 17 28 import geniusweb.party.DefaultParty; 18 import geniusweb.party.inform.ActionDone;19 import geniusweb.party.inform.Finished;20 import geniusweb.party.inform.Inform;21 import geniusweb.party.inform.Settings;22 import geniusweb.party.inform.YourTurn;23 29 import geniusweb.profile.PartialOrdering; 24 30 import geniusweb.profile.Profile; … … 33 39 * A simple party that places random bids and accepts when it receives an offer 34 40 * with sufficient utility. 41 * <h2>voting</h2> If the party receives a parameter "minPower", it will 42 * {@link Vote} using that minPower instead of the default value 2 if it 43 * receives a {@link Voting} request. 35 44 */ 36 45 public class RandomParty extends DefaultParty { … … 41 50 protected ProfileInterface profileint; 42 51 private Progress progress; 52 private Settings settings; 53 private Votes lastvotes; 43 54 44 55 public RandomParty() { … … 58 69 this.me = settings.getID(); 59 70 this.progress = settings.getProgress(); 71 this.settings = settings; 60 72 } else if (info instanceof ActionDone) { 61 73 Action otheract = ((ActionDone) info).getAction(); … … 64 76 } 65 77 } else if (info instanceof YourTurn) { 66 myTurn(); 67 if (progress instanceof ProgressRounds) { 68 progress = ((ProgressRounds) progress).advance(); 69 } 78 makeOffer(); 79 nextRound(); 70 80 } else if (info instanceof Finished) { 71 81 getReporter().log(Level.INFO, "Final ourcome:" + info); 82 } else if (info instanceof Voting) { 83 lastvotes = vote((Voting) info); 84 getConnection().send(lastvotes); 85 nextRound(); 86 } else if (info instanceof OptIn) { 87 // just repeat our last vote. 88 getConnection().send(lastvotes); 89 nextRound(); 72 90 } 73 91 } catch (Exception e) { … … 76 94 } 77 95 96 private void nextRound() { 97 if (progress instanceof ProgressRounds) { 98 progress = ((ProgressRounds) progress).advance(); 99 } 100 101 } 102 78 103 @Override 79 104 public Capabilities getCapabilities() { 80 return new Capabilities(new HashSet<>(Arrays.asList("SAOP"))); 105 return new Capabilities( 106 new HashSet<>(Arrays.asList("SAOP", "AMOP", "MOPAC"))); 81 107 } 82 108 … … 86 112 } 87 113 88 private void myTurn() throws IOException { 114 /** 115 * send our next offer 116 */ 117 private void makeOffer() throws IOException { 89 118 Action action; 90 if (isGood(lastReceivedBid)) { 119 String protocol = settings.getProtocol().getURI().getPath(); 120 if ((protocol.equals("SAOP") || protocol.equals("SHAOP")) 121 && isGood(lastReceivedBid)) { 91 122 action = new Accept(me, lastReceivedBid); 92 123 } else { … … 105 136 } 106 137 107 private boolean isGood(Bid bid) throws IOException { 138 /** 139 * @param bid the bid to check 140 * @return true iff bid is good for us. 141 */ 142 private boolean isGood(Bid bid) { 108 143 if (bid == null) 109 144 return false; 110 Profile profile = profileint.getProfile(); 111 if (profile instanceof UtilitySpace) { 145 Profile profile; 146 try { 147 profile = profileint.getProfile(); 148 } catch (IOException e) { 149 throw new IllegalStateException(e); 150 } 151 if (profile instanceof UtilitySpace) 112 152 return ((UtilitySpace) profile).getUtility(bid).doubleValue() > 0.6; 113 }114 153 if (profile instanceof PartialOrdering) { 115 154 return ((PartialOrdering) profile).isPreferredOrEqual(bid, 116 155 profile.getReservationBid()); 117 156 } 118 throw new IllegalArgumentException( 119 "Can not handle profile type " + profile.getClass()); 157 return false; 158 } 159 160 /** 161 * @param voting the {@link Voting} object containing the options 162 * 163 * @return our next Votes. 164 */ 165 private Votes vote(Voting voting) throws IOException { 166 Object val = settings.getParameters().get("minPowwer"); 167 Integer n = (val instanceof Integer) ? (Integer) val : 2; 168 for (Bid bid : voting.getBids()) { 169 System.out.println("Bid " + bid + "=" 170 + ((UtilitySpace) profileint.getProfile()).getUtility(bid)); 171 } 172 List<Vote> votes = voting.getBids().stream().distinct() 173 .filter(bid -> isGood(bid)).map(bid -> new Vote(me, bid, n)) 174 .collect(Collectors.toList()); 175 return new Votes(me, votes); 120 176 } 121 177 -
exampleparties/randomparty/src/test/java/geniusweb/exampleparties/randomparty/RandomPartyTest.java
r10 r21 16 16 import java.nio.file.Files; 17 17 import java.nio.file.Paths; 18 import java.util.Arrays; 19 import java.util.Collections; 18 20 import java.util.LinkedList; 19 21 import java.util.List; … … 32 34 import geniusweb.actions.Offer; 33 35 import geniusweb.actions.PartyId; 36 import geniusweb.actions.Votes; 34 37 import geniusweb.bidspace.AllBidsList; 35 38 import geniusweb.connection.ConnectionEnd; 39 import geniusweb.inform.ActionDone; 40 import geniusweb.inform.Finished; 41 import geniusweb.inform.Inform; 42 import geniusweb.inform.Settings; 43 import geniusweb.inform.Voting; 44 import geniusweb.inform.YourTurn; 36 45 import geniusweb.issuevalue.Bid; 37 46 import geniusweb.party.Capabilities; 38 import geniusweb.party.inform.ActionDone;39 import geniusweb.party.inform.Finished;40 import geniusweb.party.inform.Inform;41 import geniusweb.party.inform.Settings;42 import geniusweb.party.inform.YourTurn;43 47 import geniusweb.profile.Profile; 44 48 import geniusweb.profile.utilityspace.LinearAdditive; … … 53 57 public class RandomPartyTest { 54 58 59 private static final PartyId PARTY1 = new PartyId("party1"); 55 60 private static final String SAOP = "SAOP"; 56 61 private static final PartyId otherparty = new PartyId("other"); … … 60 65 private RandomParty party; 61 66 private final TestConnection connection = new TestConnection(); 62 private final ProtocolRef protocol = mock(ProtocolRef.class);67 private final ProtocolRef protocol = new ProtocolRef(SAOP); 63 68 private final ProgressRounds progress = mock(ProgressRounds.class); 64 69 private Settings settings; … … 70 75 IOException, URISyntaxException { 71 76 party = new RandomParty(); 72 settings = new Settings( new PartyId("party1"),77 settings = new Settings(PARTY1, 73 78 new ProfileRef(new URI("file:" + PROFILE)), protocol, progress, 74 79 parameters); … … 189 194 } 190 195 196 @Test 197 public void testVoting() { 198 party.connect(connection); 199 party.notifyChange(settings); 200 201 Bid bid = findGoodBid(); 202 party.notifyChange(new Voting(Arrays.asList(bid), 203 Collections.singletonMap(PARTY1, 1))); 204 assertEquals(1, connection.getActions().size()); 205 Action action = connection.getActions().get(0); 206 assertTrue(action instanceof Votes); 207 assertEquals(1, ((Votes) action).getVotes().size()); 208 assertEquals(bid, ((Votes) action).getVotes().get(0).getBid()); 209 } 210 191 211 } 192 212
Note:
See TracChangeset
for help on using the changeset viewer.