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 | |
10 | import org.junit.Before; |
11 | import org.junit.Test; |
12 | import org.opt4j.common.archive.DefaultArchive; |
13 | import org.opt4j.core.Archive; |
14 | import org.opt4j.core.Individual; |
15 | import org.opt4j.core.IndividualStateListener; |
16 | import org.opt4j.core.Objective; |
17 | import org.opt4j.core.Objectives; |
18 | import org.opt4j.core.Objective.Sign; |
19 | import org.opt4j.core.domination.ParetoDomination; |
20 | |
21 | import de.uka.ipd.sdq.tcfmoop.config.ParetoOptimalSetStabilityConfig; |
22 | import de.uka.ipd.sdq.tcfmoop.config.ParetoOptimalSetStabilityConfig.EvaluationMode; |
23 | import de.uka.ipd.sdq.tcfmoop.config.exceptions.InvalidConfigException; |
24 | import de.uka.ipd.sdq.tcfmoop.terminationcriteria.ParetoOptimalSetStabilityCriterion; |
25 | |
26 | /** |
27 | * @author Atanas Dimitrov |
28 | * |
29 | */ |
30 | public class ParetoOptimalSetStabilityCriterionTest { |
31 | ParetoOptimalSetStabilityConfig possconf; |
32 | ParetoOptimalSetStabilityCriterion posscrit; |
33 | |
34 | Archive archive = new DefaultArchive(); |
35 | |
36 | Objectives o1 = new Objectives(new ParetoDomination()); |
37 | Objectives o2 = new Objectives(new ParetoDomination()); |
38 | Objectives o3 = new Objectives(new ParetoDomination()); |
39 | Objectives o4 = new Objectives(new ParetoDomination()); |
40 | Objectives o5 = new Objectives(new ParetoDomination()); |
41 | |
42 | MyIndividual i1 = new MyIndividual(); |
43 | MyIndividual i2 = new MyIndividual(); |
44 | MyIndividual i3 = new MyIndividual(); |
45 | MyIndividual i4 = new MyIndividual(); |
46 | MyIndividual i5 = new MyIndividual(); |
47 | |
48 | Objective X = new Objective("X", Sign.MIN); |
49 | Objective Y = new Objective("Y", Sign.MIN); |
50 | |
51 | /** |
52 | * @throws java.lang.Exception |
53 | */ |
54 | @Before |
55 | public void setUp() throws Exception { |
56 | |
57 | possconf = new ParetoOptimalSetStabilityConfig(); |
58 | |
59 | o1.add(X, 2); |
60 | o1.add(Y, 6); |
61 | |
62 | o2.add(X, 4); |
63 | o2.add(Y, 3); |
64 | |
65 | o3.add(X, 6); |
66 | o3.add(Y, 1); |
67 | |
68 | o4.add(X, 8); |
69 | o4.add(Y, 0.5); |
70 | |
71 | o5.add(X, 9); |
72 | o5.add(Y, 1.5); |
73 | |
74 | i1.setObjectives(o1); |
75 | i2.setObjectives(o2); |
76 | i3.setObjectives(o3); |
77 | i4.setObjectives(o4); |
78 | i5.setObjectives(o5); |
79 | |
80 | archive.add(i1); |
81 | archive.add(i2); |
82 | archive.add(i3); |
83 | archive.add(i4); |
84 | |
85 | possconf.setMinimumIterationsToSurvive(3); |
86 | possconf.setMinimumSurvivors(3); |
87 | possconf.setMinimumSurvivorsInPercentage(0.75); |
88 | possconf.setEvaluationMode(EvaluationMode.EXACT_NUMBER); |
89 | if(!possconf.validateConfiguration()){ |
90 | throw new Exception(); |
91 | } |
92 | |
93 | } |
94 | |
95 | /** |
96 | * Test method for {@link de.uka.ipd.sdq.tcfmoop.terminationcriteria.AbstractTerminationCriterion#getEvaluationResult()}. |
97 | */ |
98 | @Test |
99 | public void testExactNumbersMode() { |
100 | |
101 | //Test exact number |
102 | posscrit = new ParetoOptimalSetStabilityCriterion(possconf, null, archive); |
103 | posscrit.evaluate(0, 0); |
104 | assertFalse(posscrit.getEvaluationResult()); |
105 | posscrit.evaluate(0, 0); |
106 | assertFalse(posscrit.getEvaluationResult()); |
107 | posscrit.evaluate(0, 0); |
108 | assertTrue(posscrit.getEvaluationResult()); |
109 | |
110 | posscrit = new ParetoOptimalSetStabilityCriterion(possconf, null, archive); |
111 | posscrit.evaluate(0, 0); |
112 | assertFalse(posscrit.getEvaluationResult()); |
113 | posscrit.evaluate(0, 0); |
114 | assertFalse(posscrit.getEvaluationResult()); |
115 | archive.remove(i2); |
116 | archive.add(i5); |
117 | posscrit.evaluate(0, 0); |
118 | assertTrue(posscrit.getEvaluationResult()); |
119 | |
120 | archive.remove(i5); |
121 | archive.add(i2); |
122 | posscrit = new ParetoOptimalSetStabilityCriterion(possconf, null, archive); |
123 | posscrit.evaluate(0, 0); |
124 | assertFalse(posscrit.getEvaluationResult()); |
125 | posscrit.evaluate(0, 0); |
126 | assertFalse(posscrit.getEvaluationResult()); |
127 | archive.remove(i2); |
128 | archive.remove(i3); |
129 | posscrit.evaluate(0, 0); |
130 | assertFalse(posscrit.getEvaluationResult()); |
131 | |
132 | } |
133 | |
134 | /** |
135 | * Test method for {@link de.uka.ipd.sdq.tcfmoop.terminationcriteria.AbstractTerminationCriterion#getEvaluationResult()}. |
136 | */ |
137 | @Test |
138 | public void testPercentageMode() { |
139 | |
140 | //Test percentage |
141 | try { |
142 | possconf.setEvaluationMode(EvaluationMode.PERCENTAGE); |
143 | } catch (InvalidConfigException e) { |
144 | // TODO Auto-generated catch block |
145 | e.printStackTrace(); |
146 | } |
147 | posscrit = new ParetoOptimalSetStabilityCriterion(possconf, null, archive); |
148 | posscrit.evaluate(0, 0); |
149 | assertFalse(posscrit.getEvaluationResult()); |
150 | posscrit.evaluate(0, 0); |
151 | assertFalse(posscrit.getEvaluationResult()); |
152 | posscrit.evaluate(0, 0); |
153 | assertTrue(posscrit.getEvaluationResult()); |
154 | |
155 | posscrit = new ParetoOptimalSetStabilityCriterion(possconf, null, archive); |
156 | posscrit.evaluate(0, 0); |
157 | assertFalse(posscrit.getEvaluationResult()); |
158 | posscrit.evaluate(0, 0); |
159 | assertFalse(posscrit.getEvaluationResult()); |
160 | archive.remove(i2); |
161 | archive.add(i5); |
162 | posscrit.evaluate(0, 0); |
163 | assertTrue(posscrit.getEvaluationResult()); |
164 | |
165 | archive.remove(i5); |
166 | archive.add(i2); |
167 | posscrit = new ParetoOptimalSetStabilityCriterion(possconf, null, archive); |
168 | posscrit.evaluate(0, 0); |
169 | assertFalse(posscrit.getEvaluationResult()); |
170 | posscrit.evaluate(0, 0); |
171 | assertFalse(posscrit.getEvaluationResult()); |
172 | archive.remove(i2); |
173 | archive.remove(i3); |
174 | archive.add(i5); |
175 | posscrit.evaluate(0, 0); |
176 | assertFalse(posscrit.getEvaluationResult()); |
177 | |
178 | } |
179 | |
180 | public class MyIndividual extends Individual{ |
181 | public MyIndividual(){ |
182 | this.setIndividualStatusListeners(new HashSet<IndividualStateListener>()); |
183 | } |
184 | } |
185 | |
186 | } |