source: src/main/java/agents/anac/y2010/Southampton/SouthamptonAgent.java

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

Initial import : Genius 9.0.0

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