1 | package agents.anac.y2019.ibasic.boacomponents;
|
---|
2 |
|
---|
3 | import java.util.Arrays;
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.ArrayList;
|
---|
6 |
|
---|
7 | import genius.core.Bid;
|
---|
8 | import genius.core.Domain;
|
---|
9 | import genius.core.issue.Issue;
|
---|
10 | import genius.core.issue.IssueDiscrete;
|
---|
11 | import genius.core.issue.ValueDiscrete;
|
---|
12 | import genius.core.utility.AbstractUtilitySpace;
|
---|
13 | import genius.core.uncertainty.AdditiveUtilitySpaceFactory;
|
---|
14 | import genius.core.uncertainty.BidRanking;
|
---|
15 | import genius.core.uncertainty.UserModel;
|
---|
16 |
|
---|
17 | import ilog.concert.*;
|
---|
18 | import ilog.cplex.*;
|
---|
19 |
|
---|
20 | public class IBasicPU
|
---|
21 | {
|
---|
22 | public static AbstractUtilitySpace createUtilitySpace(Domain domain, UserModel userModel)
|
---|
23 | {
|
---|
24 | //Loading of the IloCplex Modeler
|
---|
25 | IloCplexModeler icp = new IloCplexModeler();
|
---|
26 |
|
---|
27 | // Obtain the list of issues and the bid ranking.
|
---|
28 | List<Issue> issueList = domain.getIssues();
|
---|
29 | BidRanking bidRanking = userModel.getBidRanking();
|
---|
30 |
|
---|
31 | // Create an array that stores all issues for easy lookup of issue numbers.
|
---|
32 | Issue[] issueArray = new Issue[issueList.size()];
|
---|
33 |
|
---|
34 | // Also create a list containing an array of discrete values for each issue for easy lookup later on.
|
---|
35 | List<ValueDiscrete[]> valueArrayList = new ArrayList<ValueDiscrete[]>();
|
---|
36 |
|
---|
37 | // Fill the issue array and calculate the total number of values.
|
---|
38 | for (int index = 0; index < issueList.size(); index++)
|
---|
39 | issueArray[index] = issueList.get(index);
|
---|
40 |
|
---|
41 | // Fill the value array list.
|
---|
42 | for (int i = 0; i < issueList.size(); i++)
|
---|
43 | {
|
---|
44 | IssueDiscrete discIssue = (IssueDiscrete) issueArray[i];
|
---|
45 | List<ValueDiscrete> discValues = discIssue.getValues();
|
---|
46 | ValueDiscrete[] array = new ValueDiscrete[discValues.size()];
|
---|
47 | for (int index = 0; index < discValues.size(); index ++)
|
---|
48 | {
|
---|
49 | array[index] = discValues.get(index);
|
---|
50 | }
|
---|
51 | valueArrayList.add(array);
|
---|
52 | }
|
---|
53 |
|
---|
54 | //The try catch is where the linear optimization problem is solved. This will happen in three steps:
|
---|
55 | //1st: We determine the entry variables
|
---|
56 | //2nd: We set the constraints
|
---|
57 | //3rd: We solve the problem and create a utility space from the resulting soluion
|
---|
58 |
|
---|
59 | try {
|
---|
60 | //--------------------------------------------------------------------------------------------------------------------------------------------------------
|
---|
61 | //STEP 1: We initialize the entry variables. This happens using three sets of variables.
|
---|
62 | //1st: The phi values that correspond to each issue/value
|
---|
63 | //2nd: The set of slack variables
|
---|
64 | //3rd: The phi values that correspond to the maximum phi values
|
---|
65 | //Finally we initialize the objective function to be minimized.
|
---|
66 |
|
---|
67 | IloCplex cplexModel = new IloCplex();
|
---|
68 |
|
---|
69 | // Initialize the first set of entry arrays. Each issue corresponds to an array where its phi values are stored.
|
---|
70 | List<IloNumVar[]> entryArrayList = new ArrayList<IloNumVar[]>();
|
---|
71 | for (int i=0; i<issueList.size(); i++)
|
---|
72 | {
|
---|
73 | IssueDiscrete discIssue = (IssueDiscrete) issueArray[i];
|
---|
74 | List<ValueDiscrete> discValues = discIssue.getValues();
|
---|
75 | entryArrayList.add(new IloNumVar[discValues.size()]);
|
---|
76 | }
|
---|
77 |
|
---|
78 | // We fill these arrays with a default number.
|
---|
79 | for (IloNumVar[] array: entryArrayList)
|
---|
80 | for (int index =0; index<array.length; index++)
|
---|
81 | array[index]=cplexModel.numVar(0.0, Double.MAX_VALUE);
|
---|
82 |
|
---|
83 | // Initialize the second entry array, which contains all slack variables and fill it.
|
---|
84 | IloNumVar[] slackVariables = new IloNumVar[bidRanking.getSize()-1];
|
---|
85 | for (int index=0; index<bidRanking.getSize()-1; index++)
|
---|
86 | slackVariables[index] = cplexModel.numVar(0, Double.MAX_VALUE);
|
---|
87 |
|
---|
88 | // Initialise the third entry array, which contains the max phi values for each issue and fill it.
|
---|
89 | IloNumVar[] maxPhiVariables = new IloNumVar[issueList.size()];
|
---|
90 | for (int index=0; index<issueList.size(); index++)
|
---|
91 | maxPhiVariables[index] = cplexModel.numVar(0, Double.MAX_VALUE);
|
---|
92 |
|
---|
93 | // Initialise the expression to be minimised, i.e. the sum of all slack variables.
|
---|
94 | IloLinearNumExpr objectiveFunction = cplexModel.linearNumExpr();
|
---|
95 | for (int index=0; index<bidRanking.getSize()-1; index++) {
|
---|
96 | objectiveFunction.addTerm(1, slackVariables[index]);
|
---|
97 | }
|
---|
98 | cplexModel.addMinimize(objectiveFunction);
|
---|
99 |
|
---|
100 | //--------------------------------------------------------------------------------------------------------------------------------------------------------
|
---|
101 | // STEP 2: We initialize the constraints. A total of
|
---|
102 | // Note that the positivity constraints are already contained in the definition of the unknown variables, therefore
|
---|
103 | // we do not explicitly have to add the constraints that the slack variables and the phi-variables should be positive.
|
---|
104 | // 1st: For each pairwise comparison of the outcomes the sum of the difference in Utility and the slack variables should be higher than 0.
|
---|
105 | // 2nd: The sum of the phi variables of the values of the highest bid should be the highest possible utility.
|
---|
106 | // 3rd: The sum of the phi variables of the values of the lowest bid should be the lowest possible utility.
|
---|
107 | // More: The sum of the highest phi variables for each issue has to be 1.
|
---|
108 | // 4th: We set of constraints that the maximum variables should be equal to the maximum phi values of the given index.
|
---|
109 | // 5th: We set the constraint that the maximum variables should sum to 1.
|
---|
110 |
|
---|
111 |
|
---|
112 | //Initialization of each constraint and the highest and lowestbids.
|
---|
113 | IloLinearNumExpr constraint1;
|
---|
114 | IloLinearNumExpr constraint2 = cplexModel.linearNumExpr();
|
---|
115 | IloLinearNumExpr constraint3 = cplexModel.linearNumExpr();
|
---|
116 | IloLinearNumExpr constraint5 = cplexModel.linearNumExpr();
|
---|
117 | Bid highestBid = bidRanking.getMaximalBid();
|
---|
118 | Bid lowestBid = bidRanking.getMinimalBid();
|
---|
119 |
|
---|
120 | for (int index=0; index<bidRanking.getSize()-1; index++)
|
---|
121 | {
|
---|
122 | constraint1 = cplexModel.linearNumExpr();
|
---|
123 |
|
---|
124 | // We add the slack variable to the constraint.
|
---|
125 | constraint1.addTerm(1, slackVariables[index]);
|
---|
126 | Bid higherBid = bidRanking.getBidOrder().get(bidRanking.getSize()-1-index);
|
---|
127 | Bid nextBid = bidRanking.getBidOrder().get(bidRanking.getSize()-2-index);
|
---|
128 |
|
---|
129 | // We add the difference in Utility in the phi variables for each issue.
|
---|
130 | for (int j=0; j<issueList.size(); j++)
|
---|
131 | {
|
---|
132 | ValueDiscrete higherValueDiscrete = (ValueDiscrete) higherBid.getValue(issueArray[j]);
|
---|
133 | ValueDiscrete nextValueDiscrete = (ValueDiscrete) nextBid.getValue(issueArray[j]);
|
---|
134 |
|
---|
135 | int higherValueIndex = Arrays.asList(valueArrayList.get(j)).indexOf(higherValueDiscrete);
|
---|
136 | int nextValueIndex = Arrays.asList(valueArrayList.get(j)).indexOf(nextValueDiscrete);
|
---|
137 |
|
---|
138 | constraint1.addTerm(1, entryArrayList.get(j)[higherValueIndex]);
|
---|
139 | constraint1.addTerm(-1, entryArrayList.get(j)[nextValueIndex]);
|
---|
140 | }
|
---|
141 |
|
---|
142 | //Set the constraint that this sum has to be larger than 0
|
---|
143 | cplexModel.addGe(constraint1,0.0);
|
---|
144 | }
|
---|
145 |
|
---|
146 | //We initialize the rest of the constraints in one loop over the issues.
|
---|
147 | for (int index = 0; index < issueList.size(); index++)
|
---|
148 | {
|
---|
149 | //2nd constraint
|
---|
150 | ValueDiscrete highestValueDiscrete = (ValueDiscrete) highestBid.getValue(issueArray[index]);
|
---|
151 | int highestValueIndex = Arrays.asList(valueArrayList.get(index)).indexOf(highestValueDiscrete);
|
---|
152 | constraint2.addTerm(1, entryArrayList.get(index)[highestValueIndex]);
|
---|
153 |
|
---|
154 | //3rd constraint
|
---|
155 | ValueDiscrete lowestValueDiscrete = (ValueDiscrete) lowestBid.getValue(issueArray[index]);
|
---|
156 | int lowestValueIndex = Arrays.asList(valueArrayList.get(index)).indexOf(lowestValueDiscrete);
|
---|
157 | constraint3.addTerm(1, entryArrayList.get(index)[lowestValueIndex]);
|
---|
158 |
|
---|
159 | //4th constraint
|
---|
160 | IloLinearNumExpr constraint4 = cplexModel.linearNumExpr();
|
---|
161 | constraint4.addTerm(1,maxPhiVariables[index]);
|
---|
162 | constraint4.addTerm(-1,(IloNumVar)icp.max(entryArrayList.get(index)));
|
---|
163 | cplexModel.addEq(constraint4,0);
|
---|
164 |
|
---|
165 | //5th constraint
|
---|
166 | constraint5.addTerm(1, maxPhiVariables[index]);
|
---|
167 | }
|
---|
168 | // The sum has to be equal to the highest utility for the second constraint
|
---|
169 | cplexModel.addEq(constraint2,bidRanking.getHighUtility());
|
---|
170 |
|
---|
171 | // The sum has to be equal to the lowest utility for the third constraint.
|
---|
172 | cplexModel.addEq(constraint3,bidRanking.getLowUtility());
|
---|
173 |
|
---|
174 | //Add the constraint of the 5th
|
---|
175 | cplexModel.addEq(constraint5, 1.0);
|
---|
176 |
|
---|
177 | //--------------------------------------------------------------------------------------------------------------------------------------------------------
|
---|
178 | //Step three: solve the linear optimisation problem and create a utility space from the resulting solution.
|
---|
179 | //This happens by simply setting each valuation of each value to the corresponding phi value.
|
---|
180 | //We do not need to take weight into account, since this is already incorporated in the phi-value.
|
---|
181 |
|
---|
182 | if (cplexModel.solve())
|
---|
183 | {
|
---|
184 | AdditiveUtilitySpaceFactory createdUtilitySpace = new AdditiveUtilitySpaceFactory(domain);
|
---|
185 |
|
---|
186 | //We loop through the issues and get the valuation of the each value and if it is the highest value in the set,
|
---|
187 | //we keep it and end by setting the weight to this number
|
---|
188 | for (int index = 0; index < issueList.size(); index++)
|
---|
189 | {
|
---|
190 | IssueDiscrete issue = (IssueDiscrete) issueList.get(index);
|
---|
191 | List<ValueDiscrete> values = issue.getValues();
|
---|
192 |
|
---|
193 | double totalPhi = 0.0;
|
---|
194 | double maxphi = 0.0;
|
---|
195 |
|
---|
196 | for (ValueDiscrete singleValue: values)
|
---|
197 | {
|
---|
198 | double valueValuation = cplexModel.getValue(entryArrayList.get(index)[Arrays.asList(valueArrayList.get(index)).indexOf(singleValue)]);
|
---|
199 | if (valueValuation > maxphi)
|
---|
200 | maxphi = valueValuation;
|
---|
201 | totalPhi += valueValuation;
|
---|
202 | }
|
---|
203 | createdUtilitySpace.setWeight(issueList.get(index), maxphi);
|
---|
204 |
|
---|
205 | //Some phi values end up with 0 values, which needs to be corrected. As a solution we divide the highest value by the total amount of values.
|
---|
206 | double averagePhi = totalPhi / values.size();
|
---|
207 | for (ValueDiscrete value: values)
|
---|
208 | {
|
---|
209 | int valueIndex = Arrays.asList(valueArrayList.get(index)).indexOf(value);
|
---|
210 | double valueEvaluation = cplexModel.getValue(entryArrayList.get(index)[valueIndex]);
|
---|
211 | if (valueEvaluation == 0.0)
|
---|
212 | {
|
---|
213 | valueEvaluation = averagePhi;
|
---|
214 | }
|
---|
215 | createdUtilitySpace.setUtility(issueList.get(index), value,valueEvaluation);
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | //We return the created utility space after the loop has finished, marking the end of this method.
|
---|
220 | return createdUtilitySpace.getUtilitySpace();
|
---|
221 |
|
---|
222 | }
|
---|
223 | else
|
---|
224 | return null;
|
---|
225 |
|
---|
226 |
|
---|
227 | }
|
---|
228 | catch (IloException ex)
|
---|
229 | {
|
---|
230 | return null;
|
---|
231 | }
|
---|
232 |
|
---|
233 | }
|
---|
234 | }
|
---|