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