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