Changeset 18 for opponentmodel/src
- Timestamp:
- 06/11/20 16:34:40 (4 years ago)
- Location:
- opponentmodel/src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
opponentmodel/src/main/java/geniusweb/opponentmodel/FrequencyOpponentModel.java
r14 r18 6 6 import java.util.stream.Collectors; 7 7 8 import geniusweb.actions.Action; 9 import geniusweb.actions.Offer; 8 10 import geniusweb.issuevalue.Bid; 9 11 import geniusweb.issuevalue.Domain; … … 12 14 import geniusweb.profile.utilityspace.NumberValueSetUtilities; 13 15 import geniusweb.profile.utilityspace.UtilitySpace; 16 import geniusweb.progress.Progress; 14 17 15 18 /** … … 23 26 * 24 27 */ 25 public class FrequencyOpponentModel implements OpponentModel, UtilitySpace{28 public class FrequencyOpponentModel implements UtilitySpace, OpponentModel { 26 29 27 30 private static final int DECIMALS = 4; // accuracy of our computations. … … 32 35 33 36 public FrequencyOpponentModel(Domain domain) { 34 // map with empth hashmap for each issue. 37 /* 38 * simply reset the whole model and make a new one... maybe this can be 39 * done smarter for minor changes? map with empth hashmap for each 40 * issue. 41 */ 35 42 this(domain, 36 43 domain.getIssues().stream().collect( … … 39 46 } 40 47 48 /** 49 * internal constructor. Assumes the freqs keyset is equal to the available 50 * issues. 51 * 52 * @param domain the domain 53 * @param freqs the observed frequencies for all issue values. This map is 54 * assumed to be a fresh private-access only copy. 55 * @param total the total number of bids contained in the freqs map. This 56 * must be equal to the sum of the Integer values in the 57 * {@link #bidFrequencies} for each issue (this is not 58 * checked). 59 */ 60 private FrequencyOpponentModel(Domain domain, 61 Map<String, Map<Value, Integer>> freqs, BigDecimal total) { 62 if (domain == null) { 63 throw new NullPointerException("domain=null"); 64 } 65 this.domain = domain; 66 this.bidFrequencies = freqs; 67 this.totalBids = total; 68 } 69 41 70 @Override 42 71 public BigDecimal getUtility(Bid bid) { … … 86 115 } 87 116 88 @Override 89 public FrequencyOpponentModel update(Bid bid) { 117 /** 118 * {@inheritDoc} 119 * 120 * <h1>change info</h1> This replaces update(Bid). Now you pass the entire 121 * action and the progress instead of just the bid. 122 */ 123 @Override 124 public FrequencyOpponentModel with(Action action, Progress progress) { 125 if (!(action instanceof Offer)) 126 return this; 127 128 Bid bid = ((Offer) action).getBid(); 90 129 String err = domain.isComplete(bid); 91 130 if (err != null) { … … 108 147 109 148 /** 110 * internal constructor. Assumes the freqs keyset is equal to the available111 * issues.112 *113 * @param domain the domain114 * @param freqs the observed frequencies for all issue values. This map is115 * assumed to be a fresh private-access only copy.116 * @param total the total number of bids contained in the freqs map. This117 * must be equal to the sum of the Integer values in the118 * {@link #bidFrequencies} for each issue (this is not119 * checked).120 */121 private FrequencyOpponentModel(Domain domain,122 Map<String, Map<Value, Integer>> freqs, BigDecimal total) {123 if (domain == null) {124 throw new NullPointerException("domain=null");125 }126 this.domain = domain;127 this.bidFrequencies = freqs;128 this.totalBids = total;129 }130 131 /**132 149 * 133 150 * @param freqs … … 141 158 } 142 159 return map; 160 } 161 162 @Override 163 public Bid getReservationBid() { 164 // don't throw but we really have no clue. 165 return new Bid(new HashMap<>()); 143 166 } 144 167 … … 188 211 } 189 212 190 @Override191 public Bid getReservationBid() {192 throw new UnsupportedOperationException();193 }194 195 213 } -
opponentmodel/src/main/java/geniusweb/opponentmodel/OpponentModel.java
r14 r18 1 1 package geniusweb.opponentmodel; 2 2 3 import geniusweb.issuevalue.Bid; 3 import geniusweb.actions.Action; 4 import geniusweb.issuevalue.Domain; 4 5 import geniusweb.profile.Profile; 5 6 import geniusweb.profile.utilityspace.UtilitySpace; 7 import geniusweb.progress.Progress; 6 8 7 9 /** 8 10 * An opponentmodel estimates a {@link UtilitySpace} from received opponent 9 * bids. 11 * actions. 12 * <h1>Requirement</h1> A OpponentModel must have a constructor that takes the 13 * Domain as argument. unfortunately this can not be enforced in a java 14 * interface 10 15 */ 11 16 public interface OpponentModel extends Profile { 17 12 18 /** 13 * 14 * @param bid the next bid that was received 15 * @return a new {@link OpponentModel} that takes the next bid into account. 19 * Update this with a new action that was done by the opponent that this 20 * model is modeling. {@link #with(Domain)} must be called before calling 21 * this. 22 * 23 * @param action the new incoming action. 24 * @param progress the current progress of the negotiation. Calls to this 25 * must be done with increasing progress. 26 * @return the updated {@link OpponentModel} 16 27 */ 17 OpponentModel update(Bid bid);28 OpponentModel with(Action action, Progress progress); 18 29 19 30 } -
opponentmodel/src/test/java/geniusweb/profile/opponentmodel/FrequencyOppModelTest.java
r14 r18 3 3 import static org.junit.Assert.assertEquals; 4 4 import static org.junit.Assert.assertTrue; 5 import static org.mockito.Mockito.mock; 5 6 6 7 import java.math.BigDecimal; … … 14 15 import org.junit.Test; 15 16 17 import geniusweb.actions.Offer; 18 import geniusweb.actions.PartyId; 16 19 import geniusweb.issuevalue.Bid; 17 20 import geniusweb.issuevalue.DiscreteValue; … … 23 26 import geniusweb.issuevalue.ValueSet; 24 27 import geniusweb.opponentmodel.FrequencyOpponentModel; 28 import geniusweb.progress.Progress; 25 29 import tudelft.utilities.junit.GeneralTests; 26 30 … … 32 36 private static final DiscreteValue I1V2 = new DiscreteValue("i1v2"); 33 37 private static final DiscreteValue I1V2b = new DiscreteValue("i1v2b"); 38 private static final Progress progress = mock(Progress.class); 39 private static final PartyId other = new PartyId("other"); 34 40 35 41 private static Domain domain, domain2, domain3; … … 85 91 oppModel2 = new FrequencyOpponentModel(domain2); 86 92 oppModel3 = new FrequencyOpponentModel(domain3); 87 oppModel4 = (FrequencyOpponentModel) oppModel3.update(bid1);93 oppModel4 = oppModel3.with(new Offer(other, bid1), progress); 88 94 89 95 } … … 106 112 107 113 @Test(expected = NullPointerException.class) 108 public void smoke NullTest() {114 public void smokeTestNull() { 109 115 new FrequencyOpponentModel(null); 110 116 } … … 124 130 @Test 125 131 public void testUpdate() { 126 FrequencyOpponentModel oppModel = oppModel1.update(bid1); 132 FrequencyOpponentModel oppModel = oppModel1.with(new Offer(other, bid1), 133 progress); 127 134 assertTrue(BigDecimal.ONE.compareTo(oppModel.getUtility(bid1)) == 0); 128 135 assertTrue(BigDecimal.ZERO.compareTo(oppModel.getUtility(bid3)) == 0); … … 135 142 // bid1 and bid2 both want I1V1. They differ on the number value. 136 143 // bid3 wants I1V2 but does have the number value from bid2 137 FrequencyOpponentModel oppModel = oppModel1.update(bid1).update(bid2); 144 FrequencyOpponentModel oppModel = oppModel1 145 .with(new Offer(other, bid1), progress) 146 .with(new Offer(other, bid2), progress); 138 147 assertTrue(new BigDecimal("0.75") 139 148 .compareTo(oppModel.getUtility(bid1)) == 0);
Note:
See TracChangeset
for help on using the changeset viewer.