1 | package de.uka.ipd.sdq.tcfmoop.terminationcriteria; |
2 | |
3 | import java.util.HashMap; |
4 | import java.util.LinkedList; |
5 | import java.util.List; |
6 | import java.util.Map; |
7 | |
8 | import org.opt4j.core.Archive; |
9 | import org.opt4j.core.Individual; |
10 | import org.opt4j.core.Population; |
11 | |
12 | import de.uka.ipd.sdq.tcfmoop.config.IConfiguration; |
13 | import de.uka.ipd.sdq.tcfmoop.config.ParetoOptimalSetStabilityConfig; |
14 | import de.uka.ipd.sdq.tcfmoop.config.ParetoOptimalSetStabilityConfig.EvaluationMode; |
15 | import de.uka.ipd.sdq.tcfmoop.outputtree.Node; |
16 | import de.uka.ipd.sdq.tcfmoop.outputtree.Node.NodeType; |
17 | |
18 | /** |
19 | * @author Atanas Dimitrov |
20 | */ |
21 | public class ParetoOptimalSetStabilityCriterion extends |
22 | AbstractTerminationCriterion { |
23 | |
24 | /* |
25 | * The evaluation modes that this criterion supports. If mode is set to EXACT_NUMBER, then the criterion searches every time |
26 | * for exactly x candidates who have survived for n iterations. If PERCENTAGE is set as evaluation mode, then the number of |
27 | * the survived candidates is relative to the current amount of candidates in the archive. |
28 | */ |
29 | private EvaluationMode mode; |
30 | //The minimum number of individual who must survive |
31 | private int minimumSurvivors; |
32 | //The minimum number of individual in percentage who must survive |
33 | private Double minimumSurvivorsInPercentage; |
34 | //The minimum amount of iterations these individuals have to survive |
35 | private int minimumIterationsToSurvive; |
36 | //Stores information about the individuals, including survived generations |
37 | private Map<Individual, IndividualStatistics> survivalsCounter; |
38 | //Denotes how many individuals have remained pareto optimal the longest or how many have reached the required minimum number of iterations to survive. |
39 | private int numberOfIndividualsThatHasRemainedOptimalTheLongest; |
40 | //Shows the currently reached maximum number of survived iterations. |
41 | private int currentlyReachedMaxNumberOfIterations; |
42 | |
43 | //OutputNodes |
44 | //static |
45 | @SuppressWarnings("unused") |
46 | private Node evaluationModeNode; |
47 | //dynamic |
48 | private Node survivedGenerationsNode; |
49 | private Node survivorsNode; |
50 | |
51 | public ParetoOptimalSetStabilityCriterion(IConfiguration conf, Population population, |
52 | Archive archive) { |
53 | super(conf, population, archive); |
54 | if((conf instanceof ParetoOptimalSetStabilityConfig) && conf.validateConfiguration()){ |
55 | this.minimumIterationsToSurvive = ((ParetoOptimalSetStabilityConfig)(conf)).getMinimumIterationsToSurvive(); |
56 | this.mode = ((ParetoOptimalSetStabilityConfig)(conf)).getEvaluationMode(); |
57 | if(this.mode == EvaluationMode.EXACT_NUMBER){ |
58 | this.minimumSurvivors = ((ParetoOptimalSetStabilityConfig)(conf)).getMinimumSurvivors(); |
59 | }else{ |
60 | this.minimumSurvivorsInPercentage = ((ParetoOptimalSetStabilityConfig)(conf)).getMinimumSurvivorsInPercentage(); |
61 | } |
62 | }else{ |
63 | throw new RuntimeException("ParetoOptimalSetStabilityCriterion.initialize: " + |
64 | "wrong or invalid configuration object"); |
65 | } |
66 | survivalsCounter = new HashMap<Individual, IndividualStatistics>(); |
67 | this.initializeOutputTree(archive); |
68 | } |
69 | |
70 | private void initializeOutputTree(Archive archive){ |
71 | this.outputInformation.updateValue("Pareto Optimal Set Stability"); |
72 | this.outputInformation.getChildren().clear(); |
73 | |
74 | this.evaluationModeNode = this.outputInformation.addChild("Evaluation Mode: " + this.mode.name(), NodeType.PARAMETER); |
75 | this.survivedGenerationsNode = this.outputInformation.addChild("Survived Generations: " + this.currentlyReachedMaxNumberOfIterations + "/" + this.minimumIterationsToSurvive, NodeType.PARAMETER); |
76 | |
77 | if(this.mode == EvaluationMode.EXACT_NUMBER){ |
78 | this.survivorsNode = this.outputInformation.addChild("Survivors: " + this.numberOfIndividualsThatHasRemainedOptimalTheLongest + "/" + this.minimumSurvivors, NodeType.PARAMETER); |
79 | }else{ |
80 | this.survivorsNode = this.outputInformation.addChild("Survivors: " + (double)this.numberOfIndividualsThatHasRemainedOptimalTheLongest/archive.size()*100 + "%/" + this.minimumSurvivorsInPercentage*100 + "%", NodeType.PARAMETER); |
81 | } |
82 | this.outputInformation.getChildren().add(this.suggestedStop); |
83 | } |
84 | |
85 | /** |
86 | * {@inheritDoc} |
87 | * Implements the Pareto Optimal Set Stability Criterion: This criterion evaluates how many candidates |
88 | * have remained pareto optimal and for how many iterations. If the numbers reach the supplied requirements |
89 | * this criterion decides that the spareto optimal set is stable enough and probably would not change in the near future. |
90 | * And therefore suggests the termination of the optimization. |
91 | */ |
92 | @Override |
93 | public void evaluateImpl(int iteration, long currentTime) { |
94 | for(Individual indi : archive){ |
95 | if(this.survivalsCounter.containsKey(indi)){ |
96 | this.survivalsCounter.get(indi).renewSurvivalStatus(iteration); |
97 | }else{ |
98 | this.survivalsCounter.put(indi, new IndividualStatistics(iteration)); |
99 | } |
100 | } |
101 | |
102 | //Select the objects to delete |
103 | |
104 | List<Individual> individualsToRemove = new LinkedList<Individual>(); |
105 | |
106 | for(Map.Entry<Individual, IndividualStatistics> mapEntry : this.survivalsCounter.entrySet()){ |
107 | if(!mapEntry.getValue().hasSurvived(iteration)){ |
108 | individualsToRemove.add(mapEntry.getKey()); |
109 | } |
110 | } |
111 | |
112 | //Delete these objects |
113 | |
114 | for(Individual i : individualsToRemove){ |
115 | this.survivalsCounter.remove(i); |
116 | } |
117 | |
118 | //Count the survivors |
119 | |
120 | this.currentlyReachedMaxNumberOfIterations = 0; |
121 | this.numberOfIndividualsThatHasRemainedOptimalTheLongest = 0; |
122 | |
123 | for(IndividualStatistics iStat : this.survivalsCounter.values()){ |
124 | //Has the currently highest generation number reached the required minimum |
125 | if(this.currentlyReachedMaxNumberOfIterations == this.minimumIterationsToSurvive){ |
126 | if(iStat.getNumberOfSurvivedGenerations() >= this.minimumIterationsToSurvive){ |
127 | this.numberOfIndividualsThatHasRemainedOptimalTheLongest++; |
128 | } |
129 | }else{ |
130 | //Currently reached Generation number is lower than the required minimum |
131 | if(iStat.getNumberOfSurvivedGenerations() > this.currentlyReachedMaxNumberOfIterations){ |
132 | this.numberOfIndividualsThatHasRemainedOptimalTheLongest = 1; |
133 | this.currentlyReachedMaxNumberOfIterations = iStat.getNumberOfSurvivedGenerations(); |
134 | if(this.currentlyReachedMaxNumberOfIterations > this.minimumIterationsToSurvive){ |
135 | this.currentlyReachedMaxNumberOfIterations = this.minimumIterationsToSurvive; |
136 | } |
137 | }else if(iStat.getNumberOfSurvivedGenerations() == this.currentlyReachedMaxNumberOfIterations){ |
138 | this.numberOfIndividualsThatHasRemainedOptimalTheLongest++; |
139 | } |
140 | } |
141 | |
142 | } |
143 | |
144 | //Make the decision based on the mode |
145 | if(this.currentlyReachedMaxNumberOfIterations == this.minimumIterationsToSurvive && |
146 | ((this.mode == EvaluationMode.EXACT_NUMBER && this.numberOfIndividualsThatHasRemainedOptimalTheLongest >= this.minimumSurvivors) || |
147 | (this.mode == EvaluationMode.PERCENTAGE && (double)this.numberOfIndividualsThatHasRemainedOptimalTheLongest/this.archive.size() >= this.minimumSurvivorsInPercentage))){ |
148 | this.evaluationResult = true; |
149 | }else{ |
150 | this.evaluationResult = false; |
151 | } |
152 | } |
153 | |
154 | /** |
155 | * {@inheritDoc} |
156 | */ |
157 | @Override |
158 | public void updateOutputInformation() { |
159 | this.survivedGenerationsNode.updateValue("Survived Generations: " + this.currentlyReachedMaxNumberOfIterations + "/" + this.minimumIterationsToSurvive); |
160 | if(this.mode == EvaluationMode.EXACT_NUMBER){ |
161 | this.survivorsNode.updateValue("Survivors: " + this.numberOfIndividualsThatHasRemainedOptimalTheLongest + "/" + this.minimumSurvivors); |
162 | }else{ |
163 | this.survivorsNode.updateValue("Survivors: " + (double)this.numberOfIndividualsThatHasRemainedOptimalTheLongest/this.archive.size()*100 + "%/" + this.minimumSurvivorsInPercentage*100 + "%"); |
164 | } |
165 | } |
166 | |
167 | /** |
168 | * A helper class for storing individuals survival information. |
169 | * @author Atanas Dimitrov |
170 | */ |
171 | private class IndividualStatistics{ |
172 | private int survivedGenerations; |
173 | private int lastUpdated; |
174 | |
175 | public IndividualStatistics(int generationNumber){ |
176 | this.survivedGenerations = 1; |
177 | this.lastUpdated = generationNumber; |
178 | } |
179 | |
180 | /** |
181 | * Automatically updates the complete statistic for the individual in question. |
182 | * @param generationNumber |
183 | */ |
184 | public void renewSurvivalStatus(int generationNumber){ |
185 | this.survivedGenerations++; |
186 | this.lastUpdated = generationNumber; |
187 | } |
188 | |
189 | /** |
190 | * Answer whether the individual has survived the supplied iteration. |
191 | * @param iterationNumber - number if the iteration |
192 | * @return true if the individual has survived the iteration |
193 | */ |
194 | public boolean hasSurvived(int iterationNumber){ |
195 | return (this.lastUpdated >= iterationNumber); |
196 | } |
197 | |
198 | /** |
199 | * Return the number of the survived iterations for the individual |
200 | * @return the number of the survived iterations for the individual |
201 | */ |
202 | public int getNumberOfSurvivedGenerations(){ |
203 | return this.survivedGenerations; |
204 | } |
205 | } |
206 | |
207 | } |