Changeset 21 for exampleparties/randomparty/src/main
- Timestamp:
- 09/22/20 08:52:04 (4 years ago)
- File:
-
- 1 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
Note:
See TracChangeset
for help on using the changeset viewer.