1 | package agents.anac.y2017.rubick;
|
---|
2 | /*
|
---|
3 | * OKAN TUNALI -
|
---|
4 | */
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.LinkedHashMap;
|
---|
8 | import java.util.LinkedList;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import genius.core.AgentID;
|
---|
12 | import genius.core.Bid;
|
---|
13 | import genius.core.actions.Accept;
|
---|
14 | import genius.core.actions.Action;
|
---|
15 | import genius.core.actions.Offer;
|
---|
16 | import genius.core.bidding.BidDetails;
|
---|
17 | import genius.core.boaframework.SortedOutcomeSpace;
|
---|
18 | import genius.core.issue.Issue;
|
---|
19 | import genius.core.issue.IssueDiscrete;
|
---|
20 | import genius.core.issue.Value;
|
---|
21 | import genius.core.list.Tuple;
|
---|
22 | import genius.core.parties.AbstractNegotiationParty;
|
---|
23 | import genius.core.parties.NegotiationInfo;
|
---|
24 | import genius.core.persistent.StandardInfo;
|
---|
25 | import genius.core.persistent.StandardInfoList;
|
---|
26 |
|
---|
27 | public class Rubick extends AbstractNegotiationParty {
|
---|
28 |
|
---|
29 | private Bid lastReceivedBid = null;
|
---|
30 | private StandardInfoList history;
|
---|
31 | private LinkedList<String> parties = new LinkedList<>();
|
---|
32 | private LinkedList<LinkedList<Double>> histOpp0 = new LinkedList<>();
|
---|
33 | private LinkedList<LinkedList<Double>> histOpp1 = new LinkedList<>();
|
---|
34 | private boolean isHistoryAnalyzed = false;
|
---|
35 | private int numberOfReceivedOffer = 0;
|
---|
36 | private LinkedList<Integer> profileOrder = new LinkedList<>();
|
---|
37 | private String[] opponentNames = new String[2];
|
---|
38 | private double[] acceptanceLimits = { 0.0, 0.0 };
|
---|
39 | private double maxReceivedBidutil = 0.0;
|
---|
40 | private String lastpartyname = "";
|
---|
41 | private SortedOutcomeSpace sos = null;
|
---|
42 | private LinkedList<Bid> bestAcceptedBids = new LinkedList<>();
|
---|
43 | private double threshold = 0;
|
---|
44 |
|
---|
45 | protected LinkedList<LinkedHashMap<Value, Integer>> frequentValuesList0 = new LinkedList<>();
|
---|
46 | protected LinkedList<LinkedHashMap<Value, Integer>> frequentValuesList1 = new LinkedList<>();
|
---|
47 |
|
---|
48 | ArrayList<Value> opp0bag = new ArrayList<Value>();
|
---|
49 | ArrayList<Value> opp1bag = new ArrayList<Value>();
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public void init(NegotiationInfo info) {
|
---|
53 |
|
---|
54 | super.init(info);
|
---|
55 |
|
---|
56 | String domainName = utilitySpace.getFileName();
|
---|
57 | domainName = domainName.substring(domainName.lastIndexOf("/") + 1,
|
---|
58 | domainName.lastIndexOf("."));
|
---|
59 |
|
---|
60 | sos = new SortedOutcomeSpace(utilitySpace);
|
---|
61 | history = (StandardInfoList) getData().get();
|
---|
62 |
|
---|
63 | sortPartyProfiles(getPartyId().toString());
|
---|
64 | threshold = info.getUtilitySpace().getReservationValue();
|
---|
65 | maxReceivedBidutil = threshold; // to see the logic
|
---|
66 | initializeOpponentModelling();
|
---|
67 | }
|
---|
68 |
|
---|
69 | @SuppressWarnings("unchecked")
|
---|
70 | public void initializeOpponentModelling() {
|
---|
71 |
|
---|
72 | int issueSize = utilitySpace.getDomain().getIssues().size();
|
---|
73 |
|
---|
74 | for (int i = 0; i < issueSize; i++) {
|
---|
75 | LinkedHashMap<Value, Integer> valueAmountMap = new LinkedHashMap<>();
|
---|
76 |
|
---|
77 | frequentValuesList0.add(valueAmountMap); // first add an empty map.
|
---|
78 | valueAmountMap = new LinkedHashMap<>();
|
---|
79 | frequentValuesList1.add(valueAmountMap);
|
---|
80 |
|
---|
81 | int issuecnt = 0;
|
---|
82 | for (Issue issue : utilitySpace.getDomain().getIssues()) {
|
---|
83 | IssueDiscrete issued = (IssueDiscrete) issue;
|
---|
84 |
|
---|
85 | if (issuecnt == i) {
|
---|
86 | for (Value value : issued.getValues()) {
|
---|
87 | frequentValuesList0.get(i).put(value, 0);
|
---|
88 | frequentValuesList1.get(i).put(value, 0);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | issuecnt++;
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public Action chooseAction(List<Class<? extends Action>> validActions) {
|
---|
98 |
|
---|
99 | double decisiveUtil = checkAcceptance();
|
---|
100 |
|
---|
101 | if (decisiveUtil == -1) {
|
---|
102 | return new Accept(getPartyId(), lastReceivedBid);
|
---|
103 | } else {
|
---|
104 | Bid bid = generateBid(decisiveUtil);
|
---|
105 | return new Offer(getPartyId(), bid);
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | public void receiveMessage(AgentID sender, Action action) {
|
---|
111 | super.receiveMessage(sender, action);
|
---|
112 | if (action instanceof Accept) {
|
---|
113 |
|
---|
114 | Bid acceptedBid = ((Accept) action).getBid();
|
---|
115 |
|
---|
116 | if (bestAcceptedBids.isEmpty()) {
|
---|
117 | bestAcceptedBids.add(acceptedBid);
|
---|
118 | } else if (!bestAcceptedBids.contains(acceptedBid)) {
|
---|
119 | int size = bestAcceptedBids.size();
|
---|
120 | for (int i = 0; i < size; i++) {
|
---|
121 | if (getUtility(acceptedBid) > getUtility(
|
---|
122 | bestAcceptedBids.get(i))) {
|
---|
123 | bestAcceptedBids.add(i, acceptedBid); // collect best
|
---|
124 | // accepted
|
---|
125 | // Bids.
|
---|
126 | break;
|
---|
127 | } else if (i == bestAcceptedBids.size() - 1) { // if new
|
---|
128 | // accepted
|
---|
129 | // bid has
|
---|
130 | // the least
|
---|
131 | // util in
|
---|
132 | // the list.
|
---|
133 | bestAcceptedBids.add(acceptedBid);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | if (action instanceof Offer) {
|
---|
140 | lastReceivedBid = ((Offer) action).getBid();
|
---|
141 |
|
---|
142 | if (maxReceivedBidutil < getUtilityWithDiscount(lastReceivedBid))
|
---|
143 | maxReceivedBidutil = getUtilityWithDiscount(lastReceivedBid)
|
---|
144 | * 0.95;
|
---|
145 |
|
---|
146 | numberOfReceivedOffer++;
|
---|
147 |
|
---|
148 | String partyName = getPartyName(action.getAgent().toString());
|
---|
149 | lastpartyname = partyName;
|
---|
150 |
|
---|
151 | BidResolver(lastReceivedBid, partyName);
|
---|
152 |
|
---|
153 | if (!parties.contains(partyName)) {
|
---|
154 | sortPartyProfiles(action.getAgent().toString());
|
---|
155 | }
|
---|
156 |
|
---|
157 | if (parties.size() == 3 && !history.isEmpty()
|
---|
158 | && isHistoryAnalyzed == false) {
|
---|
159 | // System.out.println("about to analyze: ");
|
---|
160 | analyzeHistory();
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | private int takeTheChance(double maxReceived) {
|
---|
166 |
|
---|
167 | int pow = 1;
|
---|
168 | double chance = rand.nextDouble();
|
---|
169 | // System.out.println("chance: " + chance);
|
---|
170 | if (chance > 0.95 + 0.05 * maxReceived)
|
---|
171 | pow = 2;
|
---|
172 | else if (chance > 0.93 + 0.07 * maxReceived)
|
---|
173 | pow = 3;
|
---|
174 | else
|
---|
175 | pow = 10;
|
---|
176 |
|
---|
177 | return pow;
|
---|
178 | }
|
---|
179 |
|
---|
180 | private double checkAcceptance() {
|
---|
181 |
|
---|
182 | int pow = takeTheChance(maxReceivedBidutil);
|
---|
183 | double targetutil = 1 - (Math.pow(timeline.getTime(), pow)
|
---|
184 | * Math.abs(rand.nextGaussian() / 3));
|
---|
185 |
|
---|
186 | if (numberOfReceivedOffer < 2)
|
---|
187 | return 1;
|
---|
188 | else if (!history.isEmpty()) {
|
---|
189 | double upperLimit = maxReceivedBidutil;
|
---|
190 |
|
---|
191 | // pick the highest as the upper limit
|
---|
192 | for (double du : acceptanceLimits) {
|
---|
193 | if (upperLimit < du)
|
---|
194 | upperLimit = du;
|
---|
195 | }
|
---|
196 | upperLimit = 0.90 * upperLimit;
|
---|
197 | pow = takeTheChance(upperLimit);
|
---|
198 | targetutil = 1 - (Math.pow(timeline.getTime(), pow)
|
---|
199 | * Math.abs(rand.nextGaussian() / 3));
|
---|
200 | targetutil = upperLimit + (1 - upperLimit) * targetutil;
|
---|
201 |
|
---|
202 | } else {
|
---|
203 |
|
---|
204 | if (maxReceivedBidutil < 0.8)
|
---|
205 | maxReceivedBidutil = 0.8;
|
---|
206 |
|
---|
207 | targetutil = maxReceivedBidutil
|
---|
208 | + (1 - maxReceivedBidutil) * targetutil;
|
---|
209 |
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (getUtilityWithDiscount(lastReceivedBid) > targetutil
|
---|
213 | || timeline.getTime() > 0.999)
|
---|
214 | return -1; // Accept
|
---|
215 | else
|
---|
216 | return targetutil;
|
---|
217 | }
|
---|
218 |
|
---|
219 | private Bid generateBid(double targetutil) {
|
---|
220 | Bid bid = null;
|
---|
221 |
|
---|
222 | if (timeline.getTime() > 0.995 && !bestAcceptedBids.isEmpty()) {
|
---|
223 | // game has almost ended. Offer one from best accepted bids
|
---|
224 | int s = bestAcceptedBids.size();
|
---|
225 |
|
---|
226 | if (s > 3)
|
---|
227 | s = 3;
|
---|
228 |
|
---|
229 | // pick from top 3
|
---|
230 | int ind = rand.nextInt(s);
|
---|
231 | bid = bestAcceptedBids.get(ind);
|
---|
232 | } else {
|
---|
233 |
|
---|
234 | // find candidate bids in range target utility and 1
|
---|
235 | if (opp0bag.size() > 0 && opp1bag.size() > 0)
|
---|
236 | bid = searchCandidateBids(targetutil);
|
---|
237 |
|
---|
238 | if (bid == null)
|
---|
239 | bid = sos.getBidNearUtility(targetutil).getBid();
|
---|
240 | }
|
---|
241 |
|
---|
242 | System.out.flush();
|
---|
243 | return bid;
|
---|
244 |
|
---|
245 | }
|
---|
246 |
|
---|
247 | public Bid searchCandidateBids(double targetutil) {
|
---|
248 | double bu = 0.0;
|
---|
249 | Value valu = null;
|
---|
250 | // search for maximum match
|
---|
251 | LinkedList<Integer> intersection = new LinkedList<>();
|
---|
252 | LinkedList<Bid> candidateBids = new LinkedList<>();
|
---|
253 |
|
---|
254 | for (BidDetails bd : sos.getAllOutcomes()) {
|
---|
255 | bu = getUtility(bd.getBid());
|
---|
256 |
|
---|
257 | if (bu >= targetutil) {
|
---|
258 | int score = 0;
|
---|
259 | for (int isn = 0; isn < bd.getBid().getIssues().size(); isn++) {
|
---|
260 | valu = bd.getBid().getValue(isn + 1);
|
---|
261 |
|
---|
262 | if (valu == opp0bag.get(isn))
|
---|
263 | score++;
|
---|
264 |
|
---|
265 | if (valu == opp1bag.get(isn))
|
---|
266 | score++;
|
---|
267 | }
|
---|
268 |
|
---|
269 | intersection.add(score);
|
---|
270 | candidateBids.add(bd.getBid());
|
---|
271 |
|
---|
272 | } else
|
---|
273 | break;
|
---|
274 | }
|
---|
275 |
|
---|
276 | int max = -1;
|
---|
277 | for (int i = 0; i < intersection.size(); i++) {
|
---|
278 | if (max < intersection.get(i))
|
---|
279 | max = i; // if find find higher score, make it max.
|
---|
280 | }
|
---|
281 |
|
---|
282 | if (candidateBids.size() > 1) {
|
---|
283 | return candidateBids.get(max);
|
---|
284 | }
|
---|
285 |
|
---|
286 | return null;
|
---|
287 | }
|
---|
288 |
|
---|
289 | public void BidResolver(Bid bid, String partyname) {
|
---|
290 | Value valu = null;
|
---|
291 | if (partyname.equals(opponentNames[0])) {
|
---|
292 |
|
---|
293 | for (int isn = 0; isn < bid.getIssues().size(); isn++) {
|
---|
294 | valu = bid.getValue(isn + 1);
|
---|
295 | int prevAmount = frequentValuesList0.get(isn).get(valu);
|
---|
296 |
|
---|
297 | frequentValuesList0.get(isn).put(valu, prevAmount + 1);
|
---|
298 | }
|
---|
299 |
|
---|
300 | } else if (partyname.equals(opponentNames[1])) {
|
---|
301 | for (int isn = 0; isn < bid.getIssues().size(); isn++) {
|
---|
302 | valu = bid.getValue(isn + 1);
|
---|
303 | int prevAmount = frequentValuesList1.get(isn).get(valu);
|
---|
304 | frequentValuesList1.get(isn).put(valu, prevAmount + 1);
|
---|
305 | }
|
---|
306 | }
|
---|
307 |
|
---|
308 | if (numberOfReceivedOffer > 2)
|
---|
309 | extractOpponentPreferences();
|
---|
310 |
|
---|
311 | }
|
---|
312 |
|
---|
313 | public void printFreqs(int opid) {
|
---|
314 |
|
---|
315 | System.out.println("opid : " + opid);
|
---|
316 | for (int i = 0; i < frequentValuesList0.size(); i++) {
|
---|
317 |
|
---|
318 | if (opid == 0) {
|
---|
319 | for (Value val : frequentValuesList0.get(i).keySet()) {
|
---|
320 | System.out.println("freq0: is: " + (i + 1) + " value :"
|
---|
321 | + val + " amount: "
|
---|
322 | + frequentValuesList0.get(i).get(val));
|
---|
323 | }
|
---|
324 | } else {
|
---|
325 | for (Value val : frequentValuesList1.get(i).keySet()) {
|
---|
326 | System.out.println("freq1: is: " + (i + 1) + " value :"
|
---|
327 | + val + " amount: "
|
---|
328 | + frequentValuesList1.get(i).get(val));
|
---|
329 | }
|
---|
330 | }
|
---|
331 | System.out.println("\n");
|
---|
332 | }
|
---|
333 | System.out.println("\n");
|
---|
334 |
|
---|
335 | }
|
---|
336 |
|
---|
337 | public void extractOpponentPreferences() {
|
---|
338 | // find the best intersection
|
---|
339 | ArrayList<Value> opp0priors = new ArrayList<Value>();
|
---|
340 | ArrayList<Value> opp1priors = new ArrayList<Value>();
|
---|
341 |
|
---|
342 | opp0bag = new ArrayList<Value>();
|
---|
343 | opp1bag = new ArrayList<Value>();
|
---|
344 |
|
---|
345 | LinkedList<Double> meanEvalValues0 = new LinkedList<>();
|
---|
346 | LinkedList<Double> meanEvalValues1 = new LinkedList<>();
|
---|
347 |
|
---|
348 | for (int i = 0; i < frequentValuesList0.size(); i++) {
|
---|
349 | double sum = 0.0;
|
---|
350 | for (Value val : frequentValuesList0.get(i).keySet()) {
|
---|
351 | sum += frequentValuesList0.get(i).get(val); // find the average
|
---|
352 | // eval value of
|
---|
353 | // that issue
|
---|
354 | }
|
---|
355 | meanEvalValues0.add(sum / frequentValuesList0.size());
|
---|
356 |
|
---|
357 | sum = 0.0;
|
---|
358 | for (Value val : frequentValuesList1.get(i).keySet()) {
|
---|
359 | sum += frequentValuesList1.get(i).get(val); // find the average
|
---|
360 | // eval value of
|
---|
361 | // that issue
|
---|
362 | }
|
---|
363 | meanEvalValues1.add(sum / frequentValuesList1.size());
|
---|
364 | }
|
---|
365 |
|
---|
366 | // select ones with over average
|
---|
367 | for (int i = 0; i < frequentValuesList0.size(); i++) {
|
---|
368 | for (Value val : frequentValuesList0.get(i).keySet()) {
|
---|
369 | if (frequentValuesList0.get(i).get(val) >= meanEvalValues0
|
---|
370 | .get(i)) {
|
---|
371 | opp0priors.add(val);
|
---|
372 | }
|
---|
373 | }
|
---|
374 | opp0bag.add(opp0priors.get(rand.nextInt(opp0priors.size())));
|
---|
375 | opp0priors = new ArrayList<Value>();
|
---|
376 |
|
---|
377 | for (Value val : frequentValuesList1.get(i).keySet()) {
|
---|
378 | if (frequentValuesList1.get(i).get(val) >= meanEvalValues1
|
---|
379 | .get(i)) {
|
---|
380 | opp1priors.add(val);
|
---|
381 | }
|
---|
382 | }
|
---|
383 | opp1bag.add(opp1priors.get(rand.nextInt(opp1priors.size())));
|
---|
384 | opp1priors = new ArrayList<Value>();
|
---|
385 |
|
---|
386 | }
|
---|
387 |
|
---|
388 | }
|
---|
389 |
|
---|
390 | private Integer extractPartyID(String partyID) {
|
---|
391 | return Integer.parseInt(
|
---|
392 | partyID.substring(partyID.indexOf("@") + 1, partyID.length()));
|
---|
393 | }
|
---|
394 |
|
---|
395 | private void analyzeHistory() {
|
---|
396 | isHistoryAnalyzed = true;
|
---|
397 |
|
---|
398 | for (int h = 0; h <= history.size() - 1; h++) { // from older to recent
|
---|
399 | // history
|
---|
400 |
|
---|
401 | LinkedList<Double> utilsOp1 = new LinkedList<>();
|
---|
402 | LinkedList<Double> utilsOp2 = new LinkedList<>();
|
---|
403 |
|
---|
404 | StandardInfo info = history.get(h);
|
---|
405 |
|
---|
406 | boolean historyMatch = true;
|
---|
407 |
|
---|
408 | int cnt = 0;
|
---|
409 | for (Tuple<String, Double> offered : info.getUtilities()) {
|
---|
410 |
|
---|
411 | String partyname = getPartyName(offered.get1());
|
---|
412 | Double util = offered.get2();
|
---|
413 |
|
---|
414 | if (cnt < 3 && !partyname.equals(parties.get(cnt))) {
|
---|
415 | historyMatch = false;
|
---|
416 | break;
|
---|
417 | } else {
|
---|
418 | // check if there's a confusion
|
---|
419 |
|
---|
420 | if (partyname.equals(opponentNames[0])) {
|
---|
421 | utilsOp1.add(util);
|
---|
422 | if (util > acceptanceLimits[0])
|
---|
423 | acceptanceLimits[0] = util;
|
---|
424 |
|
---|
425 | } else if (partyname.equals(opponentNames[1])) {
|
---|
426 | utilsOp2.add(util);
|
---|
427 | if (util > acceptanceLimits[1])
|
---|
428 | acceptanceLimits[1] = util;
|
---|
429 | }
|
---|
430 |
|
---|
431 | }
|
---|
432 | cnt++;
|
---|
433 | }
|
---|
434 |
|
---|
435 | }
|
---|
436 |
|
---|
437 | }
|
---|
438 |
|
---|
439 | private void sortPartyProfiles(String partyID) {
|
---|
440 |
|
---|
441 | // System.out.println("\ninSorting ID: " + partyID);
|
---|
442 | int pid = extractPartyID(partyID);
|
---|
443 | String partyName = getPartyName(partyID);
|
---|
444 |
|
---|
445 | if (profileOrder.isEmpty()) {
|
---|
446 |
|
---|
447 | profileOrder.add(pid);
|
---|
448 | parties.add(partyName);
|
---|
449 |
|
---|
450 | } else {
|
---|
451 | int size = profileOrder.size();
|
---|
452 | for (int id = 0; id < size; id++) {
|
---|
453 |
|
---|
454 | if (pid < profileOrder.get(id)) { // Find smaller put instead of
|
---|
455 | // it ~ before it
|
---|
456 | profileOrder.add(id, pid);
|
---|
457 | parties.add(id, partyName);
|
---|
458 | break;
|
---|
459 | } else if (id == profileOrder.size() - 1) {
|
---|
460 | profileOrder.add(pid);
|
---|
461 | parties.add(partyName);
|
---|
462 | }
|
---|
463 |
|
---|
464 | }
|
---|
465 | }
|
---|
466 |
|
---|
467 | int p = 0;
|
---|
468 | for (String party : parties) {
|
---|
469 |
|
---|
470 | // if it's not my name
|
---|
471 | if (!party.equals(getPartyName(getPartyId().toString()))) {
|
---|
472 | {
|
---|
473 | opponentNames[p] = party;
|
---|
474 | p++;
|
---|
475 | }
|
---|
476 |
|
---|
477 | }
|
---|
478 | }
|
---|
479 |
|
---|
480 | }
|
---|
481 |
|
---|
482 | private String getPartyName(String partyID) {
|
---|
483 | return partyID.substring(0, partyID.indexOf("@"));
|
---|
484 | }
|
---|
485 |
|
---|
486 | @Override
|
---|
487 | public String getDescription() {
|
---|
488 | return "ANAC2017";
|
---|
489 | }
|
---|
490 |
|
---|
491 | }
|
---|