source: src/main/java/agents/anac/y2012/IAMhaggler2012/agents2011/SouthamptonAgent.java@ 340

Last change on this file since 340 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 10.1 KB
Line 
1package agents.anac.y2012.IAMhaggler2012.agents2011;
2
3import java.util.ArrayList;
4import java.util.Random;
5
6import agents.Jama.Matrix;
7import agents.anac.y2012.IAMhaggler2012.agents2011.southampton.utils.ActionCreator;
8import genius.core.Bid;
9import genius.core.BidIterator;
10import genius.core.actions.Accept;
11import genius.core.actions.Action;
12import genius.core.actions.EndNegotiation;
13import genius.core.actions.Offer;
14
15/**
16 * @author Colin Williams
17 *
18 */
19public abstract class SouthamptonAgent extends VersionIndependentAgent {
20
21 private static enum ActionType {
22 ACCEPT, BREAKOFF, OFFER, START;
23 }
24
25 /**
26 * Our maximum aspiration level.
27 */
28 protected double MAXIMUM_ASPIRATION = 0.9;
29
30 /**
31 * Gets the version number.
32 *
33 * @return the version number.
34 */
35 @Override
36 public String getVersion() {
37 return "2.0";
38 }
39
40 /**
41 * The message received from the opponent.
42 */
43 private Action messageOpponent;
44
45 /**
46 * My previous action.
47 */
48 protected Action myLastAction = null;
49
50 /**
51 * My previous bid.
52 */
53 protected Bid myLastBid = null;
54
55 /**
56 * The opponent's previous bid.
57 */
58 protected Bid opponentPreviousBid = null;
59
60 protected double acceptMultiplier = 1.02;
61
62 private boolean opponentIsHardHead;
63
64 private ArrayList<Bid> opponentBids;
65
66 private ArrayList<Bid> myPreviousBids;
67
68 protected boolean debug;
69
70 public final Action chooseAction(long ourTime, long opponentTime) {
71 setOurTime(ourTime);
72 setOpponentTime(opponentTime);
73 return chooseAction(false);
74 }
75
76 public final Action chooseAction() {
77 return chooseAction(true);
78 }
79
80 /*
81 * (non-Javadoc)
82 *
83 * @see negotiator.Agent#chooseAction()
84 */
85 private final Action chooseAction(boolean recordTimes) {
86 Action chosenAction = null;
87 Bid opponentBid = null;
88 log("Choose action");
89
90 try {
91 switch (getActionType(this.messageOpponent)) {
92 case OFFER:
93 opponentBid = ((Offer) this.messageOpponent).getBid();
94 chosenAction = handleOffer(opponentBid);
95 break;
96 case ACCEPT:
97 case BREAKOFF:
98 break;
99 default:
100 if (this.myLastAction == null) {
101 Bid b = proposeInitialBid();
102 if (b == null)
103 chosenAction = new EndNegotiation(getAgentID());
104 else
105 chosenAction = new Offer(getAgentID(), b);
106 } else {
107 chosenAction = this.myLastAction;
108 }
109 break;
110 }
111
112 } catch (Exception e) {
113 log("Exception in chooseAction:" + e.getMessage());
114 e.printStackTrace();
115 chosenAction = ActionCreator.createOffer(this, myLastBid);
116 }
117 myLastAction = chosenAction;
118 if (myLastAction instanceof Offer) {
119 Bid b = ((Offer) myLastAction).getBid();
120 myPreviousBids.add(b);
121 myLastBid = b;
122 }
123
124 return chosenAction;
125 }
126
127 /*
128 * (non-Javadoc)
129 *
130 * @see java.lang.Object#finalize()
131 */
132 @Override
133 protected void finalize() throws Throwable {
134 // displayFrame.dispose();
135 super.finalize();
136 }
137
138 /**
139 * Get the action type of a given action.
140 *
141 * @param action
142 * The action.
143 * @return The action type of the action.
144 */
145 private ActionType getActionType(Action action) {
146 ActionType actionType = ActionType.START;
147 if (action instanceof Offer)
148 actionType = ActionType.OFFER;
149 else if (action instanceof Accept)
150 actionType = ActionType.ACCEPT;
151 else if (action instanceof EndNegotiation)
152 actionType = ActionType.BREAKOFF;
153 return actionType;
154 }
155
156 /**
157 * Get the number of the agent.
158 *
159 * @return the number of the agent.
160 */
161 public int getAgentNo() {
162 if (this.getName() == "Agent A")
163 return 1;
164 if (this.getName() == "Agent B")
165 return 2;
166 return 0;
167 }
168
169 /**
170 * Get all of the bids in a utility range.
171 *
172 * @param lowerBound
173 * The minimum utility level of the bids.
174 * @param upperBound
175 * The maximum utility level of the bids.
176 * @return all of the bids in a utility range.
177 * @throws Exception
178 */
179 private ArrayList<Bid> getBidsInRange(double lowerBound, double upperBound)
180 throws Exception {
181 ArrayList<Bid> bidsInRange = new ArrayList<Bid>();
182 BidIterator iter = new BidIterator(utilitySpace.getDomain());
183 while (iter.hasNext()) {
184 Bid tmpBid = iter.next();
185 double util = 0;
186 try {
187 util = utilitySpace.getUtility(tmpBid);
188 if (util >= lowerBound && util <= upperBound)
189 bidsInRange.add(tmpBid);
190 } catch (Exception e) {
191 e.printStackTrace();
192 }
193 }
194
195 return bidsInRange;
196 }
197
198 /**
199 * Get a random bid in a given utility range.
200 *
201 * @param lowerBound
202 * The lower bound on utility.
203 * @param upperBound
204 * The upper bound on utility.
205 * @return a random bid in a given utility range.
206 * @throws Exception
207 */
208 protected Bid getRandomBidInRange(double lowerBound, double upperBound)
209 throws Exception {
210 ArrayList<Bid> bidsInRange = getBidsInRange(lowerBound, upperBound);
211
212 int index = (new Random()).nextInt(bidsInRange.size() - 1);
213
214 return bidsInRange.get(index);
215 }
216
217 /**
218 * Handle an opponent's offer.
219 *
220 * @param opponentBid
221 * The bid made by the opponent.
222 * @return the action that we should take in response to the opponent's
223 * offer.
224 * @throws Exception
225 */
226 private Action handleOffer(Bid opponentBid) throws Exception {
227 Action chosenAction = null;
228
229 if (myLastAction == null) {
230 // Special case to handle first action
231 Bid b = proposeInitialBid();
232 if (b == null) {
233 chosenAction = ActionCreator.createEndNegotiation(this);
234 } else {
235 myLastBid = b;
236 chosenAction = ActionCreator.createOffer(this, b);
237 }
238 } else if (utilitySpace.getUtility(opponentBid) * acceptMultiplier >= utilitySpace
239 .getUtility(myLastBid)) {
240 // Accept opponent's bid based on my previous bid.
241 chosenAction = ActionCreator.createAccept(this, opponentBid);
242 log("Opponent's bid is good enough compared to my last bid, ACCEPTED.");
243 opponentBids.add(opponentBid);
244 opponentPreviousBid = opponentBid;
245 } else if (utilitySpace.getUtility(opponentBid) * acceptMultiplier >= MAXIMUM_ASPIRATION) {
246 // Accept opponent's bid based on my previous bid.
247 chosenAction = ActionCreator.createAccept(this, opponentBid);
248 log("Utility of opponent bid: "
249 + utilitySpace.getUtility(opponentBid));
250 log("acceptMultiplier: " + acceptMultiplier);
251 log("MAXIMUM_ASPIRATION: " + MAXIMUM_ASPIRATION);
252 log("Opponent's bid is good enough compared to my maximum aspiration, ACCEPTED.");
253 opponentBids.add(opponentBid);
254 opponentPreviousBid = opponentBid;
255 } else {
256 Bid plannedBid = proposeNextBid(opponentBid);
257 if (plannedBid == null)
258 chosenAction = ActionCreator.createEndNegotiation(this);
259 else
260 chosenAction = ActionCreator.createOffer(this, plannedBid);
261
262 if (opponentBid == null)
263 logError("opponentBid is null");
264 if (plannedBid == null)
265 logError("plannedBid is null");
266
267 if (utilitySpace.getUtility(opponentBid) * acceptMultiplier >= utilitySpace
268 .getUtility(plannedBid)) {
269 // Accept opponent's bid based on my planned bid.
270 chosenAction = ActionCreator.createAccept(this, opponentBid);
271 log("Opponent's bid is good enough compared to my planned bid, ACCEPTED");
272 }
273 opponentBids.add(opponentBid);
274 opponentPreviousBid = opponentBid;
275 }
276
277 return chosenAction;
278 }
279
280 /*
281 * (non-Javadoc)
282 *
283 * @see negotiator.Agent#init()
284 */
285 public void init() {
286 messageOpponent = null;
287 myLastBid = null;
288 myLastAction = null;
289
290 log(this.utilitySpace.toString());
291
292 myPreviousBids = new ArrayList<Bid>();
293 opponentBids = new ArrayList<Bid>();
294 opponentIsHardHead = true;
295 }
296
297 /**
298 * Output a message, but only if debugging is turned on.
299 *
300 * @param message
301 * The message to output.
302 */
303 public final void log(String message) {
304 if (debug)
305 System.out.println(message);
306 }
307
308 /**
309 * Output a message, but only if debugging is turned on.
310 *
311 * @param message
312 * The message to output.
313 */
314 public final void logError(String message) {
315 if (debug)
316 System.err.println(message);
317 }
318
319 /**
320 * Output a message, but only if debugging is turned on.
321 *
322 * @param message
323 * The message to output.
324 */
325 public final void flushLog() {
326 if (debug)
327 System.out.flush();
328 }
329
330 /**
331 * Output a matrix, but only if debugging is turned on.
332 *
333 * @param matrix
334 * The matrix to output.
335 */
336 public final void log(Matrix matrix) {
337 if (debug)
338 matrix.print(7, 4);
339 }
340
341 /**
342 * Propose the initial bid.
343 *
344 * @return The action to be bid.
345 * @throws Exception
346 */
347 protected abstract Bid proposeInitialBid() throws Exception;
348
349 /**
350 * Propose the next bid.
351 *
352 * @param opponentBid
353 * The bid that has just been made by the opponent.
354 * @return The action to be bid.
355 * @throws Exception
356 */
357 protected abstract Bid proposeNextBid(Bid opponentBid) throws Exception;
358
359 public final void ReceiveMessage(Action opponentAction, long ourTime,
360 long opponentTime) {
361 setOurTime(ourTime);
362 setOpponentTime(opponentTime);
363 ReceiveMessage(opponentAction, true);
364 }
365
366 public final void ReceiveMessage(Action opponentAction) {
367 ReceiveMessage(opponentAction, true);
368 }
369
370 /*
371 * (non-Javadoc)
372 *
373 * @see negotiator.Agent#ReceiveMessage(negotiator.actions.Action)
374 */
375 private final void ReceiveMessage(Action opponentAction, boolean recordTimes) {
376
377 // Log the received opponentAction
378 if (opponentAction == null) {
379 log("Received (null) from opponent.");
380 } else {
381 log("--------------------------------------------------------------------------------");
382 log("Received " + opponentAction.toString() + " from opponent.");
383 if (opponentAction instanceof Offer) {
384 OfferReceived((Offer) opponentAction);
385 try {
386 log("It has a utility of "
387 + utilitySpace.getUtility(((Offer) opponentAction)
388 .getBid()));
389
390 if (opponentIsHardHead
391 && opponentBids.size() > 0
392 && Math.abs(utilitySpace.getUtility(opponentBids
393 .get(0))
394 - utilitySpace
395 .getUtility(((Offer) opponentAction)
396 .getBid())) > 0.02) {
397 log("Opponent of " + getName()
398 + " no longer considered to be hardheaded");
399 opponentIsHardHead = false;
400 }
401
402 } catch (Exception e) {
403 e.printStackTrace();
404 }
405 }
406 }
407
408 // Store the received opponentAction
409 messageOpponent = opponentAction;
410 }
411
412 public void OfferReceived(Offer opponentAction) {
413 }
414}
Note: See TracBrowser for help on using the repository browser.