Changeset 4 for exampleparties/timedependentparty/src/main
- Timestamp:
- 09/18/19 10:00:22 (5 years ago)
- Location:
- exampleparties/timedependentparty/src/main/java/geniusweb/exampleparties/timedependentparty
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
exampleparties/timedependentparty/src/main/java/geniusweb/exampleparties/timedependentparty/TimeDependentParty.java
r3 r4 3 3 import java.io.IOException; 4 4 import java.math.BigDecimal; 5 import java.math.BigInteger; 5 6 import java.net.URI; 6 7 import java.net.URISyntaxException; … … 14 15 import geniusweb.actions.Offer; 15 16 import geniusweb.actions.PartyId; 16 import geniusweb.bidspace.AllBidsList;17 import geniusweb.bidspace.BidsWithUtility;18 17 import geniusweb.issuevalue.Bid; 19 18 import geniusweb.party.Capabilities; … … 24 23 import geniusweb.party.inform.Settings; 25 24 import geniusweb.party.inform.YourTurn; 25 import geniusweb.profile.Profile; 26 26 import geniusweb.profile.utilityspace.LinearAdditiveUtilitySpace; 27 import geniusweb.profile.utilityspace.UtilitySpace;28 27 import geniusweb.profileconnection.ProfileConnectionFactory; 29 28 import geniusweb.profileconnection.ProfileInterface; … … 31 30 import geniusweb.progress.ProgressRounds; 32 31 import geniusweb.references.ProtocolRef; 32 import tudelft.utilities.immutablelist.ImmutableList; 33 33 import tudelft.utilities.logging.Reporter; 34 34 … … 41 41 public abstract class TimeDependentParty extends DefaultParty { 42 42 43 private static final BigDecimal DEC0001 = new BigDecimal("0.0001"); 44 private static final BigDecimal DEC100 = new BigDecimal("100"); 43 45 private ProfileInterface profileint; 46 private LinearAdditiveUtilitySpace utilspace = null; // last received space 44 47 private PartyId me; 45 48 private Progress progress; 46 private Double minUtil = null, maxUtil = null;47 49 private Bid lastReceivedBid = null; 48 private double tolerance;50 private ExtendedUtilSpace extendedspace; 49 51 50 52 public TimeDependentParty() { … … 77 79 this.me = settings.getID(); 78 80 this.progress = settings.getProgress(); 79 computeMinMax();80 81 } else if (info instanceof ActionDone) { 81 82 Action otheract = ((ActionDone) info).getAction(); … … 117 118 118 119 private void myTurn() throws IOException { 119 Action myAction = null;120 updateUtilSpace(); 120 121 Bid bid = makeBid(); 121 UtilitySpace utilspace = getUtilSpace();122 122 123 if (lastReceivedBid != null && utilspace.getUtility(lastReceivedBid) 124 .compareTo(utilspace.getUtility(bid)) >= 0) { 123 Action myAction; 124 if (bid == null || lastReceivedBid != null 125 && utilspace.getUtility(lastReceivedBid) 126 .compareTo(utilspace.getUtility(bid)) >= 0) { 127 // if bid==null we failed to suggest next bid. 125 128 myAction = new Accept(me, lastReceivedBid); 126 129 } else { 127 myAction = new Offer(me, makeBid());130 myAction = new Offer(me, bid); 128 131 } 129 132 getConnection().send(myAction); … … 131 134 } 132 135 133 private UtilitySpace getUtilSpace() { 134 return (LinearAdditiveUtilitySpace) profileint.getProfile(); 136 private LinearAdditiveUtilitySpace updateUtilSpace() { 137 Profile newutilspace = profileint.getProfile(); 138 if (!newutilspace.equals(utilspace)) { 139 utilspace = (LinearAdditiveUtilitySpace) newutilspace; 140 extendedspace = new ExtendedUtilSpace(utilspace); 141 } 142 return utilspace; 135 143 } 136 144 137 145 /** 138 * Computes the fields minutil and maxUtil if not yet computed. minutil is 139 * the minimum utility of any bid, but at least the utility of the 140 * reservation bid. maxUtil is the maximum util of any bid. 141 * <p> 142 * TODO this is simplistic, very expensive method and may cause us to run 143 * out of time on large domains. 144 * <p> 145 * Can be called only after profileint has been set. 146 * @return next possible bid with current target utility, or null if no such 147 * bid. 146 148 */ 147 private void computeMinMax() {148 if (this.maxUtil != null && this.minUtil != null)149 return;150 this.minUtil = 1d;151 this.maxUtil = 0d;152 UtilitySpace utilspace = getUtilSpace();153 AllBidsList allbidslist = new AllBidsList(154 profileint.getProfile().getDomain());155 for (Bid bid : allbidslist) {156 double util = utilspace.getUtility(bid).doubleValue();157 if (util < minUtil)158 minUtil = util;159 if (util > maxUtil)160 maxUtil = util;161 }162 163 Bid rvbid = utilspace.getReservationBid();164 if (rvbid != null) {165 double rv = utilspace.getUtility(rvbid).doubleValue();166 if (rv > minUtil)167 minUtil = rv;168 }169 tolerance = 100 / allbidslist.size().longValue();170 tolerance = Math.max(tolerance, 0.1);171 }172 173 149 private Bid makeBid() { 174 150 double time = progress.get(System.currentTimeMillis()); 175 double utilityGoal = utilityGoal(time, getE()); 176 BidsWithUtility options = new BidsWithUtility( 177 (LinearAdditiveUtilitySpace) profileint.getProfile(), 178 BigDecimal.valueOf(utilityGoal), 179 BigDecimal.valueOf(utilityGoal + tolerance)); 151 BigDecimal utilityGoal = utilityGoal(time, getE()); 152 ImmutableList<Bid> options = extendedspace.getBids(utilityGoal); 153 if (options.size() == BigInteger.ZERO) 154 return null; 180 155 // pick a random one. 181 156 return options.get(new Random().nextInt(options.size().intValue())); … … 190 165 * @return the utility goal for this time and e value 191 166 */ 192 private double utilityGoal(double t, double e) { 167 private BigDecimal utilityGoal(double t, double e) { 168 BigDecimal minUtil = extendedspace.getMin(); 169 BigDecimal maxUtil = extendedspace.getMax(); 170 193 171 double ft = 0; 194 172 if (e != 0) 195 173 ft = Math.pow(t, 1 / e); 196 // we subtract 0.000001 to correct possibly small round-up errors 197 return minUtil + (maxUtil - minUtil) * (1 - ft) - 0.000001; 174 // we subtract epsilon to correct possibly small round-up errors 175 return new BigDecimal(minUtil.doubleValue() 176 + (maxUtil.doubleValue() - minUtil.doubleValue()) * (1 - ft)) 177 .min(maxUtil).max(minUtil); 198 178 } 199 179 }
Note:
See TracChangeset
for help on using the changeset viewer.