source: src/main/java/negotiator/boaframework/offeringstrategy/anac2012/AgentMR_Offering.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 negotiator.boaframework.offeringstrategy.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.NegotiationSession;
14import genius.core.boaframework.NoModel;
15import genius.core.boaframework.OMStrategy;
16import genius.core.boaframework.OfferingStrategy;
17import genius.core.boaframework.OpponentModel;
18import genius.core.boaframework.SortedOutcomeSpace;
19import genius.core.issue.Issue;
20import genius.core.issue.IssueDiscrete;
21import genius.core.issue.IssueInteger;
22import genius.core.issue.IssueReal;
23import genius.core.issue.Value;
24import genius.core.issue.ValueDiscrete;
25import genius.core.issue.ValueInteger;
26import genius.core.issue.ValueReal;
27import genius.core.utility.AdditiveUtilitySpace;
28import negotiator.boaframework.opponentmodel.DefaultModel;
29import negotiator.boaframework.sharedagentstate.anac2012.AgentMRSAS;
30
31/**
32 * This is the decoupled Bidding Strategy of AgentMR
33 *
34 * @author Alex Dirkzwager
35 */
36public class AgentMR_Offering extends OfferingStrategy {
37
38 private boolean EQUIVALENCE_TEST = false;
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 BidDetails offereMaxBid = null;
46 private static double offereMaxUtility;
47 private int currentBidNumber = 0;
48 private int lastBidNumber = 1;
49 private AdditiveUtilitySpace utilitySpace;
50 private boolean alreadyDone = false;
51 private SortedOutcomeSpace outcomeSpace;
52
53 public AgentMR_Offering() {
54 }
55
56 public AgentMR_Offering(NegotiationSession negoSession, OpponentModel model, OMStrategy oms) throws Exception {
57 init(negoSession, model, oms, null);
58 }
59
60 /**
61 * Init required for the Decoupled Framework.
62 */
63 @Override
64 public void init(NegotiationSession negoSession, OpponentModel model, OMStrategy oms,
65 Map<String, Double> parameters) throws Exception {
66 super.init(negoSession, model, omStrategy, parameters);
67 if (model instanceof DefaultModel) {
68 model = new NoModel();
69 }
70 if (!(model instanceof NoModel)) {
71 outcomeSpace = new SortedOutcomeSpace(negoSession.getUtilitySpace());
72 }
73 this.opponentModel = model;
74 this.omStrategy = oms;
75
76 helper = new AgentMRSAS(negotiationSession);
77 firstOffer = true;
78 try {
79 utilitySpace = (AdditiveUtilitySpace) negoSession.getUtilitySpace();
80 getDiscountFactor();
81 getReservationFactor();
82
83 Bid b = negoSession.getMaxBidinDomain().getBid();
84 bidTables.put(b, getUtility(b));
85 ((AgentMRSAS) helper).getBidRunk().add(b);
86 if (discountFactor) {
87 ((AgentMRSAS) helper).setSigmoidGain(-3.0);
88 ((AgentMRSAS) helper).setPercent(0.55);
89 } else {
90 ((AgentMRSAS) helper).setSigmoidGain(-5.0);
91 ((AgentMRSAS) helper).setPercent(0.70);
92 }
93 if (EQUIVALENCE_TEST) {
94 random100 = new Random(100);
95 } else {
96 random100 = new Random();
97 }
98 } catch (Exception e) {
99 e.printStackTrace();
100 }
101
102 }
103
104 @Override
105 public BidDetails determineOpeningBid() {
106
107 return determineNextBid();
108 }
109
110 @Override
111 public BidDetails determineNextBid() {
112 if (negotiationSession.getOpponentBidHistory().getHistory().isEmpty()) {
113 if (!alreadyDone) {
114 ((AgentMRSAS) helper).updateMinimumBidUtility(0);
115 alreadyDone = true;
116 }
117
118 return negotiationSession.getMaxBidinDomain();
119
120 }
121 try {
122 BidDetails partnerBid;
123 if (firstOffer) {
124 partnerBid = negotiationSession.getOpponentBidHistory().getHistory().get(0);
125 } else {
126 partnerBid = negotiationSession.getOpponentBidHistory().getLastBidDetails();
127 }
128 // get current time
129 double time = negotiationSession.getTime();
130 double offeredutil;
131 if (discountFactor) {
132 offeredutil = getUtility(partnerBid.getBid())
133 * (1 / Math.pow(negotiationSession.getUtilitySpace().getDiscountFactor(), time));
134
135 } else {
136 offeredutil = getUtility(partnerBid.getBid());
137
138 }
139 // System.out.println(firstOffer);
140 if (firstOffer) {
141 // System.out.println("Decoupled partnerBid: " +
142 // partnerBid.getBid());
143 // System.out.println("Decoupled offeredutil: " + offeredutil);
144
145 offereMaxBid = partnerBid;
146 offereMaxUtility = offeredutil;
147 ((AgentMRSAS) helper).setFirstOffereUtility(offeredutil);
148
149 observationUtility.add(offeredutil);
150 if (offeredutil > 0.5) {
151 ((AgentMRSAS) helper).setP(0.90);
152 } else {
153 ((AgentMRSAS) helper).setP(0.80);
154 }
155 firstOffer = !firstOffer;
156 }
157 ((AgentMRSAS) helper).updateMinimumBidUtility(time);
158
159 if (offeredutil > offereMaxUtility) {
160 offereMaxBid = partnerBid;
161 offereMaxUtility = offeredutil;
162 // addObservation
163 observationUtility.add(offeredutil);
164 if ((time > 0.5) && !discountFactor) {
165 newupdateSigmoidFunction();
166 }
167 }
168
169 // forecasting
170 if ((time > 0.5) && forecastTime) {
171 updateSigmoidFunction();
172 forecastTime = !forecastTime;
173 }
174
175 if (offereMaxUtility > ((AgentMRSAS) helper).getMinimumBidUtility()) {
176 nextBid = offereMaxBid;
177 } else if (time > 0.985) {
178 if (offereMaxUtility > ((AgentMRSAS) helper).getReservation()) {
179 nextBid = offereMaxBid;
180 } else {
181 Bid nBid = ((AgentMRSAS) helper).getBidRunk()
182 .get(((AgentMRSAS) helper).getBidRunk().size() - lastBidNumber);
183 nextBid = new BidDetails(nBid, negotiationSession.getUtilitySpace().getUtility(nBid));
184 lastBidNumber++;
185 }
186 } else {
187 if (offeredutil > ((AgentMRSAS) helper).getMinimumOffereDutil()) {
188 HashMap<Bid, Double> getBids = getBidTable(1);
189 if (getBids.size() >= 1) {
190 // BidTable
191 currentBidNumber = 0;
192 ((AgentMRSAS) helper).getBidRunk().clear();
193 bidTables = getBids;
194 sortBid(getBids); // Sort BidTable
195 } else {
196 getBids = getBidTable(2);
197 if (getBids.size() >= 1) {
198 sortBid(getBids); // Sort BidTable
199 Bid maxBid = getMaxBidUtility(getBids);
200 currentBidNumber = ((AgentMRSAS) helper).getBidRunk().indexOf(maxBid);
201 }
202 }
203 Bid nBid = ((AgentMRSAS) helper).getBidRunk().get(currentBidNumber);
204 nextBid = new BidDetails(nBid, negotiationSession.getUtilitySpace().getUtility(nBid));
205
206 if (currentBidNumber + 1 < ((AgentMRSAS) helper).getBidRunk().size()) {
207 currentBidNumber++;
208 }
209
210 } else {
211 HashMap<Bid, Double> getBids = getBidTable(2);
212 if (getBids.size() >= 1) {
213 sortBid(getBids); // Sort BidTable
214 Bid maxBid = getMaxBidUtility(getBids);
215 currentBidNumber = ((AgentMRSAS) helper).getBidRunk().indexOf(maxBid);
216 }
217
218 Bid nBid = ((AgentMRSAS) helper).getBidRunk().get(currentBidNumber);
219 nextBid = new BidDetails(nBid, negotiationSession.getUtilitySpace().getUtility(nBid));
220
221 if (currentBidNumber + 1 < ((AgentMRSAS) helper).getBidRunk().size()) {
222 currentBidNumber++;
223 } else {
224 currentBidNumber = 0;
225 }
226 }
227
228 }
229 } catch (Exception e) {
230 e.printStackTrace();
231 }
232
233 if (!(opponentModel instanceof NoModel)) {
234 try {
235 nextBid = omStrategy.getBid(outcomeSpace, utilitySpace.getUtility(nextBid.getBid()));
236 } catch (Exception e) {
237 e.printStackTrace();
238 }
239 }
240 return nextBid;
241
242 }
243
244 private void getReservationFactor() {
245 if (utilitySpace.getReservationValue() != null) {
246 ((AgentMRSAS) helper).setReservation(utilitySpace.getReservationValue());
247 }
248 }
249
250 private void getDiscountFactor() {
251 discountFactor = utilitySpace.isDiscounted();
252 }
253
254 private void newupdateSigmoidFunction() {
255 double latestObservation = observationUtility.get(observationUtility.size() - 1);
256 double concessionPercent = Math.abs(latestObservation - ((AgentMRSAS) helper).getFirstOffereUtility())
257 / (1.0 - ((AgentMRSAS) helper).getFirstOffereUtility());
258 double modPercent = Math
259 .abs(((AgentMRSAS) helper).getMinimumOffereDutil() - ((AgentMRSAS) helper).getFirstOffereUtility())
260 / (1.0 - ((AgentMRSAS) helper).getFirstOffereUtility());
261
262 if (modPercent < concessionPercent) {
263 ((AgentMRSAS) helper).setPercent(concessionPercent);
264 }
265 }
266
267 private Bid getMaxBidUtility(HashMap<Bid, Double> bidTable) {
268 Double maxBidUtility = 0.0;
269 Bid maxBid = null;
270 for (Bid b : bidTable.keySet()) {
271 if (getUtility(b) > maxBidUtility) {
272 maxBidUtility = getUtility(b);
273 maxBid = b;
274 }
275 }
276 return maxBid;
277 }
278
279 /**
280 * BidTable
281 *
282 * @param bidTable
283 */
284 private void sortBid(final HashMap<Bid, Double> getBids) {
285
286 for (Bid bid : getBids.keySet()) {
287 bidTables.put(bid, getUtility(bid));
288 ((AgentMRSAS) helper).getBidRunk().add(bid); // Add bidRunk
289 }
290
291 if (!EQUIVALENCE_TEST) {
292 Collections.sort(((AgentMRSAS) helper).getBidRunk(), new Comparator<Bid>() {
293 @Override
294 public int compare(Bid o1, Bid o2) {
295 return (int) Math.ceil(-(bidTables.get(o1) - bidTables.get(o2)));
296 }
297 });
298 }
299 }
300
301 private Bid clone(Bid source) throws Exception {
302 HashMap<Integer, Value> hash = new HashMap<Integer, Value>();
303 for (Issue i : utilitySpace.getDomain().getIssues()) {
304 hash.put(i.getNumber(), source.getValue(i.getNumber()));
305 }
306 return new Bid(utilitySpace.getDomain(), hash);
307 }
308
309 /**
310 * @param maxBid
311 * @return
312 * @throws Exception
313 */
314 private HashMap<Bid, Double> getBidTable(int flag) throws Exception {
315 HashMap<Bid, Double> getBids = new HashMap<Bid, Double>();
316
317 // Random randomnr = new Random();
318 List<Issue> issues = utilitySpace.getDomain().getIssues();
319 Bid standardBid = null;
320
321 for (Issue lIssue : issues) {
322 switch (lIssue.getType()) {
323 case DISCRETE:
324 IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
325 for (ValueDiscrete value : lIssueDiscrete.getValues()) {
326 if (flag == 0) {
327 standardBid = utilitySpace.getMaxUtilityBid();
328 } else if (flag == 1) {
329 standardBid = negotiationSession.getOpponentBidHistory().getLastBid();
330 } else {
331 standardBid = ((AgentMRSAS) helper).getBidRunk().get(currentBidNumber);
332 }
333 standardBid = clone(standardBid);
334 standardBid = standardBid.putValue(lIssue.getNumber(), value);
335 double utility = getUtility(standardBid);
336 if ((utility > ((AgentMRSAS) helper).getMinimumBidUtility())
337 && (!((AgentMRSAS) helper).getBidRunk().contains(standardBid))) {
338 getBids.put(standardBid, utility);
339 }
340 }
341 break;
342 case REAL:
343 IssueReal lIssueReal = (IssueReal) lIssue;
344 int optionInd = random100.nextInt(lIssueReal.getNumberOfDiscretizationSteps() - 1);
345 Value pValue = new ValueReal(
346 lIssueReal.getLowerBound() + (lIssueReal.getUpperBound() - lIssueReal.getLowerBound())
347 * (double) (optionInd) / (double) (lIssueReal.getNumberOfDiscretizationSteps()));
348 standardBid = standardBid.putValue(lIssueReal.getNumber(), pValue);
349 double utility = getUtility(standardBid);
350 getBids.put(standardBid, utility);
351 break;
352 case INTEGER:
353 IssueInteger lIssueInteger = (IssueInteger) lIssue;
354 int optionIndex2 = lIssueInteger.getLowerBound()
355 + random100.nextInt(lIssueInteger.getUpperBound() - lIssueInteger.getLowerBound());
356 Value pValue2 = new ValueInteger(optionIndex2);
357 standardBid = standardBid.putValue(lIssueInteger.getNumber(), pValue2);
358 double utility2 = getUtility(standardBid);
359 getBids.put(standardBid, utility2);
360 break;
361 default:
362 throw new Exception("issue type " + lIssue.getType() + " not supported by AgentMR");
363 }
364 }
365
366 return getBids;
367 }
368
369 public double getUtility(Bid bid) {
370 return negotiationSession.getUtilitySpace().getUtilityWithDiscount(bid, negotiationSession.getTimeline());
371 }
372
373 private void updateSigmoidFunction() {
374 int observationSize = observationUtility.size();
375 double latestObservation = observationUtility.get(observationSize - 1);
376 double concessionPercent = Math.abs(latestObservation - ((AgentMRSAS) helper).getFirstOffereUtility())
377 / (1.0 - ((AgentMRSAS) helper).getFirstOffereUtility());
378
379 if (discountFactor) {
380 if ((concessionPercent < 0.20) || (observationSize < 3)) {
381 ((AgentMRSAS) helper).setPercent(0.35);
382 ((AgentMRSAS) helper).setSigmoidGain(-2);
383 } else {
384 ((AgentMRSAS) helper).setPercent(0.45);
385 }
386 } else {
387 if ((concessionPercent < 0.20) || (observationSize < 3)) {
388 ((AgentMRSAS) helper).setPercent(0.50);
389 ((AgentMRSAS) helper).setSigmoidGain(-4);
390 } else if (concessionPercent > 0.60) {
391 ((AgentMRSAS) helper).setPercent(0.80);
392 ((AgentMRSAS) helper).setSigmoidGain(-6);
393 } else {
394 ((AgentMRSAS) helper).setPercent(0.60);
395 }
396 }
397 }
398
399 @Override
400 public String getName() {
401 return "2012 - AgentMR";
402 }
403
404}
Note: See TracBrowser for help on using the repository browser.