diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/BestFirstStrategyForModelGeneration.java')
-rw-r--r-- | Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/BestFirstStrategyForModelGeneration.java | 79 |
1 files changed, 31 insertions, 48 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/BestFirstStrategyForModelGeneration.java b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/BestFirstStrategyForModelGeneration.java index 60f46033..081e48fa 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/BestFirstStrategyForModelGeneration.java +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/BestFirstStrategyForModelGeneration.java | |||
@@ -21,6 +21,7 @@ import java.util.PriorityQueue; | |||
21 | import java.util.Random; | 21 | import java.util.Random; |
22 | 22 | ||
23 | import org.apache.log4j.Logger; | 23 | import org.apache.log4j.Logger; |
24 | import org.eclipse.emf.ecore.EObject; | ||
24 | import org.eclipse.emf.ecore.util.EcoreUtil; | 25 | import org.eclipse.emf.ecore.util.EcoreUtil; |
25 | import org.eclipse.viatra.dse.api.strategy.interfaces.IStrategy; | 26 | import org.eclipse.viatra.dse.api.strategy.interfaces.IStrategy; |
26 | import org.eclipse.viatra.dse.base.ThreadContext; | 27 | import org.eclipse.viatra.dse.base.ThreadContext; |
@@ -75,8 +76,6 @@ public class BestFirstStrategyForModelGeneration implements IStrategy { | |||
75 | // Running | 76 | // Running |
76 | private PriorityQueue<TrajectoryWithFitness> trajectoiresToExplore; | 77 | private PriorityQueue<TrajectoryWithFitness> trajectoiresToExplore; |
77 | private SolutionStore solutionStore; | 78 | private SolutionStore solutionStore; |
78 | private SolutionStoreWithCopy solutionStoreWithCopy; | ||
79 | private SolutionStoreWithDiversityDescriptor solutionStoreWithDiversityDescriptor; | ||
80 | private volatile boolean isInterrupted = false; | 79 | private volatile boolean isInterrupted = false; |
81 | private ModelResult modelResultByInternalSolver = null; | 80 | private ModelResult modelResultByInternalSolver = null; |
82 | private Random random = new Random(); | 81 | private Random random = new Random(); |
@@ -95,14 +94,9 @@ public class BestFirstStrategyForModelGeneration implements IStrategy { | |||
95 | this.workspace = workspace; | 94 | this.workspace = workspace; |
96 | this.configuration = configuration; | 95 | this.configuration = configuration; |
97 | this.method = method; | 96 | this.method = method; |
97 | //logger.setLevel(Level.DEBUG); | ||
98 | } | 98 | } |
99 | 99 | ||
100 | public SolutionStoreWithCopy getSolutionStoreWithCopy() { | ||
101 | return solutionStoreWithCopy; | ||
102 | } | ||
103 | public SolutionStoreWithDiversityDescriptor getSolutionStoreWithDiversityDescriptor() { | ||
104 | return solutionStoreWithDiversityDescriptor; | ||
105 | } | ||
106 | public int getNumberOfStatecoderFail() { | 100 | public int getNumberOfStatecoderFail() { |
107 | return numberOfStatecoderFail; | 101 | return numberOfStatecoderFail; |
108 | } | 102 | } |
@@ -121,9 +115,6 @@ public class BestFirstStrategyForModelGeneration implements IStrategy { | |||
121 | matchers.add(matcher); | 115 | matchers.add(matcher); |
122 | } | 116 | } |
123 | 117 | ||
124 | this.solutionStoreWithCopy = new SolutionStoreWithCopy(); | ||
125 | this.solutionStoreWithDiversityDescriptor = new SolutionStoreWithDiversityDescriptor(configuration.diversityRequirement); | ||
126 | |||
127 | final ObjectiveComparatorHelper objectiveComparatorHelper = context.getObjectiveComparatorHelper(); | 118 | final ObjectiveComparatorHelper objectiveComparatorHelper = context.getObjectiveComparatorHelper(); |
128 | this.comparator = new Comparator<TrajectoryWithFitness>() { | 119 | this.comparator = new Comparator<TrajectoryWithFitness>() { |
129 | @Override | 120 | @Override |
@@ -146,13 +137,13 @@ public class BestFirstStrategyForModelGeneration implements IStrategy { | |||
146 | return; | 137 | return; |
147 | } | 138 | } |
148 | 139 | ||
149 | final Fitness firstFittness = context.calculateFitness(); | 140 | final Fitness firstfitness = context.calculateFitness(); |
150 | checkForSolution(firstFittness); | 141 | solutionStore.newSolution(context); |
151 | 142 | ||
152 | final ObjectiveComparatorHelper objectiveComparatorHelper = context.getObjectiveComparatorHelper(); | 143 | final ObjectiveComparatorHelper objectiveComparatorHelper = context.getObjectiveComparatorHelper(); |
153 | final Object[] firstTrajectory = context.getTrajectory().toArray(new Object[0]); | 144 | final Object[] firstTrajectory = context.getTrajectory().toArray(new Object[0]); |
154 | TrajectoryWithFitness currentTrajectoryWithFittness = new TrajectoryWithFitness(firstTrajectory, firstFittness); | 145 | TrajectoryWithFitness currentTrajectoryWithfitness = new TrajectoryWithFitness(firstTrajectory, firstfitness); |
155 | trajectoiresToExplore.add(currentTrajectoryWithFittness); | 146 | trajectoiresToExplore.add(currentTrajectoryWithfitness); |
156 | 147 | ||
157 | //if(configuration) | 148 | //if(configuration) |
158 | visualiseCurrentState(); | 149 | visualiseCurrentState(); |
@@ -167,22 +158,22 @@ public class BestFirstStrategyForModelGeneration implements IStrategy { | |||
167 | 158 | ||
168 | mainLoop: while (!isInterrupted && !configuration.progressMonitor.isCancelled()) { | 159 | mainLoop: while (!isInterrupted && !configuration.progressMonitor.isCancelled()) { |
169 | 160 | ||
170 | if (currentTrajectoryWithFittness == null) { | 161 | if (currentTrajectoryWithfitness == null) { |
171 | if (trajectoiresToExplore.isEmpty()) { | 162 | if (trajectoiresToExplore.isEmpty()) { |
172 | logger.debug("State space is fully traversed."); | 163 | logger.debug("State space is fully traversed."); |
173 | return; | 164 | return; |
174 | } else { | 165 | } else { |
175 | currentTrajectoryWithFittness = selectState(); | 166 | currentTrajectoryWithfitness = selectState(); |
176 | if (logger.isDebugEnabled()) { | 167 | if (logger.isDebugEnabled()) { |
177 | logger.debug("Current trajectory: " + Arrays.toString(context.getTrajectory().toArray())); | 168 | logger.debug("Current trajectory: " + Arrays.toString(context.getTrajectory().toArray())); |
178 | logger.debug("New trajectory is chosen: " + currentTrajectoryWithFittness); | 169 | logger.debug("New trajectory is chosen: " + currentTrajectoryWithfitness); |
179 | } | 170 | } |
180 | context.getDesignSpaceManager().executeTrajectoryWithMinimalBacktrackWithoutStateCoding(currentTrajectoryWithFittness.trajectory); | 171 | context.getDesignSpaceManager().executeTrajectoryWithMinimalBacktrackWithoutStateCoding(currentTrajectoryWithfitness.trajectory); |
181 | } | 172 | } |
182 | } | 173 | } |
183 | 174 | ||
184 | // visualiseCurrentState(); | 175 | // visualiseCurrentState(); |
185 | // boolean consistencyCheckResult = checkConsistency(currentTrajectoryWithFittness); | 176 | // boolean consistencyCheckResult = checkConsistency(currentTrajectoryWithfitness); |
186 | // if(consistencyCheckResult == true) { | 177 | // if(consistencyCheckResult == true) { |
187 | // continue mainLoop; | 178 | // continue mainLoop; |
188 | // } | 179 | // } |
@@ -194,10 +185,11 @@ public class BestFirstStrategyForModelGeneration implements IStrategy { | |||
194 | final Object nextActivation = iterator.next(); | 185 | final Object nextActivation = iterator.next(); |
195 | // if (!iterator.hasNext()) { | 186 | // if (!iterator.hasNext()) { |
196 | // logger.debug("Last untraversed activation of the state."); | 187 | // logger.debug("Last untraversed activation of the state."); |
197 | // trajectoiresToExplore.remove(currentTrajectoryWithFittness); | 188 | // trajectoiresToExplore.remove(currentTrajectoryWithfitness); |
198 | // } | 189 | // } |
199 | logger.debug("Executing new activation: " + nextActivation); | 190 | logger.debug("Executing new activation: " + nextActivation); |
200 | context.executeAcitvationId(nextActivation); | 191 | context.executeAcitvationId(nextActivation); |
192 | method.getStatistics().incrementDecisionCount(); | ||
201 | 193 | ||
202 | visualiseCurrentState(); | 194 | visualiseCurrentState(); |
203 | // for(ViatraQueryMatcher<? extends IPatternMatch> matcher : matchers) { | 195 | // for(ViatraQueryMatcher<? extends IPatternMatch> matcher : matchers) { |
@@ -209,7 +201,7 @@ public class BestFirstStrategyForModelGeneration implements IStrategy { | |||
209 | // System.out.println("---------"); | 201 | // System.out.println("---------"); |
210 | // } | 202 | // } |
211 | 203 | ||
212 | boolean consistencyCheckResult = checkConsistency(currentTrajectoryWithFittness); | 204 | boolean consistencyCheckResult = checkConsistency(currentTrajectoryWithfitness); |
213 | if(consistencyCheckResult == true) { continue mainLoop; } | 205 | if(consistencyCheckResult == true) { continue mainLoop; } |
214 | 206 | ||
215 | if (context.isCurrentStateAlreadyTraversed()) { | 207 | if (context.isCurrentStateAlreadyTraversed()) { |
@@ -220,38 +212,38 @@ public class BestFirstStrategyForModelGeneration implements IStrategy { | |||
220 | context.backtrack(); | 212 | context.backtrack(); |
221 | } else { | 213 | } else { |
222 | final Fitness nextFitness = context.calculateFitness(); | 214 | final Fitness nextFitness = context.calculateFitness(); |
223 | checkForSolution(nextFitness); | 215 | solutionStore.newSolution(context); |
224 | if (context.getDepth() > configuration.searchSpaceConstraints.maxDepth) { | 216 | if (context.getDepth() > configuration.searchSpaceConstraints.maxDepth) { |
225 | logger.debug("Reached max depth."); | 217 | logger.debug("Reached max depth."); |
226 | context.backtrack(); | 218 | context.backtrack(); |
227 | continue; | 219 | continue; |
228 | } | 220 | } |
229 | 221 | ||
230 | TrajectoryWithFitness nextTrajectoryWithFittness = new TrajectoryWithFitness( | 222 | TrajectoryWithFitness nextTrajectoryWithfitness = new TrajectoryWithFitness( |
231 | context.getTrajectory().toArray(), nextFitness); | 223 | context.getTrajectory().toArray(), nextFitness); |
232 | trajectoiresToExplore.add(nextTrajectoryWithFittness); | 224 | trajectoiresToExplore.add(nextTrajectoryWithfitness); |
233 | 225 | ||
234 | int compare = objectiveComparatorHelper.compare(currentTrajectoryWithFittness.fitness, | 226 | int compare = objectiveComparatorHelper.compare(currentTrajectoryWithfitness.fitness, |
235 | nextTrajectoryWithFittness.fitness); | 227 | nextTrajectoryWithfitness.fitness); |
236 | if (compare < 0) { | 228 | if (compare < 0) { |
237 | logger.debug("Better fitness, moving on: " + nextFitness); | 229 | logger.debug("Better fitness, moving on: " + nextFitness); |
238 | currentTrajectoryWithFittness = nextTrajectoryWithFittness; | 230 | currentTrajectoryWithfitness = nextTrajectoryWithfitness; |
239 | continue mainLoop; | 231 | continue mainLoop; |
240 | } else if (compare == 0) { | 232 | } else if (compare == 0) { |
241 | logger.debug("Equally good fitness, moving on: " + nextFitness); | 233 | logger.debug("Equally good fitness, moving on: " + nextFitness); |
242 | currentTrajectoryWithFittness = nextTrajectoryWithFittness; | 234 | currentTrajectoryWithfitness = nextTrajectoryWithfitness; |
243 | continue mainLoop; | 235 | continue mainLoop; |
244 | } else { | 236 | } else { |
245 | logger.debug("Worse fitness."); | 237 | logger.debug("Worse fitness."); |
246 | currentTrajectoryWithFittness = null; | 238 | currentTrajectoryWithfitness = null; |
247 | continue mainLoop; | 239 | continue mainLoop; |
248 | } | 240 | } |
249 | } | 241 | } |
250 | } | 242 | } |
251 | 243 | ||
252 | logger.debug("State is fully traversed."); | 244 | logger.debug("State is fully traversed."); |
253 | trajectoiresToExplore.remove(currentTrajectoryWithFittness); | 245 | trajectoiresToExplore.remove(currentTrajectoryWithfitness); |
254 | currentTrajectoryWithFittness = null; | 246 | currentTrajectoryWithfitness = null; |
255 | 247 | ||
256 | } | 248 | } |
257 | logger.info("Interrupted."); | 249 | logger.info("Interrupted."); |
@@ -263,25 +255,13 @@ public class BestFirstStrategyForModelGeneration implements IStrategy { | |||
263 | activationIds = new ArrayList<Object>(context.getUntraversedActivationIds()); | 255 | activationIds = new ArrayList<Object>(context.getUntraversedActivationIds()); |
264 | Collections.shuffle(activationIds); | 256 | Collections.shuffle(activationIds); |
265 | } catch (NullPointerException e) { | 257 | } catch (NullPointerException e) { |
258 | // logger.warn("Unexpected state code: " + context.getDesignSpaceManager().getCurrentState()); | ||
266 | numberOfStatecoderFail++; | 259 | numberOfStatecoderFail++; |
267 | activationIds = Collections.emptyList(); | 260 | activationIds = Collections.emptyList(); |
268 | } | 261 | } |
269 | return activationIds; | 262 | return activationIds; |
270 | } | 263 | } |
271 | 264 | ||
272 | private void checkForSolution(final Fitness fittness) { | ||
273 | if (fittness.isSatisifiesHardObjectives()) { | ||
274 | if (solutionStoreWithDiversityDescriptor.isDifferent(context)) { | ||
275 | solutionStoreWithCopy.newSolution(context); | ||
276 | solutionStoreWithDiversityDescriptor.newSolution(context); | ||
277 | solutionStore.newSolution(context); | ||
278 | configuration.progressMonitor.workedModelFound(configuration.solutionScope.numberOfRequiredSolution); | ||
279 | |||
280 | logger.debug("Found a solution."); | ||
281 | } | ||
282 | } | ||
283 | } | ||
284 | |||
285 | @Override | 265 | @Override |
286 | public void interruptStrategy() { | 266 | public void interruptStrategy() { |
287 | isInterrupted = true; | 267 | isInterrupted = true; |
@@ -311,13 +291,16 @@ public class BestFirstStrategyForModelGeneration implements IStrategy { | |||
311 | } | 291 | } |
312 | 292 | ||
313 | public void visualiseCurrentState() { | 293 | public void visualiseCurrentState() { |
314 | PartialInterpretationVisualiser partialInterpretatioVisualiser = configuration.debugCongiguration.partialInterpretatioVisualiser; | 294 | PartialInterpretationVisualiser partialInterpretatioVisualiser = configuration.debugConfiguration.partialInterpretatioVisualiser; |
315 | if(partialInterpretatioVisualiser != null && this.configuration.documentationLevel == DocumentationLevel.FULL && workspace != null) { | 295 | if(partialInterpretatioVisualiser != null && this.configuration.documentationLevel == DocumentationLevel.FULL && workspace != null) { |
316 | PartialInterpretation p = (PartialInterpretation) (context.getModel()); | 296 | PartialInterpretation p = (PartialInterpretation) (context.getModel()); |
317 | int id = ++numberOfPrintedModel; | 297 | int id = ++numberOfPrintedModel; |
318 | if (id % configuration.debugCongiguration.partalInterpretationVisualisationFrequency == 0) { | 298 | if (id % configuration.debugConfiguration.partalInterpretationVisualisationFrequency == 0) { |
319 | PartialInterpretationVisualisation visualisation = partialInterpretatioVisualiser.visualiseConcretization(p); | 299 | PartialInterpretationVisualisation visualisation = partialInterpretatioVisualiser.visualiseConcretization(p); |
320 | visualisation.writeToFile(workspace, String.format("state%09d.png", id)); | 300 | logger.debug("Visualizing state: " + id + " (" + context.getDesignSpaceManager().getCurrentState() + ")"); |
301 | String name = String.format("state%09d", id); | ||
302 | visualisation.writeToFile(workspace, name + ".png"); | ||
303 | workspace.writeModel((EObject) context.getModel(), name + ".xmi"); | ||
321 | } | 304 | } |
322 | } | 305 | } |
323 | } | 306 | } |