source: boa/src/main/java/geniusweb/boa/BoaState.java@ 20

Last change on this file since 20 was 20, checked in by bart, 4 years ago

Added BOA support, some bug fixes

File size: 7.8 KB
Line 
1package geniusweb.boa;
2
3import java.lang.reflect.Parameter;
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.HashMap;
7import java.util.List;
8import java.util.Map;
9
10import geniusweb.actions.Action;
11import geniusweb.actions.PartyId;
12import geniusweb.boa.acceptancestrategy.AcceptanceStrategy;
13import geniusweb.boa.biddingstrategy.BiddingStrategy;
14import geniusweb.issuevalue.Bid;
15import geniusweb.opponentmodel.OpponentModel;
16import geniusweb.party.inform.Settings;
17import geniusweb.profile.Profile;
18import geniusweb.progress.Progress;
19import geniusweb.progress.ProgressRounds;
20import tudelft.utilities.logging.Reporter;
21
22/**
23 * Stores the current state of the negotiation for a {@link BoaParty}, including
24 * the state of the opponent models. immutable.
25 *
26 */
27public class BoaState {
28 private final Settings settings;
29 private final Profile profile;
30 private final Progress progress;
31 private final Class<? extends OpponentModel> opponentModelClass;
32 private final Map<PartyId, OpponentModel> opponentModels;
33 private final List<Action> actionHistory;
34 private final BiddingStrategy biddingStrategy;
35 private final AcceptanceStrategy acceptanceStrategy;
36 private final Reporter reporter;
37
38 /**
39 * Initial state. Update using with(settings).
40 */
41 public BoaState(Reporter reporter) {
42 this(null, null, null, null, null, null, Collections.emptyList(),
43 Collections.emptyMap(), reporter);
44 if (reporter == null) {
45 throw new NullPointerException("reporter must be not null");
46 }
47 }
48
49 /**
50 * Internal constructor. Use
51 * {@link #with(Settings, BiddingStrategy, AcceptanceStrategy, Class, Reporter)}
52 *
53 * @param settings the negoi {@link Settings}
54 * @param profile the {@link Profile} to be used
55 * @param progr the {@link Progress}
56 * @param bidstrat the {@link BiddingStrategy}
57 * @param acceptstrat the {@link AcceptanceStrategy}
58 * @param omClass the class of the {@link OpponentModel}
59 * @param actionHistory list of {@link Action}s that have been done in the
60 * negotiatino (by us and others), first action is the
61 * oldest.
62 * @param oppMods a map of {@link OpponentModel}s, one for each
63 * {@link PartyId} encountered in the negotiation
64 * @param reporter the {@link Reporter} that can be used to log
65 * messages. Should equal {@link BoaParty}'s reporter
66 */
67 private BoaState(Settings settings, Profile profile, Progress progr,
68 BiddingStrategy bidstrat, AcceptanceStrategy acceptstrat,
69 Class<? extends OpponentModel> omClass, List<Action> actionHistory,
70 Map<PartyId, OpponentModel> oppMods, Reporter reporter) {
71 this.settings = settings;
72 this.profile = profile;
73 this.progress = progr;
74 this.biddingStrategy = bidstrat;
75 this.acceptanceStrategy = acceptstrat;
76 this.opponentModelClass = omClass;
77 this.actionHistory = actionHistory;
78 this.opponentModels = oppMods;
79 this.reporter = reporter;
80 }
81
82 /**
83 * This should be the first update call used.
84 *
85 * @param settings the negoi {@link Settings}
86 * @param bidstrat the {@link BiddingStrategy}
87 * @param acceptstrat the {@link AcceptanceStrategy}
88 * @param omClass the class of the {@link OpponentModel}
89 * @param reporter the {@link Reporter} that can be used to log messages
90 * @return initialized BoaState
91 */
92 public BoaState with(Settings newsettings, BiddingStrategy bidstrat,
93 AcceptanceStrategy acceptstrat,
94 Class<? extends OpponentModel> omClass) {
95 if (settings != null)
96 throw new IllegalStateException("settings already set");
97 if (newsettings == null || omClass == null || bidstrat == null
98 || acceptstrat == null || reporter == null)
99 throw new NullPointerException(
100 "settings, reporter, bidstrat, acceptstrat, omClass and oModels must be not null");
101 return new BoaState(newsettings, null, newsettings.getProgress(),
102 bidstrat, acceptstrat, omClass, actionHistory,
103 new HashMap<PartyId, OpponentModel>(), reporter);
104 }
105
106 public BoaState with(Profile newprofile) {
107 return new BoaState(settings, newprofile, progress, biddingStrategy,
108 acceptanceStrategy, opponentModelClass, actionHistory,
109 opponentModels, reporter);
110
111 }
112
113 /**
114 *
115 * @return the settings for this negotiation, includes my {@link PartyId},
116 * {@link Parameter}, {@link Profile} etc.
117 */
118 public Settings getSettings() {
119 return settings;
120 }
121
122 /**
123 *
124 * @return the {@link Profile} as fetched from the profiles server, or null
125 * if no profile was received yet.
126 */
127 public Profile getProfile() {
128 return profile;
129 }
130
131 /**
132 *
133 * @param action the action that was done by some participant (possibly us).
134 * @return new BoaState that includes action.getBid as last bid (if action
135 * contains a bid), and with updated opponentmodel of that
136 * participant.
137 * @throws InstantiationFailedException if one of provided classes can not
138 * be instantiated.
139 */
140 public BoaState with(Action action) throws InstantiationFailedException {
141 List<Action> newactions = new ArrayList<Action>(actionHistory);
142 newactions.add(action);
143 Progress newprogress = progress;
144 Map<PartyId, OpponentModel> newmodels = opponentModels;
145
146 if (action.getActor().equals(settings.getID())) {
147 // ourside, just advance progress
148 if (progress instanceof ProgressRounds) {
149 // bit hacky, maybe we should wait
150 newprogress = ((ProgressRounds) progress).advance();
151 }
152 } else {
153 // not ourselves, update the OpponentModel
154 newmodels = updateModels(action);
155 }
156 return new BoaState(settings, profile, newprogress, biddingStrategy,
157 acceptanceStrategy, opponentModelClass, newactions, newmodels,
158 reporter);
159 }
160
161 /**
162 * @return the current {@link Progress}
163 */
164 public Progress getProgress() {
165 return progress;
166 }
167
168 /**
169 * @return list of actions done so far, oldest action first
170 */
171 public List<Action> getActionHistory() {
172 return Collections.unmodifiableList(actionHistory);
173 }
174
175 /**
176 *
177 * @param bid
178 * @return true iff {@link #acceptanceStrategy} says the bid is acceptable
179 */
180 public boolean isAcceptable(Bid bid) {
181 return acceptanceStrategy.isAcceptable(bid, this);
182 }
183
184 /**
185 *
186 * @return the next recommended action according to the
187 * {@link #biddingStrategy}
188 */
189 public Action getAction() {
190 return biddingStrategy.getAction(this);
191 }
192
193 /**
194 *
195 * @return the Reporter (same as the BoaParty's reporter)
196 */
197 public Reporter getReporter() {
198 return reporter;
199 }
200
201 /**
202 * @param action
203 * @return the map with the opponentmodels, with the model for the party
204 * that did the action updated.
205 * @throws InstantiationFailedException
206 */
207 private Map<PartyId, OpponentModel> updateModels(Action action)
208 throws InstantiationFailedException {
209 PartyId actor = action.getActor();
210 Map<PartyId, OpponentModel> updated = extendedOpponentModels(actor);
211 updated.put(actor, updated.get(actor).with(action, progress));
212 return updated;
213 }
214
215 /**
216 * Get the current opponent model, extended with newparty if there is no
217 * model yet for newparty.
218 *
219 * @param newparty a possibly new party
220 * @return a (possibly new) opponentModels map
221 * @throws InstantiationException
222 * @throws IllegalAccessException
223 * @throws InstantiationFailedException
224 */
225 private Map<PartyId, OpponentModel> extendedOpponentModels(PartyId newparty)
226 throws InstantiationFailedException {
227 // check if this is new party
228 if (opponentModels.containsKey(newparty))
229 return opponentModels;
230
231 Map<PartyId, OpponentModel> newmodels = new HashMap<>(opponentModels);
232 OpponentModel newmodel;
233 try {
234 newmodel = opponentModelClass.newInstance()
235 .with(profile.getDomain(), profile.getReservationBid());
236 } catch (Exception e) {
237 throw new InstantiationFailedException(
238 "Failed to instantiate " + opponentModelClass, e);
239 }
240 newmodels.put(newparty, newmodel);
241 return newmodels;
242 }
243
244}
Note: See TracBrowser for help on using the repository browser.