1 | package geniusweb.ip.mainSolver;
|
---|
2 |
|
---|
3 | import java.util.Arrays;
|
---|
4 |
|
---|
5 | import geniusweb.ip.general.Combinations;
|
---|
6 | import geniusweb.ip.general.General;
|
---|
7 | import geniusweb.ip.inputOutput.Input;
|
---|
8 | import geniusweb.ip.ipSolver.IDPSolver_whenRunning_ODPIP;
|
---|
9 | import geniusweb.ip.ipSolver.IntegerPartitionGraph;
|
---|
10 |
|
---|
11 | public class Result {
|
---|
12 | public Result(Input input) // The constructor
|
---|
13 | {
|
---|
14 | totalNumOfCS = Combinations.getNumOfCS(input.numOfAgents);
|
---|
15 | totalNumOfCoalitionsInSearchSpace = Combinations
|
---|
16 | .getNumOfCoalitionsInSearchSpace(input.numOfAgents);
|
---|
17 | dpMaxSizeThatWasComputedSoFar = 1;
|
---|
18 | }
|
---|
19 |
|
---|
20 | public int numOfAgents; // the number of agents
|
---|
21 |
|
---|
22 | public long totalNumOfCS; // The total number of coalition structures in the
|
---|
23 | // search space
|
---|
24 |
|
---|
25 | public long totalNumOfCoalitionsInSearchSpace; // The number of coailtions
|
---|
26 | // in the search space.
|
---|
27 | // Here,
|
---|
28 | // for every CS, we count the number of coalitions in it.
|
---|
29 |
|
---|
30 | public long totalNumOfExpansions; // the total number of expansions in the
|
---|
31 | // search space, given the tree-like
|
---|
32 | // representation of different
|
---|
33 | // integer-partition-based subspaces
|
---|
34 |
|
---|
35 | public IDPSolver_whenRunning_ODPIP idpSolver_whenRunning_ODPIP;
|
---|
36 |
|
---|
37 | // ***************************************************************************
|
---|
38 | // ***** *****
|
---|
39 | // ***** CPLEX results *****
|
---|
40 | // ***** *****
|
---|
41 | // ***************************************************************************
|
---|
42 |
|
---|
43 | public long cplexTime; // only relevant when running ILOG's CPLEX
|
---|
44 |
|
---|
45 | public double cpleXTime_confidenceInterval; // the error when averaging over
|
---|
46 | // multiple instances
|
---|
47 |
|
---|
48 | public int[][] cplexBestCSFound; // only relevant when running ILOG's CPLEX
|
---|
49 |
|
---|
50 | public double cplexValueOfBestCSFound; // only relevant when running ILOG's
|
---|
51 | // CPLEX
|
---|
52 |
|
---|
53 | public double cpleXValueOfBestCSFound_confidenceInterval; // the error when
|
---|
54 | // averaging
|
---|
55 | // over multiple
|
---|
56 | // instances
|
---|
57 |
|
---|
58 | // ***************************************************************************
|
---|
59 | // ***** *****
|
---|
60 | // ***** Dynamic Programming (DP, IDP, or ODP) results *****
|
---|
61 | // ***** *****
|
---|
62 | // ***************************************************************************
|
---|
63 |
|
---|
64 | private int dpMaxSizeThatWasComputedSoFar; // only relevant when running DP,
|
---|
65 | // IDP, or ODP
|
---|
66 |
|
---|
67 | private int[][] dpBestCSFound; // The best coalition structure found by DP,
|
---|
68 | // IDP, or ODP
|
---|
69 |
|
---|
70 | private double dpValueOfBestCSFound; // The value of the best coalition
|
---|
71 | // structure found by DP, IDP, or
|
---|
72 | // ODP
|
---|
73 |
|
---|
74 | public long dpTime; // The time required for DP, IDP, or ODP
|
---|
75 |
|
---|
76 | public long[] dpTimeForEachSize; // The time required for DP, IDP, or ODP
|
---|
77 | // for each size
|
---|
78 |
|
---|
79 | // ***************************************************************************
|
---|
80 | // ***** *****
|
---|
81 | // ***** IP or ODP-IP results *****
|
---|
82 | // ***** *****
|
---|
83 | // ***************************************************************************
|
---|
84 |
|
---|
85 | private int[][] ipBestCSFound; // only relevant when running IP or ODP-IP
|
---|
86 |
|
---|
87 | private double ipValueOfBestCSFound; // only relevant when running IP or
|
---|
88 | // ODP-IP
|
---|
89 |
|
---|
90 | public double ipValueOfBestCS_confidenceInterval; // the error when
|
---|
91 | // averaging over
|
---|
92 | // multiple runs
|
---|
93 |
|
---|
94 | public long ipStartTime; // only relevant when running IP or ODP-IP
|
---|
95 |
|
---|
96 | public double ipTimeForScanningTheInput_confidenceInterval; // the error
|
---|
97 | // when
|
---|
98 | // averaging
|
---|
99 | // over multiple
|
---|
100 | // runs
|
---|
101 |
|
---|
102 | public long ipTime; // only relevant when running IP or ODP-IP
|
---|
103 |
|
---|
104 | public double ipTime_confidenceInterval; // the error when averaging over
|
---|
105 | // multiple runs
|
---|
106 |
|
---|
107 | public long ipTimeForScanningTheInput; // only relevant when running IP or
|
---|
108 | // ODP-IP
|
---|
109 |
|
---|
110 | public long ipNumOfExpansions; // only relevant when running IP or ODP-IP
|
---|
111 |
|
---|
112 | public double ipNumOfExpansions_confidenceInterval; // the error when
|
---|
113 | // averaging over
|
---|
114 | // multiple runs
|
---|
115 |
|
---|
116 | public double ipUpperBoundOnOptimalValue; // only relevant when running IP
|
---|
117 | // or ODP-IP
|
---|
118 |
|
---|
119 | public double ipUpperBoundOnOptimalValue_confidenceInterval; // the error
|
---|
120 | // when
|
---|
121 | // averaging
|
---|
122 | // over
|
---|
123 | // multiple
|
---|
124 | // runs
|
---|
125 |
|
---|
126 | public double ipLowerBoundOnOptimalValue; // only relevant when running IP
|
---|
127 | // or ODP-IP
|
---|
128 |
|
---|
129 | public IntegerPartitionGraph ipIntegerPartitionGraph; // only relevant when
|
---|
130 | // running IP or
|
---|
131 | // ODP-IP
|
---|
132 |
|
---|
133 | private double[] max_f; // e.g., max_f[3] is the maximum f value that the
|
---|
134 | // dynamic programming solver has computed for a
|
---|
135 | // coalition of size 4
|
---|
136 |
|
---|
137 | // *****************************************************************************************************
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Initializes the main parameters
|
---|
141 | */
|
---|
142 | public void initialize() {
|
---|
143 | ipStartTime = System.currentTimeMillis();
|
---|
144 | ipNumOfExpansions = 0;
|
---|
145 | ipValueOfBestCSFound = -1;
|
---|
146 | ipBestCSFound = null;
|
---|
147 | totalNumOfExpansions = 0;
|
---|
148 | }
|
---|
149 |
|
---|
150 | // *****************************************************************************************************
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Only relevant when running DP, IDP, or ODP
|
---|
154 | *
|
---|
155 | * @param CS the best coalition structure found so far
|
---|
156 | * @param value the value of the best coalition structure found so far
|
---|
157 | */
|
---|
158 | public void updateDPSolution(int[][] CS, double value) {
|
---|
159 | if (get_dpValueOfBestCSFound() < value) {
|
---|
160 | set_dpValueOfBestCSFound(value);
|
---|
161 | set_dpBestCSFound(General.copyArray(CS));
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Only relevant when running IP or ODP-IP
|
---|
167 | *
|
---|
168 | * @param CS the best coalition structure found so far
|
---|
169 | * @param value the value of the best coalition structure found so far
|
---|
170 | */
|
---|
171 | public synchronized void updateIPSolution(int[][] CS, double value) {
|
---|
172 | if (get_ipValueOfBestCSFound() <= value) {
|
---|
173 | set_ipValueOfBestCSFound(value);
|
---|
174 | set_ipBestCSFound(General.copyArray(CS));
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | // *****************************************************************************************************
|
---|
179 |
|
---|
180 | public synchronized void set_dpMaxSizeThatWasComputedSoFar(int size) {
|
---|
181 | dpMaxSizeThatWasComputedSoFar = size;
|
---|
182 | }
|
---|
183 |
|
---|
184 | public synchronized int get_dpMaxSizeThatWasComputedSoFar() {
|
---|
185 | return dpMaxSizeThatWasComputedSoFar;
|
---|
186 | }
|
---|
187 |
|
---|
188 | public synchronized void set_dpBestCSFound(int[][] CS) {
|
---|
189 | dpBestCSFound = General.copyArray(CS);
|
---|
190 | }
|
---|
191 |
|
---|
192 | public synchronized int[][] get_dpBestCSFound() {
|
---|
193 | return dpBestCSFound;
|
---|
194 | }
|
---|
195 |
|
---|
196 | public synchronized void set_dpValueOfBestCSFound(double value) {
|
---|
197 | dpValueOfBestCSFound = value;
|
---|
198 | }
|
---|
199 |
|
---|
200 | public synchronized double get_dpValueOfBestCSFound() {
|
---|
201 | return dpValueOfBestCSFound;
|
---|
202 | }
|
---|
203 |
|
---|
204 | public /* synchronized */ void set_ipBestCSFound(int[][] CS) {
|
---|
205 | ipBestCSFound = General.copyArray(CS);
|
---|
206 | }
|
---|
207 |
|
---|
208 | public /* synchronized */ int[][] get_ipBestCSFound() {
|
---|
209 | return ipBestCSFound;
|
---|
210 | }
|
---|
211 |
|
---|
212 | public /* synchronized */ void set_ipValueOfBestCSFound(double value) {
|
---|
213 | ipValueOfBestCSFound = value;
|
---|
214 | }
|
---|
215 |
|
---|
216 | public /* synchronized */ double get_ipValueOfBestCSFound() {
|
---|
217 | return ipValueOfBestCSFound;
|
---|
218 | }
|
---|
219 |
|
---|
220 | public void set_max_f(int index, double value) {
|
---|
221 | max_f[index] = value;
|
---|
222 | }
|
---|
223 |
|
---|
224 | public double get_max_f(int index) {
|
---|
225 | return (max_f[index]);
|
---|
226 | }
|
---|
227 |
|
---|
228 | public void init_max_f(Input input, double[][] maxValueForEachSize) {
|
---|
229 | max_f = new double[input.numOfAgents];
|
---|
230 | for (int i = 0; i < input.numOfAgents; i++)
|
---|
231 | set_max_f(i, 0);
|
---|
232 | for (int i = 0; i < input.numOfAgents; i++) {
|
---|
233 | double value = input.getCoalitionValue((1 << i)); // compute max_f
|
---|
234 | // for
|
---|
235 | // coalitions of
|
---|
236 | // size = 1
|
---|
237 | if (get_max_f(0) < value)
|
---|
238 | set_max_f(0, value);
|
---|
239 | }
|
---|
240 | for (int i = 1; i < input.numOfAgents; i++) // compute max_f for
|
---|
241 | // coalitions of size = i+1
|
---|
242 | set_max_f(i, maxValueForEachSize[i][0]);
|
---|
243 | }
|
---|
244 |
|
---|
245 | @Override
|
---|
246 | public String toString() {
|
---|
247 | // ASSUMES IP. Copied form MainFrame#printResultOnGUI.
|
---|
248 | StringBuilder text = new StringBuilder();
|
---|
249 |
|
---|
250 | text.append("\nThe time for IP to scan the input (in milliseconds):\n"
|
---|
251 | + ipTimeForScanningTheInput + "\n");
|
---|
252 | text.append("\nThe total run time " + " (in milliseconds):\n" + ipTime
|
---|
253 | + "\n");
|
---|
254 | text.append("\nThe best coalition structure found " + " is:\n"
|
---|
255 | + Arrays.deepToString(get_ipBestCSFound()) + "\n");
|
---|
256 | text.append("\nThe value of this coalition structure is:\n"
|
---|
257 | + get_ipValueOfBestCSFound() + "\n");
|
---|
258 | text.append("\nThe number of expansions made by IP:\n"
|
---|
259 | + ipNumOfExpansions + "\n");
|
---|
260 | text.append(
|
---|
261 | "\nBased on this, the percentage of search-space that was searched "
|
---|
262 | + " is:\n" + (double) (ipNumOfExpansions * 100)
|
---|
263 | / totalNumOfExpansions
|
---|
264 | + "%\n");
|
---|
265 | return text.toString();
|
---|
266 | }
|
---|
267 |
|
---|
268 | } |
---|