1 | package agents.anac.y2013.MetaAgent.portfolio.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 . Uses the bid evaluators and thus is only
|
---|
24 | * working with {@link AdditiveUtilitySpace}.
|
---|
25 | */
|
---|
26 | public class BidChooser {
|
---|
27 |
|
---|
28 | private AdditiveUtilitySpace utilitySpace;
|
---|
29 | private OpponentBids opponentBids;
|
---|
30 | private ArrayList<Bid> allBids = null;
|
---|
31 | private Bid maxLastOpponentBid;
|
---|
32 | private int numPossibleBids = 0;
|
---|
33 | private int index = 0;
|
---|
34 | private double lastTimeLeft = 0;
|
---|
35 | private AgentID agentID;
|
---|
36 | private int minSize = 160000;
|
---|
37 | private Bid myBestBid = null;
|
---|
38 |
|
---|
39 | public BidChooser(AdditiveUtilitySpace utilitySpace, AgentID agentID,
|
---|
40 | OpponentBids OpponentBids) {
|
---|
41 | this.utilitySpace = utilitySpace;
|
---|
42 | this.agentID = agentID;
|
---|
43 | this.opponentBids = OpponentBids;
|
---|
44 | }
|
---|
45 |
|
---|
46 | private void initBids() {
|
---|
47 |
|
---|
48 | // get all bids
|
---|
49 | allBids = getAllBids();
|
---|
50 |
|
---|
51 | BidsComparator bidsComparator = new BidsComparator(utilitySpace);
|
---|
52 |
|
---|
53 | // sort the bids in order of highest utility
|
---|
54 | // System.out.println("before size " + allBids.size() +" time " +
|
---|
55 | // System.currentTimeMillis());
|
---|
56 | Collections.sort(allBids, bidsComparator);
|
---|
57 | // System.out.println("after time " + System.currentTimeMillis());
|
---|
58 |
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Calculate the next bid for the agent (from 1/4 most optimal bids)
|
---|
63 | *
|
---|
64 | */
|
---|
65 | public Action getNextBid(double time) {
|
---|
66 | Action currentAction = null;
|
---|
67 | try {
|
---|
68 | Bid newBid = allBids.get(index);
|
---|
69 | currentAction = new Offer(agentID, newBid);
|
---|
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 > 0 && 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 | System.out.println("UTILITY: " + utiliy);
|
---|
122 | if (utiliy > maxUtilty)
|
---|
123 | maxUtilty = utiliy;
|
---|
124 | }
|
---|
125 |
|
---|
126 | for (int i = numPossibleBids + 1; i < allBids
|
---|
127 | .size(); i++) {
|
---|
128 | double utiliy = opponentBids
|
---|
129 | .getOpponentBidUtility(
|
---|
130 | utilitySpace.getDomain(),
|
---|
131 | allBids.get(i));
|
---|
132 | if (utiliy >= maxUtilty) {
|
---|
133 | numPossibleBids = i;
|
---|
134 | break;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | maxLastOpponentBid = opponentBids
|
---|
138 | .getMaxUtilityBidForMe();
|
---|
139 | lastTimeLeft = time;
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | }
|
---|
144 | }
|
---|
145 | } catch (Exception e) {
|
---|
146 | e.printStackTrace();
|
---|
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 | currentAction = new Offer(agentID, newBid);
|
---|
163 | index++;
|
---|
164 | double myBestUtility = utilitySpace.getUtilityWithDiscount(
|
---|
165 | myBestBid, time);
|
---|
166 | double oppBestUtility = utilitySpace.getUtilityWithDiscount(
|
---|
167 | opponentBids.getOpponentsBids().get(0), time);
|
---|
168 | double downBond = myBestUtility - (myBestUtility - oppBestUtility)
|
---|
169 | / 4;
|
---|
170 | // check if time passes and compromise a little bit
|
---|
171 | if (time - lastTimeLeft > 0.1
|
---|
172 | && numPossibleBids < allBids.size() - 1
|
---|
173 | && downBond <= utilitySpace.getUtilityWithDiscount(
|
---|
174 | allBids.get(numPossibleBids + 1), time)) {
|
---|
175 | double futureUtility = utilitySpace.getUtilityWithDiscount(
|
---|
176 | allBids.get(numPossibleBids), time + 0.1);
|
---|
177 | while (utilitySpace.getUtilityWithDiscount(
|
---|
178 | allBids.get(numPossibleBids), time) >= futureUtility
|
---|
179 | && numPossibleBids < allBids.size() - 1)
|
---|
180 | numPossibleBids++;
|
---|
181 | lastTimeLeft = time;
|
---|
182 | }
|
---|
183 | if (index > numPossibleBids)
|
---|
184 | index = 0;
|
---|
185 | } catch (Exception e) {
|
---|
186 | e.printStackTrace();
|
---|
187 | }
|
---|
188 | maxLastOpponentBid = opponentBids.getMaxUtilityBidForMe();
|
---|
189 | return currentAction;
|
---|
190 |
|
---|
191 | }
|
---|
192 |
|
---|
193 | /*
|
---|
194 | * returns the Evaluator of an issue
|
---|
195 | */
|
---|
196 | public Evaluator getMyEvaluator(int issueID) {
|
---|
197 | return utilitySpace.getEvaluator(issueID);
|
---|
198 | }
|
---|
199 |
|
---|
200 | /*
|
---|
201 | * returns all bids
|
---|
202 | */
|
---|
203 | private ArrayList<Bid> getAllBids() {
|
---|
204 | ArrayList<Bid> bids = new ArrayList<Bid>();
|
---|
205 | List<Issue> issues = utilitySpace.getDomain().getIssues();
|
---|
206 |
|
---|
207 | HashMap<Integer, Value> issusesFirstValue = new HashMap<Integer, Value>();
|
---|
208 | for (Issue issue : issues) {
|
---|
209 |
|
---|
210 | Value v = getIsuueValues(issue).get(0);
|
---|
211 | issusesFirstValue.put(issue.getNumber(), v);
|
---|
212 | }
|
---|
213 | try {
|
---|
214 | bids.add(new Bid(utilitySpace.getDomain(), issusesFirstValue));
|
---|
215 | } catch (Exception e) {
|
---|
216 | e.printStackTrace();
|
---|
217 | }
|
---|
218 |
|
---|
219 | for (Issue issue : issues) {
|
---|
220 | ArrayList<Bid> tempBids = new ArrayList<Bid>();
|
---|
221 | ArrayList<Value> issueValues = getIsuueValues(issue);
|
---|
222 |
|
---|
223 | for (Bid bid : bids) {
|
---|
224 |
|
---|
225 | for (Value value : issueValues) {
|
---|
226 |
|
---|
227 | HashMap<Integer, Value> lNewBidValues = getBidValues(bid);
|
---|
228 | lNewBidValues.put(issue.getNumber(), value);
|
---|
229 |
|
---|
230 | try {
|
---|
231 | Bid newBid = new Bid(utilitySpace.getDomain(),
|
---|
232 | lNewBidValues);
|
---|
233 | tempBids.add(newBid);
|
---|
234 |
|
---|
235 | } catch (Exception e) {
|
---|
236 | e.printStackTrace();
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|
240 | bids = tempBids;
|
---|
241 | }
|
---|
242 |
|
---|
243 | // remove bids that are not good enough (the utility is less the 1/4 of
|
---|
244 | // the difference between the players)
|
---|
245 |
|
---|
246 | double myBestUtility = 1;
|
---|
247 | double oppBestUtility = 0;
|
---|
248 | try {
|
---|
249 | myBestBid = utilitySpace.getMaxUtilityBid();
|
---|
250 | myBestUtility = utilitySpace.getUtility(myBestBid);
|
---|
251 | oppBestUtility = utilitySpace.getUtility(opponentBids
|
---|
252 | .getOpponentsBids().get(0));
|
---|
253 | } catch (Exception e1) {
|
---|
254 | e1.printStackTrace();
|
---|
255 | }
|
---|
256 |
|
---|
257 | return filterBids(bids, myBestUtility, oppBestUtility, 0.75D);
|
---|
258 | }
|
---|
259 |
|
---|
260 | private ArrayList<Bid> filterBids(ArrayList<Bid> bids,
|
---|
261 | double myBestUtility, double oppBestUtility, double fraction) {
|
---|
262 | double downBond = myBestUtility - (myBestUtility - oppBestUtility)
|
---|
263 | * fraction;
|
---|
264 | ArrayList<Bid> filteredBids = new ArrayList<Bid>();
|
---|
265 | for (Bid bid : bids) {
|
---|
266 | try {
|
---|
267 | double reservation = utilitySpace.getReservationValue() != null ? utilitySpace
|
---|
268 | .getReservationValue() : 0;
|
---|
269 | if (utilitySpace.getUtility(bid) < downBond
|
---|
270 | || utilitySpace.getUtility(bid) < reservation)
|
---|
271 | continue;
|
---|
272 | else
|
---|
273 | filteredBids.add(bid);
|
---|
274 |
|
---|
275 | } catch (Exception e) {
|
---|
276 | e.printStackTrace();
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | // System.out.println("need to remove " + bids.size() +" removed" +
|
---|
281 | // bidstoRemove.size() + "time " );
|
---|
282 | if (filteredBids.size() < minSize) {
|
---|
283 | return filteredBids;
|
---|
284 | }
|
---|
285 | return filterBids(filteredBids, myBestUtility, oppBestUtility,
|
---|
286 | fraction * 0.85D);
|
---|
287 | }
|
---|
288 |
|
---|
289 | /*
|
---|
290 | * returns bid values
|
---|
291 | */
|
---|
292 | private HashMap<Integer, Value> getBidValues(Bid bid) {
|
---|
293 | HashMap<Integer, Value> bidValues = new HashMap<Integer, Value>();
|
---|
294 | List<Issue> allIsuues = utilitySpace.getDomain().getIssues();
|
---|
295 | for (Issue issue : allIsuues) {
|
---|
296 | try {
|
---|
297 | bidValues.put(issue.getNumber(),
|
---|
298 | bid.getValue(issue.getNumber()));
|
---|
299 | } catch (Exception e) {
|
---|
300 | e.printStackTrace();
|
---|
301 | }
|
---|
302 |
|
---|
303 | }
|
---|
304 | return bidValues;
|
---|
305 | }
|
---|
306 |
|
---|
307 | /*
|
---|
308 | * returns issue values
|
---|
309 | */
|
---|
310 | public ArrayList<Value> getIsuueValues(Issue issue) {
|
---|
311 |
|
---|
312 | Evaluator e = getMyEvaluator(issue.getNumber());
|
---|
313 | ArrayList<Value> retValues = new ArrayList<Value>();
|
---|
314 | switch (e.getType()) {
|
---|
315 | case DISCRETE:
|
---|
316 | EvaluatorDiscrete eD = ((EvaluatorDiscrete) e);
|
---|
317 | retValues.addAll(eD.getValues());
|
---|
318 | break;
|
---|
319 | case REAL:
|
---|
320 | EvaluatorReal eR = ((EvaluatorReal) e);
|
---|
321 |
|
---|
322 | double intervalReal = (eR.getUpperBound() - eR.getLowerBound()) / 10;
|
---|
323 | for (int i = 0; i <= 10; i++) {
|
---|
324 | retValues.add(new ValueReal(eR.getLowerBound() + i
|
---|
325 | * intervalReal));
|
---|
326 | }
|
---|
327 | break;
|
---|
328 | case INTEGER:
|
---|
329 | EvaluatorInteger eI = ((EvaluatorInteger) e);
|
---|
330 |
|
---|
331 | int intervalInteger = (eI.getUpperBound() - eI.getLowerBound()) / 10;
|
---|
332 | for (int i = 0; i <= 10; i++) {
|
---|
333 | retValues.add(new ValueInteger(eI.getLowerBound() + i
|
---|
334 | * intervalInteger));
|
---|
335 | }
|
---|
336 | break;
|
---|
337 | }
|
---|
338 | return retValues;
|
---|
339 | }
|
---|
340 |
|
---|
341 | /*
|
---|
342 | * returns the minimum utility of the bid that the agent voted
|
---|
343 | */
|
---|
344 | public double getMyBidsMinUtility(double time) {
|
---|
345 | if (allBids == null)
|
---|
346 | initBids();
|
---|
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 | }
|
---|