| 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.tcfmoop.terminationcriteria; |
| 5 | |
| 6 | import java.util.HashMap; |
| 7 | import java.util.Map; |
| 8 | |
| 9 | import org.opt4j.core.Archive; |
| 10 | import org.opt4j.core.Individual; |
| 11 | import org.opt4j.core.Objective; |
| 12 | import org.opt4j.core.Objectives; |
| 13 | import org.opt4j.core.Population; |
| 14 | import org.opt4j.core.Value; |
| 15 | import org.opt4j.core.Objective.Sign; |
| 16 | |
| 17 | import de.uka.ipd.sdq.tcfmoop.config.IConfiguration; |
| 18 | import de.uka.ipd.sdq.tcfmoop.config.MinimalQualityCriteriaValueConfig; |
| 19 | import de.uka.ipd.sdq.tcfmoop.outputtree.Node; |
| 20 | import de.uka.ipd.sdq.tcfmoop.outputtree.Node.NodeType; |
| 21 | |
| 22 | /** |
| 23 | * @author Atanas Dimitrov |
| 24 | * |
| 25 | */ |
| 26 | public class MinimalQualityCriteriaValueCriterion extends |
| 27 | AbstractTerminationCriterion { |
| 28 | |
| 29 | //Already configured Objectives with their minimum required values. |
| 30 | private Map<Objective, Value<?>> configuredObjectives = new HashMap<Objective, Value<?>>(); |
| 31 | //The required number of candidates to have quality criteria higher then the one supplied. |
| 32 | private int numberOfCandidatesToConform; |
| 33 | //The current number of candidates to have quality criteria higher then the one supplied. |
| 34 | private int currentlyConformingCandidates; |
| 35 | |
| 36 | //OutputNodes |
| 37 | //dynamic |
| 38 | private Node conformingCandidatesNode; |
| 39 | |
| 40 | public MinimalQualityCriteriaValueCriterion(IConfiguration conf, Population population, |
| 41 | Archive archive) { |
| 42 | super(conf, population, archive); |
| 43 | if((conf instanceof MinimalQualityCriteriaValueConfig) && conf.validateConfiguration()){ |
| 44 | this.numberOfCandidatesToConform = ((MinimalQualityCriteriaValueConfig)(conf)).getNumberOfCandidatesToConform(); |
| 45 | this.configuredObjectives = ((MinimalQualityCriteriaValueConfig)(conf)).getObjectiveMinimalValues(); |
| 46 | |
| 47 | }else{ |
| 48 | throw new RuntimeException("MinimalQualityCriteriaValueCriterion.initialize: " + |
| 49 | "wrong or invalid configuration object"); |
| 50 | } |
| 51 | |
| 52 | this.initializeOutputTree(); |
| 53 | } |
| 54 | |
| 55 | private void initializeOutputTree(){ |
| 56 | this.outputInformation.updateValue("Minimal Quality Criteria Value"); |
| 57 | this.outputInformation.getChildren().clear(); |
| 58 | |
| 59 | this.conformingCandidatesNode = this.outputInformation.addChild("Conforming Candidates: " + this.currentlyConformingCandidates + "/" + this.numberOfCandidatesToConform, NodeType.PARAMETER); |
| 60 | this.outputInformation.getChildren().add(this.suggestedStop); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * {@inheritDoc} |
| 65 | * Implements the Minimal Quality Criteria Value Criterion: This criterion ensures that a subset of x pareto optimal |
| 66 | * candidates have quality criteria values higher than the previously supplied ones, before suggesting termination. |
| 67 | */ |
| 68 | @Override |
| 69 | public void evaluateImpl(int iteration, long currentTime) { |
| 70 | |
| 71 | this.currentlyConformingCandidates = 0; |
| 72 | |
| 73 | for(Individual indi : this.archive){ |
| 74 | if(this.doesConformToMinimalValues(indi)){ |
| 75 | currentlyConformingCandidates++; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if(this.currentlyConformingCandidates >= this.numberOfCandidatesToConform){ |
| 80 | this.evaluationResult = true; |
| 81 | }else{ |
| 82 | this.evaluationResult = false; |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Checks whether a specific individual conforms to the minimum quality criteria values set before. |
| 89 | * @param indi the Individual to be checked. |
| 90 | * @return true - if the candidate conforms, else - false. |
| 91 | */ |
| 92 | private boolean doesConformToMinimalValues(Individual indi){ |
| 93 | Objectives currentCandidatesObjectives = indi.getObjectives(); |
| 94 | |
| 95 | if(!currentCandidatesObjectives.getKeys().containsAll(this.configuredObjectives.keySet())){ |
| 96 | throw new RuntimeException("MinimalQualityCriteriaValueCriterion.doesConformToMinimalValues: Individual does not contain all configured Objectives"); |
| 97 | } |
| 98 | |
| 99 | for(Map.Entry<Objective, Value<?>> keyValuePair : this.configuredObjectives.entrySet()){ |
| 100 | if(keyValuePair.getKey().getSign() == Sign.MAX){ |
| 101 | if(!(currentCandidatesObjectives.get(keyValuePair.getKey()).getDouble() >= keyValuePair.getValue().getDouble())){ |
| 102 | return false; |
| 103 | } |
| 104 | }else if(keyValuePair.getKey().getSign() == Sign.MIN){ |
| 105 | if(!(currentCandidatesObjectives.get(keyValuePair.getKey()).getDouble() <= keyValuePair.getValue().getDouble())){ |
| 106 | return false; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * {@inheritDoc} |
| 115 | */ |
| 116 | @Override |
| 117 | public void updateOutputInformation() { |
| 118 | this.conformingCandidatesNode.updateValue("Conforming Candidates: " + this.currentlyConformingCandidates + "/" + this.numberOfCandidatesToConform); |
| 119 | } |
| 120 | |
| 121 | } |