| 1 | package de.uka.ipd.sdq.dsexplore.opt4j.optimizer; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Enumeration; |
| 5 | import java.util.HashSet; |
| 6 | import java.util.LinkedList; |
| 7 | import java.util.List; |
| 8 | import java.util.Set; |
| 9 | |
| 10 | import org.apache.log4j.LogManager; |
| 11 | import org.apache.log4j.Logger; |
| 12 | import org.apache.log4j.WriterAppender; |
| 13 | import org.eclipse.core.runtime.CoreException; |
| 14 | import org.opt4j.core.Archive; |
| 15 | import org.opt4j.core.Individual; |
| 16 | import org.opt4j.core.IndividualBuilder; |
| 17 | import org.opt4j.core.Population; |
| 18 | import org.opt4j.core.optimizer.AbstractOptimizer; |
| 19 | import org.opt4j.core.optimizer.Completer; |
| 20 | import org.opt4j.core.optimizer.Control; |
| 21 | import org.opt4j.core.optimizer.Iterations; |
| 22 | import org.opt4j.core.optimizer.StopException; |
| 23 | import org.opt4j.core.optimizer.TerminationException; |
| 24 | import org.opt4j.core.problem.Genotype; |
| 25 | import org.opt4j.operator.copy.Copy; |
| 26 | import org.opt4j.start.Constant; |
| 27 | |
| 28 | import com.google.inject.Inject; |
| 29 | |
| 30 | import de.uka.ipd.sdq.dsexplore.helper.FilterParetoOptimalIndividuals; |
| 31 | import de.uka.ipd.sdq.dsexplore.helper.ResultsWriter; |
| 32 | import de.uka.ipd.sdq.dsexplore.opt4j.optimizer.heuristic.operators.TacticOperatorsManager; |
| 33 | import de.uka.ipd.sdq.dsexplore.opt4j.optimizer.heuristic.operators.TacticsResultCandidate; |
| 34 | import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSECreator; |
| 35 | import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEIndividual; |
| 36 | import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEIndividualBuilder; |
| 37 | import de.uka.ipd.sdq.dsexplore.opt4j.start.Opt4JStarter; |
| 38 | |
| 39 | /** |
| 40 | * |
| 41 | * @author martens |
| 42 | * |
| 43 | */ |
| 44 | public class RuleBasedSearch extends AbstractOptimizer { |
| 45 | |
| 46 | private TacticOperatorsManager tacticsManager; |
| 47 | private int generations; |
| 48 | |
| 49 | private boolean fullSearch; |
| 50 | |
| 51 | /** Logger for log4j. */ |
| 52 | private static Logger logger = |
| 53 | Logger.getLogger("de.uka.ipd.sdq.dsexplore.opt4j.optimizer.RuleBasedSearch"); |
| 54 | |
| 55 | @Inject |
| 56 | public RuleBasedSearch(Population population, Archive archive, |
| 57 | IndividualBuilder individualBuilder, Completer completer, |
| 58 | Control control, Copy<Genotype> copy, @Iterations int generations, |
| 59 | @Constant(value = "fullSearch", namespace = RuleBasedSearch.class) boolean fullSearch) { |
| 60 | super(population, archive, individualBuilder, completer, control); |
| 61 | this.tacticsManager = new TacticOperatorsManager(copy, (DSEIndividualBuilder)individualBuilder); |
| 62 | this.generations = generations; |
| 63 | this.fullSearch = fullSearch; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public void optimize() throws StopException, TerminationException { |
| 68 | |
| 69 | Individual ind = individualBuilder.build(); |
| 70 | this.population.add(ind); |
| 71 | |
| 72 | try { |
| 73 | DSECreator creator = Opt4JStarter.getDSECreator(); |
| 74 | // also take additional predefined candidates, if any |
| 75 | int numberOfPredefinedOnes = creator.getNumberOfNotEvaluatedPredefinedOnes(); |
| 76 | for (int i = 1; i <= numberOfPredefinedOnes; i++) { |
| 77 | Individual ind2 = individualBuilder.build(); |
| 78 | this.population.add(ind2); |
| 79 | } |
| 80 | } catch (CoreException e) { |
| 81 | throw new RuntimeException(e); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | |
| 86 | nextIteration(); |
| 87 | |
| 88 | int i = 0; |
| 89 | for (; i < this.generations; i++){ |
| 90 | |
| 91 | List<Individual> nextGeneration = new LinkedList<Individual>(); |
| 92 | |
| 93 | for (Individual individual : this.population) { |
| 94 | if (individual instanceof DSEIndividual){ |
| 95 | DSEIndividual dseIndividual = (DSEIndividual)individual; |
| 96 | List<TacticsResultCandidate> candidateResults = this.tacticsManager.getAllCandidates(dseIndividual); |
| 97 | nextGeneration.addAll(candidateResults); |
| 98 | |
| 99 | } else { |
| 100 | throw new RuntimeException("Encountered a non-DSEIndividual in rule based search that I cannot handle. Aborting."); |
| 101 | } |
| 102 | |
| 103 | } |
| 104 | |
| 105 | //remove duplicates |
| 106 | int nextGenerationSize = nextGeneration.size(); |
| 107 | nextGeneration.removeAll(population); |
| 108 | nextGeneration.removeAll(archive); |
| 109 | logger.info("Removed "+(nextGeneration.size() - nextGenerationSize)+" duplicate candidate (that were in the previous population or in the archive)."); |
| 110 | |
| 111 | completer.complete(nextGeneration); |
| 112 | |
| 113 | this.population.clear(); |
| 114 | this.population.addAll(nextGeneration); |
| 115 | |
| 116 | if (!this.fullSearch){ |
| 117 | List<Individual> archiveAndNewUnion = new ArrayList<Individual>(this.archive.size() + nextGeneration.size()); |
| 118 | archiveAndNewUnion.addAll(nextGeneration); |
| 119 | archiveAndNewUnion.addAll(archive); |
| 120 | List<Individual> optimalCandidates = FilterParetoOptimalIndividuals.filterPareto(archiveAndNewUnion); |
| 121 | this.population.retainAll(optimalCandidates); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | if (this.population.size() == 0){ |
| 127 | logger.warn("No more individuals in population, aborting after iteration "+i); |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | nextIteration(); |
| 132 | |
| 133 | } |
| 134 | logger.warn("Finished rule-based search after "+i+" iterations."); |
| 135 | if (i == this.generations){ |
| 136 | logger.warn("Stop condition was the configured maximum number of iterations, more rule applications may be possible. There were "+population.size()+" candidates in the final population."); |
| 137 | } |
| 138 | |
| 139 | ResultsWriter writer = new ResultsWriter(Opt4JStarter.getDSEWorkflowConfig().getResultFolder()+"rule-based search results"); |
| 140 | for (Individual individual : this.archive) { |
| 141 | if (individual instanceof DSEIndividual){ |
| 142 | DSEIndividual dseIndiv = (DSEIndividual)individual; |
| 143 | writer.writeIndividual(dseIndiv); |
| 144 | } else { |
| 145 | logger.warn("Encountered a non DSE-Individual in the rule based search, aborting to write results."); |
| 146 | } |
| 147 | } |
| 148 | writer.flush(); |
| 149 | |
| 150 | flushAllLogs(); |
| 151 | |
| 152 | |
| 153 | } |
| 154 | |
| 155 | @SuppressWarnings("unchecked") |
| 156 | public static void flushAllLogs() |
| 157 | { |
| 158 | try |
| 159 | { |
| 160 | Set<WriterAppender> flushedFileAppenders = new HashSet<WriterAppender>(); |
| 161 | Enumeration currentLoggers = LogManager.getLoggerRepository().getCurrentLoggers(); |
| 162 | while(currentLoggers.hasMoreElements()) |
| 163 | { |
| 164 | Object nextLogger = currentLoggers.nextElement(); |
| 165 | if(nextLogger instanceof Logger) |
| 166 | { |
| 167 | Logger currentLogger = (Logger) nextLogger; |
| 168 | Enumeration allAppenders = currentLogger.getAllAppenders(); |
| 169 | while(allAppenders.hasMoreElements()) |
| 170 | { |
| 171 | Object nextElement = allAppenders.nextElement(); |
| 172 | if(nextElement instanceof WriterAppender) |
| 173 | { |
| 174 | WriterAppender fileAppender = (WriterAppender) nextElement; |
| 175 | if(!flushedFileAppenders.contains(fileAppender) && !fileAppender.getImmediateFlush()) |
| 176 | { |
| 177 | flushedFileAppenders.add(fileAppender); |
| 178 | //log.info("Appender "+fileAppender.getName()+" is not doing immediateFlush "); |
| 179 | fileAppender.setImmediateFlush(true); |
| 180 | currentLogger.info("FLUSH"); |
| 181 | fileAppender.setImmediateFlush(false); |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | //log.info("fileAppender"+fileAppender.getName()+" is doing immediateFlush"); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | catch(RuntimeException e) |
| 193 | { |
| 194 | logger.error("Failed flushing logs",e); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | |
| 199 | |
| 200 | } |