1 | package geniusweb.exampleparties.anaconda;
|
---|
2 |
|
---|
3 | import java.math.BigInteger;
|
---|
4 | import java.util.*;
|
---|
5 |
|
---|
6 | import geniusweb.bidspace.AllBidsList;
|
---|
7 | import geniusweb.issuevalue.Bid;
|
---|
8 | import geniusweb.issuevalue.Domain;
|
---|
9 | import geniusweb.issuevalue.Value;
|
---|
10 | import geniusweb.issuevalue.ValueSet;
|
---|
11 | import geniusweb.profile.PartialOrdering;
|
---|
12 | import tudelft.utilities.immutablelist.Tuple;
|
---|
13 |
|
---|
14 | @SuppressWarnings("serial")
|
---|
15 | public class selfMap extends HashMap<String, List<impUnit>> {
|
---|
16 | private Domain domain;
|
---|
17 | private HashMap<Bid, Double> bidsRank;
|
---|
18 | private List<Tuple<Bid, Double>> sorted_bids;
|
---|
19 |
|
---|
20 | // importance map
|
---|
21 | public selfMap(PartialOrdering profile) {
|
---|
22 | super();
|
---|
23 | this.domain = profile.getDomain();
|
---|
24 | this.bidsRank = new HashMap<Bid, Double>();
|
---|
25 | // Create empty my import map and opponent's value map
|
---|
26 | for (String issue : domain.getIssues()) {
|
---|
27 | ValueSet values = domain.getValues(issue);
|
---|
28 | List<impUnit> issueImpUnit = new ArrayList<>();
|
---|
29 | for (Value value : values) {
|
---|
30 | issueImpUnit.add(new impUnit(value));
|
---|
31 | }
|
---|
32 | this.put(issue, issueImpUnit);
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | public void comparison_update(Bid betterBid, Bid worseBid) {
|
---|
37 | //update of freq table
|
---|
38 | for (String issue : betterBid.getIssues()) {
|
---|
39 | List<impUnit> currentIssueList = this.get(issue);
|
---|
40 | for (impUnit currentUnit : currentIssueList) {
|
---|
41 | if (currentUnit.valueOfIssue.toString().equals(betterBid.getValue(issue).toString())) {
|
---|
42 | //System.out.println(issue+":"+betterBid.getValue(issue).toString() + "&" + worseBid.getValue(issue).toString());
|
---|
43 | //System.out.println(betterBid.getValue(issue).toString() != worseBid.getValue(issue).toString());
|
---|
44 | if (!betterBid.getValue(issue).toString().equals(worseBid.getValue(issue).toString()))
|
---|
45 | {
|
---|
46 | currentUnit.victories++;
|
---|
47 | }
|
---|
48 |
|
---|
49 | currentUnit.total_count++;
|
---|
50 | break;
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | // Calculate weights
|
---|
56 | for (List<impUnit> impUnitList : this.values()) {
|
---|
57 | for (impUnit currentUnit : impUnitList) {
|
---|
58 | //System.out.println(currentUnit.valueOfIssue.toString()+":"+String.valueOf(currentUnit.count) + ":" +
|
---|
59 | //String.valueOf(currentUnit.weightSum) + ":" + String.valueOf(currentUnit.meanWeightSum));
|
---|
60 | if (currentUnit.total_count == 0) {
|
---|
61 | currentUnit.probability = 0.0;
|
---|
62 | } else {
|
---|
63 | currentUnit.probability = (double) currentUnit.victories
|
---|
64 | / (double) currentUnit.total_count;
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public void initial_update(List<Bid> bidOrdering, AllBidsList allBids) {
|
---|
71 | for(int i = 0; i < bidOrdering.size(); i++) {
|
---|
72 | for(int j = i+1; j < bidOrdering.size(); j++) {
|
---|
73 | try {
|
---|
74 | Bid worseBid = bidOrdering.get(i);
|
---|
75 | Bid betterBid = bidOrdering.get(j);
|
---|
76 | this.comparison_update(betterBid, worseBid);
|
---|
77 | } catch (Exception e) {
|
---|
78 | throw e;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 | Bid minBid = bidOrdering.get(0);
|
---|
83 | Bid maxBid = bidOrdering.get(bidOrdering.size() - 1);
|
---|
84 | for (BigInteger i = BigInteger.ZERO; i.compareTo(allBids.size()) < 0; i=i.add(BigInteger.ONE)) {
|
---|
85 | Bid currentBid = allBids.get(i);
|
---|
86 | if (!bidOrdering.contains(currentBid)) {
|
---|
87 | this.comparison_update(currentBid, minBid);
|
---|
88 | this.comparison_update(maxBid, currentBid);
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | this.updateBidsRank(allBids);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public List<Bid> getBidsInRange(double lowerImportance, double higherImportance, AllBidsList allBids) {
|
---|
96 | List<Bid> foundBids = new ArrayList<Bid>();
|
---|
97 |
|
---|
98 | for (Bid bid: allBids) {
|
---|
99 | double impOfBid = getImportance(bid);
|
---|
100 | if (impOfBid >= lowerImportance &&
|
---|
101 | impOfBid <= higherImportance) {
|
---|
102 | foundBids.add(bid);
|
---|
103 | }
|
---|
104 | }
|
---|
105 | return foundBids;
|
---|
106 | }
|
---|
107 |
|
---|
108 | public Bid getBidForElicit(double lowerImportance, double higherImportance, AllBidsList allBids) {
|
---|
109 | Map<String, Value> issuevalues = new HashMap<String, Value>();
|
---|
110 | for (String issue : domain.getIssues()) {
|
---|
111 | ValueSet values = domain.getValues(issue);
|
---|
112 | int min_counter = Integer.MAX_VALUE;
|
---|
113 | for (Value value : values) {
|
---|
114 | for (impUnit i : this.get(issue)) {
|
---|
115 | if (i.valueOfIssue.equals(value) && i.total_count < min_counter) {
|
---|
116 | issuevalues.put(issue, value);
|
---|
117 | break;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|
122 | Bid b = new Bid(issuevalues);
|
---|
123 | return b;
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | public void updateBidsRank(AllBidsList allBids) {
|
---|
128 | List<Tuple<Bid, Double>> bids_imp = new ArrayList();
|
---|
129 |
|
---|
130 | for (Bid bid: allBids) {
|
---|
131 | double imp = getImportance(bid);
|
---|
132 | this.bidsRank.put(bid, imp);
|
---|
133 | bids_imp.add(new Tuple(bid, imp));
|
---|
134 | }
|
---|
135 |
|
---|
136 | bids_imp.sort(new bidImpComparator());
|
---|
137 | this.sorted_bids = bids_imp;
|
---|
138 | }
|
---|
139 |
|
---|
140 | public double getImportance(Bid bid) {
|
---|
141 | double bidImportance = 0.0;
|
---|
142 | for (String issue : bid.getIssues()) {
|
---|
143 | Value value = bid.getValue(issue);
|
---|
144 | double valueImportance = 0.0;
|
---|
145 | boolean flag = false;
|
---|
146 |
|
---|
147 | for (impUnit i : this.get(issue)) {
|
---|
148 | if (i.valueOfIssue.equals(value)) {
|
---|
149 | flag = true;
|
---|
150 | valueImportance = i.probability;
|
---|
151 | break;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | bidImportance += Math.log(valueImportance);
|
---|
156 | }
|
---|
157 | return bidImportance;
|
---|
158 | }
|
---|
159 |
|
---|
160 | public int importanceToRankAbs(double importance) {
|
---|
161 | int rank = 0;
|
---|
162 |
|
---|
163 | for (Bid bid: this.bidsRank.keySet()) {
|
---|
164 | double impOfBid = this.bidsRank.get(bid);
|
---|
165 | if (impOfBid < importance) {
|
---|
166 | rank ++;
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | return rank;
|
---|
171 | }
|
---|
172 |
|
---|
173 | public Bid getBestBid(List<Bid> bids) {
|
---|
174 | Bid best_bid = null;
|
---|
175 | double best_bid_imp = Double.NEGATIVE_INFINITY;
|
---|
176 |
|
---|
177 | for (Bid bid : bids) {
|
---|
178 | double bid_imp = this.bidsRank.get(bid);
|
---|
179 |
|
---|
180 | if (bid_imp > best_bid_imp) {
|
---|
181 | best_bid_imp = bid_imp;
|
---|
182 | best_bid = bid;
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | return best_bid;
|
---|
187 | }
|
---|
188 |
|
---|
189 | public double getMaxBidImp() {
|
---|
190 | return rank_to_importance(this.sorted_bids.size() - 1);
|
---|
191 | }
|
---|
192 |
|
---|
193 | public double rank_to_importance(int rank) {
|
---|
194 | return this.sorted_bids.get(rank).get2();
|
---|
195 | }
|
---|
196 |
|
---|
197 | public Bid getBidInRank(int bid_imp) {
|
---|
198 | return this.sorted_bids.get(bid_imp).get1();
|
---|
199 | }
|
---|
200 |
|
---|
201 | static class bidImpComparator implements Comparator<Tuple<Bid, Double>> {
|
---|
202 | public int compare(Tuple<Bid, Double> o1, Tuple<Bid, Double> o2) {
|
---|
203 | return o1.get2().compareTo(o2.get2());
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|