| 1 | package de.uka.ipd.sdq.dsexplore.opt4j.optimizer.heuristic.operators; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.List; |
| 6 | import java.util.Random; |
| 7 | |
| 8 | import org.opt4j.core.problem.Genotype; |
| 9 | import org.opt4j.operator.copy.Copy; |
| 10 | |
| 11 | import de.uka.ipd.sdq.dsexplore.helper.ResultsWriter; |
| 12 | import de.uka.ipd.sdq.dsexplore.launch.DSEWorkflowConfiguration; |
| 13 | import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEIndividual; |
| 14 | import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEIndividualBuilder; |
| 15 | import de.uka.ipd.sdq.dsexplore.opt4j.start.Opt4JStarter; |
| 16 | |
| 17 | /** |
| 18 | * |
| 19 | * @author martens, beyer |
| 20 | * |
| 21 | */ |
| 22 | public class TacticOperatorsManager { |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * Heuristics to be used to generate candidates |
| 27 | */ |
| 28 | protected Collection<ITactic> heuristics = new ArrayList<ITactic>(); |
| 29 | protected ResultsWriter writer; |
| 30 | protected Random generator = new Random(); |
| 31 | |
| 32 | /** |
| 33 | * @param copy Creates copy of genotypes |
| 34 | * @param individualBuilder Builds individual |
| 35 | */ |
| 36 | public TacticOperatorsManager(Copy<Genotype> copy, DSEIndividualBuilder individualBuilder) { |
| 37 | DSEWorkflowConfiguration configuration = Opt4JStarter.getDSEWorkflowConfig(); |
| 38 | heuristics = TacticOperatorsFactory.getActivatedInstances(copy, individualBuilder, configuration); |
| 39 | this.writer = new ResultsWriter(Opt4JStarter.getDSEWorkflowConfig().getResultFolder()+"heuristicsInfo"); |
| 40 | writer.writeToLogFile("Tactic;Candidate numeric id;Parent numeric id;Candidate genome (several cols);Parent genome (several cols);Utilization value and whether returned\n"); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Applies registered heuristics to individual and returns candidates |
| 45 | * @param individual |
| 46 | * @return |
| 47 | */ |
| 48 | public List<TacticsResultCandidate> getAllCandidates(DSEIndividual individual) { |
| 49 | |
| 50 | //results cache for the current candidate. |
| 51 | UtilisationResultCacheAndHelper resultsCache = new UtilisationResultCacheAndHelper(); |
| 52 | |
| 53 | |
| 54 | List<TacticsResultCandidate> result = new ArrayList<TacticsResultCandidate>(); |
| 55 | Collection<TacticsResultCandidate> candidatesFromCurrentHeuristic; |
| 56 | for (ITactic heuristic : heuristics) { |
| 57 | candidatesFromCurrentHeuristic = heuristic.getHeuristicCandidates(individual, resultsCache); |
| 58 | if (candidatesFromCurrentHeuristic.size() > 0) { |
| 59 | this.writer.writeTacticCandidateInfo(heuristic, candidatesFromCurrentHeuristic); |
| 60 | result.addAll(candidatesFromCurrentHeuristic); |
| 61 | } |
| 62 | } |
| 63 | return result; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | /** |
| 69 | * Applies registered heuristics to individual and returns random candidate |
| 70 | * based on candidate weight and heuristic weight. Returns null if no heuristic |
| 71 | * can be be applied. |
| 72 | * @param individual |
| 73 | * @return Random candidate from registered heuristic or null if no heuristic can be applied |
| 74 | */ |
| 75 | public DSEIndividual getCandidate(DSEIndividual individual) { |
| 76 | Collection <TacticsResultCandidate> candidates = getAllCandidates(individual); |
| 77 | double sumOfAllWeights = 0; |
| 78 | for (TacticsResultCandidate c : candidates) { |
| 79 | sumOfAllWeights += c.getFinalWeight(); |
| 80 | } |
| 81 | |
| 82 | double threshold = generator.nextDouble()*sumOfAllWeights; |
| 83 | double currentLevel = 0; |
| 84 | for (TacticsResultCandidate c : candidates) { |
| 85 | currentLevel += c.getFinalWeight(); |
| 86 | // use spectrum of candidate weights to select candidate. The larger the |
| 87 | // weight of the candidate the higher the probability of being selected |
| 88 | if (currentLevel >= threshold) { |
| 89 | this.writer.writeTacticManagerChoice(c); |
| 90 | return c; |
| 91 | } |
| 92 | } |
| 93 | if (candidates.size() > 0) { |
| 94 | throw new RuntimeException("Random selection failed."); |
| 95 | } |
| 96 | // won't be executed unless candidates list is empty |
| 97 | else { |
| 98 | return null; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | public Collection<ITactic> getHeuristics() { |
| 103 | return heuristics; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | } |