source: src/main/java/genius/core/Agent.java@ 126

Last change on this file since 126 was 126, checked in by Aron Hammond, 6 years ago

Added function to calculate opposition to MultiLateralAnalysis.java

Moved code to add RLBOA listeners to RLBOAUtils is misc package

Added input for strategyParameters to SessionPanel (gui)

!! close SessionInfo after tournament; this caused /tmp/ to fill up with GeniusData files

Our own package:

  • Added opponents and strategies that are mentioned in the report
  • Change class hierarchy, agents can now extend from RLBOAagentBilateral to inherit RL functionality.
  • States extend from AbstractState
File size: 11.6 KB
Line 
1package genius.core;
2
3import java.io.Serializable;
4import java.util.Date;
5import java.util.HashMap;
6
7import genius.core.actions.Action;
8import genius.core.exceptions.Warning;
9import genius.core.parties.NegotiationParty;
10import genius.core.protocol.BilateralAtomicNegotiationSession;
11import genius.core.timeline.TimeLineInfo;
12import genius.core.timeline.Timeline;
13import genius.core.tournament.VariablesAndValues.AgentParamValue;
14import genius.core.tournament.VariablesAndValues.AgentParameterVariable;
15import genius.core.utility.AbstractUtilitySpace;
16import genius.core.utility.AdditiveUtilitySpace;
17import genius.core.utility.DataObjects;
18
19/**
20 * The old basic class for bilateral negotiation agents. Please use
21 * {@link NegotiationParty} and derived classes.
22 */
23@Deprecated
24public abstract class Agent extends AgentAdapter {
25 /**
26 *
27 */
28 private static final long serialVersionUID = -8831662293608224409L;
29 /**
30 * Technical remarks.
31 *
32 * It would have been much more flexible and cleaner if Agent were an
33 * interface. Unfortunately this was never done and we are now stuck in a
34 * historic, de facto 'standard' that we can not easily change. #915.
35 *
36 */
37
38 /** ID of the agent as assigned by the protocol. */
39 private AgentID agentID;
40 /** Name of the name as set by the method setName. */
41 private String fName = null;
42 /** Preference profile of the agent as assigned by the tournamentrunner. */
43 public AbstractUtilitySpace utilitySpace;
44 /**
45 * Date object specifying when the negotiation started.
46 *
47 * @deprecated Use timeline instead.
48 */
49 @Deprecated
50 public Date startTime;
51 /**
52 * Total time which an agent has to complete the negotiation.
53 *
54 * @deprecated Use timeline instead.
55 */
56 @Deprecated
57 public Integer totalTime; // total time to complete entire nego, in seconds.
58 /** Use timeline for everything time-related. */
59 public TimeLineInfo timeline;
60 /**
61 * A session can be repeated multiple times. This parameter specifies which
62 * match we are at in the range [0, totalMatches - 1].
63 */
64 public int sessionNr;
65 /**
66 * Amount of repetitions of this session, how many times this session is
67 * repeated in total.
68 */
69 public int sessionsTotal;
70 /**
71 * Reference to protocol which is set when experimental setup is enabled.
72 * Usually null, except in some experimental setups.
73 */
74 public BilateralAtomicNegotiationSession fNegotiation;
75 /**
76 * Parameters given to the agent which may be specified in the agent.
77 *
78 * @deprecated
79 */
80 @Deprecated
81 protected HashMap<AgentParameterVariable, AgentParamValue> parametervalues;
82 /**
83 * Parameters given to the agent which may be specified in the agent
84 * repository.
85 */
86 protected StrategyParameters strategyParameters;
87
88 /**
89 * Empty constructor used to initialize the agent. Later on internalInit is
90 * called to set all variables.
91 */
92 public Agent() {
93 this.strategyParameters = new StrategyParameters();
94 }
95
96 /**
97 * @return version of the agent.
98 */
99 public String getVersion() {
100 return "unknown";
101 };
102
103 /**
104 * This method is called by the protocol every time before starting a new
105 * session after the internalInit method is called. User can override this
106 * method.
107 */
108 public void init() {
109 }
110
111 /**
112 * This method is called by the protocol to initialize the agent with a new
113 * session information.
114 *
115 * @param startTimeP
116 * @param totalTimeP
117 * @param timeline
118 * keeping track of the time in the negotiation.
119 * @param us
120 * utility space of the agent for the session.
121 * @param params
122 * parameters of the agent.
123 */
124 public final void internalInit(int sessionNr, int sessionsTotal,
125 Date startTimeP, Integer totalTimeP, TimeLineInfo timeline,
126 AbstractUtilitySpace us,
127 HashMap<AgentParameterVariable, AgentParamValue> params,
128 AgentID id) {
129 startTime = startTimeP;
130 totalTime = totalTimeP;
131 this.timeline = timeline;
132 this.sessionNr = sessionNr;
133 this.sessionsTotal = sessionsTotal;
134 this.agentID = id;
135 utilitySpace = us;
136 parametervalues = params;
137 if (parametervalues != null && !parametervalues.isEmpty())
138 System.out.println("Agent " + getName()
139 + " initted with parameters " + parametervalues);
140 return;
141 }
142
143 /**
144 * informs you which action the opponent did
145 *
146 * @param opponentAction
147 * the opponent {@link Action}
148 */
149 public void ReceiveMessage(Action opponentAction) {
150 return;
151 }
152
153 /**
154 * this function is called after ReceiveMessage, with an Offer-action.
155 *
156 * @return (should return) the bid-action the agent wants to make.
157 */
158 public abstract Action chooseAction();
159
160 /**
161 * @return name of the agent.
162 */
163 public String getName() {
164 return fName;
165 }
166
167 /**
168 * @return a type of parameters used solely by the BayesianAgent.
169 * @deprecated
170 */
171 @Deprecated
172 public HashMap<AgentParameterVariable, AgentParamValue> getParameterValues() {
173 return parametervalues;
174 }
175
176 /**
177 * Sets the name of the agent to the given name.
178 *
179 * @param pName
180 * to which the agent's name must be set.
181 */
182 public final void setName(String pName) {
183 if (this.fName == null)
184 this.fName = pName;
185 return;
186 }
187
188 /**
189 * A convenience method to get the discounted utility of a bid. This method
190 * will take discount factors into account (if any), using the status of the
191 * current {@link #timeline}.
192 *
193 * @see AdditiveUtilitySpace
194 * @param bid
195 * of which we are interested in the utility.
196 * @return discounted utility of the given bid.
197 */
198 public double getUtility(Bid bid) {
199 return utilitySpace.getUtilityWithDiscount(bid, timeline);
200 }
201
202 /**
203 * Let the agent wait in case of a time-based protocol. Example:<br>
204 * sleep(0.1) will let the agent sleep for 10% of the negotiation time (as
205 * defined by the {@link Timeline}).
206 *
207 * @param fraction
208 * should be between 0 and 1.
209 */
210 public void sleep(double fraction) {
211 if (timeline.getType().equals(Timeline.Type.Time)) {
212 long sleep = (long) ((timeline.getTotalTime() * 1000) * fraction);
213 try {
214 Thread.sleep(sleep);
215 } catch (InterruptedException e) {
216 e.printStackTrace();
217 }
218 }
219 }
220
221 /**
222 * Method which informs an agent about the utility it received.
223 *
224 * @param dUtil
225 * discounted utility of previous session round.
226 */
227 public void endSession(NegotiationResult dUtil) {
228 }
229
230 /**
231 * @return ID of the agent as assigned by the protocol.
232 */
233 public AgentID getAgentID() {
234 return agentID;
235 }
236
237 public StrategyParameters getStrategyParameters() {
238 return strategyParameters;
239 }
240
241 /**
242 * Indicates what negotiation settings are supported by an agent, such as
243 * linear or non-linear utility spaces. The default is non-restrictive, but
244 * can be overriden if the agent can only be used in certain settings.
245 */
246 public SupportedNegotiationSetting getSupportedNegotiationSetting() {
247 return SupportedNegotiationSetting.getDefault();
248 }
249
250 /**
251 * Used to parse parameters presented in the agent repository. The
252 * particular implementation below parses parameters such as time=0.9;e=1.0.
253 *
254 * @param variables
255 * @throws Exception
256 */
257 public void parseStrategyParameters(String variables) throws Exception {
258 if (variables != null) {
259 String[] vars = variables.split(";");
260 for (String var : vars) {
261 String[] expression = var.split("=");
262 if (expression.length == 2) {
263 strategyParameters.addVariable(expression[0],
264 expression[1]);
265 } else {
266 throw new Exception(
267 "Expected variablename and result but got "
268 + expression.length + " elements. "
269 + "Correct in XML or overload the method.");
270 }
271 }
272 }
273 }
274
275 /**
276 * @return which session we are in. If a session is first started it is
277 * zero.
278 */
279 public int getSessionNumber() {
280 return sessionNr;
281 }
282
283 /**
284 * @return total sessions. How many times the session is repeated.
285 */
286 public int getSessionsTotal() {
287 return sessionsTotal;
288 }
289
290 /**
291 * Saves information (dataToSave) about the current session for future
292 * loading by the agent, when negotiating again with the specific preference
293 * profile referred by "filename".
294 *
295 * Important: 1) The dataToSave must implement {@link Serializable}
296 * interface. This is to make sure the data can be saved. 2) If the function
297 * is used more than once regarding the same preference profile, it will
298 * override the data saved from last session with the new Object
299 * "dataToSave".
300 *
301 * @param dataToSave
302 * the data regarding the last session that "agent" wants to
303 * save.
304 * @return true if dataToSave is successfully saved, false otherwise.
305 */
306 final protected boolean saveSessionData(Serializable dataToSave) {
307 String agentClassName = getUniqueIdentifier();
308 try { // utility may be null
309 String prefProfName = utilitySpace.getFileName();
310 return DataObjects.getInstance().saveData(dataToSave,
311 agentClassName, prefProfName);
312 } catch (Exception e) {
313 new Warning("Exception during saving data for agent "
314 + agentClassName + " : " + e.getMessage());
315 e.printStackTrace();
316 return false;
317 }
318 }
319
320 /**
321 * @return unique identifier of the Agent object.
322 */
323 protected String getUniqueIdentifier() {
324 return getClass().getName();
325 }
326
327 /**
328 * Loads the {@link Serializable} data for the agent. If the function
329 * "saveSessionData" wasn't used for this type of agent with the current
330 * preference profile of the agent - the data will be null. Otherwise, it
331 * will load the saved data of the agent for this specific preference
332 * profile.
333 *
334 * @return the {@link Serializable} data if the load is successful, null
335 * otherwise.
336 */
337 final protected Serializable loadSessionData() {
338
339 String agentClassName = getUniqueIdentifier();
340 try { // utility may be null
341 String prefProfName = utilitySpace.getFileName();
342 return DataObjects.getInstance().loadData(agentClassName,
343 prefProfName);
344 } catch (Exception e) {
345 new Warning("Exception during loading data for agent "
346 + agentClassName + " : " + e.getMessage());
347 e.printStackTrace();
348 return null;
349 }
350 }
351
352 @Override
353 public Agent getAgent() {
354 return this;
355 }
356
357 /***
358 * WARNING hashCode and equals are incomplete because we ignore
359 * {@link AgentAdapter} {@link BilateralAtomicNegotiationSession}
360 * {@link StrategyParameters} {@link TimeLineInfo}
361 * {@link AbstractUtilitySpace} because they did not implement
362 * equals/hashcode.
363 */
364 @Override
365 public int hashCode() {
366 final int prime = 31;
367 int result = 1;
368 result = prime * result + ((agentID == null) ? 0 : agentID.hashCode());
369 result = prime * result + ((fName == null) ? 0 : fName.hashCode());
370 result = prime * result + sessionNr;
371 result = prime * result + sessionsTotal;
372 result = prime * result
373 + ((startTime == null) ? 0 : startTime.hashCode());
374 result = prime * result
375 + ((totalTime == null) ? 0 : totalTime.hashCode());
376 return result;
377 }
378
379 @Override
380 public boolean equals(Object obj) {
381 if (this == obj)
382 return true;
383 if (obj == null)
384 return false;
385 if (getClass() != obj.getClass())
386 return false;
387 Agent other = (Agent) obj;
388 if (agentID == null) {
389 if (other.agentID != null)
390 return false;
391 } else if (!agentID.equals(other.agentID))
392 return false;
393 if (fName == null) {
394 if (other.fName != null)
395 return false;
396 } else if (!fName.equals(other.fName))
397 return false;
398 if (sessionNr != other.sessionNr)
399 return false;
400 if (sessionsTotal != other.sessionsTotal)
401 return false;
402 if (startTime == null) {
403 if (other.startTime != null)
404 return false;
405 } else if (!startTime.equals(other.startTime))
406 return false;
407 if (totalTime == null) {
408 if (other.totalTime != null)
409 return false;
410 } else if (!totalTime.equals(other.totalTime))
411 return false;
412 return true;
413 }
414
415}
Note: See TracBrowser for help on using the repository browser.