source: src/main/java/negotiator/boaframework/acceptanceconditions/anac2012/AC_AgentMR.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: 15.7 KB
Line 
1package negotiator.boaframework.acceptanceconditions.anac2012;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.Comparator;
6import java.util.HashMap;
7import java.util.List;
8import java.util.Map;
9import java.util.Random;
10
11import genius.core.Bid;
12import genius.core.bidding.BidDetails;
13import genius.core.boaframework.AcceptanceStrategy;
14import genius.core.boaframework.Actions;
15import genius.core.boaframework.NegotiationSession;
16import genius.core.boaframework.OfferingStrategy;
17import genius.core.boaframework.OpponentModel;
18import genius.core.issue.Issue;
19import genius.core.issue.IssueDiscrete;
20import genius.core.issue.IssueInteger;
21import genius.core.issue.IssueReal;
22import genius.core.issue.Value;
23import genius.core.issue.ValueDiscrete;
24import genius.core.issue.ValueInteger;
25import genius.core.issue.ValueReal;
26import genius.core.utility.AdditiveUtilitySpace;
27import negotiator.boaframework.sharedagentstate.anac2012.AgentMRSAS;
28
29/**
30 * This is the Acceptance condition of AgentMR Due to the connectedness of the
31 * original code if another Bidding strategy is being used, the Bidding Strategy
32 * of AgentMR will be run in the background
33 *
34 * @author Alexander Dirkzwager
35 */
36public class AC_AgentMR extends AcceptanceStrategy {
37
38 private boolean EQUIVALENCE_TEST = true;
39 private Random random100;
40 private ArrayList<Double> observationUtility = new ArrayList<Double>();
41 private HashMap<Bid, Double> bidTables = new HashMap<Bid, Double>();
42 private static boolean firstOffer;
43 private static boolean forecastTime = true;
44 private static boolean discountFactor;
45 private static double offereMaxUtility;
46 private int currentBidNumber = 0;
47 private AdditiveUtilitySpace utilitySpace;
48 private boolean alreadyDone = false;
49 private Actions nextAction;
50
51 private boolean activeHelper = false;
52 private static final double MINIMUM_ACCEPT_P = 0.965;
53
54 public AC_AgentMR() {
55 }
56
57 public AC_AgentMR(NegotiationSession negoSession, OfferingStrategy strat) throws Exception {
58 initializeAgent(negoSession, strat);
59 }
60
61 @Override
62 public void init(NegotiationSession negoSession, OfferingStrategy strat, OpponentModel opponentModel,
63 Map<String, Double> parameters) throws Exception {
64 initializeAgent(negoSession, strat);
65 }
66
67 public void initializeAgent(NegotiationSession negotiationSession, OfferingStrategy os) throws Exception {
68 this.negotiationSession = negotiationSession;
69 this.offeringStrategy = os;
70
71 if (offeringStrategy.getHelper() == null || (!offeringStrategy.getHelper().getName().equals("AgentMR"))) {
72 helper = new AgentMRSAS(negotiationSession);
73 activeHelper = true;
74 } else {
75 helper = (AgentMRSAS) offeringStrategy.getHelper();
76 }
77
78 if (discountFactor) {
79 ((AgentMRSAS) helper).setSigmoidGain(-3.0);
80 ((AgentMRSAS) helper).setPercent(0.55);
81 } else {
82 ((AgentMRSAS) helper).setSigmoidGain(-5.0);
83 ((AgentMRSAS) helper).setPercent(0.70);
84 }
85
86 if (activeHelper) {
87 firstOffer = true;
88 try {
89 utilitySpace = (AdditiveUtilitySpace) negotiationSession.getUtilitySpace();
90 getDiscountFactor();
91 getReservationFactor();
92
93 Bid b = negotiationSession.getMaxBidinDomain().getBid();
94 bidTables.put(b, getUtility(b));
95 ((AgentMRSAS) helper).getBidRunk().add(b);
96
97 if (EQUIVALENCE_TEST) {
98 random100 = new Random(100);
99 } else {
100 random100 = new Random();
101 }
102 } catch (Exception e) {
103 e.printStackTrace();
104 }
105 }
106 }
107
108 @Override
109 public Actions determineAcceptability() {
110 if (activeHelper)
111 nextAction = activeDetermineAcceptability();
112 else
113 nextAction = regularDetermineAcceptability();
114 return nextAction;
115 }
116
117 private Actions activeDetermineAcceptability() {
118 nextAction = Actions.Reject;
119 if (negotiationSession.getOpponentBidHistory().getHistory().isEmpty()) {
120 if (!alreadyDone) {
121 ((AgentMRSAS) helper).updateMinimumBidUtility(0);
122 alreadyDone = true;
123 }
124 return Actions.Reject;
125 }
126
127 try {
128 BidDetails partnerBid;
129 if (firstOffer) {
130 partnerBid = negotiationSession.getOpponentBidHistory().getHistory().get(0);
131 } else {
132 partnerBid = negotiationSession.getOpponentBidHistory().getLastBidDetails();
133 }
134
135 // get current time
136 double time = negotiationSession.getTime();
137 // System.out.println("test: " +
138 // negotiationSession.getDiscountedUtility(negotiationSession.getOpponentBidHistory().getFirstBidDetails().getBid(),
139 // negotiationSession.getOpponentBidHistory().getFirstBidDetails().getTime()));
140 double offeredutil;
141 if (discountFactor) {
142 offeredutil = getUtility(partnerBid.getBid())
143 * (1 / Math.pow(negotiationSession.getUtilitySpace().getDiscountFactor(), time));
144 } else {
145 offeredutil = getUtility(partnerBid.getBid());
146
147 }
148 if (firstOffer) {
149 offereMaxUtility = offeredutil;
150 ((AgentMRSAS) helper).setFirstOffereUtility(offeredutil);
151
152 observationUtility.add(offeredutil); // addObservation
153 if (offeredutil > 0.5) {
154 ((AgentMRSAS) helper).setP(0.90);
155 } else {
156 ((AgentMRSAS) helper).setP(0.80);
157 }
158 firstOffer = !firstOffer;
159 }
160 ((AgentMRSAS) helper).updateMinimumBidUtility(time);
161
162 /*
163 * if (partnerBid.equals(previousPartnerBid)) { if (currentBidNumber
164 * > 0 && 0.5 > 0.65) { currentBidNumber--; //
165 * 確率的�Bidを移動 } }
166 */
167 if (offeredutil > offereMaxUtility) {
168 offereMaxUtility = offeredutil;
169 // addObservation
170 observationUtility.add(offeredutil);
171 if ((time > 0.5) && !discountFactor) {
172 newupdateSigmoidFunction();
173 }
174 }
175
176 // forecasting
177 if ((time > 0.5) && forecastTime) {
178 updateSigmoidFunction();
179 forecastTime = !forecastTime;
180 }
181
182 double P = Paccept(offeredutil, time);
183
184 if ((P > MINIMUM_ACCEPT_P)
185 || (negotiationSession.getOpponentBidHistory().getLastBidDetails()
186 .getMyUndiscountedUtil() > ((AgentMRSAS) helper).getMinimumBidUtility())
187 || ((AgentMRSAS) helper).getBidRunk()
188 .contains(negotiationSession.getOpponentBidHistory().getLastBid())) {
189 nextAction = Actions.Accept;
190 } else {
191 if (offeredutil > ((AgentMRSAS) helper).getMinimumOffereDutil()) {
192 HashMap<Bid, Double> getBids = getBidTable(1);
193 if (getBids.size() >= 1) {
194 // BidTable
195 currentBidNumber = 0;
196 ((AgentMRSAS) helper).getBidRunk().clear();
197 bidTables = getBids;
198 sortBid(getBids); // Sort BidTable
199 } else {
200 getBids = getBidTable(2);
201 if (getBids.size() >= 1) {
202 sortBid(getBids); // Sort BidTable
203 Bid maxBid = getMaxBidUtility(getBids);
204 currentBidNumber = ((AgentMRSAS) helper).getBidRunk().indexOf(maxBid);
205 }
206 }
207 if (currentBidNumber + 1 < ((AgentMRSAS) helper).getBidRunk().size()) {
208 // System.out.println("Decoupled
209 // currentBidNumberChange1");
210
211 currentBidNumber++;
212 }
213
214 } else {
215 HashMap<Bid, Double> getBids = getBidTable(2);
216
217 if (getBids.size() >= 1) {
218 sortBid(getBids); // Sort BidTable
219 Bid maxBid = getMaxBidUtility(getBids);
220 currentBidNumber = ((AgentMRSAS) helper).getBidRunk().indexOf(maxBid);
221 }
222
223 if (currentBidNumber + 1 < ((AgentMRSAS) helper).getBidRunk().size()) {
224 currentBidNumber++;
225 } else {
226 currentBidNumber = 0;
227 }
228 }
229 }
230 } catch (Exception e) {
231 e.printStackTrace();
232 }
233 return nextAction;
234 }
235
236 private Actions regularDetermineAcceptability() {
237
238 if (!(negotiationSession.getOpponentBidHistory().isEmpty())) {
239 double offeredutil = negotiationSession.getOpponentBidHistory().getFirstBidDetails()
240 .getMyUndiscountedUtil();
241 ((AgentMRSAS) helper).setFirstOffereUtility(offeredutil);
242 }
243 double P;
244 try {
245 if (negotiationSession.getOpponentBidHistory().isEmpty()) {
246 return Actions.Reject;
247 }
248 P = Paccept(negotiationSession.getOpponentBidHistory().getLastBidDetails().getMyUndiscountedUtil(),
249 negotiationSession.getTime());
250
251 if (activeHelper) {
252 ((AgentMRSAS) helper).updateMinimumBidUtility(negotiationSession.getTime());
253 }
254
255 // System.out.println("Decoupled condition1: " + (P >
256 // MINIMUM_ACCEPT_P));
257 // System.out.println("Decoupled condition2: " +
258 // (negotiationSession.getOpponentBidHistory().getLastBidDetails().getMyUndiscountedUtil()
259 // > ((AgentMRSAS) helper).getMinimumBidUtility()));
260 // System.out.println("Decoupled condition3: " +((AgentMRSAS)
261 // helper).getBidRunk().contains(negotiationSession.getOpponentBidHistory().getLastBid()));
262 // Accept
263 if ((P > MINIMUM_ACCEPT_P)
264 || (negotiationSession.getOpponentBidHistory().getLastBidDetails()
265 .getMyUndiscountedUtil() > ((AgentMRSAS) helper).getMinimumBidUtility())
266 || ((AgentMRSAS) helper).getBidRunk()
267 .contains(negotiationSession.getOpponentBidHistory().getLastBid())) {
268 return Actions.Accept;
269 }
270 } catch (Exception e) {
271 e.printStackTrace();
272 }
273
274 return Actions.Reject;
275 }
276
277 private double getUtility(Bid bid) {
278 return negotiationSession.getUtilitySpace().getUtilityWithDiscount(bid, negotiationSession.getTimeline());
279 }
280
281 private void getReservationFactor() {
282 if (utilitySpace.getReservationValue() != null) {
283 ((AgentMRSAS) helper).setReservation(utilitySpace.getReservationValue());
284 }
285 }
286
287 private void getDiscountFactor() {
288 discountFactor = utilitySpace.isDiscounted();
289 }
290
291 /**
292 * This function determines the accept probability for an offer. At t=0 it
293 * will prefer high-utility offers. As t gets closer to 1, it will accept
294 * lower utility offers with increasing probability. it will never accept
295 * offers with utility 0.
296 *
297 * @param u
298 * is the utility
299 * @param t
300 * is the time as fraction of the total available time (t=0 at
301 * start, and t=1 at end time)
302 * @return the probability of an accept at time t
303 * @throws Exception
304 * if you use wrong values for u or t.
305 *
306 */
307 double Paccept(double u, double t1) throws Exception {
308 double t = t1 * t1 * t1; // steeper increase when deadline approaches.
309 if (u < 0 || u > 1.05)
310 throw new Exception("utility " + u + " outside [0,1]");
311 // normalization may be slightly off, therefore we have a broad boundary
312 // up to 1.05
313 if (t < 0 || t > 1)
314 throw new Exception("time " + t + " outside [0,1]");
315 if (u > 1.)
316 u = 1;
317 if (t == 0.5)
318 return u;
319 return (u - 2. * u * t + 2. * (-1. + t + Math.sqrt(sq(-1. + t) + u * (-1. + 2 * t)))) / (-1. + 2 * t);
320 }
321
322 double sq(double x) {
323 return x * x;
324 }
325
326 private void newupdateSigmoidFunction() {
327 double latestObservation = observationUtility.get(observationUtility.size() - 1);
328 double concessionPercent = Math.abs(latestObservation - ((AgentMRSAS) helper).getFirstOffereUtility())
329 / (1.0 - ((AgentMRSAS) helper).getFirstOffereUtility());
330 double modPercent = Math
331 .abs(((AgentMRSAS) helper).getMinimumOffereDutil() - ((AgentMRSAS) helper).getFirstOffereUtility())
332 / (1.0 - ((AgentMRSAS) helper).getFirstOffereUtility());
333
334 if (modPercent < concessionPercent) {
335 ((AgentMRSAS) helper).setPercent(concessionPercent);
336 }
337 }
338
339 private void updateSigmoidFunction() {
340 int observationSize = observationUtility.size();
341 double latestObservation = observationUtility.get(observationSize - 1); // 最新�相手BidUtil
342 double concessionPercent = Math.abs(latestObservation - ((AgentMRSAS) helper).getFirstOffereUtility())
343 / (1.0 - ((AgentMRSAS) helper).getFirstOffereUtility());
344
345 if (discountFactor) {
346 if ((concessionPercent < 0.20) || (observationSize < 3)) {
347 ((AgentMRSAS) helper).setPercent(0.35);
348 ((AgentMRSAS) helper).setSigmoidGain(-2);
349 } else {
350 ((AgentMRSAS) helper).setPercent(0.45);
351 }
352 } else {
353 if ((concessionPercent < 0.20) || (observationSize < 3)) {
354 ((AgentMRSAS) helper).setPercent(0.50);
355 ((AgentMRSAS) helper).setSigmoidGain(-4);
356 } else if (concessionPercent > 0.60) {
357 ((AgentMRSAS) helper).setPercent(0.80);
358 ((AgentMRSAS) helper).setSigmoidGain(-6);
359 } else {
360 ((AgentMRSAS) helper).setPercent(0.60);
361 }
362 }
363 }
364
365 /**
366 * @param maxBid
367 * @return
368 * @throws Exception
369 */
370 private HashMap<Bid, Double> getBidTable(int flag) throws Exception {
371 HashMap<Bid, Double> getBids = new HashMap<Bid, Double>();
372
373 // Random randomnr = new Random();
374 List<Issue> issues = utilitySpace.getDomain().getIssues();
375 Bid standardBid = null;
376
377 for (Issue lIssue : issues) {
378 switch (lIssue.getType()) {
379 case DISCRETE:
380 IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
381 for (ValueDiscrete value : lIssueDiscrete.getValues()) {
382 if (flag == 0) {
383 standardBid = utilitySpace.getMaxUtilityBid(); // 自分�最高値
384 } else if (flag == 1) {
385 standardBid = negotiationSession.getOpponentBidHistory().getLastBid();
386 } else {
387 standardBid = ((AgentMRSAS) helper).getBidRunk().get(currentBidNumber);
388 }
389 standardBid = clone(standardBid);
390 standardBid = standardBid.putValue(lIssue.getNumber(), value);
391 double utility = getUtility(standardBid);
392 // System.out.println("Decoupled minimumBidUtility: " +
393 // ((AgentMRSAS) helper).getMinimumBidUtility());
394 if ((utility > ((AgentMRSAS) helper).getMinimumBidUtility())
395 && (!((AgentMRSAS) helper).getBidRunk().contains(standardBid))) {
396 getBids.put(standardBid, utility);
397 }
398 }
399 break;
400 case REAL:
401 IssueReal lIssueReal = (IssueReal) lIssue;
402 int optionInd = random100.nextInt(lIssueReal.getNumberOfDiscretizationSteps() - 1);
403 Value pValue = new ValueReal(
404 lIssueReal.getLowerBound() + (lIssueReal.getUpperBound() - lIssueReal.getLowerBound())
405 * (double) (optionInd) / (double) (lIssueReal.getNumberOfDiscretizationSteps()));
406 standardBid = standardBid.putValue(lIssueReal.getNumber(), pValue);
407 double utility = getUtility(standardBid);
408 getBids.put(standardBid, utility);
409 break;
410 case INTEGER:
411 IssueInteger lIssueInteger = (IssueInteger) lIssue;
412 int optionIndex2 = lIssueInteger.getLowerBound()
413 + random100.nextInt(lIssueInteger.getUpperBound() - lIssueInteger.getLowerBound());
414 Value pValue2 = new ValueInteger(optionIndex2);
415 standardBid = standardBid.putValue(lIssueInteger.getNumber(), pValue2);
416 double utility2 = getUtility(standardBid);
417 getBids.put(standardBid, utility2);
418 break;
419 default:
420 throw new Exception("issue type " + lIssue.getType() + " not supported by AgentMR");
421 }
422 }
423
424 return getBids;
425 }
426
427 /**
428 * BidTable
429 *
430 * @param bidTable
431 */
432 private void sortBid(final HashMap<Bid, Double> getBids) {
433
434 for (Bid bid : getBids.keySet()) {
435 bidTables.put(bid, getUtility(bid));
436 ((AgentMRSAS) helper).getBidRunk().add(bid); // Add bidRunk
437 }
438
439 if (!EQUIVALENCE_TEST) {
440 // Bidソート処�
441 Collections.sort(((AgentMRSAS) helper).getBidRunk(), new Comparator<Bid>() {
442 @Override
443 public int compare(Bid o1, Bid o2) {
444 return (int) Math.ceil(-(bidTables.get(o1) - bidTables.get(o2)));
445 }
446 });
447 }
448 }
449
450 private Bid getMaxBidUtility(HashMap<Bid, Double> bidTable) {
451 Double maxBidUtility = 0.0;
452 Bid maxBid = null;
453 for (Bid b : bidTable.keySet()) {
454 if (getUtility(b) > maxBidUtility) {
455 maxBidUtility = getUtility(b);
456 maxBid = b;
457 }
458 }
459 return maxBid;
460 }
461
462 private Bid clone(Bid source) throws Exception {
463 HashMap<Integer, Value> hash = new HashMap<Integer, Value>();
464 for (Issue i : utilitySpace.getDomain().getIssues()) {
465 hash.put(i.getNumber(), source.getValue(i.getNumber()));
466 }
467 return new Bid(utilitySpace.getDomain(), hash);
468 }
469
470 @Override
471 public String getName() {
472 return "2012 - AgentMR";
473 }
474
475}
Note: See TracBrowser for help on using the repository browser.