source: src/main/java/agents/anac/y2016/maxoops/OPTComponent.java

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

Initial import : Genius 9.0.0

File size: 15.8 KB
Line 
1package agents.anac.y2016.maxoops;
2
3import java.util.ArrayList;
4import java.util.Enumeration;
5import java.util.HashMap;
6import java.util.HashSet;
7import java.util.Iterator;
8import java.util.List;
9import java.util.Map;
10import java.util.Map.Entry;
11
12import agents.Jama.Matrix;
13
14import java.util.Random;
15import java.util.Set;
16
17import genius.core.Bid;
18import genius.core.BidHistory;
19import genius.core.Domain;
20import genius.core.bidding.BidDetails;
21import genius.core.issue.Objective;
22import genius.core.issue.Value;
23import genius.core.issue.ValueDiscrete;
24import genius.core.issue.ValueInteger;
25import genius.core.issue.ValueReal;
26import genius.core.utility.AdditiveUtilitySpace;
27import genius.core.utility.EVALUATORTYPE;
28import genius.core.utility.Evaluator;
29import genius.core.utility.EvaluatorDiscrete;
30import genius.core.utility.EvaluatorInteger;
31import genius.core.utility.EvaluatorReal;
32
33public class OPTComponent {
34
35 // Adjustable Parameters
36 protected final double flrate;
37
38 MaxOops agent;
39 Domain domain;
40
41 public AdditiveUtilitySpace myUtilitySpace;
42 public HashMap<Objective, Evaluator> fEvaluators;
43 public Payoff[] payoffs;
44 public Matrix w;
45
46 public OPTComponent(MaxOops agent, AdditiveUtilitySpace utilitySpace) {
47 // Set Adjustable Parameters First
48 agent.params.addParam("OPTComponent.flrate", 0.1);
49 this.flrate = agent.params.getParam("OPTComponent.flrate");
50 this.agent = agent;
51 this.myUtilitySpace = utilitySpace;
52 this.domain = utilitySpace.getDomain();
53 this.fEvaluators = new HashMap<Objective, Evaluator>();
54 Set<Entry<Objective, Evaluator>> fEvaluatorsSet = this.myUtilitySpace
55 .getEvaluators();
56 for (Entry<Objective, Evaluator> entry : fEvaluatorsSet) {
57 this.fEvaluators.put(entry.getKey(), entry.getValue());
58 }
59 this.w = new Matrix(agent.numIssues, 1);
60 Objective root = agent.domain.getObjectivesRoot();
61 int i = 0;
62 for (Enumeration<Objective> issueEnum = root
63 .getPreorderIssueEnumeration(); issueEnum.hasMoreElements();) {
64 Objective is = (Objective) issueEnum.nextElement();
65 Evaluator eval = (Evaluator) fEvaluators.get(is);
66 this.w.set(i++, 0, eval.getWeight());
67 }
68 this.payoffs = new Payoff[agent.numParties - 1];
69 for (i = 0; i < agent.numParties - 1; i++) {
70 this.payoffs[i] = new Payoff(i, agent, myUtilitySpace, this);
71 }
72 }
73
74 public Bid getOptimalBidByThredhold(double f_thre) {
75 Bid[] firstBid = new Bid[agent.numParties - 1];
76 Bid[] bestBid = new Bid[agent.numParties - 1];
77 Bid[] lastBid = new Bid[agent.numParties - 1];
78 for (int i = 0; i < payoffs.length; i++) {
79 if (!agent.opponentsDistinctBids[i].isEmpty()) {
80 firstBid[i] = agent.opponentsDistinctBids[i]
81 .getFirstBidDetails().getBid();
82 bestBid[i] = agent.opponentsDistinctBids[i].getBestBidDetails()
83 .getBid();
84 lastBid[i] = agent.lastBid;
85 }
86 }
87 BidHistory bidsBySumOfPayoffs = new BidHistory();
88 int lbInd = Math.max(0, (int) (f_thre * 100) - 1);
89 int ubInd = Math.min((int) ((f_thre + 1. * agent.stdUtil) * 100) + 1,
90 100);
91 HashSet<Bid> searchBids = new HashSet<Bid>();
92 for (int i = lbInd; i <= ubInd; i++) {
93 searchBids.addAll(agent.hashBids.get(i));
94 }
95 if (agent.DMC.timeline.getTime() > 0.99 * agent.delta
96 && searchBids.size() < 1) {
97 for (int i = ubInd + 1; i <= 100; i++) {
98 searchBids.addAll(agent.hashBids.get(i));
99 }
100 }
101 MaxOops.log3.println("ubInd = " + ubInd + "lbInd = " + lbInd
102 + "f_thre = " + f_thre + ", bids size =" + searchBids.size());
103 Iterator<Bid> bidIter = searchBids.iterator();
104 Random rand = new Random((long) (f_thre * 10000) % 1001);
105 while (bidIter.hasNext()) {
106 Bid bid = bidIter.next();
107 double sumOfPayoffs = 0.;
108 for (int i = 0; i < agent.numParties; i++) {
109 if (i == agent.numParties - 1) {
110 sumOfPayoffs += myUtilitySpace.getUtility(bid);
111 break;
112 }
113 double sim1 = payoffs[i].getSimilarity(firstBid[i], bid) * 0.3
114 * rand.nextFloat();
115 double sim2 = payoffs[i].getSimilarity(bestBid[i], bid) * 0.2
116 * rand.nextFloat();
117 double sim3 = payoffs[i].getSimilarity(lastBid[i], bid) * 0.1
118 * rand.nextFloat();
119 sumOfPayoffs += payoffs[i].getPayoff(bid)
120 + (sim1 + sim2 + sim3) * rand.nextFloat()
121 * agent.stdUtil * 10;
122 }
123 bidsBySumOfPayoffs.add(new BidDetails(bid, sumOfPayoffs,
124 myUtilitySpace.getUtility(bid)));
125 }
126 if (bidsBySumOfPayoffs.isEmpty()) {
127 return null;
128 }
129 return bidsBySumOfPayoffs.getBestBidDetails().getBid();
130 }
131
132 public Matrix normalise(Matrix x) {
133 double min = 0, max = 0;
134 double[][] arr = x.getArray();
135 for (double[] vec : arr) {
136 for (double val : vec) {
137 min = Math.min(min, val);
138 max = Math.max(max, val);
139 }
140 }
141 x = x.plus(new Matrix(agent.numIssues, 1, Math.abs(min)));
142 x = x.times(1. / x.norm1());
143 return x;
144 }
145
146 public Matrix getMatrixFromBids(List<Bid> bids) {
147 int rows = bids.size();
148 Matrix X = new Matrix(rows, agent.numIssues);
149 for (int i = 0; i < rows; i++) {
150 X.setMatrix(new int[] { i }, 0, agent.numIssues - 1,
151 getVectorFromBid(bids.get(i)).transpose());
152 }
153 return X;
154 }
155
156 public List<Value> getValuesFromBid(Bid bid) {
157 Objective root = agent.domain.getObjectivesRoot();
158 List<Value> values = new ArrayList<Value>();
159 for (Enumeration<Objective> issueEnum = root
160 .getPreorderIssueEnumeration(); issueEnum.hasMoreElements();) {
161 Objective is = (Objective) issueEnum.nextElement();
162 Evaluator eval = (Evaluator) fEvaluators.get(is);
163 EVALUATORTYPE type = eval.getType();
164 switch (type) {
165 case REAL:
166 try {
167 values.add((ValueReal) bid.getValue(is.getNumber()));
168 } catch (Exception e) {
169 // TODO Auto-generated catch block
170 e.printStackTrace();
171 }
172 break;
173 case DISCRETE:
174 try {
175 values.add((ValueDiscrete) bid.getValue(is.getNumber()));
176 } catch (Exception e) {
177 // TODO Auto-generated catch block
178 e.printStackTrace();
179 }
180 break;
181 case INTEGER:
182 values.add((ValueInteger) bid.getValue(is.getNumber()));
183 break;
184 default:
185 break;
186 }
187 }
188 return values;
189 }
190
191 public Matrix getVectorFromBid(Bid bid) {
192 Objective root = agent.domain.getObjectivesRoot();
193 Matrix x = new Matrix(agent.numIssues, 1);
194 int i = 0;
195 for (Enumeration<Objective> issueEnum = root
196 .getPreorderIssueEnumeration(); issueEnum.hasMoreElements();) {
197 Objective is = (Objective) issueEnum.nextElement();
198 Evaluator eval = (Evaluator) fEvaluators.get(is);
199 double xi = 0;
200 EVALUATORTYPE type = eval.getType();
201 switch (type) {
202 case REAL:
203 EvaluatorReal evalReal = (EvaluatorReal) eval;
204 try {
205 xi = evalReal.getEvaluation(((ValueReal) bid.getValue(is
206 .getNumber())).getValue());
207 } catch (Exception e) {
208 // TODO Auto-generated catch block
209 e.printStackTrace();
210 }
211 break;
212 case DISCRETE:
213 EvaluatorDiscrete evalDis = (EvaluatorDiscrete) eval;
214 try {
215 xi = evalDis.getEvaluation((ValueDiscrete) bid.getValue(is
216 .getNumber()));
217 } catch (Exception e) {
218 // TODO Auto-generated catch block
219 e.printStackTrace();
220 }
221 break;
222 case INTEGER:
223 EvaluatorInteger evalInt = (EvaluatorInteger) eval;
224 xi = evalInt.getEvaluation(((ValueInteger) bid.getValue(is
225 .getNumber())).getValue());
226 break;
227 default:
228 xi = eval.getEvaluation(myUtilitySpace, bid, is.getNumber());
229 break;
230 }
231 x.set(i++, 0, xi);
232 }
233 return x;
234 }
235}
236
237class Payoff {
238
239 protected final int updateIterationLimit;
240
241 int opponent;
242 MaxOops agent;
243 Matrix w;
244 AdditiveUtilitySpace myUtilitySpace;
245 OPTComponent mother;
246 List<Bid> minibatchBidList;
247 int minibatchUpdateSize;
248 List<Map<Value, Integer>> freq;
249 List<Map<Value, Double>> eval;
250 double lastAvgPayoff;
251 private int iteration = 0;
252 private Matrix mem = null, g = null, g2 = null;
253
254 public Payoff(int opponent, MaxOops agent,
255 AdditiveUtilitySpace myUtilitySpace, OPTComponent mother) {
256 agent.params.addParam("OPTComponent.updateIterationLimit", 50);
257 this.updateIterationLimit = (int) agent.params
258 .getParam("OPTComponent.updateIterationLimit");
259 this.opponent = opponent;
260 this.iteration = 0;
261 this.agent = agent;
262 this.myUtilitySpace = myUtilitySpace;
263 this.minibatchBidList = new ArrayList<Bid>();
264 this.minibatchUpdateSize = 10;
265 this.mother = mother;
266 this.lastAvgPayoff = agent.secMaxUtil;
267 this.w = new Matrix(agent.numIssues, 1, 1. / agent.numIssues);
268 this.w = this.w.plus(mother.w).times(0.5);
269 this.freq = new ArrayList<Map<Value, Integer>>();
270 this.eval = new ArrayList<Map<Value, Double>>();
271 Objective root = agent.domain.getObjectivesRoot();
272 for (Enumeration<Objective> issueEnum = root
273 .getPreorderIssueEnumeration(); issueEnum.hasMoreElements();) {
274 Map<Value, Integer> valuesFreq = new HashMap<Value, Integer>();
275 Map<Value, Double> valuesEval = new HashMap<Value, Double>();
276 Objective is = (Objective) issueEnum.nextElement();
277 Evaluator e = (Evaluator) mother.fEvaluators.get(is);
278 EVALUATORTYPE type = e.getType();
279 Double xi = 0.;
280 switch (type) {
281 case REAL:
282 EvaluatorReal evalReal = (EvaluatorReal) e;
283 Double range = evalReal.getUpperBound()
284 - evalReal.getLowerBound();
285 for (Integer vi = 0; vi < 500; vi++) {
286 valuesFreq.put(new ValueInteger(vi), 0);
287 try {
288 Double v = range * vi / 500. + evalReal.getLowerBound();
289 xi = evalReal.getEvaluation(v);
290 } catch (Exception err) {
291 // TODO Auto-generated catch block
292 err.printStackTrace();
293 }
294 valuesEval.put(new ValueInteger(vi), xi);
295 }
296 break;
297 case DISCRETE:
298 EvaluatorDiscrete evalDis = (EvaluatorDiscrete) e;
299 Set<ValueDiscrete> values = evalDis.getValues();
300 for (ValueDiscrete v : values) {
301 valuesFreq.put(v, 0);
302 try {
303 xi = evalDis.getEvaluation(v);
304 } catch (Exception err) {
305 // TODO Auto-generated catch block
306 err.printStackTrace();
307 }
308 valuesEval.put(v, xi);
309 }
310 break;
311 case INTEGER:
312 EvaluatorInteger evalInt = (EvaluatorInteger) e;
313 for (Integer v = evalInt.getLowerBound(); v <= evalInt
314 .getUpperBound(); v++) {
315 valuesFreq.put(new ValueInteger(v), 0);
316 xi = evalInt.getEvaluation(v);
317 valuesEval.put(new ValueInteger(v), xi);
318 }
319 break;
320 default:
321 break;
322 }
323 this.freq.add(valuesFreq);
324 this.eval.add(valuesEval);
325 }
326 }
327
328 public void updateFrequency(Bid bid) {
329 List<Value> values = mother.getValuesFromBid(bid);
330 Objective root = agent.domain.getObjectivesRoot();
331 int i = 0;
332 for (Enumeration<Objective> issueEnum = root
333 .getPreorderIssueEnumeration(); issueEnum.hasMoreElements();) {
334 Objective is = (Objective) issueEnum.nextElement();
335 Evaluator e = (Evaluator) mother.fEvaluators.get(is);
336 EVALUATORTYPE type = e.getType();
337 switch (type) {
338 case REAL:
339 EvaluatorReal evalReal = (EvaluatorReal) e;
340 Double v = ((ValueReal) values.get(i)).getValue();
341 Double range = evalReal.getUpperBound()
342 - evalReal.getLowerBound();
343 Integer vi = (int) ((v - evalReal.getLowerBound()) * 500. / range);
344 ValueInteger Vi = new ValueInteger(vi);
345 this.freq.get(i).put(Vi, 1 + this.freq.get(i).get(Vi));
346 break;
347 case DISCRETE:
348 if (!this.freq.get(i).containsKey(values.get(i))) {
349 this.freq.get(i).put(values.get(i), 1);
350 } else {
351 this.freq.get(i).put(values.get(i),
352 1 + this.freq.get(i).get(values.get(i)));
353 }
354 break;
355 case INTEGER:
356 if (!this.freq.get(i).containsKey(values.get(i))) {
357 this.freq.get(i).put(values.get(i), 1);
358 } else {
359 this.freq.get(i).put(values.get(i),
360 1 + this.freq.get(i).get(values.get(i)));
361 }
362 break;
363 default:
364 break;
365 }
366 i++;
367 }
368 }
369
370 public void updateEvaluation() {
371 if (iteration >= updateIterationLimit / 2) {
372 return;
373 }
374 for (int i = 0; i < this.freq.size(); i++) {
375 double time = agent.DMC.timeline.getTime();
376 Double maxOfLogFreq = 0.;
377 for (Integer f : this.freq.get(i).values()) {
378 maxOfLogFreq = Math.max(Math.log(f + 1), maxOfLogFreq);
379 }
380 double wt = (0.5 - time) / 3.;
381 for (Value e : this.eval.get(i).keySet()) {
382 Integer f = this.freq.get(i).get(e);
383 Double ori = this.eval.get(i).get(e);
384 this.eval.get(i).put(e,
385 ori * (1 - wt) + wt * (Math.log(f + 1) / maxOfLogFreq));
386 }
387 Double maxOfEvals = 0.;
388 for (Double e : this.eval.get(i).values()) {
389 maxOfEvals = Math.max(e, maxOfEvals);
390 }
391 for (Value e : this.eval.get(i).keySet()) {
392 this.eval.get(i).put(e, this.eval.get(i).get(e) / maxOfEvals);
393 }
394 }
395 }
396
397 public void initWeights(Bid firstBid) {
398 iteration = 1;
399 this.updateFrequency(firstBid);
400 Matrix tw = mother.normalise(mother.getVectorFromBid(firstBid));
401 double min = 0, max = 0;
402 double[][] arr = tw.getArray();
403 for (double[] vec : arr) {
404 for (double val : vec) {
405 min = Math.min(min, val);
406 max = Math.max(max, val);
407 }
408 }
409 double updateWeight = 1. / (1. + (max - min));
410 this.w = tw.times(updateWeight).plus(this.w.times(1 - updateWeight));
411 }
412
413 public void updateWeights(Bid newBid) {
414 if (iteration >= updateIterationLimit
415 && agent.DMC.timeline.getTime() > 0.6) {
416 return;
417 }
418 this.updateFrequency(newBid);
419 minibatchBidList.add(newBid);
420 if (minibatchBidList.size() < minibatchUpdateSize) {
421 return;
422 }
423 this.updateEvaluation();
424 iteration += 1;
425 Matrix tw = w.copy();
426 Matrix X = mother.getMatrixFromBids(minibatchBidList);
427 Matrix y = new Matrix(minibatchUpdateSize, 1, lastAvgPayoff);
428 Matrix y_ = X.times(w);
429 Matrix dfdw = new Matrix(agent.numIssues, 1);
430 dfdw = X.transpose().times(y_.minus(y)).times(2. / minibatchUpdateSize);
431 lastAvgPayoff = y_.norm1() / minibatchUpdateSize;
432 minibatchBidList.clear();
433 Matrix ones = new Matrix(agent.numIssues, 1, 1.);
434 Matrix zeros = new Matrix(agent.numIssues, 1);
435 if (mem == null || g == null || g2 == null) {
436 mem = ones.copy();
437 g = zeros.copy();
438 g2 = zeros.copy();
439 }
440 Matrix r = ones.arrayRightDivide(mem.plus(ones));
441 g = g.arrayTimes(ones.minus(r)).plus(r.arrayTimes(dfdw));
442 g2 = g2.arrayTimes(ones.minus(r)).plus(
443 r.arrayTimes(dfdw.arrayTimes(dfdw)));
444 mem = mem.arrayTimes(ones.times(1 - mother.flrate)).plus(ones);
445 Matrix alrate = g.arrayTimes(g).arrayRightDivide(
446 g2.plus(ones.times(1e-13)));
447 for (int i = 0; i < agent.numIssues; i++) {
448 double alrateij = Math.min(alrate.get(i, 0), mother.flrate);
449 alrate.set(i, 0, alrateij / (Math.sqrt(g2.get(i, 0)) + 1e-13));
450 }
451 double avgPayoffDiff = dfdw.norm1();
452 tw = mother.normalise(tw.minus(dfdw.arrayTimes(alrate))).times(
453 avgPayoffDiff);
454 tw = tw.plus(w.times(1. - avgPayoffDiff));
455 double min = 0, max = 0;
456 double[][] arr = tw.getArray();
457 for (double[] vec : arr) {
458 for (double val : vec) {
459 min = Math.min(min, val);
460 max = Math.max(max, val);
461 }
462 }
463 this.w = tw;
464 }
465
466 public double getPayoff(Bid bid) {
467 double payoff = 0;
468 payoff = w.arrayTimes(getVectorFromBid(bid)).norm1();
469 return payoff;
470 }
471
472 public double getSimilarity(Bid bid1, Bid bid2) {
473 try {
474 Matrix x1 = getVectorFromBid(bid1);
475 Matrix x2 = getVectorFromBid(bid2);
476 return (x1.arrayTimes(x2)).norm1() * 1. / x1.norm1() / x2.norm1();
477 } catch (Exception err) {
478 return 0;
479 }
480 }
481
482 public Matrix getVectorFromBid(Bid bid) {
483 Matrix x = new Matrix(agent.numIssues, 1);
484 List<Value> values = mother.getValuesFromBid(bid);
485 int i = 0;
486 Objective root = agent.domain.getObjectivesRoot();
487 for (Enumeration<Objective> issueEnum = root
488 .getPreorderIssueEnumeration(); issueEnum.hasMoreElements();) {
489 Objective is = (Objective) issueEnum.nextElement();
490 Evaluator e = (Evaluator) mother.fEvaluators.get(is);
491 EVALUATORTYPE type = e.getType();
492 switch (type) {
493 case REAL:
494 EvaluatorReal evalReal = (EvaluatorReal) e;
495 Double v = ((ValueReal) values.get(i)).getValue();
496 Double range = evalReal.getUpperBound()
497 - evalReal.getLowerBound();
498 Integer vi = (int) ((v - evalReal.getLowerBound()) * 500. / range);
499 ValueInteger Vi = new ValueInteger(vi);
500 x.set(i, 0, this.eval.get(i).get(Vi));
501 break;
502 case DISCRETE:
503 x.set(i, 0, this.eval.get(i).get(values.get(i)));
504 break;
505 case INTEGER:
506 x.set(i, 0, this.eval.get(i).get(values.get(i)));
507 break;
508 default:
509 break;
510 }
511 i++;
512 }
513 return x;
514 }
515
516}
Note: See TracBrowser for help on using the repository browser.