source: src/main/java/agents/anac/y2019/minf/etc/LP_Estimation.java

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

Add ANAC 2019 agents

  • Property svn:executable set to *
File size: 4.6 KB
Line 
1package agents.anac.y2019.minf.etc;
2
3import genius.core.Bid;
4import genius.core.Domain;
5import genius.core.issue.IssueDiscrete;
6import genius.core.issue.ValueDiscrete;
7import genius.core.uncertainty.AdditiveUtilitySpaceFactory;
8import genius.core.uncertainty.BidRanking;
9import genius.core.uncertainty.OutcomeComparison;
10
11import agents.org.apache.commons.math.optimization.linear.*;
12import agents.org.apache.commons.math.optimization.RealPointValuePair;
13import agents.org.apache.commons.math.optimization.GoalType;
14
15import java.util.ArrayList;
16import java.util.Arrays;
17import java.util.List;
18
19public class LP_Estimation {
20 private AdditiveUtilitySpaceFactory additiveUtilitySpaceFactory;
21 private BidRanking bidRanking;
22 private List<IssueDiscrete> issues;
23
24 public LP_Estimation(){
25 }
26
27 public LP_Estimation(Domain d, BidRanking r){
28 additiveUtilitySpaceFactory = new AdditiveUtilitySpaceFactory(d);
29 issues = additiveUtilitySpaceFactory.getIssues();
30 bidRanking = r;
31 }
32
33 public AdditiveUtilitySpaceFactory Estimation()
34 throws Exception{
35 int issue_num = issues.size();
36 int[] var_num = new int[issue_num];
37 int[] cum_sum = new int[issue_num+1];
38 int num_vars = 0; // phyの数(求めたいW*V)
39 int num_comp = bidRanking.getSize() - 1;
40 double high = bidRanking.getHighUtility();
41 double low = bidRanking.getLowUtility();
42
43 for (IssueDiscrete i : issues){
44 int itr = i.getNumber()-1;
45 var_num[itr] = i.getNumberOfValues();
46 cum_sum[itr] = num_vars;
47 num_vars += i.getNumberOfValues();
48 }
49 cum_sum[cum_sum.length-1] = num_vars;
50
51 // 制約式の数がvars+comp*2+2,変数の数がvars+comp
52 double[][] cons_A = new double[num_vars + num_comp * 2 + 2][num_vars + num_comp];
53 double[] cons_b = new double[num_vars + num_comp * 2 + 2];
54 double[] obj_c = new double[num_vars + num_comp];
55 Arrays.fill(cons_b, 0.0D);
56 Arrays.fill(obj_c, 0.0D);
57 for (int i = 0; i < num_comp; i++){ obj_c[num_vars + i] = 1.0D; }
58
59 int pos = 0;
60
61 for (OutcomeComparison c : bidRanking.getPairwiseComparisons()){
62 Bid bid_low = c.getBid1();
63 Bid bid_high = c.getBid2();
64
65 for (IssueDiscrete i : issues){
66 int itr = i.getNumber()-1;
67 if (!bid_low.getValue(i).equals(bid_high.getValue(i))) {
68 cons_A[pos][cum_sum[itr] + i.getValueIndex(bid_low.getValue(i).toString())] = -1.0D;
69 cons_A[pos][cum_sum[itr] + i.getValueIndex(bid_high.getValue(i).toString())] = 1.0D;
70 }
71 }
72 cons_A[pos][num_vars + pos] = 1.0D;
73 pos++;
74 }
75
76 for (int i = 0; i < num_vars + num_comp; i++){ cons_A[pos++][i] = 1.0D; }
77
78 for (IssueDiscrete i : issues){
79 int itr = i.getNumber()-1;
80 cons_A[pos][cum_sum[itr] + i.getValueIndex(bidRanking.getMaximalBid().getValue(i).toString())] = 1.0D;
81 cons_A[pos+1][cum_sum[itr] + i.getValueIndex(bidRanking.getMinimalBid().getValue(i).toString())] = 1.0D;
82 }
83 cons_b[pos] = high;
84 cons_b[pos+1] = low;
85
86 LinearObjectiveFunction lof = new LinearObjectiveFunction(obj_c, 0.0D);
87 List<LinearConstraint> lc = new ArrayList<>();
88 for (int i = 0; i < cons_A.length-2; i++) {
89 lc.add(new LinearConstraint(cons_A[i], Relationship.GEQ, cons_b[i]));
90 }
91 lc.add(new LinearConstraint(cons_A[cons_A.length-2], Relationship.EQ, cons_b[cons_b.length-2]));
92 lc.add(new LinearConstraint(cons_A[cons_A.length-1], Relationship.EQ, cons_b[cons_b.length-1]));
93
94 SimplexSolver ss = new SimplexSolver();
95 ss.setMaxIterations(2147483647);
96
97 RealPointValuePair pvp = ss.optimize(lof, lc, GoalType.MINIMIZE, false);
98
99 double[] optimal = Arrays.copyOfRange(pvp.getPoint(), 0, num_vars);
100 for (int i = 0; i < optimal.length; i++){ optimal[i] = Math.max(0.0D, optimal[i]); }
101
102 for (IssueDiscrete i : issues) {
103 int itr = i.getNumber()-1;
104 double max = 0.0;
105 double[] tmp = Arrays.copyOfRange(optimal, cum_sum[itr], cum_sum[itr+1]);
106
107 for (double d : tmp) { max = Math.max(d, max); }
108
109 additiveUtilitySpaceFactory.setWeight(i, max);
110
111 for (ValueDiscrete v : i.getValues()) {
112 additiveUtilitySpaceFactory.setUtility(i, v, tmp[i.getValueIndex(v)]);
113 }
114 }
115
116 additiveUtilitySpaceFactory.normalizeWeights();
117
118 return additiveUtilitySpaceFactory;
119 }
120}
Note: See TracBrowser for help on using the repository browser.