1 | package de.uka.ipd.sdq.dsexplore.opt4j.optimizer.heuristic.operators; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.List; |
5 | |
6 | import org.opt4j.core.problem.Genotype; |
7 | import org.opt4j.operator.copy.Copy; |
8 | |
9 | import de.uka.ipd.sdq.dsexplore.launch.DSEWorkflowConfiguration; |
10 | import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEIndividualBuilder; |
11 | import de.uka.ipd.sdq.dsexplore.qml.contract.QMLContract.EvaluationAspect; |
12 | import de.uka.ipd.sdq.dsexplore.qml.contracttype.QMLContractType.Dimension; |
13 | import de.uka.ipd.sdq.dsexplore.qml.reader.QMLDimensionReader; |
14 | |
15 | public abstract class AbstractTactic implements ITactic { |
16 | /** |
17 | * Weight of this heuristics. Used for randomly select a candidate |
18 | * out of a set of candidates |
19 | */ |
20 | private double heuristicWeight; |
21 | private int numberOfGeneratedCandidates = 0; |
22 | /** |
23 | * Creates copy of genotypes |
24 | */ |
25 | protected final Copy<Genotype> copy; |
26 | |
27 | /** |
28 | * Builds individual |
29 | */ |
30 | protected final DSEIndividualBuilder individualBuilder; |
31 | |
32 | /** |
33 | * The paths of the QML dimension definition. These dimension are improved by applying this tactic |
34 | * May be empty. |
35 | */ |
36 | private String[] improvesDimensionPath; |
37 | |
38 | /** |
39 | * cache id |
40 | */ |
41 | private List<String> dimensionIdList = null; |
42 | |
43 | /** |
44 | * @param copy Creates copy of genotypes |
45 | * @param individualBuilder Builds individual |
46 | */ |
47 | public AbstractTactic(Copy<Genotype> copy, |
48 | DSEIndividualBuilder individualBuilder, DSEWorkflowConfiguration configuration, |
49 | String[] strings) { |
50 | this.copy = copy; |
51 | this.individualBuilder = individualBuilder; |
52 | this.improvesDimensionPath = strings; |
53 | } |
54 | |
55 | final protected void increaseCounterOfGeneratedCandidates() { |
56 | numberOfGeneratedCandidates++; |
57 | } |
58 | |
59 | /** |
60 | * @return Weight used for selecting candidate out of a set of candidates |
61 | */ |
62 | final public double getHeuristicWeight() { |
63 | return heuristicWeight; |
64 | } |
65 | |
66 | final protected void setHeuristicWeight(double weight) { |
67 | heuristicWeight = weight; |
68 | } |
69 | |
70 | /** |
71 | * @return Number of candidates generated by this heuristic |
72 | */ |
73 | final public int getNumberOfGeneratedCandidates() { |
74 | return numberOfGeneratedCandidates; |
75 | } |
76 | |
77 | @Override |
78 | public final boolean improves(Dimension dimension, EvaluationAspect aspect) { |
79 | |
80 | // initialise dimension id list if necessary |
81 | if(dimensionIdList == null) { |
82 | dimensionIdList = new ArrayList<String>(this.improvesDimensionPath.length); |
83 | for (String dimensionPath : this.improvesDimensionPath) { |
84 | String dimensionId = new QMLDimensionReader().getDimension(dimensionPath).getId(); |
85 | dimensionIdList.add(dimensionId); |
86 | } |
87 | |
88 | } |
89 | |
90 | for (String dimensionId : this.dimensionIdList) { |
91 | // return true if any of this tactics improved dimensions matches |
92 | if (dimension.getId().equals(dimensionId)) |
93 | return true; |
94 | } |
95 | |
96 | //if no dimension matches, return false |
97 | return false; |
98 | } |
99 | |
100 | @Override |
101 | public boolean doesNotImprove(Dimension dimension, EvaluationAspect aspect) { |
102 | return !improves(dimension, aspect); |
103 | } |
104 | |
105 | } |