| 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.tcfmoop.terminationcriteria; |
| 5 | |
| 6 | import java.util.Collection; |
| 7 | import java.util.LinkedList; |
| 8 | |
| 9 | import org.opt4j.core.Archive; |
| 10 | import org.opt4j.core.Individual; |
| 11 | import org.opt4j.core.Objectives; |
| 12 | import org.opt4j.core.Population; |
| 13 | |
| 14 | import de.uka.ipd.sdq.tcfmoop.config.IConfiguration; |
| 15 | import de.uka.ipd.sdq.tcfmoop.config.InsignificantParetoFrontChangeConfig; |
| 16 | import de.uka.ipd.sdq.tcfmoop.outputtree.Node; |
| 17 | import de.uka.ipd.sdq.tcfmoop.outputtree.Node.NodeType; |
| 18 | |
| 19 | /** |
| 20 | * @author Atanas Dimitrov |
| 21 | * |
| 22 | */ |
| 23 | public class InsignificantParetoFrontChangeCriterion extends |
| 24 | AbstractTerminationCriterion { |
| 25 | |
| 26 | //The minimum significant difference. Everything below this is considered insignificant |
| 27 | private double requiredPercentageDifference; |
| 28 | //The current difference between the two fronts |
| 29 | private double currentPercentageDifference; |
| 30 | //The x in the n-x. iteration. Gives information about the generation of the second front |
| 31 | private int pastInterationNumber; |
| 32 | //An archive of the last x pareto fronts |
| 33 | private LinkedList<LinkedList<Objectives>> paretoFrontMemory = new LinkedList<LinkedList<Objectives>>(); |
| 34 | |
| 35 | //OutputNodes |
| 36 | //static |
| 37 | @SuppressWarnings("unused") |
| 38 | private Node generationToCompareWithNode; |
| 39 | //dynamic |
| 40 | private Node currentDifferenceNode; |
| 41 | |
| 42 | public InsignificantParetoFrontChangeCriterion(IConfiguration conf, Population population, |
| 43 | Archive archive) { |
| 44 | super(conf, population, archive); |
| 45 | if((conf instanceof InsignificantParetoFrontChangeConfig) && conf.validateConfiguration()){ |
| 46 | this.requiredPercentageDifference = ((InsignificantParetoFrontChangeConfig)(conf)).getMinimumAllowedDifference(); |
| 47 | this.pastInterationNumber = ((InsignificantParetoFrontChangeConfig)(conf)).getPastIterationNumber(); |
| 48 | |
| 49 | }else{ |
| 50 | throw new RuntimeException("InsignificantParetoFrontChangeCriterion.initialize: " + |
| 51 | "wrong or invalid configuration object"); |
| 52 | } |
| 53 | initializeOutputTree(); |
| 54 | } |
| 55 | |
| 56 | private void initializeOutputTree(){ |
| 57 | this.outputInformation.updateValue("Insignificant Pareto Front Change"); |
| 58 | this.outputInformation.getChildren().clear(); |
| 59 | |
| 60 | this.generationToCompareWithNode = this.outputInformation.addChild("Current Generation is compared with: " + this.pastInterationNumber + " generation ago", NodeType.PARAMETER); |
| 61 | this.currentDifferenceNode = this.outputInformation.addChild("Current Difference: " + this.currentPercentageDifference + "/" + this.requiredPercentageDifference, NodeType.PARAMETER); |
| 62 | this.outputInformation.getChildren().add(this.suggestedStop); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * {@inheritDoc} |
| 67 | * Implements the Insignificant Pareto Front Change Criterion: This criterion compares the pareto front from the |
| 68 | * n. iteration with the pareto front from the n-x. iteration and calculates the difference between the two. |
| 69 | * If this difference is below the supplied minimum, it is reported that the optimization should stop. |
| 70 | */ |
| 71 | @Override |
| 72 | public void evaluateImpl(int iteration, long currentTime) { |
| 73 | |
| 74 | this.memorizeCurrentParetoFront(); |
| 75 | this.clearOutDatedParetoFronts(); |
| 76 | |
| 77 | if(this.paretoFrontMemory.size() <= this.pastInterationNumber){ |
| 78 | this.evaluationResult = false; |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | this.currentPercentageDifference = this.calcuteDifference(this.paretoFrontMemory.getFirst(), this.paretoFrontMemory.getLast()); |
| 83 | |
| 84 | if(this.currentPercentageDifference < this.requiredPercentageDifference){ |
| 85 | this.evaluationResult = true; |
| 86 | }else{ |
| 87 | this.evaluationResult = false; |
| 88 | } |
| 89 | |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Copies the current pareto front to a local archive. |
| 94 | */ |
| 95 | private void memorizeCurrentParetoFront(){ |
| 96 | LinkedList<Objectives> newFront = new LinkedList<Objectives>(); |
| 97 | for(Individual indi : this.archive){ |
| 98 | newFront.add(indi.getObjectives()); |
| 99 | } |
| 100 | this.paretoFrontMemory.addFirst(newFront); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Clear every pareto front that lies beyond the n-x. one. |
| 105 | */ |
| 106 | private void clearOutDatedParetoFronts(){ |
| 107 | while(this.paretoFrontMemory.size() > this.pastInterationNumber + 1){ |
| 108 | this.paretoFrontMemory.removeLast(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Calculate the difference between the two pareto fronts by measuring the the coverage between them |
| 114 | * @param newFront pareto front of a latest iteration |
| 115 | * @param oldFront pareto front of an earlier iteration |
| 116 | * @return the calculated pareto front difference. A percentage value in the interval [0, 1]. |
| 117 | */ |
| 118 | private double calcuteDifference(Collection<Objectives> newFront, Collection<Objectives> oldFront){ |
| 119 | int numberOfDominatedIndividualsInCoveredFront = 0; |
| 120 | int numberOfIndividualsInCoveredFront = oldFront.size(); |
| 121 | |
| 122 | for(Objectives indiToBeDominated : oldFront){ |
| 123 | for(Objectives coveringIndi : newFront){ |
| 124 | if(coveringIndi.dominates(indiToBeDominated)){ |
| 125 | numberOfDominatedIndividualsInCoveredFront++; |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if(numberOfIndividualsInCoveredFront == 0){ |
| 132 | if(newFront.size() == 0){ |
| 133 | return 0; |
| 134 | }else{ |
| 135 | return 1; |
| 136 | } |
| 137 | }else{ |
| 138 | return (double)numberOfDominatedIndividualsInCoveredFront/numberOfIndividualsInCoveredFront; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * {@inheritDoc} |
| 144 | */ |
| 145 | @Override |
| 146 | public void updateOutputInformation() { |
| 147 | this.currentDifferenceNode.updateValue("Current Difference: " + this.currentPercentageDifference + "/" + this.requiredPercentageDifference); |
| 148 | } |
| 149 | |
| 150 | } |