| 1 | package de.uka.ipd.sdq.dsexplore.helper; |
| 2 | |
| 3 | import java.io.BufferedReader; |
| 4 | import java.io.BufferedWriter; |
| 5 | import java.io.File; |
| 6 | import java.io.FileInputStream; |
| 7 | import java.io.FileNotFoundException; |
| 8 | import java.io.FileOutputStream; |
| 9 | import java.io.IOException; |
| 10 | import java.io.InputStreamReader; |
| 11 | import java.io.OutputStreamWriter; |
| 12 | import java.util.ArrayList; |
| 13 | import java.util.List; |
| 14 | |
| 15 | import org.eclipse.core.runtime.CoreException; |
| 16 | import org.opt4j.core.Individual; |
| 17 | import org.opt4j.core.Objective; |
| 18 | import org.opt4j.core.Value; |
| 19 | import org.opt4j.core.Individual.State; |
| 20 | import org.opt4j.core.domination.ParetoDomination; |
| 21 | |
| 22 | import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEIndividual; |
| 23 | import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEObjectives; |
| 24 | import de.uka.ipd.sdq.dsexplore.opt4j.representation.NonListenableIndividual; |
| 25 | import de.uka.ipd.sdq.pcm.designdecision.DecisionSpace; |
| 26 | |
| 27 | public class FilterParetoOptimalIndividuals { |
| 28 | |
| 29 | public static void main(String args[]) throws CoreException { |
| 30 | |
| 31 | if (args.length < 2) { |
| 32 | System.out |
| 33 | .println("You need to specify the number of columns to be read and compared as first argument and the cvs file(s) as second and optional further arguments"); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | int noColumns = Integer.parseInt(args[0]); |
| 38 | |
| 39 | List<List<DSEIndividual>> allIndividuals = new ArrayList<List<DSEIndividual>>(); |
| 40 | List<File> files = new ArrayList<File>(); |
| 41 | |
| 42 | for (int i = 1; i < args.length; i++) { |
| 43 | String filename = args[i]; |
| 44 | File file = new File(filename); |
| 45 | files.add(file); |
| 46 | |
| 47 | System.out.println("Input: "+file.getAbsolutePath()); |
| 48 | |
| 49 | List<DSEIndividual> values = readInDoubles(file, noColumns); |
| 50 | |
| 51 | List<DSEIndividual> optimal = filterPareto(values); |
| 52 | |
| 53 | writeResults("results", file, optimal); |
| 54 | |
| 55 | |
| 56 | } |
| 57 | |
| 58 | //Filter the overall optimals and put them in the lists. |
| 59 | List<DSEIndividual> overallList = new ArrayList<DSEIndividual>(); |
| 60 | for (List<DSEIndividual> list : allIndividuals) { |
| 61 | overallList.addAll(list); |
| 62 | } |
| 63 | List<DSEIndividual> overallOptimal = filterPareto(overallList); |
| 64 | |
| 65 | //remove non-optimal from list and then print the remaining ones to see how many were optimal. |
| 66 | for (List<DSEIndividual> list : allIndividuals) { |
| 67 | List<DSEIndividual> toBeRemoved = new ArrayList<DSEIndividual>(); |
| 68 | for (DSEIndividual individual : list) { |
| 69 | if (!overallOptimal.contains(individual)){ |
| 70 | toBeRemoved.add(individual); |
| 71 | } |
| 72 | } |
| 73 | list.removeAll(toBeRemoved); |
| 74 | System.out.println("Input: "+files.get(allIndividuals.indexOf(list)).getAbsolutePath()); |
| 75 | writeResults("resultsComparedWithOthers", files.get(allIndividuals.indexOf(list)), list); |
| 76 | |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | |
| 81 | @SuppressWarnings("unchecked") |
| 82 | private static void writeResults(String filenamePrefix, File oldFile, List<DSEIndividual> optimalIndividuals) { |
| 83 | |
| 84 | File file = new File(oldFile.getParentFile(),filenamePrefix+oldFile.getName()); |
| 85 | try { |
| 86 | BufferedWriter w = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file))); |
| 87 | |
| 88 | for (Individual indiv : optimalIndividuals) { |
| 89 | for (Value double1 : indiv.getObjectives().getValues()) { |
| 90 | w.write(double1.getDouble()+";"); |
| 91 | } |
| 92 | w.newLine(); |
| 93 | } |
| 94 | |
| 95 | w.flush(); |
| 96 | |
| 97 | w.close(); |
| 98 | |
| 99 | System.out.println("Written results to "+file.getAbsolutePath()); |
| 100 | } catch (FileNotFoundException e) { |
| 101 | System.out.println("Writing failed."); |
| 102 | e.printStackTrace(); |
| 103 | } catch (IOException e) { |
| 104 | System.out.println("Writing failed."); |
| 105 | e.printStackTrace(); |
| 106 | } |
| 107 | |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * |
| 112 | * @param <AnyIndiv> |
| 113 | * @param individuals The list is not modified |
| 114 | * @return A copy of the given list with all non-optimal Individuals removed. |
| 115 | */ |
| 116 | public static final <AnyIndiv extends Individual> List<AnyIndiv> filterPareto(final List<AnyIndiv> individuals) { |
| 117 | |
| 118 | List<AnyIndiv> toBeRemoved = new ArrayList<AnyIndiv>(individuals.size()); |
| 119 | List<AnyIndiv> result = new ArrayList<AnyIndiv>(individuals.size()); |
| 120 | |
| 121 | result.addAll(individuals); |
| 122 | |
| 123 | for (int i = 0; i < individuals.size(); i++) { |
| 124 | AnyIndiv indiv1 = individuals.get(i); |
| 125 | for (int j = i + 1; j < individuals.size(); j++){ |
| 126 | AnyIndiv indiv2 = individuals.get(j); |
| 127 | if (indiv1.getObjectives().dominates(indiv2.getObjectives())){ |
| 128 | toBeRemoved.add(indiv2); |
| 129 | } else if (indiv2.getObjectives().dominates(indiv1.getObjectives())){ |
| 130 | toBeRemoved.add(indiv1); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } |
| 135 | |
| 136 | result.removeAll(toBeRemoved); |
| 137 | |
| 138 | return result; |
| 139 | |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Reads in Individuals from a file having the objectives in the first columns. |
| 144 | * Must contain headlines. |
| 145 | * @param filename |
| 146 | * @param noColumns The number of first columns that contain the objectives to be analysed. |
| 147 | * @return |
| 148 | */ |
| 149 | private static List<DSEIndividual> readInDoubles(File file, |
| 150 | int noColumns) { |
| 151 | |
| 152 | try { |
| 153 | |
| 154 | List<DSEIndividual> results = new ArrayList<DSEIndividual>(); |
| 155 | BufferedReader in = new BufferedReader(new InputStreamReader( |
| 156 | new FileInputStream(file))); |
| 157 | |
| 158 | String[] headLineArray = in.readLine().split(";"); |
| 159 | |
| 160 | List<Objective> objectiveList = new ArrayList<Objective>(); |
| 161 | |
| 162 | for (int i = 0; i < noColumns; i++) { |
| 163 | Objective o = new Objective(headLineArray[i]); |
| 164 | objectiveList.add(o); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | int noOfLine = 1; |
| 169 | |
| 170 | String line = ""; |
| 171 | while (null != (line = in.readLine())) { |
| 172 | |
| 173 | //Objectives lineList = new Objectives(); //TODO: LEGACY! |
| 174 | DSEObjectives lineList = new DSEObjectives(new ParetoDomination()); |
| 175 | |
| 176 | String[] lineArray = line.split(";"); |
| 177 | |
| 178 | if (lineArray.length == 0){ |
| 179 | //lines of the form ";;;;;;;" result in an empty array and should be skipped. |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | for (int i = 0; i < noColumns; i++) { |
| 184 | try { |
| 185 | Double value = Double.parseDouble(lineArray[i]); |
| 186 | lineList.add(objectiveList.get(i), value); |
| 187 | } catch (NumberFormatException e) { |
| 188 | System.out.println("Line " + noOfLine + " column " + i |
| 189 | + " is no double value, exiting"); |
| 190 | throw new Exception(e); |
| 191 | } |
| 192 | } |
| 193 | lineList.array(); |
| 194 | DSEIndividual indiv = new DummyDSECandidate(null, ""+noOfLine); |
| 195 | indiv.setObjectives(lineList); |
| 196 | indiv.setState(State.EVALUATED); |
| 197 | results.add(indiv); |
| 198 | noOfLine++; |
| 199 | } |
| 200 | in.close(); |
| 201 | |
| 202 | return results; |
| 203 | |
| 204 | } catch (Exception ex) { |
| 205 | ex.printStackTrace(); |
| 206 | } |
| 207 | return null; |
| 208 | } |
| 209 | |
| 210 | static class DummyDSECandidate extends NonListenableIndividual { |
| 211 | |
| 212 | private String id; |
| 213 | |
| 214 | public DummyDSECandidate(DecisionSpace problem, String id){ |
| 215 | super(problem); |
| 216 | this.id = id; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * |
| 221 | * @return may be null if this individual does not have a genotype yet. |
| 222 | */ |
| 223 | @Override |
| 224 | public String getGenotypeString() { |
| 225 | return id; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | } |