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