source: src/main/java/agents/anac/y2019/fsega2019/fsegaoppmodel/MyBayesianOpponentModel.java@ 346

Last change on this file since 346 was 202, checked in by Katsuhide Fujita, 5 years ago

Add ANAC 2019 agents (3)

  • Property svn:executable set to *
File size: 14.5 KB
Line 
1package agents.anac.y2019.fsega2019.fsegaoppmodel;
2
3
4import java.io.FileWriter;
5import java.util.ArrayList;
6import java.util.Collections;
7import java.util.List;
8
9import genius.core.Bid;
10import genius.core.Domain;
11import genius.core.issue.Issue;
12import genius.core.issue.IssueDiscrete;
13import genius.core.utility.AbstractUtilitySpace;
14import genius.core.utility.EvaluatorDiscrete;
15import genius.core.utility.UtilitySpace;
16
17
18
19public class MyBayesianOpponentModel extends OpponentModel
20{
21 private AbstractUtilitySpace uUS;
22 private ArrayList<UtilitySpaceHypothesis> uUSHypothesis;
23 private double previousBidUtility;
24 private double SIGMA = 0.25;
25 private double CONCESSION_STRATEGY = 0.035; //estimated opponent concession strategy
26 private int initialNumberHyps = 0;
27
28 //scalable
29 private boolean bUseMostProb = true;
30 private ArrayList<UtilitySpaceHypothesis> mostProbHyps;
31
32 public MyBayesianOpponentModel(AbstractUtilitySpace pUS)
33 {
34 //TODO: for test
35 //System.out.println("Model creation");
36
37 if(pUS == null)
38 throw new NullPointerException("MyBayesianOpponentModel: utility space = null");
39 uUS = pUS;
40
41 previousBidUtility = 1;
42 dDomain = pUS.getDomain();
43 //aBiddingHistory = new ArrayList<Bid>();
44
45 List<Issue> issues = dDomain.getIssues();
46 ArrayList<ArrayList<EvaluatorHypothesis>> aaEvaluatorHypothesis = new ArrayList<ArrayList<EvaluatorHypothesis>> ();
47
48 int numberOfIssues = issues.size();
49
50 //generate weight hypothesis ==> <count of issues>! hypothesis
51 WeightHypothesis[] weightHypothesis = new WeightHypothesis[factorial(numberOfIssues)];
52
53 //create all permutations
54 double[] P = new double[numberOfIssues];
55
56 //normalize weights
57 for(int i = 0; i < numberOfIssues; i++)
58 P[i] = 2.0 * (i + 1) / (double)(numberOfIssues * (numberOfIssues + 1));
59 weightPermutations(0, weightHypothesis, P, numberOfIssues - 1);
60
61 //add initial probabilities
62 for(int i = 0; i < weightHypothesis.length; i++)
63 weightHypothesis[i].setProbability(1.0/weightHypothesis.length);
64
65 //generate evaluator hypotheses
66 for(int i = 0; i < numberOfIssues; i++)
67 {
68 ArrayList<EvaluatorHypothesis> lEvalHyps;
69// switch(uUS.getEvaluator(issues.get(i).getNumber()).getType())
70// {
71// case DISCRETE:
72 lEvalHyps = new ArrayList<EvaluatorHypothesis>();
73 aaEvaluatorHypothesis.add(lEvalHyps);
74 IssueDiscrete lDiscIssue = (IssueDiscrete)(dDomain.getIssues().get(i));
75
76 //uphill
77 EvaluatorDiscrete lDiscreteEvaluator = new EvaluatorDiscrete();
78 for(int j = 0; j < lDiscIssue.getNumberOfValues(); j++)
79 lDiscreteEvaluator.addEvaluation(lDiscIssue.getValue(j), 1000 * j + 1);
80 EvaluatorHypothesis lEvaluatorHypothesis = new EvaluatorHypothesis(lDiscreteEvaluator, "uphill");
81
82 lEvalHyps.add(lEvaluatorHypothesis);
83
84 //downhill
85 lDiscreteEvaluator = new EvaluatorDiscrete();
86 for(int j=0; j < lDiscIssue.getNumberOfValues(); j++)
87 lDiscreteEvaluator.addEvaluation(lDiscIssue.getValue(j), 1000 * (lDiscIssue.getNumberOfValues() - j - 1) + 1);
88 lEvaluatorHypothesis = new EvaluatorHypothesis(lDiscreteEvaluator, "downhill");
89
90 lEvalHyps.add(lEvaluatorHypothesis);
91
92 //triangular
93 lDiscreteEvaluator = new EvaluatorDiscrete();
94 int halfway=lDiscIssue.getNumberOfValues()/2;
95 for(int j=0;j<lDiscIssue.getNumberOfValues();j++)
96 if(j<halfway)
97 lDiscreteEvaluator.addEvaluation(lDiscIssue.getValue(j),1000*j+1);
98 else
99 lDiscreteEvaluator.addEvaluation(lDiscIssue.getValue(j),
100 1000*(lDiscIssue.getNumberOfValues()-j-1)+1);
101 lEvaluatorHypothesis = new EvaluatorHypothesis (lDiscreteEvaluator, "triangular");
102
103 lEvalHyps.add(lEvaluatorHypothesis);
104// break;
105//
106// //Eval hypothesis for real / price attributes
107// case PRICE:
108// case REAL:
109// lEvalHyps = new ArrayList<EvaluatorHypothesis>();
110// aaEvaluatorHypothesis.add(lEvalHyps);
111// IssueReal lRealIssue = (IssueReal)(dDomain.getIssue(i));
112//
113// //uphill
114// EvaluatorReal lRealEvaluator = new EvaluatorReal();
115// lRealEvaluator.setLowerBound(lRealIssue.getLowerBound());
116// lRealEvaluator.setUpperBound(lRealIssue.getUpperBound());
117// lRealEvaluator.setType(EVALFUNCTYPE.LINEAR);
118// lRealEvaluator.addParam(1, 1.0/(lRealEvaluator.getUpperBound()-lRealEvaluator.getLowerBound()));
119// lRealEvaluator.addParam(0, -lRealEvaluator.getLowerBound()/(lRealEvaluator.getUpperBound()-lRealEvaluator.getLowerBound()));
120// lEvaluatorHypothesis = new EvaluatorHypothesis(lRealEvaluator, "uphill");
121// lEvalHyps.add(lEvaluatorHypothesis);
122//
123// //downhill
124// lRealEvaluator = new EvaluatorReal();
125// lRealEvaluator.setLowerBound(lRealIssue.getLowerBound());
126// lRealEvaluator.setUpperBound(lRealIssue.getUpperBound());
127// lRealEvaluator.setType(EVALFUNCTYPE.LINEAR);
128// lRealEvaluator.addParam(1, -1.0/(lRealEvaluator.getUpperBound()-lRealEvaluator.getLowerBound()));
129// lRealEvaluator.addParam(0, 1.0+lRealEvaluator.getLowerBound()/(lRealEvaluator.getUpperBound()-lRealEvaluator.getLowerBound()));
130// lEvaluatorHypothesis = new EvaluatorHypothesis(lRealEvaluator, "downhill");
131// lEvalHyps.add(lEvaluatorHypothesis);
132//
133// //triangular
134// int lTotalTriangularFns = 1;
135// for(int k=1;k<=lTotalTriangularFns;k++)
136// {
137// lRealEvaluator = new EvaluatorReal();
138// lRealEvaluator.setLowerBound(lRealIssue.getLowerBound());
139// lRealEvaluator.setUpperBound(lRealIssue.getUpperBound());
140// lRealEvaluator.setType(EVALFUNCTYPE.TRIANGULAR);
141// lRealEvaluator.addParam(0, lRealEvaluator.getLowerBound());
142// lRealEvaluator.addParam(1, lRealEvaluator.getUpperBound());
143// lRealEvaluator.addParam(2, lRealEvaluator.getLowerBound()+(double)k*(lRealEvaluator.getUpperBound()-lRealEvaluator.getLowerBound())/(lTotalTriangularFns+1));
144// lEvaluatorHypothesis = new EvaluatorHypothesis(lRealEvaluator, "triangular");
145// lEvaluatorHypothesis.setProbability((double)1/3);
146// lEvalHyps.add(lEvaluatorHypothesis);
147// }
148// for(int k=0;k<lEvalHyps.size();k++) {
149// lEvalHyps.get(k).setProbability((double)1/lEvalHyps.size());
150// }
151//
152// break;
153//
154// default:
155// throw new NullPointerException("Evaluator type not implemented: eval type - " + uUS.getEvaluator(issues.get(i).getNumber()).getType());
156 }
157 //}
158
159 //build evaluation hypothesis
160 ArrayList<EvaluatorHypothesis[]> evalHypothesis = new ArrayList<EvaluatorHypothesis[]>();
161// uUS.getDomain().getIssues().size()
162
163 EvaluatorHypothesis[] ehTmp = new EvaluatorHypothesis[uUS.getDomain().getIssues().size()];
164
165
166 buildEvaluationHypothesis(evalHypothesis, ehTmp, uUS.getDomain().getIssues().size() - 1, aaEvaluatorHypothesis);
167
168 //build user space hypothesis
169 buildUtilitySpaceHypothesis(weightHypothesis, evalHypothesis);
170
171 // HOSOKAWA: removed this part
172// try
173// {
174// FileWriter fw = new FileWriter("E:\\Hyopothesis.txt");
175// fw.write(uUSHypothesis.toString());
176// fw.flush();
177// fw.close();
178// }
179// catch(Exception e)
180// { }
181 initialNumberHyps = uUSHypothesis.size();
182 }
183
184 private void buildEvaluationHypothesis(ArrayList<EvaluatorHypothesis[]> pHyps, EvaluatorHypothesis[] pEval, int m, ArrayList<ArrayList<EvaluatorHypothesis>> paaEval)
185 {
186 if(m==0)
187 {
188 ArrayList<EvaluatorHypothesis> lEvalHyps = paaEval.get(uUS.getDomain().getIssues().size()-1);
189 for (int i=0;i<lEvalHyps.size();i++)
190 {
191 pEval[uUS.getDomain().getIssues().size()-1] = lEvalHyps.get(i);
192 EvaluatorHypothesis[] lTmp = new EvaluatorHypothesis[uUS.getDomain().getIssues().size()];
193 //copy to temporary array
194 for(int j =0;j<lTmp.length;j++)
195 lTmp[j] = pEval[j];
196 pHyps.add(lTmp);
197 }
198 }
199 else
200 {
201 ArrayList<EvaluatorHypothesis> lEvalHyps = paaEval.get(uUS.getDomain().getIssues().size()-m-1);
202 for (int i=0;i<lEvalHyps.size();i++)
203 {
204 pEval[uUS.getDomain().getIssues().size()-m-1] = lEvalHyps.get(i);
205 buildEvaluationHypothesis(pHyps, pEval, m-1, paaEval);
206 }
207 }
208}
209
210 private void buildUtilitySpaceHypothesis(WeightHypothesis[] pWeightHypothesis, ArrayList<EvaluatorHypothesis[]> pEvalHypothesis)
211 {
212 uUSHypothesis = new ArrayList<UtilitySpaceHypothesis>();
213 for(int i = 0; i < pWeightHypothesis.length; i++)
214 {
215 for(int j=0; j < pEvalHypothesis.size(); j++) {
216 UtilitySpaceHypothesis lUSHyp = new UtilitySpaceHypothesis(dDomain, uUS, pWeightHypothesis[i], pEvalHypothesis.get(j));
217 uUSHypothesis.add(lUSHyp);
218 }
219 }
220
221 //set initial probability for all hyps
222 for(int i = 0; i < uUSHypothesis.size(); i++)
223 {
224 uUSHypothesis.get(i).setProbability(1.0/(double)(uUSHypothesis.size()));
225 }
226 }
227
228 private Integer weightPermutations(Integer index, WeightHypothesis[] hyps, double[] P, int m) {
229 if(m == 0) {
230 WeightHypothesis lWH = new WeightHypothesis(dDomain);
231 for(int i = 0; i < P.length; i++) lWH.setWeight(i, P[i]);
232 hyps[index] = lWH;
233 index++;
234 } else {
235 for(int i = 0; i <= m; i++) {
236 index = weightPermutations(index, hyps, P, m-1);
237 if(i<m) {
238 //swap elements i and m
239 double tmp = P[i];
240 P[i]=P[m];
241 P[m] = tmp;
242 reverse(P, m - 1);
243 } //if
244 }
245 }
246 return index;
247 }
248
249 private void reverse(double[] array, int size)
250 {
251 int i=0, j=size;
252 while(i < j)
253 {
254 //swap i <-> j
255 double tmp = array[i];
256 array[i] = array[j];
257 array[j] = tmp;
258 i++;
259 j--;
260 }
261 }
262
263 private int factorial(int n)
264 {
265 int result = 1;
266 for(; n > 1; n--)
267 {
268 result *= n;
269 }
270 return result;
271 }
272
273 public void updateBeliefs(Bid pBid) throws Exception
274 {
275 //TODO: this variable counts the number of excludes
276 int iExcludeCnt = 0;
277
278 //calculate probability for the given bid
279 double lProbSum = 0;
280 double lMaxProb = 0;
281 for(int i = 0; i < uUSHypothesis.size(); i++)
282 {
283
284 UtilitySpaceHypothesis hyp = uUSHypothesis.get(i);
285 double condDistrib = hyp.getProbability() * conditionalDistribution(uUSHypothesis.get(i).getUtility(pBid), previousBidUtility);
286 lProbSum += condDistrib;
287 if(condDistrib > lMaxProb)
288 lMaxProb = condDistrib;
289 hyp.setProbability(condDistrib);
290
291
292 }
293
294 if(bUseMostProb)
295 mostProbHyps = new ArrayList<UtilitySpaceHypothesis>();
296
297 double mostProbHypSum = 0;
298
299
300 //update the weights hyps and evaluators hyps
301 for(int i = 0; i < uUSHypothesis.size(); i++)
302 {
303
304 UtilitySpaceHypothesis hyp = uUSHypothesis.get(i);
305 double normalizedProbability = hyp.getProbability() / lProbSum;
306
307 if(bUseMostProb)
308 if(normalizedProbability > lMaxProb * 0.95 / lProbSum) {
309 mostProbHyps.add(hyp);
310 mostProbHypSum += normalizedProbability;
311 }
312
313 //exclude if probability is 0
314 if(normalizedProbability > 0)
315 hyp.setProbability(normalizedProbability);
316 else
317 {
318 iExcludeCnt++;
319 uUSHypothesis.remove(i);
320 }
321 // --- end exclude hyps with prob. around 0
322 }
323
324 //normalize most probable hypothesis
325 if(bUseMostProb) {
326
327 for(int i=0;i<mostProbHyps.size();i++) {
328 UtilitySpaceHypothesis tmpHyp = mostProbHyps.get(i);
329 double normalizedProbability = tmpHyp.getProbability() / mostProbHypSum;
330 tmpHyp.setProbability(normalizedProbability);
331 }
332 }
333
334 //TODO: print excluded hypothesis count - with 0
335 //System.out.println("Excluded hypothesis with 0: " + iExcludeCnt);
336
337 if(uUSHypothesis.size() / initialNumberHyps >= 0.15)
338 {
339
340 //calculate utility of the next partner's bid according to the concession functions
341 previousBidUtility = previousBidUtility - CONCESSION_STRATEGY;
342
343 //sort hypotesis by probability
344
345 Collections.sort(uUSHypothesis);
346
347
348 //exclude bids with sum under 0.95
349 int cutPoint = Integer.MAX_VALUE;
350
351 double cummulativeSum = 0;
352
353 //get cutPoint
354 //and cumulative sum for normalization
355 for(int i = 0; i < uUSHypothesis.size(); i++)
356 {
357 cummulativeSum += uUSHypothesis.get(i).getProbability();
358 if(cummulativeSum > 0.95)
359 {
360 cutPoint = i;
361 break;
362 }
363 }
364 //System.out.println("Cumulative sum: " + cummulativeSum);
365
366 //eliminate from cutPoint to last item
367 if(cutPoint != Integer.MAX_VALUE)
368 {
369 for(int i = uUSHypothesis.size() - 1; i >= cutPoint; i--)
370 {
371 iExcludeCnt++;
372 uUSHypothesis.remove(i);
373 }
374 }
375
376 double normalizedUtilSum = 0;
377
378 //normalize remained hypothesis probability
379 for(int i = 0; i < uUSHypothesis.size(); i++)
380 {
381 UtilitySpaceHypothesis currentHyp = uUSHypothesis.get(i);
382 double newProbability = currentHyp.getProbability() / cummulativeSum;
383 currentHyp.setProbability(newProbability);
384 normalizedUtilSum += newProbability;
385 }
386 //TODO: print info about excludes
387 System.out.println("Excluded hypothesis at this step: " + iExcludeCnt);
388 System.out.println("Count of remained Hyps: " + uUSHypothesis.size());
389 System.out.println("NormSum: " + normalizedUtilSum);
390 }
391 }
392
393 private double conditionalDistribution(double pUtility, double pPreviousBidUtility)
394 {
395 if(pPreviousBidUtility < pUtility)
396 return 0;
397 else
398 {
399 double x = (pPreviousBidUtility - pUtility) / pPreviousBidUtility;
400 double distribution = (1 / (SIGMA * Math.sqrt(2 * Math.PI)) * Math.exp(-(x * x) / (2 * SIGMA * SIGMA)));
401 return distribution;
402 }
403 }
404
405 public double getExpectedUtility(Bid pBid) throws Exception
406 {
407 double lExpectedUtility = 0;
408
409 if(bUseMostProb &&(mostProbHyps != null)) {
410 for(int i=0; i < mostProbHyps.size(); i++) {
411 UtilitySpaceHypothesis tmpUSHypothesis = mostProbHyps.get(i);
412 double p = tmpUSHypothesis.getProbability();
413 double u = tmpUSHypothesis.getUtility(pBid);
414 lExpectedUtility += p * u;
415 }
416 }
417 else
418 {
419 for(int i = 0; i < uUSHypothesis.size(); i++)
420 {
421 UtilitySpaceHypothesis tmpUSHypothesis = uUSHypothesis.get(i);
422 double p = tmpUSHypothesis.getProbability();
423 double u = tmpUSHypothesis.getUtility(pBid);
424 lExpectedUtility += p * u;
425 }
426 }
427
428 //TODO: for test
429 //System.out.println("utility: " + lExpectedUtility + "for bid: " + pBid);
430
431 return lExpectedUtility;
432 }
433
434 public double getExpectedWeight(int pIssueNumber)
435 {
436 double lExpectedWeight = 0;
437 for(int i=0; i < uUSHypothesis.size(); i++) {
438 UtilitySpaceHypothesis lUSHyp = uUSHypothesis.get(i);
439 double p = lUSHyp.getProbability();
440 double u = lUSHyp.getHeightHyp().getWeight(pIssueNumber);
441 lExpectedWeight += p * u;
442 }
443 return lExpectedWeight;
444 }
445}
Note: See TracBrowser for help on using the repository browser.