1 | /*
|
---|
2 | * Author: Max W. Y. Lam (Aug 1 2015)
|
---|
3 | * Version: Milestone 1
|
---|
4 | *
|
---|
5 | * */
|
---|
6 |
|
---|
7 | package agents.anac.y2016.maxoops;
|
---|
8 |
|
---|
9 | import java.io.FileOutputStream;
|
---|
10 | import java.io.PrintStream;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.HashSet;
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.PriorityQueue;
|
---|
15 |
|
---|
16 | import genius.core.AgentID;
|
---|
17 | import genius.core.Bid;
|
---|
18 | import genius.core.BidHistory;
|
---|
19 | import genius.core.BidIterator;
|
---|
20 | import genius.core.Domain;
|
---|
21 | import genius.core.actions.Accept;
|
---|
22 | import genius.core.actions.Action;
|
---|
23 | import genius.core.actions.EndNegotiation;
|
---|
24 | import genius.core.actions.Inform;
|
---|
25 | import genius.core.actions.NoAction;
|
---|
26 | import genius.core.actions.Offer;
|
---|
27 | import genius.core.bidding.BidDetails;
|
---|
28 | import genius.core.parties.AbstractNegotiationParty;
|
---|
29 | import genius.core.parties.NegotiationInfo;
|
---|
30 |
|
---|
31 | public class MaxOops extends AbstractNegotiationParty {
|
---|
32 |
|
---|
33 | public boolean DEBUG = false;
|
---|
34 |
|
---|
35 | // Debug
|
---|
36 | public static PrintStream log1, log2, log3, error;
|
---|
37 | public boolean inited = false;
|
---|
38 |
|
---|
39 | // Domain Information
|
---|
40 | public Domain domain;
|
---|
41 | public double delta, theta;
|
---|
42 | public int numIssues, numBids, turn;
|
---|
43 | public double maxUtil, minUtil, secMaxUtil;
|
---|
44 | public double meanUtil, stdUtil, medianUtil, uqUtil, lqUtil;
|
---|
45 | public Bid minBid, maxBid, secMaxBid, lastBid;
|
---|
46 | public Action myLastAction, prevLastAction;
|
---|
47 | public BidHistory myBidHistory;
|
---|
48 | public BidHistory allBidHistory;
|
---|
49 | public ArrayList<HashSet<Bid>> hashBids;
|
---|
50 |
|
---|
51 | // Opponents Information
|
---|
52 | public int currentPartyID, numParties;
|
---|
53 | public int opponentsMinNumDistinctAccepts, opponentsMaxNumDistinctAccepts;
|
---|
54 | public int opponentsMinNumDistinctBids, opponentsMaxNumDistinctBids;
|
---|
55 | public Action[] opponentsLastActions;
|
---|
56 | public BidHistory[] opponentsBidHistories;
|
---|
57 | public BidHistory[] opponentsDistinctBids;
|
---|
58 | public BidHistory[] opponentsDistinctAccepts;
|
---|
59 |
|
---|
60 | // User-defined Information
|
---|
61 | public AgentParameters params;
|
---|
62 | public TFComponent TFC;
|
---|
63 | public DMComponent DMC;
|
---|
64 |
|
---|
65 | public MaxOops() throws Exception {
|
---|
66 | super();
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public void init(NegotiationInfo info) {
|
---|
71 | super.init(info);
|
---|
72 | try {
|
---|
73 | init();
|
---|
74 | } catch (Exception e) {
|
---|
75 | e.printStackTrace();
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | private void init() throws Exception {
|
---|
80 | // Debug
|
---|
81 | if (DEBUG) {
|
---|
82 | String dir = System.getProperty("user.dir");
|
---|
83 | log1 = new PrintStream(
|
---|
84 | new FileOutputStream(dir + "/log1.txt", false));
|
---|
85 | log2 = new PrintStream(
|
---|
86 | new FileOutputStream(dir + "/log2.txt", false));
|
---|
87 | log3 = new PrintStream(
|
---|
88 | new FileOutputStream(dir + "/log3.txt", false));
|
---|
89 | error = new PrintStream(
|
---|
90 | new FileOutputStream(dir + "/error.txt", false));
|
---|
91 | System.setErr(error);
|
---|
92 | } else {
|
---|
93 | log1 = log2 = log3 = System.out;
|
---|
94 | }
|
---|
95 |
|
---|
96 | // Initialize Domain Information
|
---|
97 | inited = true;
|
---|
98 | opponentsMinNumDistinctAccepts = 0;
|
---|
99 | opponentsMaxNumDistinctAccepts = 0;
|
---|
100 | opponentsMinNumDistinctBids = 0;
|
---|
101 | opponentsMaxNumDistinctBids = 0;
|
---|
102 | myLastAction = null;
|
---|
103 | prevLastAction = null;
|
---|
104 | myBidHistory = new BidHistory();
|
---|
105 | allBidHistory = new BidHistory();
|
---|
106 | domain = utilitySpace.getDomain();
|
---|
107 | numBids = (int) domain.getNumberOfPossibleBids();
|
---|
108 | delta = utilitySpace.getDiscountFactor();
|
---|
109 | theta = utilitySpace.getReservationValueUndiscounted();
|
---|
110 | numIssues = utilitySpace.getMinUtilityBid().getIssues().size();
|
---|
111 | minBid = utilitySpace.getMinUtilityBid();
|
---|
112 | maxBid = utilitySpace.getMaxUtilityBid();
|
---|
113 | lastBid = minBid;
|
---|
114 | lqUtil = minUtil * 0.75 + maxUtil * 0.25;
|
---|
115 | uqUtil = minUtil * 0.25 + maxUtil * 0.75;
|
---|
116 | meanUtil = (minUtil + maxUtil) / 2.;
|
---|
117 | minUtil = utilitySpace.getUtility(minBid);
|
---|
118 | maxUtil = utilitySpace.getUtility(maxBid);
|
---|
119 | secMaxUtil = minUtil;
|
---|
120 | hashBids = new ArrayList<HashSet<Bid>>();
|
---|
121 | for (int i = 0; i <= 100; i++) {
|
---|
122 | hashBids.add(new HashSet<Bid>());
|
---|
123 | }
|
---|
124 | PriorityQueue<Double> sortedUtils = new PriorityQueue<Double>();
|
---|
125 | int i = 0;
|
---|
126 | BidIterator bidIterator = new BidIterator(domain);
|
---|
127 | while (bidIterator.hasNext()) {
|
---|
128 | Bid b = bidIterator.next();
|
---|
129 | double util = utilitySpace.getUtility(b);
|
---|
130 | int bidInd = (int) (util * 100);
|
---|
131 | hashBids.get(bidInd).add(b);
|
---|
132 | if (util > secMaxUtil && util < maxUtil) {
|
---|
133 | secMaxUtil = util;
|
---|
134 | secMaxBid = b;
|
---|
135 | }
|
---|
136 | sortedUtils.add(util);
|
---|
137 | meanUtil += (util - meanUtil) / (i + 1);
|
---|
138 | stdUtil = (i * stdUtil * stdUtil) / (i + 1);
|
---|
139 | stdUtil = Math
|
---|
140 | .sqrt(stdUtil + Math.pow(util - meanUtil, 2) / (i + 1));
|
---|
141 | i++;
|
---|
142 | }
|
---|
143 | while (sortedUtils.size() > (numBids * 3) / 4) {
|
---|
144 | lqUtil = sortedUtils.remove();
|
---|
145 | }
|
---|
146 | while (sortedUtils.size() > numBids / 2) {
|
---|
147 | medianUtil = sortedUtils.remove();
|
---|
148 | }
|
---|
149 | while (sortedUtils.size() > numBids / 4) {
|
---|
150 | uqUtil = sortedUtils.remove();
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | public long getCurrentTurn() {
|
---|
155 | return this.turn;
|
---|
156 | }
|
---|
157 |
|
---|
158 | public long getCurrentRound() {
|
---|
159 | return this.turn / this.numParties;
|
---|
160 | }
|
---|
161 |
|
---|
162 | public int getCurrentOpponent() {
|
---|
163 | return this.getCurrentParty() - 1;
|
---|
164 | }
|
---|
165 |
|
---|
166 | public int getCurrentParty() {
|
---|
167 | return this.turn % this.numParties;
|
---|
168 | }
|
---|
169 |
|
---|
170 | @Override
|
---|
171 | public String getDescription() {
|
---|
172 | return "ANAC2016";
|
---|
173 | }
|
---|
174 |
|
---|
175 | @Override
|
---|
176 | public void receiveMessage(AgentID sender, Action action) {
|
---|
177 | super.receiveMessage(sender, action);
|
---|
178 | if (sender == null) {
|
---|
179 | turn = 0;
|
---|
180 | Inform inform = (Inform) action;
|
---|
181 | numParties = ((Integer) inform.getValue()).intValue();
|
---|
182 | opponentsLastActions = new Action[numParties - 1];
|
---|
183 | opponentsBidHistories = new BidHistory[numParties - 1];
|
---|
184 | opponentsDistinctBids = new BidHistory[numParties - 1];
|
---|
185 | opponentsDistinctAccepts = new BidHistory[numParties - 1];
|
---|
186 | for (int i = 0; i < numParties - 1; i++) {
|
---|
187 | opponentsLastActions[i] = null;
|
---|
188 | opponentsBidHistories[i] = new BidHistory();
|
---|
189 | opponentsDistinctBids[i] = new BidHistory();
|
---|
190 | opponentsDistinctAccepts[i] = new BidHistory();
|
---|
191 | }
|
---|
192 | params = new AgentParameters();
|
---|
193 | TFC = new TFComponent(this, timeline);
|
---|
194 | DMC = new DMComponent(this, utilitySpace, timeline);
|
---|
195 | return;
|
---|
196 | }
|
---|
197 | prevLastAction = action;
|
---|
198 | int opponent = getCurrentOpponent();
|
---|
199 | if (opponent < 0) {
|
---|
200 | opponent += numParties - 1;
|
---|
201 | }
|
---|
202 | double time = timeline.getTime();
|
---|
203 | opponentsLastActions[opponent] = action;
|
---|
204 | if (action instanceof Offer) {
|
---|
205 | Offer offer = (Offer) (action);
|
---|
206 | Bid bid = offer.getBid();
|
---|
207 | lastBid = bid;
|
---|
208 | double util = utilitySpace.getUtility(bid);
|
---|
209 | if (opponentsBidHistories[opponent].isEmpty()) {
|
---|
210 | DMC.bidsOpt.payoffs[opponent].initWeights(bid);
|
---|
211 | } else {
|
---|
212 | DMC.bidsOpt.payoffs[opponent].updateWeights(bid);
|
---|
213 | }
|
---|
214 | opponentsBidHistories[opponent]
|
---|
215 | .add(new BidDetails(bid, util, time));
|
---|
216 | allBidHistory.add(new BidDetails(bid, util, time));
|
---|
217 | TFC.recordUtility(util, opponent);
|
---|
218 | if (opponentsDistinctBids[opponent].filterUtility(util).isEmpty()) {
|
---|
219 | opponentsDistinctBids[opponent]
|
---|
220 | .add(new BidDetails(bid, util, time));
|
---|
221 | opponentsMinNumDistinctBids = opponentsDistinctBids[opponent]
|
---|
222 | .size();
|
---|
223 | opponentsMaxNumDistinctBids = opponentsDistinctBids[opponent]
|
---|
224 | .size();
|
---|
225 | for (int i = 0; i < numParties - 1; i++) {
|
---|
226 | if (i == opponent)
|
---|
227 | continue;
|
---|
228 | opponentsMinNumDistinctBids = Math.min(
|
---|
229 | opponentsMinNumDistinctBids,
|
---|
230 | opponentsDistinctBids[i].size());
|
---|
231 | opponentsMaxNumDistinctBids = Math.max(
|
---|
232 | opponentsMaxNumDistinctBids,
|
---|
233 | opponentsDistinctBids[i].size());
|
---|
234 | }
|
---|
235 | }
|
---|
236 | } else if (action instanceof Accept) {
|
---|
237 | double util = utilitySpace.getUtility(lastBid);
|
---|
238 | DMC.bidsOpt.payoffs[opponent].updateWeights(lastBid);
|
---|
239 | if (opponentsDistinctAccepts[opponent].filterUtility(util)
|
---|
240 | .isEmpty()) {
|
---|
241 | opponentsDistinctAccepts[opponent]
|
---|
242 | .add(new BidDetails(lastBid, util, time));
|
---|
243 | opponentsMinNumDistinctAccepts = opponentsDistinctAccepts[opponent]
|
---|
244 | .size();
|
---|
245 | opponentsMaxNumDistinctAccepts = opponentsDistinctAccepts[opponent]
|
---|
246 | .size();
|
---|
247 | for (int i = 0; i < numParties - 1; i++) {
|
---|
248 | if (i == opponent)
|
---|
249 | continue;
|
---|
250 | opponentsMinNumDistinctAccepts = Math.min(
|
---|
251 | opponentsMinNumDistinctAccepts,
|
---|
252 | opponentsDistinctAccepts[i].size());
|
---|
253 | opponentsMaxNumDistinctAccepts = Math.max(
|
---|
254 | opponentsMaxNumDistinctAccepts,
|
---|
255 | opponentsDistinctAccepts[i].size());
|
---|
256 | }
|
---|
257 | }
|
---|
258 | }
|
---|
259 | if (turn != 0)
|
---|
260 | turn++;
|
---|
261 | }
|
---|
262 |
|
---|
263 | @Override
|
---|
264 | public Action chooseAction(List<Class<? extends Action>> possibleActions) {
|
---|
265 | Action action = new NoAction(getPartyId());
|
---|
266 | try {
|
---|
267 | Bid nextBid = null;
|
---|
268 | if (possibleActions.contains(Accept.class)
|
---|
269 | || possibleActions.contains(Offer.class)) {
|
---|
270 | if (myLastAction == null) {
|
---|
271 | nextBid = secMaxBid;
|
---|
272 | if (maxUtil - secMaxUtil < (secMaxUtil - minUtil) * 1.1) {
|
---|
273 | nextBid = maxBid;
|
---|
274 | }
|
---|
275 | action = new Offer(getPartyId(), nextBid);
|
---|
276 | } else if (lastBid.equals(maxBid)) {
|
---|
277 | System.out.println("Accepted!!!!");
|
---|
278 | action = new Accept(getPartyId(), lastBid);
|
---|
279 | } else {
|
---|
280 | if (maxUtil - secMaxUtil < (secMaxUtil - minUtil) * 1.1
|
---|
281 | && lastBid.equals(secMaxBid)) {
|
---|
282 | System.out.println("Accepted!!!!");
|
---|
283 | action = new Accept(getPartyId(), lastBid);
|
---|
284 | } else {
|
---|
285 | if (DMC.termination()) {
|
---|
286 | System.out.println("Terminated!!!!");
|
---|
287 | action = new EndNegotiation(getPartyId());
|
---|
288 | } else {
|
---|
289 | if (DMC.acceptance()) {
|
---|
290 | System.out.println("Accepted!!!!");
|
---|
291 | action = new Accept(getPartyId(), lastBid);
|
---|
292 | } else {
|
---|
293 | nextBid = DMC.bidProposal();
|
---|
294 | if (nextBid == null) {
|
---|
295 | nextBid = secMaxBid;
|
---|
296 | if (maxUtil - secMaxUtil < (secMaxUtil
|
---|
297 | - minUtil) * 1.1) {
|
---|
298 | nextBid = maxBid;
|
---|
299 | }
|
---|
300 | }
|
---|
301 | action = new Offer(getPartyId(), nextBid);
|
---|
302 | }
|
---|
303 | }
|
---|
304 | }
|
---|
305 | }
|
---|
306 | }
|
---|
307 | turn++;
|
---|
308 | if (nextBid != null) {
|
---|
309 | allBidHistory.add(new BidDetails(nextBid, TFC.thresholdFunc(),
|
---|
310 | timeline.getTime()));
|
---|
311 | }
|
---|
312 | TFC.recordUtility(TFC.thresholdFunc(), -1);
|
---|
313 | myLastAction = action;
|
---|
314 | } catch (Exception e) {
|
---|
315 | System.out.println((new StringBuilder("chooseAction failed!!\n")));
|
---|
316 | e.printStackTrace(System.out);
|
---|
317 | e.printStackTrace();
|
---|
318 | }
|
---|
319 | return action;
|
---|
320 | }
|
---|
321 |
|
---|
322 | }
|
---|