1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.tcfmoop.terminationcriteria; |
5 | |
6 | import java.util.Collection; |
7 | import java.util.LinkedList; |
8 | import java.util.List; |
9 | |
10 | import org.opt4j.core.Archive; |
11 | import org.opt4j.core.Individual; |
12 | import org.opt4j.core.Objectives; |
13 | import org.opt4j.core.Population; |
14 | |
15 | import de.uka.ipd.sdq.tcfmoop.config.GivenParetoFrontIsReachedConfig; |
16 | import de.uka.ipd.sdq.tcfmoop.config.IConfiguration; |
17 | import de.uka.ipd.sdq.tcfmoop.outputtree.Node; |
18 | import de.uka.ipd.sdq.tcfmoop.outputtree.Node.NodeType; |
19 | |
20 | /** |
21 | * @author Atanas Dimitrov |
22 | * |
23 | */ |
24 | public class GivenParetoFrontIsReachedCriterion extends |
25 | AbstractTerminationCriterion { |
26 | |
27 | //The pareto front that the optimization targets |
28 | private List<Objectives> paretoFront; |
29 | //Initial size of the targeted pareto front. Needed for optimization purposes |
30 | private int initialParetoFrontSize; |
31 | //The percentage of the targeted front that needs to be dominated by the pareto front of the optimization |
32 | private double requiredPercentagesCovered; |
33 | //The part of the targeted pareto front (in percentages) that is currently covered by the front of the optimization. |
34 | private double currentPercentagesCovered; |
35 | //OutputNodes |
36 | //dynamic |
37 | private Node frontCoverageNode; |
38 | |
39 | public GivenParetoFrontIsReachedCriterion(IConfiguration conf, Population population, |
40 | Archive archive) { |
41 | super(conf, population, archive); |
42 | if((conf instanceof GivenParetoFrontIsReachedConfig) && conf.validateConfiguration()){ |
43 | this.paretoFront = ((GivenParetoFrontIsReachedConfig)(conf)).getParetoFront(); |
44 | this.requiredPercentagesCovered = ((GivenParetoFrontIsReachedConfig)(conf)).getPercentagesToCover(); |
45 | this.initialParetoFrontSize = this.paretoFront.size(); |
46 | }else{ |
47 | throw new RuntimeException("GivenParetoFrontIsReachedCriterion.initialize: " + |
48 | "wrong or invalid configuration object"); |
49 | } |
50 | initializeOutputTree(); |
51 | } |
52 | |
53 | private void initializeOutputTree(){ |
54 | this.outputInformation.updateValue("Given Pareto Front Is Reached"); |
55 | this.outputInformation.getChildren().clear(); |
56 | |
57 | this.frontCoverageNode = this.outputInformation.addChild("Front Coverage: " + this.currentPercentagesCovered + "/" + this.requiredPercentagesCovered, NodeType.PARAMETER); |
58 | this.outputInformation.getChildren().add(this.suggestedStop); |
59 | } |
60 | |
61 | /** |
62 | * {@inheritDoc} |
63 | * Implements the Given Pareto Front Is Reached Criterion: This stopping criterion receives a targeted pareto front |
64 | * that the optimization is trying to reach/dominate/cover. Once x percentages of the targeted front are reached/covered/dominated |
65 | * the criterion will report that the optimization should be stopped. |
66 | */ |
67 | @Override |
68 | public void evaluateImpl(int iteration, long currentTime) { |
69 | this.currentPercentagesCovered = this.calcuteCoverage(this.archive, this.paretoFront); |
70 | if(this.currentPercentagesCovered >= this.requiredPercentagesCovered){ |
71 | this.evaluationResult = true; |
72 | }else{ |
73 | this.evaluationResult = false; |
74 | } |
75 | } |
76 | |
77 | /** |
78 | * Calculates the coverage of the toBeCoveredFront. Returns the covered part of the toBeCoveredFront as |
79 | * a percentage value in the interval [0,1]. |
80 | * @param coveringFront the pareto front that covers/dominates/reaches. the toBeCoveredFront |
81 | * @param toBeCoveredFront the pareto front that must be covered/dominated/reached. |
82 | * @return The covered part of the toBeCoveredFront by the coveringFront in percentages [0,1] |
83 | */ |
84 | private double calcuteCoverage(Collection<Individual> coveringFront, Collection<Objectives> toBeCoveredFront){ |
85 | List<Objectives> indisToRemove = new LinkedList<Objectives>(); |
86 | |
87 | for(Objectives indiToBeDominated : toBeCoveredFront){ |
88 | for(Individual coveringIndi : coveringFront){ |
89 | if(coveringIndi.getObjectives().weaklyDominates(indiToBeDominated)){ |
90 | indisToRemove.add(indiToBeDominated); |
91 | break; |
92 | } |
93 | } |
94 | } |
95 | |
96 | toBeCoveredFront.removeAll(indisToRemove); |
97 | |
98 | return 1-((double)(toBeCoveredFront.size())/(double)(this.initialParetoFrontSize)); |
99 | } |
100 | |
101 | /** |
102 | * {@inheritDoc} |
103 | */ |
104 | @Override |
105 | public void updateOutputInformation() { |
106 | this.frontCoverageNode.updateValue("Front Coverage: " + this.currentPercentagesCovered + "/" + this.requiredPercentagesCovered); |
107 | } |
108 | |
109 | } |