1 | package de.uka.ipd.sdq.tcfmoop.tests; |
2 | |
3 | import static org.junit.Assert.*; |
4 | |
5 | import java.util.HashMap; |
6 | import java.util.HashSet; |
7 | import java.util.Map; |
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.DoubleValue; |
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.Population; |
19 | import org.opt4j.core.Value; |
20 | import org.opt4j.core.Objective.Sign; |
21 | import org.opt4j.core.domination.ParetoDomination; |
22 | |
23 | import de.uka.ipd.sdq.tcfmoop.config.MinimalQualityCriteriaValueConfig; |
24 | import de.uka.ipd.sdq.tcfmoop.terminationcriteria.MinimalQualityCriteriaValueCriterion; |
25 | |
26 | /** |
27 | * @author Atanas Dimitrov |
28 | * |
29 | */ |
30 | public class MinimalQualityCriteriaValueCriterionTest { |
31 | MinimalQualityCriteriaValueConfig mqcvconf; |
32 | MinimalQualityCriteriaValueCriterion mqcvcrit; |
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(); //signifficant |
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 | mqcvconf = new MinimalQualityCriteriaValueConfig(); |
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, 3); |
66 | o3.add(Y, 1.5); |
67 | |
68 | o4.add(X, 4); |
69 | o4.add(Y, 0.5); |
70 | |
71 | i1.setObjectives(o1); |
72 | i2.setObjectives(o2); |
73 | i3.setObjectives(o3); |
74 | i4.setObjectives(o4); |
75 | |
76 | archive.add(i1); |
77 | archive.add(i2); |
78 | archive.add(i3); |
79 | archive.add(i4); |
80 | |
81 | o5.add(X, 2); |
82 | o5.add(Y, 0.6); |
83 | i5.setObjectives(o5); |
84 | |
85 | mqcvconf.setNumberOfCandidatesToConform(3); |
86 | |
87 | Map<Objective, Value<?>> configuredObjects = new HashMap<Objective, Value<?>>(); |
88 | |
89 | configuredObjects.put(X, new DoubleValue(5d)); |
90 | configuredObjects.put(Y, new DoubleValue(2.5)); |
91 | |
92 | mqcvconf.setObjectiveMinimalValues(configuredObjects); |
93 | |
94 | if(!mqcvconf.validateConfiguration()){ |
95 | throw new Exception(); |
96 | } |
97 | |
98 | } |
99 | |
100 | /** |
101 | * Test method for {@link de.uka.ipd.sdq.tcfmoop.terminationcriteria.AbstractTerminationCriterion#getEvaluationResult()}. |
102 | */ |
103 | @Test |
104 | public void testNotEnoughConformingCandidates() { |
105 | Population population = new Population(); |
106 | population.add(i1); |
107 | |
108 | //Not Enough candidates |
109 | mqcvcrit = new MinimalQualityCriteriaValueCriterion(mqcvconf, population, archive); |
110 | mqcvcrit.evaluate(0, 0); |
111 | assertFalse(mqcvcrit.getEvaluationResult()); |
112 | |
113 | } |
114 | |
115 | /** |
116 | * Test method for {@link de.uka.ipd.sdq.tcfmoop.terminationcriteria.AbstractTerminationCriterion#getEvaluationResult()}. |
117 | */ |
118 | @Test |
119 | public void testEnoughConformingCandidates() { |
120 | Population population = new Population(); |
121 | population.add(i1); |
122 | |
123 | mqcvcrit = new MinimalQualityCriteriaValueCriterion(mqcvconf, population, archive); |
124 | |
125 | archive.remove(i2); |
126 | archive.add(i5); |
127 | |
128 | //Enough conforming candidates |
129 | mqcvcrit.evaluate(0, 0); |
130 | assertTrue(mqcvcrit.getEvaluationResult()); |
131 | |
132 | } |
133 | |
134 | public class MyIndividual extends Individual{ |
135 | public MyIndividual(){ |
136 | this.setIndividualStatusListeners(new HashSet<IndividualStateListener>()); |
137 | } |
138 | } |
139 | |
140 | } |