1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.dsexplore.opt4j.optimizer.heuristic.operators; |
5 | |
6 | import java.util.ArrayList; |
7 | import java.util.Collection; |
8 | import java.util.HashSet; |
9 | import java.util.List; |
10 | import java.util.Set; |
11 | import java.util.Map.Entry; |
12 | |
13 | import org.opt4j.core.Constraint; |
14 | import org.opt4j.core.InfeasibilityConstraint; |
15 | import org.opt4j.core.SatisfactionConstraint; |
16 | import org.opt4j.core.Value; |
17 | import org.opt4j.core.problem.Genotype; |
18 | import org.opt4j.operator.copy.Copy; |
19 | |
20 | import com.google.inject.Inject; |
21 | |
22 | import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEIndividual; |
23 | import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEIndividualBuilder; |
24 | import de.uka.ipd.sdq.dsexplore.qml.pcm.datastructures.EvaluationAspectWithContext; |
25 | import de.uka.ipd.sdq.dsexplore.qml.pcm.reader.PCMDeclarationsReader; |
26 | |
27 | /** |
28 | * Applies activated heuristics based on the constraint violation of an |
29 | * individual in the following order: |
30 | * 1) Try to get individual feasible if it is infeasible. |
31 | * 2) Try to reach all defined goals if not reached yet. |
32 | * 3) Apply any heuristic applicable if no previous case applies. |
33 | * |
34 | * @author noorshams |
35 | * |
36 | */ |
37 | public class QMLBoundDependentTacticOperatorsManager extends TacticOperatorsManager { |
38 | |
39 | @Inject |
40 | public QMLBoundDependentTacticOperatorsManager(Copy<Genotype> copy, |
41 | DSEIndividualBuilder individualBuilder) { |
42 | super(copy, individualBuilder); |
43 | } |
44 | |
45 | /** |
46 | * Applies heuristics based on all constraint violations of an individual: |
47 | * Try to make individual feasible. If feasible, try to reach all goals. If all goals |
48 | * reached, apply any heuristic. |
49 | */ |
50 | @Override |
51 | public List<TacticsResultCandidate> getAllCandidates( |
52 | DSEIndividual individual) { |
53 | //results cache for the current candidate. |
54 | UtilisationResultCacheAndHelper resultsCache = new UtilisationResultCacheAndHelper(); |
55 | |
56 | |
57 | List<TacticsResultCandidate> result = new ArrayList<TacticsResultCandidate>(); |
58 | Collection<TacticsResultCandidate> candidatesFromCurrentHeuristic; |
59 | Set<ITactic> applicableHeuristics = new HashSet<ITactic>(); |
60 | |
61 | //get all constraint violations and fulfillments |
62 | ConstraintViolationInformation cvi = analyzeConstraints(individual); |
63 | |
64 | if(cvi.getViolatedInfeasibilityConstraints().size() > 0) { |
65 | //individual is infeasible try to make it feasible |
66 | |
67 | for(InfeasibilityConstraint c : cvi.getViolatedInfeasibilityConstraints()) { |
68 | EvaluationAspectWithContext pivotAspectContext = |
69 | PCMDeclarationsReader.retranslateCriterionToEvaluationAspect(c); |
70 | |
71 | //pick applicable heuristic that improves aspect |
72 | for(ITactic h: heuristics) { |
73 | if(h.improves(pivotAspectContext.getDimension(), pivotAspectContext.getEvaluationAspect())){ |
74 | applicableHeuristics.add(h); |
75 | } |
76 | } |
77 | } |
78 | } else { |
79 | //individual is feasible try to reach all goals (-> fulfill all SatisfactionConstraints) |
80 | |
81 | if(cvi.getViolatedSatisfactionConstraints().size() > 0) { |
82 | //not all goals reached |
83 | |
84 | for (SatisfactionConstraint c : cvi.getViolatedSatisfactionConstraints()) { |
85 | EvaluationAspectWithContext pivotAspectContext = |
86 | PCMDeclarationsReader.retranslateCriterionToEvaluationAspect(c); |
87 | |
88 | //pick applicable heuristic that improves aspect |
89 | for(ITactic h: heuristics) { |
90 | if(h.improves(pivotAspectContext.getDimension(), pivotAspectContext.getEvaluationAspect())){ |
91 | applicableHeuristics.add(h); |
92 | } |
93 | } |
94 | } |
95 | } else { |
96 | //all goals reached, apply any possible heuristic |
97 | applicableHeuristics.addAll(heuristics); |
98 | } |
99 | } |
100 | |
101 | if (applicableHeuristics.size() == 0){ |
102 | //no tactic is applicable in this case, so again add all |
103 | applicableHeuristics.addAll(heuristics); |
104 | } |
105 | |
106 | //apply heuristic |
107 | for(ITactic heuristic : applicableHeuristics){ |
108 | candidatesFromCurrentHeuristic = heuristic.getHeuristicCandidates(individual, resultsCache); |
109 | if (candidatesFromCurrentHeuristic.size() > 0) { |
110 | this.writer.writeTacticCandidateInfo(heuristic, candidatesFromCurrentHeuristic); |
111 | } |
112 | result.addAll(candidatesFromCurrentHeuristic); |
113 | } |
114 | //XXX Try again to add all other tactics if the appropriate ones did not help? Or rather do not apply tactics in this case? |
115 | |
116 | return result; |
117 | } |
118 | |
119 | protected boolean isViolated(Entry<Constraint, Value<?>> constraintAndValue) { |
120 | boolean violated = false; |
121 | Constraint c = constraintAndValue.getKey(); |
122 | Value<?> v = constraintAndValue.getValue(); |
123 | |
124 | switch (c.getDirection()) { |
125 | case less: |
126 | if(!(v.getDouble() < c.getLimit())){ |
127 | violated = true; |
128 | } |
129 | break; |
130 | case lessOrEqual: |
131 | if(!(v.getDouble() <= c.getLimit())){ |
132 | violated = true; |
133 | } |
134 | break; |
135 | case equal: |
136 | if(!(v.getDouble() == c.getLimit())){ |
137 | violated = true; |
138 | } |
139 | break; |
140 | case greater: |
141 | if(!(v.getDouble() > c.getLimit())){ |
142 | violated = true; |
143 | } |
144 | break; |
145 | case greaterOrEqual: |
146 | if(!(v.getDouble() >= c.getLimit())){ |
147 | violated = true; |
148 | } |
149 | break; |
150 | default: |
151 | throw new RuntimeException("Unexpected case in switch statement!"); |
152 | } |
153 | return violated; |
154 | } |
155 | |
156 | protected boolean isFulfilled(Entry<Constraint, Value<?>> constraintAndValue) { |
157 | boolean fulfilled = false; |
158 | Constraint c = constraintAndValue.getKey(); |
159 | Value<?> v = constraintAndValue.getValue(); |
160 | |
161 | switch (c.getDirection()) { |
162 | case less: |
163 | if(v.getDouble() < c.getLimit()){ |
164 | fulfilled = true; |
165 | } |
166 | break; |
167 | case lessOrEqual: |
168 | if(v.getDouble() <= c.getLimit()){ |
169 | fulfilled = true; |
170 | } |
171 | break; |
172 | case equal: |
173 | if(v.getDouble() == c.getLimit()){ |
174 | fulfilled = true; |
175 | } |
176 | break; |
177 | case greater: |
178 | if(v.getDouble() > c.getLimit()){ |
179 | fulfilled = true; |
180 | } |
181 | break; |
182 | case greaterOrEqual: |
183 | if(v.getDouble() >= c.getLimit()){ |
184 | fulfilled = true; |
185 | } |
186 | break; |
187 | default: |
188 | throw new RuntimeException("Unexpected case in switch statement!"); |
189 | } |
190 | return fulfilled; |
191 | } |
192 | |
193 | protected ConstraintViolationInformation analyzeConstraints(DSEIndividual individual){ |
194 | ConstraintViolationInformation cvi = new ConstraintViolationInformation(); |
195 | for(Entry<Constraint, Value<?>> constraintAndValue : individual.getObjectives().getConstraints()) { |
196 | if (isViolated(constraintAndValue)) { |
197 | if (constraintAndValue.getKey() instanceof InfeasibilityConstraint) { |
198 | cvi.addViolatedInfeasibilityConstraint((InfeasibilityConstraint)constraintAndValue.getKey()); |
199 | } else { |
200 | //SatisfactionConstraint |
201 | cvi.addViolatedSatisfactionConstraint((SatisfactionConstraint)constraintAndValue.getKey()); |
202 | } |
203 | } else if (isFulfilled(constraintAndValue)) { // To ignore constraints where the value is NaN, check constraints explicitly |
204 | if (constraintAndValue.getKey() instanceof InfeasibilityConstraint) { |
205 | cvi.addFulfilledInfeasibilityConstraint((InfeasibilityConstraint)constraintAndValue.getKey()); |
206 | } else { |
207 | //SatisfactionConstraint |
208 | cvi.addFulfilledSatisfactionConstraint((SatisfactionConstraint)constraintAndValue.getKey()); |
209 | } |
210 | } |
211 | |
212 | } |
213 | |
214 | return cvi; |
215 | } |
216 | |
217 | protected class ConstraintViolationInformation { |
218 | protected List<InfeasibilityConstraint> violatedInfeasibilityConstraints = new ArrayList<InfeasibilityConstraint>(); |
219 | protected List<InfeasibilityConstraint> fulfilledInfeasibilityConstraints = new ArrayList<InfeasibilityConstraint>(); |
220 | protected List<SatisfactionConstraint> violatedSatisfactionConstraints = new ArrayList<SatisfactionConstraint>(); |
221 | protected List<SatisfactionConstraint> fulfilledSatisfactionConstraints = new ArrayList<SatisfactionConstraint>(); |
222 | |
223 | public List<InfeasibilityConstraint> getViolatedInfeasibilityConstraints() { |
224 | return violatedInfeasibilityConstraints; |
225 | } |
226 | |
227 | public void addViolatedInfeasibilityConstraint( |
228 | InfeasibilityConstraint violoatedInfeasibilityConstraint) { |
229 | this.violatedInfeasibilityConstraints.add(violoatedInfeasibilityConstraint); |
230 | } |
231 | |
232 | public List<InfeasibilityConstraint> getFulfilledInfeasibilityConstraints() { |
233 | return fulfilledInfeasibilityConstraints; |
234 | } |
235 | |
236 | public void addFulfilledInfeasibilityConstraint( |
237 | InfeasibilityConstraint fulfilledInfeasibilityConstraint) { |
238 | this.fulfilledInfeasibilityConstraints.add(fulfilledInfeasibilityConstraint); |
239 | } |
240 | |
241 | public List<SatisfactionConstraint> getViolatedSatisfactionConstraints() { |
242 | return violatedSatisfactionConstraints; |
243 | } |
244 | |
245 | public void addViolatedSatisfactionConstraint( |
246 | SatisfactionConstraint violoatedSatisfactionConstraint) { |
247 | this.violatedSatisfactionConstraints.add(violoatedSatisfactionConstraint); |
248 | } |
249 | |
250 | public List<SatisfactionConstraint> getFulfilledSatisfactionConstraints() { |
251 | return fulfilledSatisfactionConstraints; |
252 | } |
253 | |
254 | public void addFulfilledSatisfactionConstraint( |
255 | SatisfactionConstraint fulfilledSatisfactionConstraint) { |
256 | this.fulfilledSatisfactionConstraints.add(fulfilledSatisfactionConstraint); |
257 | } |
258 | } |
259 | |
260 | //Don't override, same picking method as superclass |
261 | // @Override |
262 | // public DSEIndividual getCandidate(DSEIndividual individual) |
263 | |
264 | |
265 | } |