source: src/main/java/agents/anac/y2015/AgentNeo/Groupn.java@ 1

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

Initial import : Genius 9.0.0

File size: 12.9 KB
Line 
1package agents.anac.y2015.AgentNeo;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import genius.core.AgentID;
7import genius.core.Bid;
8import genius.core.BidIterator;
9import genius.core.actions.Accept;
10import genius.core.actions.Action;
11import genius.core.actions.EndNegotiation;
12import genius.core.actions.Offer;
13import genius.core.parties.AbstractNegotiationParty;
14import genius.core.parties.NegotiationInfo;
15
16/**
17 * This is your negotiation party.
18 */
19public class Groupn extends AbstractNegotiationParty {
20
21 private double MaxUtility;
22 private double utilitythreshold;
23 private double discountFactor;
24 private BidOptions bidOptions;
25 private Action ActionOfOpponent = null;
26 private OwnBids OwnBids;
27 private Bid Maximum_Utility_Bid;// the bid with the maximum utility over the
28 // utility space.
29 private double reservationValue;
30 private Object IDOfOpponent = null;
31 private ArrayList<ArrayList<Bid>> bidsBetweenUtility;
32 private double minimumUtilityThreshold;
33 private double maximumOfBid;
34
35 @Override
36 public void init(NegotiationInfo info) {
37
38 super.init(info);
39
40 bidOptions = new BidOptions();
41 this.bidOptions.initializeDataStructures(utilitySpace.getDomain());
42 bidsBetweenUtility = new ArrayList<ArrayList<Bid>>();
43
44 this.discountFactor = 1;
45 if (utilitySpace.getDiscountFactor() <= 1D
46 && utilitySpace.getDiscountFactor() > 0D) {
47 this.discountFactor = utilitySpace.getDiscountFactor();
48 }
49 OwnBids = new OwnBids();
50 try {
51 this.Maximum_Utility_Bid = utilitySpace.getMaxUtilityBid();
52 this.utilitythreshold = utilitySpace
53 .getUtility(Maximum_Utility_Bid); // initial
54 // utility
55 // threshold
56 } catch (Exception e) {
57 throw new RuntimeException("init failed:" + e, e);
58 }
59 this.MaxUtility = this.utilitythreshold;
60 this.reservationValue = 0;
61 if (utilitySpace.getReservationValue() > 0) {
62 this.reservationValue = utilitySpace.getReservationValue();
63 }
64 // this.chooseUtilityThreshold();
65 this.calculateBidsBetweenUtility();
66 maximumOfBid = this.utilitySpace.getDomain().getNumberOfPossibleBids();
67
68 }
69
70 /**
71 * Each round this method gets called and ask you to accept or offer. The
72 * first party in the first round is a bit different, it can only propose an
73 * offer.
74 *
75 * @param validActions
76 * Either a list containing both accept and offer or only offer.
77 * @return The chosen action.
78 */
79 @Override
80 public Action chooseAction(List<Class<? extends Action>> validActions) {
81 Action action = null;
82 try {
83 Bid bid = null;
84
85 if (OwnBids.numOfBidsProposed() == 0) {
86 bid = this.Maximum_Utility_Bid;
87 System.out.println("Valid Actions == null");
88 action = new Offer(getPartyId(), bid);
89 } else if (OwnBids.numOfBidsProposed() <= 7) {
90
91 System.out.println("Number of Proposed bids < 7");
92 bid = this.Maximum_Utility_Bid;
93 System.out.println(bid);
94 action = new Offer(getPartyId(), bid);
95
96 } else {
97 System.out.println("After 7 Bids");
98 bid = AgentOffer();
99 System.out.println(bid);
100
101 if (ActionOfOpponent instanceof Offer) {
102
103 System.out.println(((Offer) ActionOfOpponent).getBid()
104 + "Opponent's Offer");
105 Boolean IsAccept = AcceptOffer(
106 ((Offer) ActionOfOpponent).getBid(), bid);
107 System.out.println(IsAccept);
108 Boolean IsTerminate = TerminateNegotiation(bid);
109 System.out.println(IsTerminate);
110
111 if (IsAccept && !IsTerminate) {
112 action = new Accept(getPartyId(),
113 ((Offer) ActionOfOpponent).getBid());
114 System.out.println("accept the offer");
115 } else if (IsAccept && IsTerminate) {
116 if (this.utilitySpace
117 .getUtility(((Offer) ActionOfOpponent)
118 .getBid()) > this.reservationValue) {
119 action = new Accept(getPartyId(),
120 ((Offer) ActionOfOpponent).getBid());
121 System.out.println("we accept the offer RANDOMLY");
122 } else {
123 bid = this.Maximum_Utility_Bid;
124 action = new Offer(getPartyId(), bid);
125 }
126 } else {
127
128 action = new Offer(getPartyId(), bid);
129 }
130
131 } else if (ActionOfOpponent instanceof Accept) {
132 System.out.println("When there is an Accept");
133 bid = AgentOffer();
134 Bid Oppbid = BidOptions.getLastBid();
135
136 Boolean IsAccept = AcceptOffer(Oppbid, bid);
137 Boolean IsTerminate = TerminateNegotiation(Oppbid);
138
139 if (IsAccept && !IsTerminate) { // True False
140 action = new Accept(getPartyId(),
141 ((Offer) ActionOfOpponent).getBid());
142 System.out.println("accept the offer");
143 } else if (IsAccept && IsTerminate) { // True True
144 if (this.utilitySpace
145 .getUtility(((Offer) ActionOfOpponent)
146 .getBid()) > this.reservationValue) {
147 action = new Accept(getPartyId(),
148 ((Offer) ActionOfOpponent).getBid());
149 System.out.println("we accept the offer RANDOMLY");
150 } else { // False False
151 bid = this.Maximum_Utility_Bid;
152 action = new Offer(getPartyId(), bid);
153 }
154 }
155 action = new Offer(getPartyId(), bid);
156
157 }
158 }
159
160 this.OwnBids.addBid(bid, utilitySpace);
161 System.out.println(bid);
162 System.out.println(action);
163
164 } catch (Exception e) {
165 System.out.println("Exception in ChooseAction:" + e.getMessage());
166 action = new EndNegotiation(getPartyId());
167 }
168 return action;
169
170 }
171
172 private boolean AcceptOffer(Bid opponentBid, Bid ownBid) {
173
174 System.out.println("Accept Proposed Offer()");
175
176 double currentUtility = 0; // opponent's proposed bid
177 double nextRoundUtility = 0; // Agent's next bid
178 double maximumThresholdUtility = 0; // maximum bid based on threshold
179 // this.concedeToOpponent = false;
180
181 try {
182 currentUtility = this.utilitySpace.getUtility(opponentBid);
183 maximumThresholdUtility = this.MaxUtility;
184 } catch (Exception e) {
185 System.out.println(e.getMessage() + "Exception in AcceptOffer #1");
186 }
187 try {
188 nextRoundUtility = this.utilitySpace.getUtility(ownBid);
189 } catch (Exception e) {
190 System.out.println(e.getMessage() + "Exception in AcceptOffer #2");
191 }
192
193 if (currentUtility >= this.utilitythreshold
194 || currentUtility >= nextRoundUtility) {
195 return true; // SCENARIO 1 Opponent's proposed utility surpasses
196 // threshold or Agent's next bid
197 } else {
198 // SCENARIO 2
199 // if the opponent's proposed utility with discount is larger than
200 // the predicted
201 // maximum utility with discount, then accept it. It is the most the
202 // Agent could ever achieve.
203 double predictMaxUtility = maximumThresholdUtility
204 * this.discountFactor;
205 System.out.println(predictMaxUtility);
206 System.out.println("SCENARIO 2a");
207 double bestMaxUtility = this.utilitySpace.getUtilityWithDiscount(
208 bidOptions.getBestBidInHistory(), timeline);
209 // What the Agent could possibly achieve given what has bids have
210 // been proposed
211 System.out.println(bestMaxUtility);
212 System.out.println("SCENARIO 2b");
213 if (bestMaxUtility > predictMaxUtility) {
214 try {
215 // if the current offer is approximately as good as the best
216 // one in the history, then accept it.
217 if (utilitySpace.getUtility(opponentBid) >= utilitySpace
218 .getUtility(bidOptions.getBestBidInHistory())
219 - 0.1 /* 0.01 */) {
220 return true;
221 } else {
222
223 return false;
224 }
225 } catch (Exception e) {
226 System.out.println("exception in AcceptOffer #3");
227 return true;
228 }
229 // in comparison with the threshold value which varies with time
230 } else if (bestMaxUtility > this.utilitythreshold
231 * Math.pow(this.discountFactor, timeline.getTime())) {
232 try {
233 // if the current offer is approximately as good as the best
234 // one in the history, then accept it.
235 if (utilitySpace.getUtility(opponentBid) >= utilitySpace
236 .getUtility(bidOptions.getBestBidInHistory())
237 - 0.1 /* 0.01 */) {
238 return true;
239 } else {
240 // this.concedeToOpponent = true;
241 return false;
242 }
243 } catch (Exception e) {
244 System.out.println("exception in AcceptOffer #4");
245 return true;
246 }
247 } else {
248 return false; // The proposed offer is entirely unacceptable.
249 }
250 }
251
252 }
253
254 private Bid AgentOffer() {
255
256 int x = 0;
257 Bid bid = null;
258 System.out.println("AgentOffer()");
259 int value = bidOptions.SimilartoOpponent(x);
260 System.out.println("SimilartoOppo()");
261 double concession1 = 0.0;
262 double concession2 = 0.0;
263
264 if (OwnBids.numOfBidsProposed() < 55) {
265 concession1 = 0.15;
266 concession2 = 0.02;
267 } else {
268 concession1 = 0.25;
269 concession2 = 0.10;
270 }
271
272 List<Bid> candidateBids = this.getBidsInRange(
273 this.MaxUtility - concession1, this.MaxUtility - concession2);
274
275 System.out.println(value);
276 switch (value) {
277
278 case 1:
279 bid = bidOptions.ChooseBid1(candidateBids,
280 this.utilitySpace.getDomain());
281 break;
282 case 2:
283 bid = bidOptions.ChooseBid2(candidateBids,
284 this.utilitySpace.getDomain());
285 break;
286 case 3:
287 bid = bidOptions.ChooseBid3(candidateBids,
288 this.utilitySpace.getDomain());
289 break;
290 }
291 return bid;
292 }
293
294 private void calculateBidsBetweenUtility() {
295 BidIterator myBidIterator = new BidIterator(
296 this.utilitySpace.getDomain());
297
298 try {
299 // double maximumUtility =
300 // utilitySpace.getUtility(utilitySpace.getMaxUtilityBid());
301 double maximumUtility = this.MaxUtility;
302 double minUtility = this.minimumUtilityThreshold;
303 int maximumRounds = (int) ((maximumUtility - minUtility) / 0.01);
304 // initalization for each arraylist storing the bids between each
305 // range
306 for (int i = 0; i < maximumRounds; i++) {
307 ArrayList<Bid> BidList = new ArrayList<Bid>();
308 // BidList.add(this.bid_maximum_utility);
309 this.bidsBetweenUtility.add(BidList);
310 }
311 this.bidsBetweenUtility.get(maximumRounds - 1)
312 .add(this.Maximum_Utility_Bid);
313 // note that here we may need to use some trick to reduce the
314 // computation cost (to be checked later);
315 // add those bids in each range into the corresponding arraylist
316 // int limits = 0;
317 if (this.maximumOfBid < 50000) {
318 while (myBidIterator.hasNext()) {
319 Bid b = myBidIterator.next();
320 for (int i = 0; i < maximumRounds; i++) {
321 if (utilitySpace.getUtility(b) <= (i + 1) * 0.01
322 + minUtility
323 && utilitySpace.getUtility(b) >= i * 0.01
324 + minUtility) {
325 this.bidsBetweenUtility.get(i).add(b);
326 break;
327 }
328 }
329 // limits++;
330 }
331 }
332 } catch (Exception e) {
333 System.out.println("Exception in calculateBidsBetweenUtility()");
334 e.printStackTrace();
335 }
336 }
337
338 private List<Bid> getBidsInRange(double lowerBound, double upperBound) {
339 List<Bid> bidsInRange = new ArrayList<Bid>();
340 try {
341 int range = (int) ((upperBound - this.minimumUtilityThreshold)
342 / 0.01);
343 System.out.println(range);
344
345 int initial = (int) ((lowerBound - this.minimumUtilityThreshold)
346 / 0.01);
347 System.out.println(initial);
348
349 for (int i = initial; i < range; i++) {
350 bidsInRange.addAll(this.bidsBetweenUtility.get(i));
351 // System.out.println(this.bidsBetweenUtility.get(i));
352 }
353 if (bidsInRange.isEmpty()) {
354 bidsInRange.add(this.Maximum_Utility_Bid);
355 }
356 } catch (Exception e) {
357 System.out.println("Exception in getBidsInRange");
358 e.printStackTrace();
359 }
360 return bidsInRange;
361 }
362
363 private boolean TerminateNegotiation(Bid ownBid) {
364
365 System.out.println("TerminateNegotiation()");
366
367 double currentUtility = 0;
368 double nextRoundUtility = 0;
369 double maximumUtility = 0;
370 // this.concedeToOpponent = false;
371 try {
372 currentUtility = this.reservationValue;
373 nextRoundUtility = this.utilitySpace.getUtility(ownBid);
374 maximumUtility = this.MaxUtility;
375 } catch (Exception e) {
376 System.out.println(e.getMessage()
377 + "Exception in method TerminateNegotiation part 1");
378 }
379
380 if (currentUtility >= this.utilitythreshold
381 || currentUtility >= nextRoundUtility) {
382 return true;
383 } else {
384 double predictMaxUtility = maximumUtility * this.discountFactor;
385 double currentMaxUtility = this.utilitySpace
386 .getReservationValueWithDiscount(timeline);
387 if (currentMaxUtility > predictMaxUtility /*
388 * && timeline.getTime()
389 * > this
390 * .concedeToDiscountingFactor
391 */) {
392 return true; // since the reservation value is going to be the
393 // most the Agent will get
394 } else {
395 return false;
396 }
397 }
398 }
399
400 /**
401 * All offers proposed by the other parties will be received as a message.
402 * You can use this information to your advantage, for example to predict
403 * their utility.
404 *
405 * @param sender
406 * The party that did the action.
407 * @param action
408 * The action that party did.
409 */
410 @Override
411 public void receiveMessage(AgentID sender, Action action) {
412 super.receiveMessage(sender, action);
413 // Here you can listen to other parties' messages
414 this.ActionOfOpponent = action;
415 this.IDOfOpponent = sender;
416
417 if (ActionOfOpponent instanceof Offer) { // if Agent is not the first to
418 // act, update opponent's
419 // offer first
420 this.bidOptions.addOpponentBid(((Offer) ActionOfOpponent).getBid(),
421 utilitySpace.getDomain(), this.utilitySpace, IDOfOpponent);
422
423 }
424
425 }
426
427 @Override
428 public String getDescription() {
429 return "ANAC2015";
430 }
431
432}
Note: See TracBrowser for help on using the repository browser.