| 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.tcfmoop.tests; |
| 5 | |
| 6 | import static org.junit.Assert.*; |
| 7 | |
| 8 | import java.util.HashSet; |
| 9 | import java.util.LinkedList; |
| 10 | import java.util.List; |
| 11 | |
| 12 | import org.junit.Before; |
| 13 | import org.junit.Test; |
| 14 | import org.opt4j.common.archive.DefaultArchive; |
| 15 | import org.opt4j.core.Archive; |
| 16 | import org.opt4j.core.Individual; |
| 17 | import org.opt4j.core.IndividualStateListener; |
| 18 | import org.opt4j.core.Objective; |
| 19 | import org.opt4j.core.Objectives; |
| 20 | import org.opt4j.core.Objective.Sign; |
| 21 | import org.opt4j.core.domination.ParetoDomination; |
| 22 | |
| 23 | import de.uka.ipd.sdq.tcfmoop.config.GivenParetoFrontIsReachedConfig; |
| 24 | import de.uka.ipd.sdq.tcfmoop.config.exceptions.InvalidConfigException; |
| 25 | import de.uka.ipd.sdq.tcfmoop.terminationcriteria.GivenParetoFrontIsReachedCriterion; |
| 26 | |
| 27 | /** |
| 28 | * @author Atanas Dimitrov |
| 29 | * |
| 30 | */ |
| 31 | public class GivenParetoFrontIsReachedCriterionTest { |
| 32 | GivenParetoFrontIsReachedConfig gpfirconf; |
| 33 | GivenParetoFrontIsReachedCriterion gpfircrit; |
| 34 | Archive front25 = new DefaultArchive(); |
| 35 | Archive front75 = new DefaultArchive(); |
| 36 | Archive front100 = new DefaultArchive(); |
| 37 | List<Objectives> paretoFront = new LinkedList<Objectives>(); |
| 38 | Objectives o1 = new Objectives(new ParetoDomination()); |
| 39 | Objectives o2 = new Objectives(new ParetoDomination()); |
| 40 | Objectives o3 = new Objectives(new ParetoDomination()); |
| 41 | Objectives o4 = new Objectives(new ParetoDomination()); |
| 42 | |
| 43 | /** |
| 44 | * @throws java.lang.Exception |
| 45 | */ |
| 46 | @Before |
| 47 | public void setUp() throws Exception { |
| 48 | |
| 49 | Objective X = new Objective("X", Sign.MIN); |
| 50 | Objective Y = new Objective("Y", Sign.MAX); |
| 51 | |
| 52 | gpfirconf = new GivenParetoFrontIsReachedConfig(); |
| 53 | |
| 54 | o1.add(X, 5); |
| 55 | o1.add(Y, 5); |
| 56 | |
| 57 | o2.add(X, 4); |
| 58 | o2.add(Y, 4); |
| 59 | |
| 60 | o3.add(X, 3); |
| 61 | o3.add(Y, 3); |
| 62 | |
| 63 | o4.add(X, 2); |
| 64 | o4.add(Y, 2); |
| 65 | |
| 66 | paretoFront.add(o1); |
| 67 | paretoFront.add(o2); |
| 68 | paretoFront.add(o3); |
| 69 | paretoFront.add(o4); |
| 70 | |
| 71 | gpfirconf.setParetoFront(paretoFront); |
| 72 | gpfirconf.setPercentagesToCover(0.75); |
| 73 | if(!gpfirconf.validateConfiguration()){ |
| 74 | throw new Exception(); |
| 75 | } |
| 76 | |
| 77 | MyIndividual i1 = new MyIndividual(); |
| 78 | i1.setObjectives(o1); |
| 79 | MyIndividual i2 = new MyIndividual(); |
| 80 | i2.setObjectives(o2); |
| 81 | MyIndividual i3 = new MyIndividual(); |
| 82 | i3.setObjectives(o3); |
| 83 | MyIndividual i4 = new MyIndividual(); |
| 84 | i4.setObjectives(o4); |
| 85 | |
| 86 | front25.add(i1); |
| 87 | |
| 88 | front75.add(i1); |
| 89 | front75.add(i2); |
| 90 | front75.add(i3); |
| 91 | |
| 92 | front100.add(i1); |
| 93 | front100.add(i2); |
| 94 | front100.add(i3); |
| 95 | front100.add(i4); |
| 96 | |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void test25PercentCoverage() { |
| 101 | |
| 102 | int initialParetoFrontSize = this.paretoFront.size(); |
| 103 | |
| 104 | gpfircrit = new GivenParetoFrontIsReachedCriterion(gpfirconf, null, front25); |
| 105 | gpfircrit.evaluate(0, 0); |
| 106 | assertFalse(gpfircrit.getEvaluationResult()); |
| 107 | |
| 108 | assertTrue(this.paretoFront.size() == initialParetoFrontSize); |
| 109 | |
| 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void test55PercentCoverage() { |
| 114 | |
| 115 | int initialParetoFrontSize = this.paretoFront.size(); |
| 116 | |
| 117 | try { |
| 118 | gpfirconf.setParetoFront(paretoFront); |
| 119 | } catch (InvalidConfigException e) { |
| 120 | // TODO Auto-generated catch block |
| 121 | e.printStackTrace(); |
| 122 | } |
| 123 | gpfircrit = new GivenParetoFrontIsReachedCriterion(gpfirconf, null, front75); |
| 124 | gpfircrit.evaluate(0, 0); |
| 125 | assertTrue(gpfircrit.getEvaluationResult()); |
| 126 | |
| 127 | assertTrue(this.paretoFront.size() == initialParetoFrontSize); |
| 128 | |
| 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void test100PercentCoverage() { |
| 133 | |
| 134 | int initialParetoFrontSize = this.paretoFront.size(); |
| 135 | |
| 136 | try { |
| 137 | gpfirconf.setParetoFront(paretoFront); |
| 138 | } catch (InvalidConfigException e) { |
| 139 | // TODO Auto-generated catch block |
| 140 | e.printStackTrace(); |
| 141 | } |
| 142 | gpfircrit = new GivenParetoFrontIsReachedCriterion(gpfirconf, null, front100); |
| 143 | gpfircrit.evaluate(0, 0); |
| 144 | assertTrue(gpfircrit.getEvaluationResult()); |
| 145 | |
| 146 | assertTrue(this.paretoFront.size() == initialParetoFrontSize); |
| 147 | |
| 148 | } |
| 149 | |
| 150 | |
| 151 | |
| 152 | public class MyIndividual extends Individual{ |
| 153 | public MyIndividual(){ |
| 154 | this.setIndividualStatusListeners(new HashSet<IndividualStateListener>()); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | } |