source: src/main/java/negotiator/boaframework/sharedagentstate/anac2012/AgentLGSAS.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: 11.9 KB
Line 
1package negotiator.boaframework.sharedagentstate.anac2012;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.List;
7
8import agents.anac.y2012.AgentLG.BidsComparator;
9import agents.anac.y2012.AgentLG.OpponentBids;
10import genius.core.Bid;
11import genius.core.Domain;
12import genius.core.bidding.BidDetails;
13import genius.core.boaframework.NegotiationSession;
14import genius.core.boaframework.NoModel;
15import genius.core.boaframework.OMStrategy;
16import genius.core.boaframework.OpponentModel;
17import genius.core.boaframework.SharedAgentState;
18import genius.core.boaframework.SortedOutcomeSpace;
19import genius.core.issue.Issue;
20import genius.core.issue.Value;
21import genius.core.issue.ValueInteger;
22import genius.core.issue.ValueReal;
23import genius.core.utility.AdditiveUtilitySpace;
24import genius.core.utility.Evaluator;
25import genius.core.utility.EvaluatorDiscrete;
26import genius.core.utility.EvaluatorInteger;
27import genius.core.utility.EvaluatorReal;
28
29public class AgentLGSAS extends SharedAgentState {
30
31 private AdditiveUtilitySpace utilitySpace;
32 private OpponentBids opponentBids;
33 private ArrayList<Bid> allBids = null;
34 private Bid maxLastOpponentBid;
35 private int numPossibleBids = 0;
36 private int index = 0;
37 private double lastTimeLeft = 0;
38 private int minSize = 160000;
39 private Bid myBestBid = null;
40 private OpponentModel opponentModel;
41 private SortedOutcomeSpace outcomeSpace;
42 private OMStrategy oms;
43
44 public AgentLGSAS(NegotiationSession negoSession,
45 OpponentBids opponentBids, OpponentModel opponentModel,
46 OMStrategy oms) {
47 NAME = "AgentLR";
48 this.oms = oms;
49 this.utilitySpace = (AdditiveUtilitySpace) negoSession
50 .getUtilitySpace();
51 this.opponentBids = opponentBids;
52 this.opponentModel = opponentModel;
53 if (!(opponentModel instanceof NoModel)) {
54 outcomeSpace = new SortedOutcomeSpace(utilitySpace);
55 }
56 }
57
58 private void initBids() {
59
60 // get all bids
61 allBids = getAllBids();
62
63 BidsComparator bidsComparator = new BidsComparator(utilitySpace);
64
65 // sort the bids in order of highest utility
66 // System.out.println("before size " + allBids.size() +" time " +
67 // System.currentTimeMillis());
68 Collections.sort(allBids, bidsComparator);
69 // System.out.println("after time " + System.currentTimeMillis());
70
71 }
72
73 /**
74 * Calculate the next bid for the agent (from 1/4 most optimal bids)
75 *
76 */
77 public BidDetails getNextBid(double time) {
78 BidDetails currentAction = null;
79 try {
80 Bid newBid = allBids.get(index);
81 currentAction = new BidDetails(newBid,
82 utilitySpace.getUtility(newBid));
83 index++;
84 if (index > numPossibleBids) {
85 // the time is over compromising in a high rate
86 if (time >= 0.9) {
87 if (time - lastTimeLeft > 0.008) {
88 double myBestUtility = utilitySpace
89 .getUtility(myBestBid);
90 double oppBestUtility = utilitySpace
91 .getUtility(opponentBids.getOpponentsBids()
92 .get(0));
93 double avg = (myBestUtility + oppBestUtility) / 2;
94
95 if (index >= allBids.size())
96 index = allBids.size() - 1;
97 else if (utilitySpace.getUtility(allBids.get(index)) < avg) {
98 index--;
99 double maxUtilty = 0;
100 int maxBidIndex = numPossibleBids;
101
102 for (int i = numPossibleBids; i <= index; i++) {
103 // finds the next better bid for the opponent
104 double utiliy = getUtilityOfOpponentsBid(
105 utilitySpace.getDomain(),
106 allBids.get(i));
107 if (utiliy > maxUtilty) {
108 maxUtilty = utiliy;
109 maxBidIndex = i;
110 }
111 }
112 numPossibleBids = maxBidIndex;
113 } else
114 index--;
115 } else
116 index = 0;
117 } else {
118 index = 0;
119 double discount = utilitySpace.getDiscountFactor();
120 // the time is over compromising in normal rate (0.05)
121 if (time - lastTimeLeft > 0.05) {
122 // compromise only if the opponent is compromising
123 if (utilitySpace.getUtility(opponentBids
124 .getMaxUtilityBidForMe()) > utilitySpace
125 .getUtility(maxLastOpponentBid)
126 || (discount < 1 && time - lastTimeLeft > 0.1)) {
127 // finds the next better bid for the opponent
128 double maxUtilty = 0;
129 for (int i = 0; i <= numPossibleBids; i++) {
130 double utiliy = getUtilityOfOpponentsBid(
131 utilitySpace.getDomain(),
132 allBids.get(i));
133 // System.out.println("UTILITY: " + utiliy);
134 if (utiliy > maxUtilty)
135 maxUtilty = utiliy;
136 }
137
138 for (int i = numPossibleBids + 1; i < allBids
139 .size(); i++) {
140 double utiliy = getUtilityOfOpponentsBid(
141 utilitySpace.getDomain(),
142 allBids.get(i));
143 if (utiliy >= maxUtilty) {
144 numPossibleBids = i;
145 break;
146 }
147 }
148 maxLastOpponentBid = opponentBids
149 .getMaxUtilityBidForMe();
150 lastTimeLeft = time;
151 }
152 }
153
154 }
155 }
156 } catch (Exception e) {
157 e.printStackTrace();
158 }
159
160 if (!(opponentModel instanceof NoModel)) {
161 try {
162 currentAction = oms.getBid(outcomeSpace,
163 utilitySpace.getUtility(currentAction.getBid()));
164 } catch (Exception e) {
165 e.printStackTrace();
166 }
167 }
168 return currentAction;
169 }
170
171 /**
172 * Calculate the next optimal bid for the agent (from 1/4 most optimal bids)
173 *
174 */
175 public BidDetails getNextOptimicalBid(double time) {
176 BidDetails currentAction = null;
177 Bid newBid = null;
178 try {
179 if (allBids == null)
180 initBids();
181 newBid = allBids.get(index);
182 // System.out.println("Decoupled getNextOptimal: " + newBid);
183
184 currentAction = new BidDetails(newBid,
185 utilitySpace.getUtility(newBid));
186 index++;
187 double myBestUtility = utilitySpace.getUtilityWithDiscount(
188 myBestBid, time);
189 double oppBestUtility = utilitySpace.getUtilityWithDiscount(
190 opponentBids.getOpponentsBids().get(0), time);
191 double downBond = myBestUtility - (myBestUtility - oppBestUtility)
192 / 4;
193 // check if time passes and compromise a little bit
194 if (time - lastTimeLeft > 0.1
195 && numPossibleBids < allBids.size() - 1
196 && downBond <= utilitySpace.getUtilityWithDiscount(
197 allBids.get(numPossibleBids + 1), time)) {
198 double futureUtility = utilitySpace.getUtilityWithDiscount(
199 allBids.get(numPossibleBids), time + 0.1);
200 while (utilitySpace.getUtilityWithDiscount(
201 allBids.get(numPossibleBids), time) >= futureUtility
202 && numPossibleBids < allBids.size() - 1)
203 numPossibleBids++;
204 lastTimeLeft = time;
205 }
206 if (index > numPossibleBids)
207 index = 0;
208 } catch (Exception e) {
209 e.printStackTrace();
210 }
211 maxLastOpponentBid = opponentBids.getMaxUtilityBidForMe();
212
213 if (!(opponentModel instanceof NoModel)) {
214 try {
215 currentAction = oms.getBid(outcomeSpace,
216 utilitySpace.getUtility(currentAction.getBid()));
217 } catch (Exception e) {
218 e.printStackTrace();
219 }
220 }
221 return currentAction;
222
223 }
224
225 /*
226 * returns the Evaluator of an issue
227 */
228 public Evaluator getMyEvaluator(int issueID) {
229 return utilitySpace.getEvaluator(issueID);
230 }
231
232 /*
233 * returns all bids
234 */
235 private ArrayList<Bid> getAllBids() {
236 ArrayList<Bid> bids = new ArrayList<Bid>();
237 List<Issue> issues = utilitySpace.getDomain().getIssues();
238
239 HashMap<Integer, Value> issusesFirstValue = new HashMap<Integer, Value>();
240 for (Issue issue : issues) {
241
242 Value v = getIsuueValues(issue).get(0);
243 issusesFirstValue.put(issue.getNumber(), v);
244 }
245 try {
246 bids.add(new Bid(utilitySpace.getDomain(), issusesFirstValue));
247 } catch (Exception e) {
248 e.printStackTrace();
249 }
250
251 for (Issue issue : issues) {
252 ArrayList<Bid> tempBids = new ArrayList<Bid>();
253 ArrayList<Value> issueValues = getIsuueValues(issue);
254
255 for (Bid bid : bids) {
256
257 for (Value value : issueValues) {
258
259 HashMap<Integer, Value> lNewBidValues = getBidValues(bid);
260 lNewBidValues.put(issue.getNumber(), value);
261
262 try {
263 Bid newBid = new Bid(utilitySpace.getDomain(),
264 lNewBidValues);
265 tempBids.add(newBid);
266
267 } catch (Exception e) {
268 e.printStackTrace();
269 }
270 }
271 }
272 bids = tempBids;
273 }
274
275 // remove bids that are not good enough (the utility is less the 1/4 of
276 // the difference between the players)
277
278 double myBestUtility = 1;
279 double oppBestUtility = 0;
280 try {
281 myBestBid = utilitySpace.getMaxUtilityBid();
282 myBestUtility = utilitySpace.getUtility(myBestBid);
283 oppBestUtility = utilitySpace.getUtility(opponentBids
284 .getOpponentsBids().get(0));
285 } catch (Exception e1) {
286 e1.printStackTrace();
287 }
288
289 return filterBids(bids, myBestUtility, oppBestUtility, 0.75D);
290 }
291
292 private ArrayList<Bid> filterBids(ArrayList<Bid> bids,
293 double myBestUtility, double oppBestUtility, double fraction) {
294 double downBond = myBestUtility - (myBestUtility - oppBestUtility)
295 * fraction;
296 ArrayList<Bid> filteredBids = new ArrayList<Bid>();
297 for (Bid bid : bids) {
298 try {
299 double reservation = utilitySpace.getReservationValue() != null ? utilitySpace
300 .getReservationValue() : 0;
301 if (utilitySpace.getUtility(bid) < downBond
302 || utilitySpace.getUtility(bid) < reservation)
303 continue;
304 else
305 filteredBids.add(bid);
306
307 } catch (Exception e) {
308 e.printStackTrace();
309 }
310 }
311
312 // System.out.println("need to remove " + bids.size() +" removed" +
313 // bidstoRemove.size() + "time " );
314 if (filteredBids.size() < minSize) {
315 return filteredBids;
316 }
317 return filterBids(filteredBids, myBestUtility, oppBestUtility,
318 fraction * 0.85D);
319 }
320
321 /*
322 * returns bid values
323 */
324 private HashMap<Integer, Value> getBidValues(Bid bid) {
325 HashMap<Integer, Value> bidValues = new HashMap<Integer, Value>();
326 List<Issue> allIsuues = utilitySpace.getDomain().getIssues();
327 for (Issue issue : allIsuues) {
328 try {
329 bidValues.put(issue.getNumber(),
330 bid.getValue(issue.getNumber()));
331 } catch (Exception e) {
332 e.printStackTrace();
333 }
334
335 }
336 return bidValues;
337 }
338
339 /*
340 * returns issue values
341 */
342 public ArrayList<Value> getIsuueValues(Issue issue) {
343
344 Evaluator e = getMyEvaluator(issue.getNumber());
345 ArrayList<Value> retValues = new ArrayList<Value>();
346 switch (e.getType()) {
347 case DISCRETE:
348 EvaluatorDiscrete eD = ((EvaluatorDiscrete) e);
349 retValues.addAll(eD.getValues());
350 break;
351 case REAL:
352 EvaluatorReal eR = ((EvaluatorReal) e);
353
354 double intervalReal = (eR.getUpperBound() - eR.getLowerBound()) / 10;
355 for (int i = 0; i <= 10; i++) {
356 retValues.add(new ValueReal(eR.getLowerBound() + i
357 * intervalReal));
358 }
359 break;
360 case INTEGER:
361 EvaluatorInteger eI = ((EvaluatorInteger) e);
362
363 int intervalInteger = (eI.getUpperBound() - eI.getLowerBound()) / 10;
364 for (int i = 0; i <= 10; i++) {
365 retValues.add(new ValueInteger(eI.getLowerBound() + i
366 * intervalInteger));
367 }
368 break;
369 }
370 return retValues;
371 }
372
373 /*
374 * returns the minimum utility of the bid that the agent voted
375 */
376 public double getMyBidsMinUtility(double time) {
377 if (allBids == null)
378 initBids();
379 return utilitySpace.getUtilityWithDiscount(
380 allBids.get(numPossibleBids), time);
381 }
382
383 /*
384 * returns the bid with the minimum utility that the agent voted
385 */
386 public Bid getMyminBidfromBids() {
387 if (allBids == null)
388 initBids();
389 return allBids.get(numPossibleBids);
390 }
391
392 /*
393 * returns the bid utility
394 */
395 public double getUtility(Bid bid) {
396 try {
397 return utilitySpace.getUtility(bid);
398 } catch (Exception e) {
399 e.printStackTrace();
400 }
401 return 0;
402 }
403
404 public double getUtilityOfOpponentsBid(Domain domain, Bid bid) {
405 double utility;
406 if (opponentModel instanceof NoModel) {
407 utility = opponentBids.getOpponentBidUtility(
408 utilitySpace.getDomain(), bid);
409 } else {
410 utility = opponentModel.getBidEvaluation(bid);
411 }
412 return utility;
413 }
414}
Note: See TracBrowser for help on using the repository browser.