1 | /**
|
---|
2 | * OptimalBidder: using the optimal stopping rule (cutoffs) for bidding
|
---|
3 | * @author rafik
|
---|
4 | **/
|
---|
5 |
|
---|
6 | package agents;
|
---|
7 |
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.HashMap;
|
---|
10 | import java.util.TreeMap;
|
---|
11 |
|
---|
12 | import genius.core.Agent;
|
---|
13 | import genius.core.Bid;
|
---|
14 | import genius.core.actions.Action;
|
---|
15 | import genius.core.actions.Offer;
|
---|
16 | import genius.core.issue.Issue;
|
---|
17 | import genius.core.issue.Value;
|
---|
18 | import genius.core.timeline.DiscreteTimeline;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * framework for optimal bidder. This agent never accepts. Tim commented:
|
---|
22 | * OptimalBidder beslist alleen over de concessies, maar kan natuurlijk goed
|
---|
23 | * worden gecombineerd met acceptatiecomponenten (optimal stopping bv) en
|
---|
24 | * leercomponenten (simple frequency learning bv). Het leren is belangrijk,
|
---|
25 | * omdat de OptimalBidder alleen een utility target oplevert. Samen met een
|
---|
26 | * leercomponent kan daar een goed Pareto-optimaal bod bij worden gevonden, maar
|
---|
27 | * dit ligt aan de toepassing.
|
---|
28 | *
|
---|
29 | */
|
---|
30 | public abstract class OptimalBidder extends Agent {
|
---|
31 | protected double rv = -1.0;
|
---|
32 | protected static int partitions;
|
---|
33 | protected static int ownTotalRounds;
|
---|
34 | protected static HashMap<Integer, Value> values;
|
---|
35 | private static ArrayList<Double> bids;
|
---|
36 | protected static Issue pie;
|
---|
37 | private Action actionOfPartner = null;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * computation of the bid for round j. FIXME this is not a bid. Maybe it's a
|
---|
41 | * target utility of the bid to place?
|
---|
42 | *
|
---|
43 | * @param round
|
---|
44 | * the number of rounds left
|
---|
45 | * @return utility for a bid to be placed when given number of rounds are
|
---|
46 | * left.
|
---|
47 | **/
|
---|
48 | public abstract double bid(int j);
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Implementors should fill the values list when this is called.
|
---|
52 | *
|
---|
53 | * @throws Exception
|
---|
54 | **/
|
---|
55 | public abstract void getValues() throws Exception;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Init is called when a next session starts with the same opponent.
|
---|
59 | **/
|
---|
60 | @Override
|
---|
61 | public void init() {
|
---|
62 | try {
|
---|
63 | ownTotalRounds = (getTotalRounds() - 1) / 2;
|
---|
64 | pie = utilitySpace.getDomain().getIssues().get(0); // unique issue
|
---|
65 |
|
---|
66 | print("=====================================================================");
|
---|
67 | print(" ownTotalRounds = " + ownTotalRounds);
|
---|
68 | print(" issue name = " + pie);
|
---|
69 | print(" issue type = " + pie.getType());
|
---|
70 |
|
---|
71 | getValues(); // setting all the values
|
---|
72 |
|
---|
73 | rv = utilitySpace.getReservationValue();
|
---|
74 |
|
---|
75 | print(" Reservation value = " + rv);
|
---|
76 |
|
---|
77 | bids = new ArrayList<Double>(ownTotalRounds);
|
---|
78 |
|
---|
79 | for (int i = 0; i < ownTotalRounds; i++)
|
---|
80 | bids.add(bid(i + 1));
|
---|
81 |
|
---|
82 | print(" Bids : ");
|
---|
83 |
|
---|
84 | for (int i = 0; i < ownTotalRounds; i++)
|
---|
85 | print("\tB[" + i + "] = " + bids.get(i));
|
---|
86 |
|
---|
87 | print("\n=====================================================================");
|
---|
88 | } catch (Exception e) {
|
---|
89 | e.printStackTrace();
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public String getVersion() {
|
---|
95 | return "2.0 (Genius 4.2)";
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | public String getName() {
|
---|
100 | return "OptimalBidder";
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | public void ReceiveMessage(Action opponentAction) {
|
---|
105 | actionOfPartner = opponentAction;
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public Action chooseAction() {
|
---|
110 | Action action = null;
|
---|
111 | try {
|
---|
112 | if (actionOfPartner == null) {
|
---|
113 | action = chooseOptimalBidAction();
|
---|
114 | }
|
---|
115 | if (actionOfPartner instanceof Offer) {
|
---|
116 | action = chooseOptimalBidAction();
|
---|
117 | }
|
---|
118 | } catch (Exception e) {
|
---|
119 | print("Exception in ChooseAction:" + e.getMessage());
|
---|
120 | }
|
---|
121 | return action;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Wrapper for getOptimalBid, for convenience
|
---|
126 | *
|
---|
127 | * @return new Bid()
|
---|
128 | **/
|
---|
129 | private Action chooseOptimalBidAction() {
|
---|
130 | Bid nextBid = null;
|
---|
131 | try {
|
---|
132 | nextBid = getOptimalBid();
|
---|
133 | } catch (Exception e) {
|
---|
134 | print("Problem with received bid: <" + e.getMessage()
|
---|
135 | + ">. Cancelling bidding");
|
---|
136 | System.out.println("\t\t\t\tErrrrr! => " + nextBid);
|
---|
137 | throw new IllegalStateException("internal failure", e);
|
---|
138 | }
|
---|
139 |
|
---|
140 | return (new Offer(getAgentID(), nextBid));
|
---|
141 | }
|
---|
142 |
|
---|
143 | public int getRound() {
|
---|
144 | return ((DiscreteTimeline) timeline).getRound();
|
---|
145 | }
|
---|
146 |
|
---|
147 | public int getRoundsLeft() {
|
---|
148 | return ((DiscreteTimeline) timeline).getRoundsLeft();
|
---|
149 | }
|
---|
150 |
|
---|
151 | public int getOwnRoundsLeft() {
|
---|
152 | return ((DiscreteTimeline) timeline).getOwnRoundsLeft();
|
---|
153 | }
|
---|
154 |
|
---|
155 | public int getTotalRounds() {
|
---|
156 | return ((DiscreteTimeline) timeline).getTotalRounds();
|
---|
157 | }
|
---|
158 |
|
---|
159 | public double getTotalTime() {
|
---|
160 | return ((DiscreteTimeline) timeline).getTotalTime();
|
---|
161 | }
|
---|
162 |
|
---|
163 | // trace
|
---|
164 | void print(String s) {
|
---|
165 | System.out.println("############ " + s);
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | *
|
---|
170 | *
|
---|
171 | */
|
---|
172 | private Bid getOptimalBid() throws Exception {
|
---|
173 | print("############ B's ####################################");
|
---|
174 | print(" Round = " + getRound());
|
---|
175 | print(" RoundsLeft = " + getRoundsLeft());
|
---|
176 | print(" OwnRoundsLeft = " + getOwnRoundsLeft());
|
---|
177 | print(" TotalRounds = " + getTotalRounds());
|
---|
178 | print(" TotalTime = " + getTotalTime());
|
---|
179 |
|
---|
180 | double min = 1.0;
|
---|
181 | int roundsleft = 0;
|
---|
182 | Value optValue = null;
|
---|
183 |
|
---|
184 | print(" bids.size = " + bids.size());
|
---|
185 | print(" getOwnRoundsLeft = " + getOwnRoundsLeft());
|
---|
186 |
|
---|
187 | TreeMap<Integer, Value> T = new TreeMap<Integer, Value>(values);
|
---|
188 |
|
---|
189 | print(" T.size = " + T.size());
|
---|
190 |
|
---|
191 | for (Integer key : T.keySet()) {
|
---|
192 | roundsleft = getOwnRoundsLeft();
|
---|
193 |
|
---|
194 | Double targetBid = bids.get(roundsleft);
|
---|
195 | double piePartition = (double) key / partitions;
|
---|
196 |
|
---|
197 | if (Math.abs(targetBid - piePartition) < min) {
|
---|
198 | min = Math.abs(targetBid - piePartition);
|
---|
199 | optValue = values.get(key);
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | HashMap<Integer, Value> optVals = new HashMap<Integer, Value>();
|
---|
204 | optVals.put(pie.getNumber(), optValue);
|
---|
205 | Bid ToBid = new Bid(utilitySpace.getDomain(), optVals);
|
---|
206 |
|
---|
207 | print(" ToBid = " + ToBid);
|
---|
208 |
|
---|
209 | return ToBid; // optimal bid
|
---|
210 |
|
---|
211 | }
|
---|
212 |
|
---|
213 | @Override
|
---|
214 | public String getDescription() {
|
---|
215 | return "using the optimal stopping rule (cutoffs) for bidding";
|
---|
216 | }
|
---|
217 |
|
---|
218 | }
|
---|