source: src/main/java/agents/anac/y2013/SlavaAgent/SlavaAgent.java

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

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 12.8 KB
Line 
1package agents.anac.y2013.SlavaAgent;
2
3import java.io.Serializable;
4import java.util.ArrayList;
5import java.util.HashMap;
6import java.util.List;
7import java.util.Map;
8import java.util.Random;
9
10import genius.core.Agent;
11import genius.core.Bid;
12import genius.core.NegotiationResult;
13import genius.core.actions.Accept;
14import genius.core.actions.Action;
15import genius.core.actions.ActionWithBid;
16import genius.core.actions.Offer;
17import genius.core.issue.ISSUETYPE;
18import genius.core.issue.Issue;
19import genius.core.issue.IssueDiscrete;
20import genius.core.issue.IssueInteger;
21import genius.core.issue.IssueReal;
22import genius.core.issue.ValueInteger;
23import genius.core.issue.ValueReal;
24
25/**
26 *
27 * @author Slava Bronfman, Moshe Hazoom & Guy Dubrovski
28 *
29 */
30public class SlavaAgent extends Agent {
31 // Last action of partner.
32 private Action actionOfPartner = null;
33
34 // Best bid offered by the opponent on this negotiation.
35 private Bid maxBidRecieved = null;
36
37 // Best bid for us.
38 private Bid maxBid;
39
40 // Stores all the best bids for us that their utility is more than
41 // UTILITY_THRESHOLD
42 private Map<Bid, Double> bestBidsMap;
43 private List<Bid> bidsAsArray;
44
45 // Best offer (the opponent offered). From disk, on previous negotiations.
46 private Bid bestOffer;
47
48 // Number of iterations in order to calculate the next random bid.
49 private final int MAX_ITERATIONS = 10000;
50
51 // Period of time we are doing exploration.
52 private final double EXPLORATION_RATE = 0.95;
53
54 // The threshold from which we offer bids to the opponent.
55 private final double UTILITY_THRESHOLD = 0.95;
56
57 // Don't accept if utility of the opponent is lower than these value.
58 private final double MIN_UTILITY_ACCEPT = 0.7;
59
60 // If the utility of the opponent is higher than these value, accept
61 // immediately.
62 private final double GOOD_ENOUGHT_UTILITY = 0.9;
63
64 /**
65 * Method calculates the bid that has a maximum value for us from the
66 * domain.
67 *
68 * @return
69 * @throws Exception
70 */
71 public Bid GetMaxBid() throws Exception {
72
73 Bid max = utilitySpace.getDomain().getRandomBid(null);
74 Bid tempBidding = utilitySpace.getDomain().getRandomBid(null);
75
76 for (int i = 0; i < utilitySpace.getDomain().getIssues().size(); i++) {
77
78 double maxUtil = 0;
79 int indexOfMaximumValue = 0;
80
81 Issue currIssue = utilitySpace.getDomain().getIssues().get(i);
82
83 if (currIssue.getType().equals(ISSUETYPE.INTEGER)) {
84 IssueInteger issueInteger = (IssueInteger) currIssue;
85 tempBidding = tempBidding.putValue(currIssue.getNumber(),
86 new ValueInteger(issueInteger.getUpperBound()));
87 maxUtil = utilitySpace.getUtility(tempBidding);
88 tempBidding = tempBidding.putValue(currIssue.getNumber(),
89 new ValueInteger(issueInteger.getLowerBound()));
90 double minUtil = utilitySpace.getUtility(tempBidding);
91 if (maxUtil > minUtil) {
92 max = max.putValue(currIssue.getNumber(),
93 new ValueInteger(issueInteger.getUpperBound()));
94 } else {
95 max = max.putValue(currIssue.getNumber(),
96 new ValueInteger(issueInteger.getLowerBound()));
97 }
98
99 } else if (currIssue.getType().equals(ISSUETYPE.REAL)) {
100 IssueReal issueReal = (IssueReal) currIssue;
101 tempBidding = tempBidding.putValue(currIssue.getNumber(),
102 new ValueReal(issueReal.getUpperBound()));
103 maxUtil = utilitySpace.getUtility(tempBidding);
104 tempBidding = tempBidding.putValue(currIssue.getNumber(),
105 new ValueReal(issueReal.getLowerBound()));
106 double minUtil = utilitySpace.getUtility(tempBidding);
107 if (maxUtil > minUtil) {
108 max = max.putValue(currIssue.getNumber(),
109 new ValueReal(issueReal.getUpperBound()));
110 } else {
111 max = max.putValue(currIssue.getNumber(),
112 new ValueReal(issueReal.getLowerBound()));
113 }
114
115 } else if (currIssue.getType().equals(ISSUETYPE.DISCRETE)) {
116 IssueDiscrete issueDiscrete = (IssueDiscrete) currIssue;
117 for (int j = 0; j < issueDiscrete.getNumberOfValues(); j++) {
118 tempBidding = tempBidding.putValue(currIssue.getNumber(),
119 issueDiscrete.getValue(j));
120 double tempUtil = utilitySpace.getUtility(tempBidding);
121 if (tempUtil > maxUtil) {
122 indexOfMaximumValue = j;
123 maxUtil = tempUtil;
124 }
125 }
126 max = max.putValue(currIssue.getNumber(),
127 issueDiscrete.getValue(indexOfMaximumValue));
128 }
129 }
130
131 return (max);
132 }
133
134 /**
135 * Initialize our parameters for the agent:
136 *
137 * maxBid - the bid that maximized our profit which we have found so far.
138 * bestOffer - the best bid for us that this opponent offered us from
139 * previous negotiations. bestBidsMap - stores all the best bids (those who
140 * has utility higher than UTILITY_THRESHOLD).
141 */
142 @Override
143 public void init() {
144 try {
145
146 // Initialize the best bid for use with a random bid.
147 this.maxBid = this.utilitySpace.getDomain().getRandomBid(null);
148
149 // Get the best bids for us that are more than a threshold.
150 this.bestBidsMap = this.getBestBidsForUs();
151 this.bidsAsArray = new ArrayList<Bid>(this.bestBidsMap.keySet());
152 } catch (Exception e) {
153 e.printStackTrace();
154 }
155
156 Serializable previousOffers = this.loadSessionData();
157
158 // There exists a previous offer from before
159 if (previousOffers != null) {
160 this.bestOffer = (Bid) previousOffers;
161 }
162 }
163
164 /**
165 * Version of the agent.
166 *
167 * @return
168 */
169 @Override
170 public String getVersion() {
171 return "1.0";
172 }
173
174 @Override
175 public String getName() {
176 return "Slava Agent";
177 }
178
179 /**
180 * Method saves the opponent's action. If it's an offer, it calculates its
181 * utility. If the utility is higher than all previous bids we have seen so
182 * far from this opponent, we are receiveMessage the variables's value.
183 *
184 * If it's the first time: If we played against this agent, we have a value
185 * on the variable "bestOffer" and we initialize the parameter
186 * "maxBidRevieved" to it (of course, only if its lower than it). Otherwise,
187 * we initialize "bestOffer" to be that bid.
188 */
189 @Override
190 public void ReceiveMessage(Action opponentAction) {
191 actionOfPartner = opponentAction;
192
193 // We are the first to offer a bid.
194 if (actionOfPartner == null) {
195 return;
196 }
197
198 // The opponent offerers a bid.
199 if (actionOfPartner instanceof Offer) {
200 Offer opponentOffer = (Offer) actionOfPartner;
201 Bid bid = opponentOffer.getBid();
202 try {
203 double utility = this.utilitySpace.getUtility(bid);
204
205 // Save the best bid that the opponent offered to us.
206 if (this.maxBidRecieved == null) { // The first offer.
207 this.maxBidRecieved = bid;
208
209 // Initialize the parameter we will later save to disk.
210 if (this.bestOffer == null) {
211 this.bestOffer = this.maxBidRecieved;
212 // Initialize the maximum bid received from the opponent
213 // (only if its lower than previous negotiations).
214 } else if (this.utilitySpace
215 .getUtility(this.maxBidRecieved) < this.utilitySpace
216 .getUtility(this.bestOffer)) {
217 this.maxBidRecieved = this.bestOffer;
218 }
219 } else if (utility > this.utilitySpace
220 .getUtility(this.maxBidRecieved)) { // Not
221 // the
222 // first
223 // offer.
224 this.maxBidRecieved = bid;
225 }
226 } catch (Exception e) {
227 e.printStackTrace();
228 }
229 }
230 }
231
232 /**
233 * Choose the next action to make for agent.
234 *
235 * If it's an offer that has utility more than GOOD_ENOUGHT_UTILITY, accept
236 * immediately.
237 *
238 * Otherwise, split to 2: Exploration and exploitation. In exploration part,
239 * receiveMessage the maximal bid for us (if found) and with probability of
240 * 0.5, offer it to the opponent and with probability of 0.5, offer to him a
241 * bid that is good enough for us randomly (one that has utility more than
242 * UTILITY_THRESHOLD). In exploitation part, offer always the best bid for
243 * us. Accept if the opponent offers a bid that has utility more than
244 * MIN_UTILITY_ACCEPT and has value higher or equals to all his previous
245 * offers. Otherwise, offer the best bid for us.
246 *
247 * On exceptions, offer the best bid for us.
248 */
249 @Override
250 public Action chooseAction() {
251 Action action = null;
252 try {
253 // We are the first to choose an action
254 if (actionOfPartner == null) {
255 this.maxBid = this.calculateNextBid();
256 action = new Offer(getAgentID(), this.maxBid);
257 }
258 if (actionOfPartner instanceof Offer) {
259 // If the user offered a bid that is good enough for us, accept.
260 Offer opponentOffer = (Offer) actionOfPartner;
261 Bid bid = opponentOffer.getBid();
262 double utility = this.utilitySpace.getUtility(bid);
263 if (utility >= this.GOOD_ENOUGHT_UTILITY) {
264 action = new Accept(this.getAgentID(),
265 ((ActionWithBid) actionOfPartner).getBid());
266 return (action);
267 }
268
269 // if the time is < EXPLORATION_RATE always offer a max bid
270 // until so
271 if (this.timeline.getTime() <= this.EXPLORATION_RATE) {
272 this.maxBid = this.calculateNextBid();
273
274 Random rand = new Random();
275
276 // In a probability of 0.5, offer the best bid
277 if (rand.nextDouble() <= 0.5) {
278 action = new Offer(getAgentID(), this.maxBid);
279 // In a probability of 0.5, offer one of the other best
280 // bids.
281 } else {
282 Bid nextBid = this.bidsAsArray
283 .get((rand.nextInt(this.bidsAsArray.size())));
284 action = new Offer(getAgentID(), nextBid);
285 }
286 } else // Exploitation
287 {
288 opponentOffer = (Offer) actionOfPartner;
289 bid = opponentOffer.getBid();
290 try {
291 utility = this.utilitySpace.getUtility(bid);
292
293 // If the user offered a bid that is more than the
294 // maximum so far, accept.
295 if (utility >= this.MIN_UTILITY_ACCEPT
296 && utility >= this.utilitySpace
297 .getUtility(this.maxBidRecieved)) {
298 action = new Accept(this.getAgentID(),
299 ((ActionWithBid) actionOfPartner).getBid());
300 } else { // Offerers the maximum bid for us.
301 action = new Offer(getAgentID(), this.maxBid);
302 }
303 } catch (Exception e) {
304 e.printStackTrace();
305 action = new Offer(getAgentID(), this.maxBid);
306 }
307 }
308 }
309 } catch (Exception e) {
310 e.printStackTrace();
311 this.maxBid = this.calculateNextBid();
312 action = new Offer(getAgentID(), this.maxBid);
313 }
314
315 // Sleep a little bit in order to see the results.
316 this.sleep(0.005);
317
318 return (action);
319 }
320
321 /**
322 * Method saves to disk the best offer bid of these domain.
323 */
324 @Override
325 public void endSession(NegotiationResult result) {
326 try {
327 // Update the parameter we save to disk
328 if (this.utilitySpace.getUtility(this.bestOffer) < this.utilitySpace
329 .getUtility(this.maxBidRecieved)) {
330 this.bestOffer = this.maxBidRecieved;
331 }
332
333 // Save it.
334 this.saveSessionData(this.bestOffer);
335 } catch (Exception e) {
336 e.printStackTrace();
337 }
338 }
339
340 /**
341 * Method calculates the next bid. It takes the maximum between the global
342 * maximum and a new randomized bid within a fixed number of iterations.
343 *
344 * @return
345 */
346 private Bid calculateNextBid() {
347 try {
348 Bid nextBid = this.generateBid();
349
350 // Return the maximum bid between the global maximum and the new
351 // randomized bid
352 if (this.utilitySpace.getUtility(nextBid) > this.utilitySpace
353 .getUtility(this.maxBid)) {
354 this.maxBid = nextBid;
355 }
356
357 return (this.maxBid);
358 } catch (Exception e) { // Return the max bid
359 e.printStackTrace();
360 return (this.maxBid);
361 }
362 }
363
364 /**
365 * Method iterates fix number of times and returns the maximum bid within
366 * all generated random bids.
367 *
368 * @return
369 * @throws Exception
370 */
371 private Bid generateBid() throws Exception {
372 double maxUtility = 0;
373 Bid maxBid = null;
374
375 for (int i = 0; i < this.MAX_ITERATIONS; i++) {
376 Bid bid = this.utilitySpace.getDomain().getRandomBid(null);
377 double utility = this.utilitySpace.getUtility(bid);
378
379 // If its value higher than the maximum so far.
380 if (utility > maxUtility) {
381 maxUtility = utility;
382 maxBid = bid;
383 }
384 }
385
386 return (maxBid);
387 }
388
389 /**
390 * Method stores and returns all the best bids for us that are more than a
391 * given threshold.
392 *
393 * @return
394 */
395 private Map<Bid, Double> getBestBidsForUs() {
396 Map<Bid, Double> bestBids = new HashMap<Bid, Double>();
397
398 for (int i = 0; i < this.MAX_ITERATIONS; i++) {
399 Bid randomBid = this.utilitySpace.getDomain().getRandomBid(null);
400 try {
401 double utility = this.utilitySpace.getUtility(randomBid);
402
403 // If the value of the random bid is more than the utility
404 // threshold, save it.
405 if (utility >= this.UTILITY_THRESHOLD) {
406 if (!bestBids.containsKey(randomBid)) {
407 bestBids.put(randomBid, utility);
408 }
409 }
410 } catch (Exception e) {
411 e.printStackTrace();
412 }
413 }
414
415 return (bestBids);
416 }
417
418 @Override
419 public String getDescription() {
420 return "ANAC2012";
421 }
422}
Note: See TracBrowser for help on using the repository browser.