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.InsignificantParetoFrontChangeConfig; |
22 | import de.uka.ipd.sdq.tcfmoop.terminationcriteria.InsignificantParetoFrontChangeCriterion; |
23 | |
24 | /** |
25 | * @author Atanas Dimitrov |
26 | * |
27 | */ |
28 | public class InsignificantParetoFrontChangeCriterionTest { |
29 | InsignificantParetoFrontChangeConfig ipfcconf; |
30 | InsignificantParetoFrontChangeCriterion ipfccrit; |
31 | |
32 | Archive archive = new DefaultArchive(); //empty |
33 | |
34 | Objectives o1 = new Objectives(new ParetoDomination()); |
35 | Objectives o2 = new Objectives(new ParetoDomination()); |
36 | Objectives o3 = new Objectives(new ParetoDomination()); |
37 | Objectives o4 = new Objectives(new ParetoDomination()); |
38 | Objectives o5 = new Objectives(new ParetoDomination()); |
39 | Objectives o6 = new Objectives(new ParetoDomination()); |
40 | |
41 | MyIndividual i1 = new MyIndividual(); |
42 | MyIndividual i2 = new MyIndividual(); |
43 | MyIndividual i3 = new MyIndividual(); |
44 | MyIndividual i4 = new MyIndividual(); |
45 | MyIndividual i5 = new MyIndividual(); |
46 | MyIndividual i6 = 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 | ipfcconf = new InsignificantParetoFrontChangeConfig(); |
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 | //frontInsig |
72 | o5.add(X, 3); |
73 | o5.add(Y, 2); |
74 | |
75 | //frontSig |
76 | o6.add(X, 1); |
77 | o6.add(Y, 5); |
78 | |
79 | i1.setObjectives(o1); |
80 | i2.setObjectives(o2); |
81 | i3.setObjectives(o3); |
82 | i4.setObjectives(o4); |
83 | i5.setObjectives(o5); |
84 | i6.setObjectives(o6); |
85 | |
86 | |
87 | ipfcconf.setMinimumAllowedDifference(0.35); |
88 | ipfcconf.setPastIterationNumber(2); |
89 | if(!ipfcconf.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 testWithAnEmptyArchive() { |
100 | |
101 | //Test with an empty archive |
102 | ipfccrit = new InsignificantParetoFrontChangeCriterion(ipfcconf, null, archive); |
103 | ipfccrit.evaluate(0, 0); |
104 | assertFalse(ipfccrit.getEvaluationResult()); |
105 | ipfccrit.evaluate(0, 0); |
106 | assertFalse(ipfccrit.getEvaluationResult()); |
107 | ipfccrit.evaluate(0, 0); |
108 | assertTrue(ipfccrit.getEvaluationResult()); |
109 | |
110 | } |
111 | |
112 | @Test |
113 | public void testInsignificantChange() { |
114 | //Test insignificant front |
115 | //Init archive |
116 | |
117 | this.archive.add(i1); |
118 | this.archive.add(i2); |
119 | this.archive.add(i3); |
120 | this.archive.add(i4); |
121 | |
122 | ipfccrit = new InsignificantParetoFrontChangeCriterion(ipfcconf, null, archive); |
123 | ipfccrit.evaluate(0, 0); |
124 | assertFalse(ipfccrit.getEvaluationResult()); |
125 | ipfccrit.evaluate(0, 0); |
126 | assertFalse(ipfccrit.getEvaluationResult()); |
127 | |
128 | //Make an insignificant change in the archive |
129 | |
130 | archive.remove(i2); |
131 | archive.add(i5); |
132 | |
133 | ipfccrit.evaluate(0, 0); |
134 | assertTrue(ipfccrit.getEvaluationResult()); |
135 | |
136 | //Roll back the archive |
137 | |
138 | archive.clear(); |
139 | this.archive.add(i1); |
140 | this.archive.add(i2); |
141 | this.archive.add(i3); |
142 | this.archive.add(i4); |
143 | |
144 | } |
145 | |
146 | @Test |
147 | public void testSignificantChange() { |
148 | |
149 | //Test significant front |
150 | ipfccrit = new InsignificantParetoFrontChangeCriterion(ipfcconf, null, archive); |
151 | ipfccrit.evaluate(0, 0); |
152 | assertFalse(ipfccrit.getEvaluationResult()); |
153 | ipfccrit.evaluate(0, 0); |
154 | assertFalse(ipfccrit.getEvaluationResult()); |
155 | |
156 | //Make a significant change to the archive |
157 | archive.remove(i1); |
158 | archive.remove(i2); |
159 | archive.add(i5); |
160 | archive.add(i6); |
161 | |
162 | ipfccrit.evaluate(0, 0); |
163 | assertFalse(ipfccrit.getEvaluationResult()); |
164 | |
165 | } |
166 | |
167 | public class MyIndividual extends Individual{ |
168 | public MyIndividual(){ |
169 | this.setIndividualStatusListeners(new HashSet<IndividualStateListener>()); |
170 | } |
171 | } |
172 | |
173 | } |