source: src/main/java/agents/anac/y2019/fsega2019/fsegaoppmodel/MyBayesianOpponentModelScalable.java

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

Add ANAC 2019 agents (3)

  • Property svn:executable set to *
File size: 17.7 KB
RevLine 
[202]1//package fsegaoppmodel;
2////import agents.bayesianopponentmodel.*;
3//import java.util.ArrayList;
4//
5//import genius.core.issue.Issue;
6//import genius.core.utility.UtilitySpace;
7//import genius.core.Bid;
8//import genius.core.BidIterator;
9//
10//
11//public class MyBayesianOpponentModelScalable extends OpponentModel {
12//
13//
14// private UtilitySpace fUS;
15// private ArrayList<ArrayList<WeightHypothesisScalable>> fWeightHyps;
16// private ArrayList<ArrayList<EvaluatorHypothesis>> fEvaluatorHyps;
17//// private ArrayList<EvaluatorHypothesis[]> fEvalHyps;
18//// public ArrayList<Bid> fBiddingHistory; // previous bids of the opponent, not our bids.
19//// private ArrayList<UtilitySpaceHypothesis> fUSHyps;
20// private double fPreviousBidUtility;
21// ArrayList<Issue> issues;
22// private double[] fExpectedWeights;
23// double minUtility, maxUtility;
24//
25// public MyBayesianOpponentModelScalable(UtilitySpace pUtilitySpace) {
26// //
27//
28// fPreviousBidUtility = 1;
29// fDomain = pUtilitySpace.getDomain();
30// issues=(ArrayList<Issue>) fDomain.getIssues();
31// fUS = pUtilitySpace;
32// fBiddingHistory = new ArrayList<Bid>();
33// fExpectedWeights = new double[pUtilitySpace.getDomain().getIssues().size()];
34// fWeightHyps = new ArrayList<ArrayList<WeightHypothesisScalable>>();
35//
36// initWeightHyps();
37//
38// //generate all possible hyps of evaluation functions
39// fEvaluatorHyps = new ArrayList<ArrayList<EvaluatorHypothesis>> ();
40// int lTotalTriangularFns = 4;
41// for(int i =0; i<(() fUS).getNrOfEvaluators();i++) {
42//// switch(fUS.getEvaluator(issues.get(i).getNumber()).getType()) {
43//// case PRICE:
44// ArrayList<EvaluatorHypothesis> lEvalHyps = new ArrayList<EvaluatorHypothesis>();
45// fEvaluatorHyps.add(lEvalHyps);
46//// //EvaluatorReal lEval = (EvaluatorReal)(fUS.getEvaluator(i));
47//// IssueReal lIssuePrice = (IssueReal)(fDomain.getIssue(i));
48//// //uphill
49//// EvaluatorReal lHypEval = new EvaluatorReal();
50//// lHypEval.setUpperBound(lIssuePrice.getUpperBound());
51//// lHypEval.setLowerBound(lIssuePrice.getLowerBound());
52//// lHypEval.setType(EVALFUNCTYPE.LINEAR);
53//// lHypEval.addParam(1, (double)1/(lHypEval.getUpperBound()-lHypEval.getLowerBound()));
54//// lHypEval.addParam(0, -lHypEval.getLowerBound()/(lHypEval.getUpperBound()-lHypEval.getLowerBound()));
55//// EvaluatorHypothesis lEvaluatorHypothesis = new EvaluatorHypothesis (lHypEval, "uphill");
56//// lEvalHyps.add(lEvaluatorHypothesis);
57//// //downhill
58//// lHypEval = new EvaluatorReal();
59//// lHypEval.setUpperBound(lIssuePrice.getUpperBound());
60//// lHypEval.setLowerBound(lIssuePrice.getLowerBound());
61//// lHypEval.setType(EVALFUNCTYPE.LINEAR);
62//// lHypEval.addParam(1, -(double)1/(lHypEval.getUpperBound()-lHypEval.getLowerBound()));
63//// lHypEval.addParam(0, (double)1+lHypEval.getLowerBound()/(lHypEval.getUpperBound()-lHypEval.getLowerBound()));
64//// lEvaluatorHypothesis = new EvaluatorHypothesis (lHypEval, "downhill");
65//// lEvalHyps.add(lEvaluatorHypothesis);
66//// for(int k=1;k<=lTotalTriangularFns;k++) {
67//// //triangular
68//// lHypEval = new EvaluatorReal();
69//// lHypEval.setUpperBound(lIssuePrice.getUpperBound());
70//// lHypEval.setLowerBound(lIssuePrice.getLowerBound());
71//// lHypEval.setType(EVALFUNCTYPE.TRIANGULAR);
72//// lHypEval.addParam(0, lHypEval.getLowerBound());
73//// lHypEval.addParam(1, lHypEval.getUpperBound());
74//// double lMaxPoint = lHypEval.getLowerBound()+(double)k*(lHypEval.getUpperBound()-lHypEval.getLowerBound())/(lTotalTriangularFns+1);
75//// lHypEval.addParam(2, lMaxPoint);
76//// lEvaluatorHypothesis = new EvaluatorHypothesis (lHypEval, "triangular " + String.valueOf(lMaxPoint));
77//// lEvalHyps.add(lEvaluatorHypothesis);
78//// }
79//// for(int k=0;k<lEvalHyps.size();k++) {
80//// lEvalHyps.get(k).setProbability((double)1/lEvalHyps.size());
81//// }
82////
83//// break;
84////
85//// case REAL:
86//// lEvalHyps = new ArrayList<EvaluatorHypothesis>();
87//// fEvaluatorHyps.add(lEvalHyps);
88//// //EvaluatorReal lEval = (EvaluatorReal)(fUS.getEvaluator(i));
89//// IssueReal lIssue = (IssueReal)(fDomain.getIssue(i));
90//// //uphill
91//// lHypEval = new EvaluatorReal();
92//// lHypEval.setUpperBound(lIssue.getUpperBound());
93//// lHypEval.setLowerBound(lIssue.getLowerBound());
94//// lHypEval.setType(EVALFUNCTYPE.LINEAR);
95//// lHypEval.addParam(1, (double)1/(lHypEval.getUpperBound()-lHypEval.getLowerBound()));
96//// lHypEval.addParam(0, -lHypEval.getLowerBound()/(lHypEval.getUpperBound()-lHypEval.getLowerBound()));
97//// lEvaluatorHypothesis = new EvaluatorHypothesis (lHypEval, "uphill");
98//// lEvalHyps.add(lEvaluatorHypothesis);
99//// //downhill
100//// lHypEval = new EvaluatorReal();
101//// lHypEval.setUpperBound(lIssue.getUpperBound());
102//// lHypEval.setLowerBound(lIssue.getLowerBound());
103//// lHypEval.setType(EVALFUNCTYPE.LINEAR);
104//// lHypEval.addParam(1, -(double)1/(lHypEval.getUpperBound()-lHypEval.getLowerBound()));
105//// lHypEval.addParam(0, (double)1+ lHypEval.getLowerBound()/(lHypEval.getUpperBound()-lHypEval.getLowerBound()));
106//// lEvaluatorHypothesis = new EvaluatorHypothesis (lHypEval, "downhill");
107//// lEvalHyps.add(lEvaluatorHypothesis);
108//// for(int k=1;k<=lTotalTriangularFns;k++) {
109//// //triangular
110//// lHypEval = new EvaluatorReal();
111//// lHypEval.setUpperBound(lIssue.getUpperBound());
112//// lHypEval.setLowerBound(lIssue.getLowerBound());
113//// lHypEval.setType(EVALFUNCTYPE.TRIANGULAR);
114//// lHypEval.addParam(0, lHypEval.getLowerBound());
115//// lHypEval.addParam(1, lHypEval.getUpperBound());
116//// double lMaxPoint = lHypEval.getLowerBound()+(double)k*(lHypEval.getUpperBound()-lHypEval.getLowerBound())/(lTotalTriangularFns+1);
117//// lHypEval.addParam(2, lMaxPoint);
118//// lEvaluatorHypothesis = new EvaluatorHypothesis (lHypEval, "triangular " + String.format("%1.2f", lMaxPoint));
119//// lEvalHyps.add(lEvaluatorHypothesis);
120//// }
121//// for(int k=0;k<lEvalHyps.size();k++) {
122//// lEvalHyps.get(k).setProbability((double)1/lEvalHyps.size());
123//// }
124////
125//// break;
126//// case DISCRETE:
127// lEvalHyps = new ArrayList<EvaluatorHypothesis>();
128// fEvaluatorHyps.add(lEvalHyps);
129// //EvaluatorReal lEval = (EvaluatorReal)(fUS.getEvaluator(i));
130// IssueDiscrete lDiscIssue = (IssueDiscrete)(fDomain.getIssue(i));
131// //uphill
132// EvaluatorDiscrete lDiscreteEval = new EvaluatorDiscrete();
133// for(int j=0;j<lDiscIssue.getNumberOfValues();j++)
134// lDiscreteEval.addEvaluation(lDiscIssue.getValue(j), 1000*j+1);
135// lEvaluatorHypothesis = new EvaluatorHypothesis (lDiscreteEval, "uphill");
136// lEvaluatorHypothesis.setProbability((double)1/3);
137// lEvalHyps.add(lEvaluatorHypothesis);
138// // downhill
139// lDiscreteEval = new EvaluatorDiscrete();
140// for(int j=0;j<lDiscIssue.getNumberOfValues();j++)
141// lDiscreteEval.addEvaluation(lDiscIssue.getValue(j), 1000*(lDiscIssue.getNumberOfValues()-j-1)+1);
142// lEvaluatorHypothesis = new EvaluatorHypothesis (lDiscreteEval, "downhill");
143// lEvaluatorHypothesis.setProbability((double)1/3);
144// lEvalHyps.add(lEvaluatorHypothesis);
145// if(lDiscIssue.getNumberOfValues()>2) {
146// lTotalTriangularFns = lDiscIssue.getNumberOfValues()-1;
147// for(int k=1;k<lTotalTriangularFns;k++) {
148// // triangular. Wouter: we need to CHECK this.
149// lDiscreteEval = new EvaluatorDiscrete();
150// for(int j=0;j<lDiscIssue.getNumberOfValues();j++)
151// if(j<k) {
152// lDiscreteEval.addEvaluation(lDiscIssue.getValue(j), 1000*j/k);
153// } else {
154// //lEval = (1.0-(double)(j-k)/(lDiscIssue.getNumberOfValues()-1.0-k));
155// lDiscreteEval.addEvaluation(lDiscIssue.getValue(j),
156// 1000*(lDiscIssue.getNumberOfValues()-j-1)/(lDiscIssue.getNumberOfValues()-k-1)+1);
157// }
158// lEvaluatorHypothesis = new EvaluatorHypothesis (lDiscreteEval, "triangular " + String.valueOf(k));
159// lEvalHyps.add(lEvaluatorHypothesis);
160// }//for
161// }//if
162// for(int k=0;k<lEvalHyps.size();k++) {
163// lEvalHyps.get(k).setProbability((double)1/lEvalHyps.size());
164// }
165// // break;
166//
167// }//switch
168// }
169// for(int i=0;i<fExpectedWeights.length;i++)
170// fExpectedWeights[i] = getExpectedWeight(i);
171//
172// //printEvalsDistribution();
173// }
174// final void initWeightHyps()
175// {
176// int lWeightHypsNumber = 11;
177// for(int i=0;i<fUS.getDomain().getIssues().size();i++)
178// {
179// ArrayList<WeightHypothesisScalable> lWeightHyps = new ArrayList<WeightHypothesisScalable>();
180// for(int j=0;j<lWeightHypsNumber;j++)
181// {
182// WeightHypothesisScalable lHyp = new WeightHypothesisScalable();
183// lHyp.setProbability((1.0-((double)j+1.0)/lWeightHypsNumber)*(1.0-((double)j+1.0)/lWeightHypsNumber)*(1.0-((double)j+1.0)/lWeightHypsNumber));
184// lHyp.setWeight((double)j/(lWeightHypsNumber-1));
185// lWeightHyps.add(lHyp);
186// }
187// double lN=0;
188// for(int j=0;j<lWeightHypsNumber;j++) {
189// lN+=lWeightHyps.get(j).getProbability();
190// }
191// for(int j=0;j<lWeightHypsNumber;j++) {
192// lWeightHyps.get(j).setProbability(lWeightHyps.get(j).getProbability()/lN);
193// }
194//
195// fWeightHyps.add(lWeightHyps);
196// }
197// }
198// private double conditionalDistribution(double pUtility, double pPreviousBidUtility)
199// {
200// double lSigma = 0.25;
201// double x = (pPreviousBidUtility - pUtility)/pPreviousBidUtility ;
202// double lResult = 1.0/(lSigma*Math.sqrt(2.0*Math.PI)) *Math.exp(-(x*x)/(2.0*lSigma*lSigma));
203// return lResult;
204// }
205// public double getExpectedEvaluationValue(Bid pBid, int pIssueNumber) throws Exception
206// {
207// double lExpectedEval = 0;
208// for(int j=0;j<fEvaluatorHyps.get(pIssueNumber).size();j++) {
209// lExpectedEval = lExpectedEval + fEvaluatorHyps.get(pIssueNumber).get(j).getProbability() *
210// fEvaluatorHyps.get(pIssueNumber).get(j).getEvaluator().getEvaluation(fUS, pBid,issues.get(pIssueNumber).getNumber());
211// }
212// return lExpectedEval;
213//
214// }
215// public final double getExpectedWeight(int pIssueNumber) {
216// double lExpectedWeight = 0;
217// for(int i=0;i<fWeightHyps.get(pIssueNumber).size();i++) {
218// lExpectedWeight += fWeightHyps.get(pIssueNumber).get(i).getProbability()*fWeightHyps.get(pIssueNumber).get(i).getWeight();
219// }
220// return lExpectedWeight;
221// }
222// private double getPartialUtility(Bid pBid, int pIssueIndex) throws Exception
223// {
224// //calculate partial utility w/o issue pIssueIndex
225// double u = 0;
226// for(int j=0;j<fDomain.getIssues().size();j++) {
227// if(pIssueIndex==j) continue;
228// //calculate expected weight of the issue
229// double w = 0 ;
230// for(int k=0;k<fWeightHyps.get(j).size();k++)
231// w += fWeightHyps.get(j).get(k).getProbability()*fWeightHyps.get(j).get(k).getWeight();
232// u = u + w*getExpectedEvaluationValue(pBid, j);
233// }
234// return u;
235// }
236// public void updateWeights() throws Exception
237// {
238// //Bid lPreviousBid = fBiddingHistory.get(fBiddingHistory.size()-2);
239// Bid lBid = fBiddingHistory.get(fBiddingHistory.size()-1);
240// ArrayList<ArrayList<WeightHypothesisScalable>> lWeightHyps = new ArrayList<ArrayList<WeightHypothesisScalable>>();
241// //make new hyps array
242// for(int i=0;i<fWeightHyps.size();i++) {
243// ArrayList<WeightHypothesisScalable> lTmp = new ArrayList<WeightHypothesisScalable>();
244// for(int j=0;j<fWeightHyps.get(i).size();j++) {
245// WeightHypothesisScalable lHyp = new WeightHypothesisScalable();
246// lHyp.setWeight(fWeightHyps.get(i).get(j).getWeight());
247// lHyp.setProbability(fWeightHyps.get(i).get(j).getProbability());
248// lTmp.add(lHyp);
249// }
250// lWeightHyps.add(lTmp);
251// }
252//
253// for(int j=0;j<fDomain.getIssues().size();j++)
254// {
255// double lN = 0;
256// double lUtility = 0;
257// for(int i=0;i<fWeightHyps.get(j).size();i++)
258// {
259// lUtility = fWeightHyps.get(j).get(i).getWeight()*getExpectedEvaluationValue(lBid, j) + getPartialUtility(lBid, j);
260// lN += fWeightHyps.get(j).get(i).getProbability()*conditionalDistribution(lUtility, fPreviousBidUtility);
261// }
262// //2. update probabilities
263// for(int i=0;i<fWeightHyps.get(j).size();i++)
264// {
265// lUtility = fWeightHyps.get(j).get(i).getWeight()*getExpectedEvaluationValue(lBid, j) + getPartialUtility(lBid, j);
266// lWeightHyps.get(j).get(i).setProbability(fWeightHyps.get(j).get(i).getProbability()*conditionalDistribution(lUtility, fPreviousBidUtility)/lN);
267// }
268// }
269// // }
270// fWeightHyps = lWeightHyps;
271// }
272//
273// public void updateEvaluationFns() throws Exception
274// {
275// Bid lBid = fBiddingHistory.get(fBiddingHistory.size()-1);
276// //make new hyps array
277//// for(int k=0;k<5;k++){
278// ArrayList<ArrayList<EvaluatorHypothesis>> lEvaluatorHyps = new ArrayList<ArrayList<EvaluatorHypothesis>>();
279// for(int i=0;i<fEvaluatorHyps.size();i++) {
280// ArrayList<EvaluatorHypothesis> lTmp = new ArrayList<EvaluatorHypothesis>();
281// for(int j=0;j<fEvaluatorHyps.get(i).size();j++) {
282// EvaluatorHypothesis lHyp = new EvaluatorHypothesis(fEvaluatorHyps.get(i).get(j).getEvaluator(), "triangular " + fEvaluatorHyps.get(i).get(j).getDescription());
283// lHyp.setProbability(fEvaluatorHyps.get(i).get(j).getProbability());
284// lTmp.add(lHyp);
285// }
286// lEvaluatorHyps.add(lTmp);
287// }
288//
289// //1. calculate the normalization factor
290//
291// for(int i=0;i<fDomain.getIssues().size();i++) {
292// //1. calculate the normalization factor
293// double lN = 0;
294// for(int j=0;j<fEvaluatorHyps.get(i).size();j++) {
295// EvaluatorHypothesis lHyp =fEvaluatorHyps.get(i).get(j);
296// lN += lHyp.getProbability()*conditionalDistribution(getPartialUtility(lBid, i)+getExpectedWeight(i)*(lHyp.getEvaluator().getEvaluation(fUS, lBid, issues.get(i).getNumber())), fPreviousBidUtility);
297// }
298// //2. update probabilities
299// for(int j=0;j<fEvaluatorHyps.get(i).size();j++) {
300// EvaluatorHypothesis lHyp =fEvaluatorHyps.get(i).get(j);
301// lEvaluatorHyps.get(i).get(j).setProbability(lHyp.getProbability()*conditionalDistribution(getPartialUtility(lBid, i)+getExpectedWeight(i)*(lHyp.getEvaluator().getEvaluation(fUS, lBid, issues.get(i).getNumber())), fPreviousBidUtility)/lN);
302// }
303// }
304// fEvaluatorHyps = lEvaluatorHyps;
305// // }
306// printEvalsDistribution();
307// }
308// public boolean haveSeenBefore(Bid pBid) {
309// for(Bid tmpBid : fBiddingHistory) {
310// if(pBid.equals(tmpBid)) return true;
311// }
312// return false;
313// }
314// public void updateBeliefs(Bid pBid) throws Exception{
315// if(haveSeenBefore(pBid)) return;
316// fBiddingHistory.add(pBid);
317//
318// //do not update the bids if it is the first bid
319// if(fBiddingHistory.size()>1) {
320//
321// //update the weights
322// updateWeights();
323// //update evaluation functions
324// updateEvaluationFns();
325// } else {
326// //do not update the weights
327// //update evaluation functions
328// updateEvaluationFns();
329// } //if
330//
331//// System.out.println(getMaxHyp().toString());
332// //calculate utility of the next partner's bid according to the concession functions
333// fPreviousBidUtility = fPreviousBidUtility-0.003;
334// for(int i=0;i<fExpectedWeights.length;i++)
335// fExpectedWeights[i] = getExpectedWeight(i);
336// findMinMaxUtility();
337// // printBestHyp();
338// }
339//
340// /**
341// * Plan: cache the results for pBid in a Hash table.
342// * empty the hash table whenever updateWeights or updateEvaluationFns is called.
343// * @param pBid
344// * @return weeighted utility where weights represent likelihood of each hypothesis
345// * @throws Exception
346// */
347// public double getExpectedUtility(Bid pBid) throws Exception
348// {
349// //calculate expected utility
350// double u = 0;
351// for(int j=0;j<fDomain.getIssues().size();j++) {
352// //calculate expected weight of the issue
353// double w = fExpectedWeights[j] ;
354///* for(int k=0;k<fWeightHyps.get(j).size();k++)
355// w += fWeightHyps.get(j).get(k).getProbability()*fWeightHyps.get(j).get(k).getWeight();(*/
356// u = u + w*getExpectedEvaluationValue(pBid, j);
357// }
358// return u;
359// }
360//
361//
362// /*private void printBestHyp() {
363// double[] lBestWeights = new double[fUS.getDomain().getIssues().size()];
364// EvaluatorHypothesis[] lBestEvals = new EvaluatorHypothesis[fUS.getDomain().getIssues().size()];
365// for(int i=0;i<fUS.getDomain().getIssues().size();i++) {
366// //find best weight
367// double lMaxWeightProb = -1;
368// for(int j=0;j<fWeightHyps.get(i).size();j++){
369// if(fWeightHyps.get(i).get(j).getProbability()>lMaxWeightProb) {
370// lMaxWeightProb = fWeightHyps.get(i).get(j).getProbability();
371// lBestWeights[i] = fWeightHyps.get(i).get(j).getWeight();
372// }
373// }
374// //find best evaluation fn
375// double lMaxEvalProb = -1;
376// for(int j=0;j<fEvaluatorHyps.get(i).size();j++){
377// if(fEvaluatorHyps.get(i).get(j).getProbability()>lMaxEvalProb ) {
378// lMaxEvalProb = fEvaluatorHyps.get(i).get(j).getProbability();
379// lBestEvals[i] = fEvaluatorHyps.get(i).get(j);
380// }
381// }
382//
383// }
384///* //print all weights
385// for(int i=0;i<fUS.getDomain().getIssues().size();i++) {
386// System.out.print(String.format("%1.2f", getExpectedWeight(i))+";");
387// }
388// //print all Evaluators
389// for(int i=0;i<fUS.getDomain().getIssues().size();i++) {
390// System.out.print(lBestEvals[i].getDesc()+";");
391// }
392// System.out.println();
393// *_/ Dan add _
394// }*/
395// void printEvalsDistribution() {
396///* for(int i=0;i<fUS.getDomain().getIssues().size();i++) {
397// for(int j=0;j<fEvaluatorHyps.get(i).size();j++)
398// System.out.print(String.format("%1.2f", fEvaluatorHyps.get(i).get(j).getProbability())+";");
399// System.out.println();
400// }*/
401//
402// }
403//
404// protected void findMinMaxUtility() throws Exception
405// {
406// BidIterator biditer=new BidIterator(fDomain);
407// minUtility=1.; maxUtility=0.; double u;
408// int i=0;
409// while (biditer.hasNext())
410// {
411// Bid b=biditer.next();
412// u=getExpectedUtility(b);
413//
414// if (minUtility>u) minUtility=u;
415// if (maxUtility<u) maxUtility=u;
416// i++;
417//// System.out.println(String.valueOf(i));
418// }
419// }
420//
421//
422//}
Note: See TracBrowser for help on using the repository browser.