source: src/main/java/agents/anac/y2019/harddealer/HardDealer.java

Last change on this file was 204, checked in by Katsuhide Fujita, 5 years ago

Fixed errors of ANAC2019 agents

  • Property svn:executable set to *
File size: 17.2 KB
Line 
1package agents.anac.y2019.harddealer;
2
3import java.util.List;
4import java.util.ArrayList;
5import java.util.Arrays;
6import java.util.Collection;
7import java.util.Collections;
8import java.util.HashMap;
9import java.util.LinkedHashMap;
10import java.util.Map;
11
12import agents.anac.y2019.harddealer.math3.optimization.GoalType;
13import agents.anac.y2019.harddealer.math3.optimization.PointValuePair;
14import agents.anac.y2019.harddealer.math3.optimization.linear.LinearConstraint;
15import agents.anac.y2019.harddealer.math3.optimization.linear.LinearObjectiveFunction;
16import agents.anac.y2019.harddealer.math3.optimization.linear.Relationship;
17import agents.anac.y2019.harddealer.math3.optimization.linear.SimplexSolver;
18import genius.core.Bid;
19import genius.core.boaframework.AcceptanceStrategy;
20import genius.core.boaframework.BoaParty;
21import genius.core.boaframework.OMStrategy;
22import genius.core.boaframework.OfferingStrategy;
23import genius.core.boaframework.OpponentModel;
24import genius.core.issue.Issue;
25import genius.core.issue.IssueDiscrete;
26import genius.core.issue.ValueDiscrete;
27import genius.core.parties.NegotiationInfo;
28import genius.core.uncertainty.AdditiveUtilitySpaceFactory;
29import genius.core.utility.AbstractUtilitySpace;
30
31
32@SuppressWarnings({"serial", "deprecation"})
33public class HardDealer extends BoaParty
34{
35 @Override
36 public void init(NegotiationInfo info)
37 {
38 // The choice for each component is made here
39 AcceptanceStrategy ac = new HardDealer_AS();
40 OfferingStrategy os = new HardDealer_BS();
41 OpponentModel om = new HardDealer_OM();
42 OMStrategy oms = new HardDealer_OMS();
43
44 // All component parameters can be set below.
45 Map<String, Double> noparams = Collections.emptyMap();
46 Map<String, Double> osParams = new HashMap<String, Double>();
47 // Set the concession parameter "e" for the offering strategy to yield Boulware-like behavior
48 osParams.put("e", 1.8 / info.getDeadline().getTimeOrDefaultTimeout());
49
50 // Initialize all the components of this party to the choices defined above
51 configure(ac, noparams,
52 os, osParams,
53 om, noparams,
54 oms, noparams);
55 super.init(info);
56 }
57
58 // total number of values
59 private int nValues;
60 // occurrences of the values in bids
61 private Integer[] occurenceCheck;
62
63 @Override
64 public AbstractUtilitySpace estimateUtilitySpace() {
65 AdditiveUtilitySpaceFactory additiveUtilitySpaceFactory = new AdditiveUtilitySpaceFactory(getDomain());
66 List<IssueDiscrete> issues = additiveUtilitySpaceFactory.getIssues();
67
68 List<Bid> ranking = userModel.getBidRanking().getBidOrder();
69 double maxUtil = userModel.getBidRanking().getHighUtility();
70 double minUtil = userModel.getBidRanking().getLowUtility();
71
72 // The linear method estimates the utilities but does nothing with the issue weights.
73 // We use a variance and spread based method to estimate these issue weights.
74 List<Bid> bidOrder = userModel.getBidRanking().getBidOrder();
75
76 /**
77 * Create a custom issue weight
78 * Iterate over all values in all bids to build up the issue weights.
79 */
80
81 int bidOrderSize = bidOrder.size();
82 int numberOfIssues = 0;
83 // Initialize dictionaries in which we will store information we will extract from the bidranking.
84 // Information for the Values
85 // v1. Store at which index each value occurs in the bidranking
86 // v2. Store the mean index in the ranking of each value
87 // v3. Store the variance for each value
88 Map<String, ArrayList<Integer>> valueIndicesListDict = new HashMap<String, ArrayList<Integer>>();
89 Map<String, Double> valueMeanIndexDict = new HashMap<String, Double>();
90 Map<String, Double> valueVarianceDict = new HashMap<String, Double>();
91
92 // Information for the Issues
93 // i1. Store the variance for each issue (combined value variance)
94 // i2. Store the spread of each issue (how far apart are the mean indices of its values)
95 // i3. Store the Weight of the Issues
96 Map<Issue, Double> issueVarianceDict = new HashMap<Issue, Double>();
97 Map<Issue, Double> issueSpreadDict = new HashMap<Issue, Double>();
98 Map<Issue, Double> issueWeightDict = new HashMap<Issue, Double>();
99
100 // Variable that can be tweaked:
101 // Tweaks how much the weights are shifted when converting from variance to partial issue weight.
102 double inverseShiftVariable = 2;
103
104 // Tweaks the ratio between the usage of the variance and the spread of the means within an issue to build the issue weight.
105 // This variable is scaled automatically dependinng on the size of the bidordering.
106 // The transition value of 40 bids in the bidranking has been chosen trough extensive testing.
107 double blendVariable = 1-(Math.min(bidOrderSize/40,1));
108
109 // Create a list of all issuevalue strings. This is used for looping over all values in the dictionary.
110 // Initialize the valueIndicesListDict with all the values and initialize empty ArrayLists.
111 ArrayList<String> listOfAllIssueValues = new ArrayList<String>();
112 for (Issue i : issues)
113 {
114 List<ValueDiscrete> values = ((IssueDiscrete) i).getValues();
115 for (ValueDiscrete v : values)
116 {
117 ArrayList<Integer> indicesList = new ArrayList<Integer>();
118 String IssueValueString = i.getName() +v.toString();
119
120 valueIndicesListDict.put(IssueValueString , indicesList);
121 listOfAllIssueValues.add(IssueValueString);
122 }
123 numberOfIssues += 1;
124 }
125
126 // 1. Build up the double representation of the issue weights by using the variance and the spread.
127 // Build the valueIndicesListDict by iterating trough all bids and storing the location of the values.
128 int bidIndex = 0;
129 for (Bid bid : bidOrder)
130 {
131 for (Issue i : issues)
132 {
133 // Fill the valueIndicesListDict with the indices at which these values occur.
134 int no = i.getNumber();
135 ValueDiscrete value = (ValueDiscrete) bid.getValue(no);
136 String IssueValueString = i.getName() +value.toString();
137 ArrayList<Integer> indicesList = valueIndicesListDict.get(IssueValueString);
138 indicesList.add(bidIndex);
139 valueIndicesListDict.replace(IssueValueString,indicesList);
140 }
141 // Setup for the next bid in the loop
142 bidIndex += 1;
143 }
144
145 // Calculate variance for each value.
146 for (String key : listOfAllIssueValues)
147 {
148 ArrayList<Integer> indicesList = valueIndicesListDict.get(key);
149 int sumOfIndices = indicesList.stream().mapToInt(Integer::intValue).sum();
150 double mu;
151 if (sumOfIndices == 0)
152 {
153 mu = bidOrderSize/2;
154 }
155 else
156 {
157 mu = sumOfIndices / indicesList.size();
158 }
159 valueMeanIndexDict.put(key,mu);
160 double P = ((double) 1) / indicesList.size();
161
162 ArrayList<Double> listOfPartialVariance = new ArrayList<Double>();
163 for (int index : indicesList)
164 {
165 double partialVariance = Math.pow((index - mu),2.0) * P;
166 listOfPartialVariance.add(partialVariance);
167 }
168
169 double variance = listOfPartialVariance.stream().reduce(0.0, Double::sum);
170 valueVarianceDict.put(key,variance);
171 }
172
173 // Add the variances of the values together to get issue variances.
174 // Determine the spread of the mean values within the issues.
175 for (Issue i : issues)
176 {
177 double issueVariance = 0;
178 double issueSpread = 0;
179 List<ValueDiscrete> values = ((IssueDiscrete) i).getValues();
180 for (ValueDiscrete v : values)
181 {
182 String IssueValueString = i.getName()+v.toString();
183 double middleIndex = bidOrderSize/2;
184 double meanIndex = valueMeanIndexDict.get(IssueValueString);
185 double distanceFromMiddleIndex = Math.abs(meanIndex - middleIndex);
186 issueSpread += distanceFromMiddleIndex;
187 issueVariance += valueVarianceDict.get(IssueValueString);
188 }
189 issueSpreadDict.put(i, issueSpread);
190 issueVarianceDict.put(i,issueVariance);
191 }
192
193 // Normalize the spread
194 double totalSpread = 0;
195 for (Issue i : issues)
196 {
197 totalSpread += issueSpreadDict.get(i);
198 }
199 for (Issue i : issues)
200 {
201 double spread = issueSpreadDict.get(i);
202 double normalizedSpread = spread/totalSpread;
203 issueSpreadDict.replace(i,normalizedSpread);
204 }
205
206 // Calculate the max variance
207 double oldValue = 0;
208 double maxVariance = 0;
209 for (Issue i : issues)
210 {
211 double newValue = issueVarianceDict.get(i);
212 maxVariance = Math.max(newValue,oldValue);
213 oldValue = newValue;
214 }
215
216 // Normalize the variance and translate it to a partial issueWeight
217 double totalIssueWeight = 0;
218 for (Issue i : issues)
219 {
220 double oldVariance = issueVarianceDict.get(i);
221 double partialIssueWeight = Math.abs(inverseShiftVariable - oldVariance/maxVariance);
222 totalIssueWeight += partialIssueWeight;
223 issueWeightDict.put(i,partialIssueWeight);
224 }
225
226 // Normalize the partial Issue weights
227 for (Issue i: issues)
228 {
229 double oldWeight = issueWeightDict.get(i);
230 double newWeight = oldWeight/totalIssueWeight;
231 issueWeightDict.replace(i,newWeight);
232 }
233 // Combine spread and variance into one issue weight and load into Genius. Use a blendVariable to adjust their ratio
234 for (Issue i : issues)
235 {
236 double finalIssueWeight = (issueWeightDict.get(i) * blendVariable) + (issueSpreadDict.get(i) * (1 - blendVariable));
237 issueWeightDict.replace(i, finalIssueWeight);
238 additiveUtilitySpaceFactory.setWeight(i, finalIssueWeight);
239 }
240
241// The issue weights are now constructed, now we are going to generate the weights of the values trough linear programming.
242 // All values are collected in a LinkedHashMap
243 LinkedHashMap<IssueDiscrete, List<ValueDiscrete>> valuesMap = new LinkedHashMap<>();
244
245 nValues = 0;
246 for (IssueDiscrete issue : issues) {
247 valuesMap.put(issue, issue.getValues());
248 nValues += issue.getValues().size();
249 }
250
251 occurenceCheck = new Integer[nValues];
252 Arrays.fill(occurenceCheck,0);
253
254 int nSlackVariables = ranking.size()-1;
255 // The number of variables needed for the linear optimization
256 int nVariables = nSlackVariables + nValues;
257
258 // The objective function is to minimize all slack variables
259 // Therefore, all the slack variables should have a coefficient of 1, and all the other should have a coefficient of 0
260 double[] functionList = new double[nVariables];
261 Arrays.fill(functionList, 0);
262
263 for (int i = 0; i < nSlackVariables; i++) {
264 functionList[i] = 1;
265 }
266
267 LinearObjectiveFunction f = new LinearObjectiveFunction(functionList, 0);
268
269 // A collection with all constraints
270 Collection<LinearConstraint> constraints = new ArrayList<>();
271 createVarGEQ0Constraints(constraints, functionList, nSlackVariables);
272 createSlackComparisonGEQ0Constraints(constraints, functionList, nSlackVariables, valuesMap, ranking);
273 createMaxMinConstraints(constraints, valuesMap, ranking, nSlackVariables, maxUtil, minUtil);
274
275
276 // Use a Simplex solver to solve f
277 SimplexSolver solver = new SimplexSolver();
278 solver.setMaxIterations(Integer.MAX_VALUE);
279 PointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, true);
280
281 // The average utility of a value is estimated based on the max utility, min utility and the variance within the bid ranking.
282 double functionIndex = totalIssueWeight/numberOfIssues;
283 double exponent = 5 - (4/(1+Math.exp(-50*(functionIndex-1))));
284 double yScale = maxUtil - minUtil;
285
286 double average = minUtil + yScale*Math.pow(0.5, exponent);
287 // Initialization of new utilities
288 // The utilities of the actual values are stored at indices after the slack variables,
289 // so start iterating after nSlackVariables
290 int iterator = nSlackVariables;
291 for (IssueDiscrete issue : issues) {
292 // In case there is not much information about the value (0, 1 or 2 occurrences), it also takes the average into consideration
293 for (int v = 0; v < issue.getNumberOfValues(); v++) {
294 if(occurenceCheck[iterator - nSlackVariables] < 3) {
295 int occ = occurenceCheck[iterator - nSlackVariables];
296 double util = ((occ * solution.getPoint()[iterator]) + average) / (occ + 1);
297 additiveUtilitySpaceFactory.setUtility(issue, issue.getValue(v), util);
298 }
299 else {
300 additiveUtilitySpaceFactory.setUtility(issue, issue.getValue(v), solution.getPoint()[iterator]);
301 }
302
303 iterator++;
304 }
305 }
306 // Normalize the weights
307 additiveUtilitySpaceFactory.normalizeWeights();
308
309 // The factory is done with setting all parameters, now return the estimated utility space
310 return additiveUtilitySpaceFactory.getUtilitySpace();
311 }
312
313
314 // Transforms a bid into a double list of 0's and 1's representing it values
315 private double[] valuesFunctionList(Bid bid, LinkedHashMap<IssueDiscrete, List<ValueDiscrete>> values, int nSlackValues) {
316 double[] linear = new double[nSlackValues + nValues];
317 Arrays.fill(linear, 0);
318
319 int count = 0;
320
321 // A 1 is placed at the corresponding value position
322 for (int i = 0; i < bid.getIssues().size(); i++) {
323 ValueDiscrete v = (ValueDiscrete) bid.getValue(i+1);
324
325 for (int val = 0; val < values.get(bid.getIssues().get(i)).size(); val++) {
326 ValueDiscrete v2 = values.get(bid.getIssues().get(i)).get(val);
327 if (v.equals(v2)) {
328 linear[count + nSlackValues] = 1;
329 occurenceCheck[count] += 1;
330 }
331 count++;
332 }
333 }
334 return linear;
335 }
336
337 // Creates a 'variable is greater or equal than 0' constraint for all slack and value variables
338 private void createVarGEQ0Constraints(Collection<LinearConstraint> constraints, double[] functionList, int nSlackVariables) {
339 for (int i = 0; i < functionList.length; i++) {
340 // reset all coefficients to 0
341 Arrays.fill(functionList, 0);
342 // set the coefficient for variable i to 1
343 functionList[i] = 1;
344 // create the greater or equal constraint
345 constraints.add(new LinearConstraint(functionList, Relationship.GEQ, 0));
346 }
347 }
348
349 // Creates the 'slack + comparison variable is greater or equal than 0' constraints for every slack and corresponding comparison variable
350 private void createSlackComparisonGEQ0Constraints(Collection<LinearConstraint> constraints, double[] functionList, int nSlackVariables, LinkedHashMap<IssueDiscrete, List<ValueDiscrete>> values, List<Bid> ranking) {
351 for (int i = 0; i < nSlackVariables; i++) {
352 // reset all coefficients to 0
353 Arrays.fill(functionList, 0);
354 // get bid o
355 Bid o = ranking.get(i);
356 // get bid o'
357 Bid oPrime = ranking.get(i+1);
358 // create a function list for o
359 double[] oList = valuesFunctionList(o, values, nSlackVariables);
360 // create a function list for o'
361 double[] oPrimeList = valuesFunctionList(oPrime, values, nSlackVariables);
362 // create the delta u as values of o - values of o'
363 for(int j = 0; j < functionList.length; j++) {
364 functionList[j] = oPrimeList[j] - oList[j];
365 }
366 // set the coefficient of slack variable i to 1
367 functionList[i] = 1;
368 constraints.add(new LinearConstraint(functionList, Relationship.GEQ, 0));
369 }
370 }
371
372 // Creates the constraints which represent the max and min utility
373 private void createMaxMinConstraints(Collection<LinearConstraint> constraints,
374 LinkedHashMap<IssueDiscrete, List<ValueDiscrete>> values, List<Bid> ranking, int nSlackVariables, double maxU, double minU) {
375
376 Bid strongestBid = ranking.get(ranking.size()-1);
377 double[] functionList = valuesFunctionList(strongestBid, values, nSlackVariables);
378 // The comparison strongest bid == maxU is added
379 constraints.add(new LinearConstraint(functionList, Relationship.EQ, maxU));
380
381 Bid weakestBid = ranking.get(0);
382 functionList = valuesFunctionList(weakestBid, values, nSlackVariables);
383 // The comparison strongest bid == maxU is added
384 constraints.add(new LinearConstraint(functionList, Relationship.EQ, minU));
385
386 }
387
388
389 @Override
390 public String getDescription()
391 {
392 return "Hardheaded but concedes at the very end to make a deal.";
393 }
394 // All the rest of the agent functionality is defined by the components selected above, using the BOA framework
395}
Note: See TracBrowser for help on using the repository browser.