1 | package de.uka.ipd.sdq.tcfmoop.terminationcriteria; |
2 | |
3 | import org.opt4j.core.Archive; |
4 | import org.opt4j.core.Population; |
5 | |
6 | import de.uka.ipd.sdq.tcfmoop.config.IConfiguration; |
7 | import de.uka.ipd.sdq.tcfmoop.config.MaxGenerationNumberConfig; |
8 | import de.uka.ipd.sdq.tcfmoop.outputtree.Node; |
9 | import de.uka.ipd.sdq.tcfmoop.outputtree.Node.NodeType; |
10 | |
11 | public class MaxGenerationNumber extends AbstractTerminationCriterion { |
12 | |
13 | //The maximum number of iterations that the optimization algorithm should execute. |
14 | private int maximumNumberOfGenerations; |
15 | //Executed iterations so far. |
16 | private int generationsSoFar; |
17 | |
18 | //OutputNodes |
19 | //dynamic |
20 | private Node currentMaxGenNode; |
21 | |
22 | public MaxGenerationNumber(IConfiguration conf, Population population, Archive archive) { |
23 | super(conf, population, archive); |
24 | if((conf instanceof MaxGenerationNumberConfig) && conf.validateConfiguration()){ |
25 | this.maximumNumberOfGenerations = ((MaxGenerationNumberConfig)(conf)).getMaximumNumberOfIterations(); |
26 | }else{ |
27 | throw new RuntimeException("MaxGenerationNumber.initialize: " + |
28 | "wrong or invalid configuration object"); |
29 | } |
30 | initializeOutputTree(); |
31 | } |
32 | |
33 | private void initializeOutputTree(){ |
34 | this.outputInformation.updateValue("Maximum Number of Generations"); |
35 | this.outputInformation.getChildren().clear(); |
36 | this.currentMaxGenNode = this.outputInformation.addChild("Current/Maximum: " + this.generationsSoFar + "/" + this.maximumNumberOfGenerations, NodeType.PARAMETER); |
37 | this.outputInformation.getChildren().add(this.suggestedStop); |
38 | } |
39 | |
40 | /** |
41 | * {@inheritDoc} |
42 | * Implements the Maximum Number of Generation Criterion. If the number of the evaluated iterations |
43 | * reaches the supplied limit, the criterion suggest that the optimization should be stopped. |
44 | */ |
45 | @Override |
46 | public void evaluateImpl(int iteration, long currentTime) { |
47 | generationsSoFar = iteration; |
48 | if(generationsSoFar >= maximumNumberOfGenerations){ |
49 | this.evaluationResult = true; |
50 | }else{ |
51 | this.evaluationResult = false; |
52 | } |
53 | } |
54 | |
55 | /** |
56 | * {@inheritDoc} |
57 | */ |
58 | @Override |
59 | public void updateOutputInformation(){ |
60 | this.currentMaxGenNode.updateValue("Current/Maximum Generation Number: " + this.generationsSoFar + "/" + this.maximumNumberOfGenerations); |
61 | } |
62 | } |