source: anac2020/Anaconda/src/main/java/geniusweb/exampleparties/anaconda/oponnentMap.java@ 14

Last change on this file since 14 was 1, checked in by wouter, 4 years ago

#1910 added anac2020 parties

File size: 5.5 KB
Line 
1package geniusweb.exampleparties.anaconda;
2
3import java.util.ArrayList;
4import java.util.Arrays;
5import java.util.Comparator;
6import java.util.HashMap;
7import java.util.LinkedHashSet;
8import java.util.List;
9import java.util.Map;
10import java.util.PriorityQueue;
11import java.util.Set;
12
13import geniusweb.bidspace.AllBidsList;
14import geniusweb.issuevalue.Bid;
15import geniusweb.issuevalue.Domain;
16import geniusweb.issuevalue.Value;
17import geniusweb.issuevalue.ValueSet;
18import geniusweb.profile.PartialOrdering;
19
20@SuppressWarnings("serial")
21public class oponnentMap extends HashMap<String, List<impUnit>> {
22 private Domain domain;
23 private HashMap<Bid, Double> bidsRank;
24 private ArrayList<Bid> historyBids;
25
26
27 // importance map
28 public oponnentMap(PartialOrdering profile) {
29 super();
30 this.domain = profile.getDomain();
31 this.bidsRank = new HashMap<Bid, Double>();
32 this.historyBids = new ArrayList<Bid>();
33 // Create empty my import map and opponent's value map
34 for (String issue : domain.getIssues()) {
35 ValueSet values = domain.getValues(issue);
36 List<impUnit> issueImpUnit = new ArrayList<>();
37 for (Value value : values) {
38 issueImpUnit.add(new impUnit(value));
39 }
40 this.put(issue, issueImpUnit);
41 }
42 }
43
44
45 public void update(Bid receivedOfferBid) {
46 for (String issue : receivedOfferBid.getIssues()) {
47 List<impUnit> currentIssueList = this.get(issue);
48 for (impUnit currentUnit : currentIssueList) {
49 if (currentUnit.valueOfIssue.equals(receivedOfferBid.getValue(issue))) {
50 currentUnit.total_count += 1;
51 break;
52 }
53 }
54 }
55 for (List<impUnit> impUnitList : this.values()) {
56 impUnitList.sort(new impUnit.impSumComparator());
57 }
58 this.historyBids.add(receivedOfferBid);
59 }
60
61
62 public void updateBidsRank(AllBidsList allBids) {
63 for (Bid bid: allBids) {
64 this.bidsRank.put(bid, getImportance(bid));
65 }
66 }
67
68
69 public List<Bid> getTopBids(List<Bid> bids, int topN) {
70 class BidComparator implements Comparator<Bid> {
71
72 @Override
73 public int compare(Bid bid1, Bid bid2) {
74 return ((Double)getImportance(bid1)).compareTo((Double)(getImportance(bid2)));
75 }
76 }
77
78 Comparator<Bid> bidComparator = new BidComparator();
79 PriorityQueue<Bid> bidsQueue = new PriorityQueue<Bid>(bids.size(), bidComparator);
80 bidsQueue.addAll(bids);
81
82 ArrayList<Bid> topBids = new ArrayList<Bid>();
83
84 for(int i = 0; i < topN && i < bidsQueue.size(); i++) {
85 Bid bid = bidsQueue.poll();
86 topBids.add(bid);
87 }
88
89 return topBids;
90 }
91
92 public double getImportance(Bid bid) {
93 int bidImportance = 0;
94 for (String issue : bid.getIssues()) {
95 Value value = bid.getValue(issue);
96 int valueImportance = 0;
97 for (impUnit i : this.get(issue)) {
98 if (i.valueOfIssue.equals(value)) {
99 valueImportance = i.total_count;
100 break;
101 }
102 }
103 bidImportance += valueImportance;
104 }
105 return bidImportance;
106 }
107
108 public List<Bid> getEqualsBids(Bid bid) {
109 List<Bid> keys = new ArrayList<>();
110 double currentRank = getImportance(bid);
111 for (Map.Entry<Bid, Double> entry : this.bidsRank.entrySet()) {
112 if (entry.getValue()==currentRank) {
113 keys.add(entry.getKey());
114 }
115 }
116 return keys;
117 }
118
119
120 public List<Bid> getMoreFreqBids(Bid bid) {
121 double currentRank = getImportance(bid);
122 Object[] arr = (Object[])(this.bidsRank.values().toArray());
123 Arrays.sort(arr);
124 Set<Object> uniqueVals = new LinkedHashSet<Object>(Arrays.asList(arr));
125 arr = uniqueVals.toArray();
126
127 int currentFreqIdx = -1;
128 int nextFreqIdx = -1;
129
130 for (int i = 0; (i < arr.length) && (currentFreqIdx == -1); i++) {
131 if (Integer.parseInt(arr[i].toString()) == currentRank) {
132 currentFreqIdx = i;
133 }
134 }
135
136 if (currentFreqIdx + 1 < arr.length) {
137 nextFreqIdx = currentFreqIdx + 1;
138 }
139 else {
140 nextFreqIdx = currentFreqIdx;
141 }
142
143 List<Bid> keys = new ArrayList<>();
144 int biggerRank = (int) arr[nextFreqIdx];
145 for (Map.Entry<Bid, Double> entry : this.bidsRank.entrySet()) {
146 if (entry.getValue()==biggerRank) {
147 keys.add(entry.getKey());
148 }
149 }
150 return keys;
151 }
152
153
154 public List<Bid> getLessFreqBids(Bid bid) {
155 double currentRank = getImportance(bid);
156 Object[] arr = (Object[])(this.bidsRank.values().toArray());
157 Arrays.sort(arr);
158 Set<Object> uniqueVals = new LinkedHashSet<Object>(Arrays.asList(arr));
159 arr = uniqueVals.toArray();
160
161 int currentFreqIdx = -1;
162 int nextFreqIdx = -1;
163
164 for (int i = 0; (i < arr.length) && (currentFreqIdx == -1); i++) {
165 if (Integer.parseInt(arr[i].toString()) == currentRank) {
166 currentFreqIdx = i;
167 }
168 }
169
170 if (currentFreqIdx - 1 >= 0) {
171 nextFreqIdx = currentFreqIdx - 1;
172 }
173 else {
174 nextFreqIdx = currentFreqIdx;
175 }
176
177 List<Bid> keys = new ArrayList<>();
178 int smallerRank = (int) arr[nextFreqIdx];
179 for (Map.Entry<Bid, Double> entry : this.bidsRank.entrySet()) {
180 if (entry.getValue()==smallerRank) {
181 keys.add(entry.getKey());
182 }
183 }
184 return keys;
185 }
186
187 public List<Bid> getBidsInRange(double lowerImportance, double higherImportance, AllBidsList allBids) {
188 List<Bid> foundBids = new ArrayList<Bid>();
189
190 for (Bid bid: allBids) {
191 double impOfBid = getImportance(bid);
192 if (impOfBid >= lowerImportance &&
193 impOfBid <= higherImportance) {
194 foundBids.add(bid);
195 }
196 }
197 return foundBids;
198 }
199
200}
Note: See TracBrowser for help on using the repository browser.