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