1 | package de.uka.ipd.sdq.tcfmoop.tests; |
2 | |
3 | import static org.junit.Assert.*; |
4 | |
5 | import java.util.HashSet; |
6 | import java.util.LinkedList; |
7 | import java.util.List; |
8 | |
9 | import org.junit.Before; |
10 | import org.junit.Test; |
11 | import org.opt4j.common.archive.DefaultArchive; |
12 | import org.opt4j.core.Archive; |
13 | import org.opt4j.core.Individual; |
14 | import org.opt4j.core.IndividualStateListener; |
15 | import org.opt4j.core.Objective; |
16 | import org.opt4j.core.Objectives; |
17 | import org.opt4j.core.Population; |
18 | import org.opt4j.core.Objective.Sign; |
19 | import org.opt4j.core.domination.ParetoDomination; |
20 | |
21 | import de.uka.ipd.sdq.tcfmoop.config.InsignificantSetQualityImprovementConfig; |
22 | import de.uka.ipd.sdq.tcfmoop.config.InsignificantSetQualityImprovementConfig.ValueDifference; |
23 | import de.uka.ipd.sdq.tcfmoop.terminationcriteria.InsignificantSetQualityImprovementCriterion; |
24 | |
25 | /** |
26 | * @author Atanas Dimitrov |
27 | * |
28 | */ |
29 | public class InsignificantSetQualityImprovementCriterionTest { |
30 | InsignificantSetQualityImprovementConfig isqiconf; |
31 | InsignificantSetQualityImprovementCriterion isqiccrit; |
32 | |
33 | Archive archive = new DefaultArchive(); |
34 | |
35 | Objectives o1 = new Objectives(new ParetoDomination()); |
36 | Objectives o2 = new Objectives(new ParetoDomination()); |
37 | Objectives o3 = new Objectives(new ParetoDomination()); |
38 | Objectives o4 = new Objectives(new ParetoDomination()); |
39 | Objectives o5 = new Objectives(new ParetoDomination()); //signifficant |
40 | Objectives o6 = new Objectives(new ParetoDomination()); //insignifficant |
41 | Objectives o7 = new Objectives(new ParetoDomination()); //insignifficant with new minimum |
42 | |
43 | MyIndividual i1 = new MyIndividual(); |
44 | MyIndividual i2 = new MyIndividual(); |
45 | MyIndividual i3 = new MyIndividual(); |
46 | MyIndividual i4 = new MyIndividual(); |
47 | MyIndividual i5 = new MyIndividual(); //signifficant |
48 | MyIndividual i6 = new MyIndividual(); //insignifficant |
49 | MyIndividual i7 = new MyIndividual(); //insignifficant with new minimum |
50 | |
51 | Objective X = new Objective("X", Sign.MIN); |
52 | Objective Y = new Objective("Y", Sign.MIN); |
53 | |
54 | /** |
55 | * @throws java.lang.Exception |
56 | */ |
57 | @Before |
58 | public void setUp() throws Exception { |
59 | |
60 | isqiconf = new InsignificantSetQualityImprovementConfig(); |
61 | |
62 | o1.add(X, 2); |
63 | o1.add(Y, 6); |
64 | |
65 | o2.add(X, 4); |
66 | o2.add(Y, 3); |
67 | |
68 | o3.add(X, 6); |
69 | o3.add(Y, 1); |
70 | |
71 | o4.add(X, 8); |
72 | o4.add(Y, 0.5); |
73 | |
74 | i1.setObjectives(o1); |
75 | i2.setObjectives(o2); |
76 | i3.setObjectives(o3); |
77 | i4.setObjectives(o4); |
78 | |
79 | archive.add(i1); |
80 | archive.add(i2); |
81 | archive.add(i3); |
82 | archive.add(i4); |
83 | |
84 | o5.add(X, 30); |
85 | o5.add(Y, 0.5); |
86 | i5.setObjectives(o5); |
87 | |
88 | o6.add(X, 5); |
89 | o6.add(Y, 0.5); |
90 | i6.setObjectives(o6); |
91 | |
92 | o7.add(X, 1); |
93 | o7.add(Y, 0.5); |
94 | i7.setObjectives(o7); |
95 | |
96 | isqiconf.setComparisionGenerations(2); |
97 | List<ValueDifference> vDiffs = new LinkedList<ValueDifference>(); |
98 | vDiffs.add(isqiconf.new ValueDifference(X, 0.25, 0.1)); |
99 | vDiffs.add(isqiconf.new ValueDifference(Y, 0.25, 0.1)); |
100 | isqiconf.setValueDifferences(vDiffs); |
101 | if(!isqiconf.validateConfiguration()){ |
102 | throw new Exception(); |
103 | } |
104 | |
105 | } |
106 | |
107 | /** |
108 | * Test method for {@link de.uka.ipd.sdq.tcfmoop.terminationcriteria.AbstractTerminationCriterion#getEvaluationResult()}. |
109 | */ |
110 | @Test |
111 | public void testInsignificantChange() { |
112 | Population population = new Population(); |
113 | population.add(i1); |
114 | |
115 | //Insignificant Change |
116 | isqiccrit = new InsignificantSetQualityImprovementCriterion(isqiconf, population, archive); |
117 | isqiccrit.evaluate(0, 0); |
118 | assertFalse(isqiccrit.getEvaluationResult()); |
119 | isqiccrit.evaluate(0, 0); |
120 | assertFalse(isqiccrit.getEvaluationResult()); |
121 | archive.add(i6); |
122 | isqiccrit.evaluate(0, 0); |
123 | assertTrue(isqiccrit.getEvaluationResult()); |
124 | archive.remove(i6); |
125 | |
126 | } |
127 | |
128 | /** |
129 | * Test method for {@link de.uka.ipd.sdq.tcfmoop.terminationcriteria.AbstractTerminationCriterion#getEvaluationResult()}. |
130 | */ |
131 | @Test |
132 | public void testInsignificantChangeWithSignificantMinimum() { |
133 | Population population = new Population(); |
134 | population.add(i1); |
135 | |
136 | //Insignificant Change with significant minimum |
137 | isqiccrit = new InsignificantSetQualityImprovementCriterion(isqiconf, population, archive); |
138 | isqiccrit.evaluate(0, 0); |
139 | assertFalse(isqiccrit.getEvaluationResult()); |
140 | isqiccrit.evaluate(0, 0); |
141 | assertFalse(isqiccrit.getEvaluationResult()); |
142 | archive.add(i7); |
143 | isqiccrit.evaluate(0, 0); |
144 | assertFalse(isqiccrit.getEvaluationResult()); |
145 | archive.remove(i7); |
146 | |
147 | } |
148 | |
149 | /** |
150 | * Test method for {@link de.uka.ipd.sdq.tcfmoop.terminationcriteria.AbstractTerminationCriterion#getEvaluationResult()}. |
151 | */ |
152 | @Test |
153 | public void testSignificantChange() { |
154 | Population population = new Population(); |
155 | population.add(i1); |
156 | |
157 | //Significant Change |
158 | isqiccrit = new InsignificantSetQualityImprovementCriterion(isqiconf, population, archive); |
159 | isqiccrit.evaluate(0, 0); |
160 | assertFalse(isqiccrit.getEvaluationResult()); |
161 | isqiccrit.evaluate(0, 0); |
162 | assertFalse(isqiccrit.getEvaluationResult()); |
163 | archive.add(i5); |
164 | isqiccrit.evaluate(0, 0); |
165 | assertFalse(isqiccrit.getEvaluationResult()); |
166 | |
167 | } |
168 | |
169 | public class MyIndividual extends Individual{ |
170 | public MyIndividual(){ |
171 | this.setIndividualStatusListeners(new HashSet<IndividualStateListener>()); |
172 | } |
173 | } |
174 | |
175 | } |