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.Offer;
|
---|
12 | import geniusweb.actions.PartyId;
|
---|
13 | import geniusweb.boa.acceptancestrategy.AcceptanceStrategy;
|
---|
14 | import geniusweb.boa.biddingstrategy.BiddingStrategy;
|
---|
15 | import geniusweb.inform.Settings;
|
---|
16 | import geniusweb.opponentmodel.OpponentModel;
|
---|
17 | import geniusweb.profile.Profile;
|
---|
18 | import geniusweb.profile.utilityspace.LinearAdditive;
|
---|
19 | import geniusweb.progress.Progress;
|
---|
20 | import geniusweb.progress.ProgressRounds;
|
---|
21 | import tudelft.utilities.logging.Reporter;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Stores the current state of the negotiation for a {@link BoaParty}, including
|
---|
25 | * the state of the opponent models. immutable.
|
---|
26 | *
|
---|
27 | */
|
---|
28 | public class BoaState {
|
---|
29 | private final Settings settings;
|
---|
30 | private final Profile profile;
|
---|
31 | private final Progress progress;
|
---|
32 | private final Class<? extends OpponentModel> opponentModelClass;
|
---|
33 | private final Map<PartyId, OpponentModel> opponentModels;
|
---|
34 | private final List<Action> actionHistory;
|
---|
35 | private final Reporter reporter;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Initial state. Update using with(settings).
|
---|
39 | *
|
---|
40 | * @param reporter the {@link Reporter} to use for logging
|
---|
41 | */
|
---|
42 | public BoaState(Reporter reporter) {
|
---|
43 | this(null, null, null, null, Collections.emptyList(),
|
---|
44 | Collections.emptyMap(), reporter);
|
---|
45 | if (reporter == null) {
|
---|
46 | throw new NullPointerException("reporter must be not null");
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Internal constructor. Use
|
---|
52 | * {@link #with(Settings, BiddingStrategy, AcceptanceStrategy, Class, Reporter)}
|
---|
53 | *
|
---|
54 | * @param settings the negoi {@link Settings}
|
---|
55 | * @param profile the {@link Profile} to be used. BOA only supports
|
---|
56 | * {@link LinearAdditive} so this can be assumed to be
|
---|
57 | * {@link LinearAdditive}.
|
---|
58 | * @param progr the {@link Progress}
|
---|
59 | * @param bidstrat the {@link BiddingStrategy}
|
---|
60 | * @param acceptstrat the {@link AcceptanceStrategy}
|
---|
61 | * @param omClass the class of the {@link OpponentModel}
|
---|
62 | * @param actionHistory list of {@link Action}s that have been done in the
|
---|
63 | * negotiatino (by us and others), first action is the
|
---|
64 | * oldest.
|
---|
65 | * @param oppMods a map of {@link OpponentModel}s, one for each
|
---|
66 | * {@link PartyId} encountered in the negotiation
|
---|
67 | * @param reporter the {@link Reporter} that can be used to log
|
---|
68 | * messages. Should equal {@link BoaParty}'s reporter
|
---|
69 | */
|
---|
70 | private BoaState(Settings settings, Profile profile, Progress progr,
|
---|
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.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 newsettings the negoi {@link Settings}
|
---|
86 | * @param omClass the class of the {@link OpponentModel}
|
---|
87 | * @return initialized BoaState
|
---|
88 | */
|
---|
89 | public BoaState with(Settings newsettings,
|
---|
90 | Class<? extends OpponentModel> omClass) {
|
---|
91 | if (settings != null)
|
---|
92 | throw new IllegalStateException("settings already set");
|
---|
93 | if (newsettings == null || omClass == null || reporter == null)
|
---|
94 | throw new NullPointerException(
|
---|
95 | "settings, reporter, omClass and oModels must be not null");
|
---|
96 | return new BoaState(newsettings, null, newsettings.getProgress(),
|
---|
97 | omClass, actionHistory, new HashMap<PartyId, OpponentModel>(),
|
---|
98 | reporter);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * @param newprofile a new profile to be used, replaces old profile.
|
---|
103 | * {@link LinearAdditive} profile (BOA does not support
|
---|
104 | * other types)
|
---|
105 | * @return new {@link BoaState}
|
---|
106 | */
|
---|
107 | public BoaState with(Profile newprofile) {
|
---|
108 | return new BoaState(settings, newprofile, progress, opponentModelClass,
|
---|
109 | actionHistory, 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. BOA only supports
|
---|
126 | * {@link LinearAdditive} so this can be assumed to be
|
---|
127 | * {@link LinearAdditive}.
|
---|
128 | */
|
---|
129 | public Profile getProfile() {
|
---|
130 | return profile;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | *
|
---|
135 | * @param action the action that was done by some participant (possibly us).
|
---|
136 | * @return new BoaState that includes action.getBid as last bid (if action
|
---|
137 | * contains a bid), and with updated opponentmodel of that
|
---|
138 | * participant.
|
---|
139 | * @throws InstantiationFailedException if one of provided classes can not
|
---|
140 | * be instantiated.
|
---|
141 | */
|
---|
142 | public BoaState with(Action action) throws InstantiationFailedException {
|
---|
143 | List<Action> newactions = new ArrayList<Action>(actionHistory);
|
---|
144 | newactions.add(action);
|
---|
145 | Progress newprogress = progress;
|
---|
146 | Map<PartyId, OpponentModel> newmodels = opponentModels;
|
---|
147 |
|
---|
148 | if (action.getActor().equals(settings.getID())) {
|
---|
149 | // ourside, just advance progress
|
---|
150 | if (progress instanceof ProgressRounds) {
|
---|
151 | // bit hacky, maybe we should wait
|
---|
152 | newprogress = ((ProgressRounds) progress).advance();
|
---|
153 | }
|
---|
154 | } else {
|
---|
155 | // not ourselves, update the OpponentModel
|
---|
156 | newmodels = updateModels(action);
|
---|
157 | }
|
---|
158 | return new BoaState(settings, profile, newprogress, opponentModelClass,
|
---|
159 | newactions, newmodels, 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 | * @return the Reporter (same as the BoaParty's reporter)
|
---|
179 | */
|
---|
180 | public Reporter getReporter() {
|
---|
181 | return reporter;
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * @param action
|
---|
186 | * @return the map with the opponentmodels, with the model for the party
|
---|
187 | * that did the action updated.
|
---|
188 | * @throws InstantiationFailedException
|
---|
189 | */
|
---|
190 | private Map<PartyId, OpponentModel> updateModels(Action action)
|
---|
191 | throws InstantiationFailedException {
|
---|
192 | PartyId actor = action.getActor();
|
---|
193 | Map<PartyId, OpponentModel> updated = extendedOpponentModels(actor);
|
---|
194 | updated.put(actor, updated.get(actor).with(action, progress));
|
---|
195 | return updated;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Get the current opponent model, extended with newparty if there is no
|
---|
200 | * model yet for newparty.
|
---|
201 | *
|
---|
202 | * @param newparty a possibly new party
|
---|
203 | * @return a (possibly new) opponentModels map
|
---|
204 | * @throws InstantiationException
|
---|
205 | * @throws IllegalAccessException
|
---|
206 | * @throws InstantiationFailedException
|
---|
207 | */
|
---|
208 | private Map<PartyId, OpponentModel> extendedOpponentModels(PartyId newparty)
|
---|
209 | throws InstantiationFailedException {
|
---|
210 | // check if this is new party
|
---|
211 | if (opponentModels.containsKey(newparty))
|
---|
212 | return opponentModels;
|
---|
213 |
|
---|
214 | Map<PartyId, OpponentModel> newmodels = new HashMap<>(opponentModels);
|
---|
215 | OpponentModel newmodel;
|
---|
216 | try {
|
---|
217 | newmodel = opponentModelClass.newInstance()
|
---|
218 | .with(settings.getParameters())
|
---|
219 | .with(profile.getDomain(), profile.getReservationBid());
|
---|
220 | } catch (Exception e) {
|
---|
221 | throw new InstantiationFailedException(
|
---|
222 | "Failed to instantiate " + opponentModelClass, e);
|
---|
223 | }
|
---|
224 | newmodels.put(newparty, newmodel);
|
---|
225 | return newmodels;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | *
|
---|
230 | * @return last received Offer, or null if no offer was done.
|
---|
231 | */
|
---|
232 | public Offer getLastReceivedOffer() {
|
---|
233 | for (int i = actionHistory.size() - 1; i >= 0; i--) {
|
---|
234 | Action act = actionHistory.get(i);
|
---|
235 | if (act instanceof Offer)
|
---|
236 | return (Offer) act;
|
---|
237 | }
|
---|
238 | return null;
|
---|
239 | }
|
---|
240 |
|
---|
241 | }
|
---|