[33] | 1 | package geniusweb.ip.inputOutput;
|
---|
| 2 |
|
---|
| 3 | import java.util.Arrays;
|
---|
| 4 |
|
---|
| 5 | import geniusweb.ip.general.Combinations;
|
---|
| 6 | import geniusweb.ip.general.General;
|
---|
| 7 | import geniusweb.ip.ipSolver.Node;
|
---|
| 8 | import geniusweb.ip.ipSolver.Subspace;
|
---|
| 9 | import geniusweb.ip.mainSolver.Result;
|
---|
| 10 |
|
---|
| 11 | public class Output {
|
---|
| 12 | private StringBuffer time_numOfRemainingCoalitions;
|
---|
| 13 | private StringBuffer time_solutionQuality;
|
---|
| 14 | private StringBuffer time_boundQuality;
|
---|
| 15 | private StringBuffer numOfSearchedCoalitions_solutionQuality;
|
---|
| 16 | private StringBuffer numOfSearchedCoalitions_boundQuality;
|
---|
| 17 | private double[] prevValues_time_numOfRemainingCoalitions;
|
---|
| 18 | private double[] prevValues_time_solutionQuality;
|
---|
| 19 | private double[] prevValues_time_boundQuality;
|
---|
| 20 | private double[] prevValues_numOfSearchedCoalitions_solutionQuality;
|
---|
| 21 | private double[] prevValues_numOfSearchedCoalitions_boundQuality;
|
---|
| 22 |
|
---|
| 23 | // ******************************************************************************************************
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Initialization
|
---|
| 27 | */
|
---|
| 28 | public Output(Input input) {
|
---|
| 29 | prevValues_time_solutionQuality = new double[2];
|
---|
| 30 | prevValues_time_boundQuality = new double[2];
|
---|
| 31 | prevValues_numOfSearchedCoalitions_boundQuality = new double[2];
|
---|
| 32 | prevValues_time_numOfRemainingCoalitions = new double[2];
|
---|
| 33 | prevValues_numOfSearchedCoalitions_solutionQuality = new double[2];
|
---|
| 34 |
|
---|
| 35 | prevValues_time_solutionQuality[0] = 0;
|
---|
| 36 | prevValues_time_solutionQuality[1] = 0;
|
---|
| 37 | prevValues_time_boundQuality[0] = 0;
|
---|
| 38 | prevValues_time_boundQuality[1] = 0;
|
---|
| 39 | prevValues_numOfSearchedCoalitions_boundQuality[0] = 0;
|
---|
| 40 | prevValues_numOfSearchedCoalitions_boundQuality[1] = 0;
|
---|
| 41 | prevValues_time_numOfRemainingCoalitions[0] = 0;
|
---|
| 42 | prevValues_time_numOfRemainingCoalitions[1] = 0;
|
---|
| 43 | prevValues_numOfSearchedCoalitions_solutionQuality[0] = 0;
|
---|
| 44 | prevValues_numOfSearchedCoalitions_solutionQuality[1] = 0;
|
---|
| 45 |
|
---|
| 46 | time_numOfRemainingCoalitions = new StringBuffer();
|
---|
| 47 | time_solutionQuality = new StringBuffer();
|
---|
| 48 | time_boundQuality = new StringBuffer();
|
---|
| 49 | numOfSearchedCoalitions_solutionQuality = new StringBuffer();
|
---|
| 50 | numOfSearchedCoalitions_boundQuality = new StringBuffer();
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | // ******************************************************************************************************
|
---|
| 54 |
|
---|
| 55 | /**
|
---|
| 56 | * Empty the contents of the String-Buffers to the relevant output files.
|
---|
| 57 | */
|
---|
| 58 | public void emptyStringBufferContentsIntoOutputFiles(Input input) {
|
---|
| 59 | if (input.printInterimResultsOfIPToFiles) {
|
---|
| 60 | General.createFolder(input.outputFolder);
|
---|
| 61 | String tempStr = input.outputFolder + "/" + "(" + input.problemID
|
---|
| 62 | + ")_";
|
---|
| 63 |
|
---|
| 64 | if (time_numOfRemainingCoalitions.length() != 0) {
|
---|
| 65 | General.printToFile(
|
---|
| 66 | tempStr + "time_numOfRemainingCoalitions.txt",
|
---|
| 67 | time_numOfRemainingCoalitions.toString(), false);
|
---|
| 68 | time_numOfRemainingCoalitions.setLength(0);
|
---|
| 69 | }
|
---|
| 70 | if (time_solutionQuality.length() != 0) {
|
---|
| 71 | General.printToFile(tempStr + "time_solutionQuality.txt",
|
---|
| 72 | time_solutionQuality.toString(), false);
|
---|
| 73 | time_solutionQuality.setLength(0);
|
---|
| 74 | }
|
---|
| 75 | if (time_boundQuality.length() != 0) {
|
---|
| 76 | General.printToFile(tempStr + "time_boundQuality.txt",
|
---|
| 77 | time_boundQuality.toString(), false);
|
---|
| 78 | time_boundQuality.setLength(0);
|
---|
| 79 | }
|
---|
| 80 | if (numOfSearchedCoalitions_solutionQuality.length() != 0) {
|
---|
| 81 | General.printToFile(
|
---|
| 82 | tempStr + "numOfSearchedCoalitions_solutionQuality.txt",
|
---|
| 83 | numOfSearchedCoalitions_solutionQuality.toString(),
|
---|
| 84 | false);
|
---|
| 85 | numOfSearchedCoalitions_solutionQuality.setLength(0);
|
---|
| 86 | }
|
---|
| 87 | if (numOfSearchedCoalitions_boundQuality.length() != 0) {
|
---|
| 88 | General.printToFile(
|
---|
| 89 | tempStr + "numOfSearchedCoalitions_boundQuality.txt",
|
---|
| 90 | numOfSearchedCoalitions_boundQuality.toString(), false);
|
---|
| 91 | numOfSearchedCoalitions_boundQuality.setLength(0);
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | // ******************************************************************************************************
|
---|
| 97 |
|
---|
| 98 | /**
|
---|
| 99 | * Print the details of IP (or ODP-IP) to string buffers.
|
---|
| 100 | */
|
---|
| 101 | public void printCurrentResultsOfIPToStringBuffer_ifPrevResultsAreDifferent(
|
---|
| 102 | Input input, Result result) {
|
---|
| 103 | if (input.printInterimResultsOfIPToFiles) {
|
---|
| 104 | long timeThatElapsedSinceIPStarted = System.currentTimeMillis()
|
---|
| 105 | - result.ipStartTime;
|
---|
| 106 | double boundQuality = result.ipUpperBoundOnOptimalValue
|
---|
| 107 | / result.get_ipValueOfBestCSFound();
|
---|
| 108 |
|
---|
| 109 | if ((prevValues_time_solutionQuality[0] != timeThatElapsedSinceIPStarted)
|
---|
| 110 | && (prevValues_time_solutionQuality[1] != result
|
---|
| 111 | .get_ipValueOfBestCSFound())) {
|
---|
| 112 | time_solutionQuality.append(timeThatElapsedSinceIPStarted + ", "
|
---|
| 113 | + result.get_ipValueOfBestCSFound() + "\n");
|
---|
| 114 | prevValues_time_solutionQuality[0] = timeThatElapsedSinceIPStarted;
|
---|
| 115 | prevValues_time_solutionQuality[1] = result
|
---|
| 116 | .get_ipValueOfBestCSFound();
|
---|
| 117 | }
|
---|
| 118 | if ((prevValues_time_boundQuality[0] != timeThatElapsedSinceIPStarted)
|
---|
| 119 | && (prevValues_time_boundQuality[1] != boundQuality)) {
|
---|
| 120 | time_boundQuality.append(timeThatElapsedSinceIPStarted + ", "
|
---|
| 121 | + boundQuality + "\n");
|
---|
| 122 | prevValues_time_boundQuality[0] = timeThatElapsedSinceIPStarted;
|
---|
| 123 | prevValues_time_boundQuality[1] = boundQuality;
|
---|
| 124 | }
|
---|
| 125 | if ((prevValues_numOfSearchedCoalitions_boundQuality[0] != result.ipNumOfExpansions)
|
---|
| 126 | && (prevValues_numOfSearchedCoalitions_boundQuality[1] != boundQuality)) {
|
---|
| 127 | numOfSearchedCoalitions_boundQuality.append(
|
---|
| 128 | result.ipNumOfExpansions + ", " + boundQuality + "\n");
|
---|
| 129 | prevValues_numOfSearchedCoalitions_boundQuality[0] = result.ipNumOfExpansions;
|
---|
| 130 | prevValues_numOfSearchedCoalitions_boundQuality[1] = boundQuality;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | if ((prevValues_numOfSearchedCoalitions_solutionQuality[0] != result.ipNumOfExpansions)
|
---|
| 134 | && (prevValues_numOfSearchedCoalitions_solutionQuality[1] != result
|
---|
| 135 | .get_ipValueOfBestCSFound())) {
|
---|
| 136 | numOfSearchedCoalitions_solutionQuality
|
---|
| 137 | .append(result.ipNumOfExpansions + ", "
|
---|
| 138 | + result.get_ipValueOfBestCSFound() + "\n");
|
---|
| 139 | prevValues_numOfSearchedCoalitions_solutionQuality[0] = result.ipNumOfExpansions;
|
---|
| 140 | prevValues_numOfSearchedCoalitions_solutionQuality[1] = result
|
---|
| 141 | .get_ipValueOfBestCSFound();
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | // ******************************************************************************************************
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * print the current results of IP to the relevant output files
|
---|
| 150 | */
|
---|
| 151 | public void printFinalResultsOfIPToFiles(Input input, Result result) {
|
---|
| 152 | if (input.printInterimResultsOfIPToFiles) {
|
---|
| 153 | General.createFolder(input.outputFolder);
|
---|
| 154 |
|
---|
| 155 | // Get the optimal coalition structure, and convert it to string
|
---|
| 156 | String bestCSFoundByIP_asString;
|
---|
| 157 | if (result.get_ipBestCSFound() == null)
|
---|
| 158 | bestCSFoundByIP_asString = "null";
|
---|
| 159 | else
|
---|
| 160 | bestCSFoundByIP_asString = Arrays
|
---|
| 161 | .deepToString(result.get_ipBestCSFound());
|
---|
| 162 |
|
---|
| 163 | // Get the level of the optimal coalition structure, and convert it
|
---|
| 164 | // to string
|
---|
| 165 | String levelOfBestCSFoundByIP_asString;
|
---|
| 166 | if (result.get_ipBestCSFound() == null)
|
---|
| 167 | levelOfBestCSFoundByIP_asString = "null";
|
---|
| 168 | else
|
---|
| 169 | levelOfBestCSFoundByIP_asString = (new Integer(
|
---|
| 170 | (result.get_ipBestCSFound()).length)).toString();
|
---|
| 171 |
|
---|
| 172 | // Get the integer partition of the optimal coailtion structure, and
|
---|
| 173 | // convert it to string
|
---|
| 174 | String integerPartitionOfBestCSFoundByIP_asString;
|
---|
| 175 | if (result.get_ipBestCSFound() == null)
|
---|
| 176 | integerPartitionOfBestCSFoundByIP_asString = "null";
|
---|
| 177 | else {
|
---|
| 178 | int[][] ipBestCSFound = result.get_ipBestCSFound();
|
---|
| 179 | int[] integerPartitionOfBestCSFoundByIP = new int[ipBestCSFound.length];
|
---|
| 180 | for (int i = 0; i < ipBestCSFound.length; i++)
|
---|
| 181 | integerPartitionOfBestCSFoundByIP[i] = ipBestCSFound[i].length;
|
---|
| 182 | integerPartitionOfBestCSFoundByIP_asString = General
|
---|
| 183 | .convertArrayToString(
|
---|
| 184 | integerPartitionOfBestCSFoundByIP);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | // Print the results
|
---|
| 188 | String tempStr = input.outputFolder + "/";
|
---|
| 189 | General.printToFile(tempStr + "bestCSFoundByIP.txt",
|
---|
| 190 | bestCSFoundByIP_asString + "\n", false);
|
---|
| 191 | General.printToFile(
|
---|
| 192 | tempStr + "integerPartitionOfBestCSFoundByIP.txt",
|
---|
| 193 | integerPartitionOfBestCSFoundByIP_asString + "\n", false);
|
---|
| 194 | General.printToFile(tempStr + "levelOfBestCSFoundByIP.txt",
|
---|
| 195 | levelOfBestCSFoundByIP_asString + "\n", false);
|
---|
| 196 | General.printToFile(tempStr + "valueOfBestCSFoundByIP.txt",
|
---|
| 197 | result.get_ipValueOfBestCSFound() + "\n", false);
|
---|
| 198 |
|
---|
| 199 | tempStr += "(" + input.problemID + ")_";
|
---|
| 200 | long timeThatElapsedSinceIPStarted = System.currentTimeMillis()
|
---|
| 201 | - result.ipStartTime;
|
---|
| 202 | double boundQuality = 1;// result.ipUpperBoundOnOptimalValue /
|
---|
| 203 | // result.ipValueOfBestCSFound;
|
---|
| 204 |
|
---|
| 205 | General.printToFile(tempStr + "time_solutionQuality.txt",
|
---|
| 206 | timeThatElapsedSinceIPStarted + ", "
|
---|
| 207 | + result.get_ipValueOfBestCSFound() + "\n",
|
---|
| 208 | false);
|
---|
| 209 | General.printToFile(tempStr + "time_boundQuality.txt",
|
---|
| 210 | timeThatElapsedSinceIPStarted + ", " + boundQuality + "\n",
|
---|
| 211 | false);
|
---|
| 212 | General.printToFile(
|
---|
| 213 | tempStr + "numOfSearchedCoalitions_boundQuality.txt",
|
---|
| 214 | result.ipNumOfExpansions + ", " + boundQuality + "\n",
|
---|
| 215 | false);
|
---|
| 216 | General.printToFile(
|
---|
| 217 | tempStr + "numOfSearchedCoalitions_solutionQuality.txt",
|
---|
| 218 | result.ipNumOfExpansions + ", "
|
---|
| 219 | + result.get_ipValueOfBestCSFound() + "\n",
|
---|
| 220 | false);
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | // ******************************************************************************************************
|
---|
| 225 |
|
---|
| 226 | /**
|
---|
| 227 | * Print the size, upperBound, and lowerBound for each subspace
|
---|
| 228 | */
|
---|
| 229 | public void printDetailsOfSubspaces(Input input, Result result) {
|
---|
| 230 | if (input.printDetailsOfSubspaces) {
|
---|
| 231 | // Count the number of possible coalition structures
|
---|
| 232 | long numOfCS = Combinations.getNumOfCS(input.numOfAgents);
|
---|
| 233 |
|
---|
| 234 | Node[][] nodes = result.ipIntegerPartitionGraph.nodes;
|
---|
| 235 | double[] percentagePerLevel = new double[nodes.length];
|
---|
| 236 | double maxPercentage = 0;
|
---|
| 237 | int ii = 0;
|
---|
| 238 | int jj = 0;
|
---|
| 239 | for (int i = 0; i < nodes.length; i++) {
|
---|
| 240 | System.out.println("");
|
---|
| 241 | percentagePerLevel[i] = 0;
|
---|
| 242 |
|
---|
| 243 | for (int j = 0; j < nodes[i].length; j++) {
|
---|
| 244 | Subspace curSubspace = nodes[i][j].subspace;
|
---|
| 245 |
|
---|
| 246 | double percentagePerSubspace = (double) (curSubspace.sizeOfSubspace
|
---|
| 247 | * 100) / numOfCS;
|
---|
| 248 | if (maxPercentage < percentagePerSubspace) {
|
---|
| 249 | maxPercentage = percentagePerSubspace;
|
---|
| 250 | ii = i; // Store the location of the biggest subspace
|
---|
| 251 | jj = j; // Store the location of the biggest subspace
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | // Update the percentage for the current level
|
---|
| 255 | percentagePerLevel[i] += percentagePerSubspace;
|
---|
| 256 |
|
---|
| 257 | // Printing the details
|
---|
| 258 | General.printArray("subspace[" + i + "][" + j + "] = ",
|
---|
| 259 | curSubspace.integers);
|
---|
| 260 | System.out.println(", size = "
|
---|
| 261 | + curSubspace.sizeOfSubspace + " ("
|
---|
| 262 | + General.setDecimalPrecision(percentagePerSubspace,
|
---|
| 263 | 5)
|
---|
| 264 | + "%)," + " UB = " + curSubspace.UB + ", LB = "
|
---|
| 265 | + curSubspace.LB + ", AVG = " + curSubspace.Avg);
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
| 268 | // Print the percentage per level
|
---|
| 269 | System.out.println("");
|
---|
| 270 | for (int i = 0; i < nodes.length; i++)
|
---|
| 271 | System.out.println("level" + (i + 1) + ": "
|
---|
| 272 | + General.setDecimalPrecision(percentagePerLevel[i], 5)
|
---|
| 273 | + "%\n");
|
---|
| 274 |
|
---|
| 275 | // Printing the biggest subspace:
|
---|
| 276 | Subspace biggestSubspace = nodes[ii][jj].subspace;
|
---|
| 277 | General.printArray("The biggest subspace is:",
|
---|
| 278 | biggestSubspace.integers);
|
---|
| 279 | System.out.println(" (" + maxPercentage + "%)\n");
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | // ******************************************************************************************************
|
---|
| 284 |
|
---|
| 285 | /**
|
---|
| 286 | * Prints to a file the time required to search a given subspace
|
---|
| 287 | */
|
---|
| 288 | public void printTimeRequiredForEachSubspace(Subspace subspace,
|
---|
| 289 | Input input) {
|
---|
| 290 | // Print the time required to search this sub-space
|
---|
| 291 | if (input.printTimeTakenByIPForEachSubspace) {
|
---|
| 292 | General.createFolder(input.outputFolder);
|
---|
| 293 | String integerPartitionAsString = General
|
---|
| 294 | .convertArrayToString(subspace.integers);
|
---|
| 295 | System.out.println("Searching " + integerPartitionAsString
|
---|
| 296 | + " took " + subspace.timeToSearchThisSubspace + " ms.");
|
---|
| 297 | General.printToFile(
|
---|
| 298 | input.outputFolder + "/" + "(" + input.problemID + ")_"
|
---|
| 299 | + "ipTimeForEachSubspace.txt",
|
---|
| 300 | "Searching " + integerPartitionAsString + " took "
|
---|
| 301 | + subspace.timeToSearchThisSubspace + "\n",
|
---|
| 302 | false);
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 | } |
---|