source: src/main/java/agents/ai2014/group4/BidGenerator.java@ 61

Last change on this file since 61 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 9.4 KB
Line 
1package agents.ai2014.group4;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.Map.Entry;
6
7import genius.core.Bid;
8import genius.core.issue.IssueDiscrete;
9import genius.core.issue.Objective;
10import genius.core.issue.Value;
11import genius.core.issue.ValueDiscrete;
12import genius.core.utility.AdditiveUtilitySpace;
13import genius.core.utility.Evaluator;
14
15import java.util.Random;
16
17/**
18 * This uses getEvaluators and hence only supports {@link AdditiveUtilitySpace}.
19 *
20 */
21public class BidGenerator {
22
23 private Group4 agent;
24
25 // Unused bids
26 private HashMap<Bid, Double> bidMap = new HashMap<Bid, Double>();
27
28 private HashMap<Bid, Double> alreadyUsedBids = new HashMap<Bid, Double>();
29 private ArrayList<Party> parties = new ArrayList<Party>();
30 private int turnsLeft;
31
32 // Based on chance, so they have to sum up to 1.00
33 private Double divisionUs = 0.75;
34 private Double divisionOther = 1 - divisionUs;
35
36 private Double chosenIssuePercentage;
37
38 private Random random = new Random();
39
40 public BidGenerator(Group4 agent, HashMap<Bid, Double> map, int deadline) {
41 this.agent = agent;
42 bidMap = map;
43 turnsLeft = deadline;
44 }
45
46 /**
47 * Returns the best issue value of the other parties.
48 *
49 * @param issueNr
50 * The issue that will be checked.
51 * @return ValueDiscrete The best value.
52 */
53 private ValueDiscrete getTheirBestValue(int issueNr) {
54 HashMap<String, Double> valueWeights = new HashMap<String, Double>();
55
56 // Zero-based index
57 issueNr = issueNr - 1;
58
59 // sum up all the values of all the parties
60 for (int i = 0; i < parties.size(); i++) {
61 for (Entry<String, Double> e : parties.get(i).getIssueModels()
62 .get(issueNr).getUtility().entrySet()) {
63 Double issWeight = parties.get(i).getIssueModels().get(issueNr)
64 .getValue();
65 if (valueWeights.get(e.getKey()) == null) {
66 valueWeights.put(e.getKey(), e.getValue() * issWeight);
67 } else {
68 valueWeights.put(e.getKey(), valueWeights.get(e.getKey())
69 + (e.getValue() * issWeight));
70 }
71 }
72 }
73
74 // take out best value
75 String bestValueKey = null;
76 Double bestValueWeight = 0.0;
77 ValueDiscrete previousValue = null;
78
79 try {
80 // Because bid values use one-based indices, +1
81 previousValue = (ValueDiscrete) agent.getLastGivenBid().getValue(
82 issueNr + 1);
83 } catch (Exception e1) {
84 e1.printStackTrace();
85 }
86
87 for (Entry<String, Double> e : valueWeights.entrySet()) {
88 if (e.getValue() > bestValueWeight) {
89 if (!previousValue.getValue().equals(e.getKey())) {
90 bestValueWeight = e.getValue();
91 bestValueKey = e.getKey();
92 }
93 }
94 }
95
96 // get the ValueDiscrete -----i use the first party since everyone has
97 // same discrete issues
98 ValueDiscrete value = null;
99 int size = parties.get(0).getIssueModels().get(issueNr).getValues()
100 .size();
101 for (int i = 0; i < size; i++) {
102
103 Party randomParty = parties.get(0);
104 IssueModel issueModel = randomParty.getIssueModels().get(issueNr);
105
106 if (issueModel.getValues().get(i).getValue().equals(bestValueKey)) {
107 value = issueModel.getValues().get(i);
108 }
109 }
110
111 return value;
112 }
113
114 /**
115 * Returns the index of the issue picked and updates the
116 * chosenIssuePercentage with the percentage.
117 *
118 * @return IssueNr The chosen issue to be changed in the next bid (one-based
119 * index).
120 */
121 private int getWeightedRandomIssueIndex() {
122 HashMap<String, Double> issueWeightsOfOtherParties = new HashMap<String, Double>();
123
124 // gets the sum of all weights of all parties for the issues
125 for (Party p : parties) {
126 for (IssueModel issueModel : p.getIssueModels()) {
127 String key = issueModel.getName();
128 Double weight;
129 if (!issueWeightsOfOtherParties.containsKey(key)) {
130 weight = issueModel.getValue();
131 issueWeightsOfOtherParties.put(key, weight);
132 } else {
133 weight = issueWeightsOfOtherParties.get(key)
134 + issueModel.getValue();
135 issueWeightsOfOtherParties.put(key, weight);
136 }
137 }
138 }
139
140 // get total weight to normalize it
141 Double totalWeight = 0.0;
142 for (Entry<String, Double> e : issueWeightsOfOtherParties.entrySet()) {
143 totalWeight += e.getValue();
144 }
145 // generate other parties weights
146 ArrayList<Double> otherWeights = new ArrayList<Double>();
147 for (Entry<String, Double> e : issueWeightsOfOtherParties.entrySet()) {
148 Double temp = e.getValue();
149 e.setValue(temp / totalWeight);
150 otherWeights.add(temp / totalWeight);
151 }
152 // make other party weights the inverse
153 // otherWeights = getInverseValues(otherWeights);
154 // NO! We want to change the issues that they *DO* care about!
155
156 // get my weights
157 ArrayList<Double> myWeights = new ArrayList<Double>();
158 for (Entry<Objective, Evaluator> e : ((AdditiveUtilitySpace) agent
159 .getUtilitySpace()).getEvaluators()) {
160 myWeights.add(e.getValue().getWeight());
161 }
162 // make my party weights the inverse
163 myWeights = getInverseValues(myWeights);
164
165 // get the array of overall weights
166 ArrayList<Double> finalWeights = new ArrayList<Double>();
167 for (int i = 0; i < myWeights.size(); i++) {
168 Double mine = myWeights.get(i) * divisionUs;
169 Double other = otherWeights.get(i) * divisionOther;
170 finalWeights.add(mine + other);
171 }
172 // pick the Issue by throwing the dice
173 Double diceThrow = random.nextDouble();
174 Double tempWeight = 0.0;
175 Integer issueNr = null;
176 for (int i = 0; i < finalWeights.size(); i++) {
177 tempWeight += finalWeights.get(i);
178 issueNr = i;
179 chosenIssuePercentage = finalWeights.get(i);
180 if (tempWeight >= diceThrow) {
181 break;
182 }
183 }
184
185 return (int) issueNr + 1;
186 }
187
188 private ArrayList<Double> getInverseValues(ArrayList<Double> values) {
189 ArrayList<Double> returnValues = new ArrayList<Double>();
190 Double sumOfDivision = 0.0;
191 for (int i = 0; i < values.size(); i++) {
192 sumOfDivision += 1 / values.get(i);
193 }
194 for (int i = 0; i < values.size(); i++) {
195 returnValues.add((1 / values.get(i)) / sumOfDivision);
196 }
197 return returnValues;
198 }
199
200 /**
201 * Generate the best overall bid, even those already offered.
202 *
203 * @return Bid The generated bid.
204 */
205 public Bid generateBestOverallBid() {
206 Bid bestBid = null;
207
208 if (alreadyUsedBids.size() == 0) {
209 bestBid = getBestUtilityBid(bidMap);
210 } else {
211 bestBid = getBestUtilityBid(alreadyUsedBids);
212 }
213
214 return bestBid;
215 }
216
217 /**
218 * Gets the best value for an issue, with all other issues set.
219 *
220 * @param condition
221 * The pre-set issues.
222 * @param issueNr
223 * The id of the issue to be changed.
224 * @return ValueDiscrete The best value.
225 */
226 public ValueDiscrete getValueBidOneOut(HashMap<Integer, Value> condition,
227 int issueNr) {
228 ValueDiscrete bestValue = null;
229 double maxUtility = 0.0;
230 AdditiveUtilitySpace us = (AdditiveUtilitySpace) agent
231 .getUtilitySpace();
232 ValueDiscrete previousValue = null;
233
234 try {
235 previousValue = (ValueDiscrete) agent.getLastGivenBid().getValue(
236 issueNr);
237 } catch (Exception e1) {
238 e1.printStackTrace();
239 }
240
241 // Zero-based index
242 IssueDiscrete issue = (IssueDiscrete) us.getDomain().getIssues()
243 .get(issueNr - 1);
244
245 for (int j = 0; j < issue.getNumberOfValues(); j++) {
246 if (!previousValue.getValue().equals(issue.getValue(j).getValue())) {
247 HashMap<Integer, Value> values = (HashMap<Integer, Value>) condition
248 .clone();
249 values.put(issueNr, issue.getValue(j));
250
251 try {
252 Bid currentBid = new Bid(us.getDomain(), values);
253
254 if (maxUtility < agent.getUtility(currentBid)) {
255 maxUtility = agent.getUtility(currentBid);
256 bestValue = issue.getValue(j);
257 }
258 } catch (Exception e) {
259 e.printStackTrace();
260 }
261 }
262 }
263 return bestValue;
264 }
265
266 /**
267 * Gets the bid with the highest utility
268 *
269 * @param bids
270 * The list of bids with their utility to choose from.
271 * @return Bid The bid with the highest utility.
272 */
273 private Bid getBestUtilityBid(HashMap<Bid, Double> bids) {
274 Double bestUtility = 0.0;
275 Bid bestBid = null;
276
277 for (Entry<Bid, Double> e : bids.entrySet()) {
278 if (e.getValue() > bestUtility) {
279 bestUtility = e.getValue();
280 bestBid = e.getKey();
281 }
282 }
283
284 // updates in already used(if not already used)
285 if (!(bids.equals(alreadyUsedBids))) {
286 alreadyUsedBids.put(bestBid, bestUtility);
287 bidMap.remove(bestBid);
288 }
289 return bestBid;
290
291 }
292
293 /**
294 * Generates the best bid based on the weights of both us and the parties
295 *
296 * @return Bid The best bid with the new issue value.
297 */
298 public Bid generateBestBid() {
299
300 if (parties.size() < agent.getNumberOfParties()) {
301 parties.clear();
302 for (Entry<String, Party> e : agent.getParties().entrySet()) {
303 parties.add(e.getValue());
304 }
305 }
306
307 int issueNr = getWeightedRandomIssueIndex();
308 Bid finalBid = null;
309 double randomD = random.nextDouble();
310 Bid lastGivenBid = agent.getLastGivenBid();
311 HashMap<Integer, Value> lastValues = (HashMap<Integer, Value>) lastGivenBid
312 .getValues().clone();
313 ValueDiscrete value = null;
314
315 // They win by chance
316 if (chosenIssuePercentage >= randomD) {
317 value = getTheirBestValue(issueNr);
318 lastValues.put(issueNr, value);
319 try {
320 finalBid = new Bid(agent.getUtilitySpace().getDomain(),
321 lastValues);
322 } catch (Exception e) {
323 e.printStackTrace();
324 }
325 }
326 // We win by chance
327 else {
328 try {
329 HashMap<Integer, Value> condition = (HashMap<Integer, Value>) lastValues
330 .clone();
331 condition.remove(issueNr);
332 value = getValueBidOneOut(condition, issueNr);
333 condition.put(issueNr, value);
334 finalBid = new Bid(agent.getUtilitySpace().getDomain(),
335 condition);
336 } catch (Exception e) {
337 e.printStackTrace();
338 }
339 }
340 return finalBid;
341 }
342
343}
Note: See TracBrowser for help on using the repository browser.