1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.tcfmoop.terminationcriteria; |
5 | |
6 | import org.opt4j.core.Archive; |
7 | import org.opt4j.core.Population; |
8 | |
9 | import de.uka.ipd.sdq.tcfmoop.config.IConfiguration; |
10 | import de.uka.ipd.sdq.tcfmoop.config.TerminationCriteriaNames; |
11 | import de.uka.ipd.sdq.tcfmoop.outputtree.Node; |
12 | import de.uka.ipd.sdq.tcfmoop.outputtree.Tree; |
13 | import de.uka.ipd.sdq.tcfmoop.outputtree.Node.NodeType; |
14 | |
15 | /** |
16 | * @author Atanas Dimitrov |
17 | * |
18 | */ |
19 | public abstract class AbstractTerminationCriterion implements ITerminationCriterion { |
20 | |
21 | //Defines whether the termination criterion has suggested the termination of the optimization yet or not |
22 | protected boolean evaluationResult = false; |
23 | //The output information tree |
24 | protected Tree outputInformation; |
25 | //Opt4J specific structures required by the optimization |
26 | //Collection of individual that toghether build the most recent set of candidates |
27 | protected Population population; |
28 | //Collection of all currently pareto optimal candidates |
29 | protected Archive archive; |
30 | //name of the termination criteria |
31 | protected TerminationCriteriaNames name; |
32 | //The number of the iteration in which this criterion has proposed a termination of the optimization |
33 | private int stopSuggestedIn = -1; |
34 | //Output node for the stop suggestion information |
35 | protected Node suggestedStop; |
36 | //Defines whether the output information tree needs to be updated |
37 | protected boolean outputNeedsUpdate = false; |
38 | |
39 | /** |
40 | * Initializes the termination criteria by supplying it with the configuration object and other needed resources. |
41 | * @param conf - The configuration object. |
42 | * @param population - The current Population |
43 | * @param archive - A reference to the archive |
44 | * @throws RuntimeException if the configuration object is not fully initialized. |
45 | */ |
46 | protected AbstractTerminationCriterion(IConfiguration conf, Population population, Archive archive) { |
47 | this.archive = archive; |
48 | this.population = population; |
49 | this.name = conf.getTerminationCriterionName(); |
50 | this.outputInformation = new Tree("Abstract Termination Criterion", NodeType.TERMINATION_CRITERIA); |
51 | this.suggestedStop = this.outputInformation.addChild("Termination suggested: " + this.evaluationResult + "; During: " + this.stopSuggestedIn, NodeType.PARAMETER); |
52 | this.outputNeedsUpdate = true; |
53 | } |
54 | |
55 | /** |
56 | * {@inheritDoc} |
57 | */ |
58 | @Override |
59 | public final boolean getEvaluationResult() { |
60 | return this.evaluationResult; |
61 | } |
62 | |
63 | /** |
64 | * {@inheritDoc} |
65 | */ |
66 | @Override |
67 | public final void evaluate(int iteration, long currentTime) { |
68 | if(!this.evaluationResult){ |
69 | this.evaluateImpl(iteration, currentTime); |
70 | if(this.evaluationResult){ |
71 | this.stopSuggestedIn = iteration; |
72 | } |
73 | this.outputNeedsUpdate = true; |
74 | } |
75 | } |
76 | |
77 | /** |
78 | * The implementation of the Evaluation logic is placed here. |
79 | * @param iteration - The number of the current iteration |
80 | * @param currentTime - The current time |
81 | */ |
82 | protected abstract void evaluateImpl(int iteration, long currentTime); |
83 | |
84 | /** |
85 | * {@inheritDoc} |
86 | */ |
87 | @Override |
88 | public TerminationCriteriaNames getName() { |
89 | return this.name; |
90 | } |
91 | |
92 | /** |
93 | * {@inheritDoc} |
94 | */ |
95 | @Override |
96 | public final Tree getOutputInformation() { |
97 | if(this.outputNeedsUpdate){ |
98 | this.outputNeedsUpdate = false; |
99 | this.updateOutputInformation(); |
100 | this.suggestedStop.updateValue("Termination suggested: " + this.evaluationResult + "; During: " + this.stopSuggestedIn); |
101 | } |
102 | return this.outputInformation; |
103 | } |
104 | |
105 | /** |
106 | * {@inheritDoc} |
107 | */ |
108 | public abstract void updateOutputInformation(); |
109 | |
110 | } |