diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/HillClimbingOnRealisticMetricStrategyForModelGeneration.java')
1 files changed, 68 insertions, 44 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/HillClimbingOnRealisticMetricStrategyForModelGeneration.java b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/HillClimbingOnRealisticMetricStrategyForModelGeneration.java index 9409ac48..4dff00cd 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/HillClimbingOnRealisticMetricStrategyForModelGeneration.java +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/HillClimbingOnRealisticMetricStrategyForModelGeneration.java | |||
@@ -24,6 +24,7 @@ import org.eclipse.viatra.query.runtime.api.IPatternMatch; | |||
24 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine; | 24 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine; |
25 | import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher; | 25 | import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher; |
26 | 26 | ||
27 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.app.Domain; | ||
27 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.app.MetricDistanceGroup; | 28 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.app.MetricDistanceGroup; |
28 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.app.PartialInterpretationMetricDistance; | 29 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.app.PartialInterpretationMetricDistance; |
29 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.DocumentationLevel; | 30 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.DocumentationLevel; |
@@ -37,6 +38,7 @@ import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretation2logic.Parti | |||
37 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation; | 38 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation; |
38 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretationVisualisation; | 39 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretationVisualisation; |
39 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretationVisualiser; | 40 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretationVisualiser; |
41 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.RealisticGuidance; | ||
40 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ViatraReasonerConfiguration; | 42 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ViatraReasonerConfiguration; |
41 | import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace; | 43 | import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace; |
42 | 44 | ||
@@ -66,12 +68,16 @@ public class HillClimbingOnRealisticMetricStrategyForModelGeneration implements | |||
66 | private Map<Object, List<Object>> stateAndActivations; | 68 | private Map<Object, List<Object>> stateAndActivations; |
67 | private Map<TrajectoryWithFitness, Double> trajectoryFit; | 69 | private Map<TrajectoryWithFitness, Double> trajectoryFit; |
68 | 70 | ||
71 | private boolean allowMustViolation; | ||
72 | private Domain domain; | ||
73 | |||
69 | // Statistics | 74 | // Statistics |
70 | private int numberOfStatecoderFail = 0; | 75 | private int numberOfStatecoderFail = 0; |
71 | private int numberOfPrintedModel = 0; | 76 | private int numberOfPrintedModel = 0; |
72 | private int numberOfSolverCalls = 0; | 77 | private int numberOfSolverCalls = 0; |
73 | private PartialInterpretationMetricDistance metricDistance; | 78 | private PartialInterpretationMetricDistance metricDistance; |
74 | 79 | ||
80 | |||
75 | public HillClimbingOnRealisticMetricStrategyForModelGeneration( | 81 | public HillClimbingOnRealisticMetricStrategyForModelGeneration( |
76 | ReasonerWorkspace workspace, | 82 | ReasonerWorkspace workspace, |
77 | ViatraReasonerConfiguration configuration, | 83 | ViatraReasonerConfiguration configuration, |
@@ -126,7 +132,12 @@ public class HillClimbingOnRealisticMetricStrategyForModelGeneration implements | |||
126 | 132 | ||
127 | trajectoiresToExplore = new PriorityQueue<TrajectoryWithFitness>(11, comparator); | 133 | trajectoiresToExplore = new PriorityQueue<TrajectoryWithFitness>(11, comparator); |
128 | stateAndActivations = new HashMap<Object, List<Object>>(); | 134 | stateAndActivations = new HashMap<Object, List<Object>>(); |
129 | metricDistance = new PartialInterpretationMetricDistance(); | 135 | |
136 | domain = Domain.valueOf(configuration.domain); | ||
137 | metricDistance = new PartialInterpretationMetricDistance(domain); | ||
138 | |||
139 | //set whether allows must violations during the realistic generation | ||
140 | allowMustViolation = configuration.allowMustViolations; | ||
130 | } | 141 | } |
131 | 142 | ||
132 | @Override | 143 | @Override |
@@ -142,7 +153,7 @@ public class HillClimbingOnRealisticMetricStrategyForModelGeneration implements | |||
142 | 153 | ||
143 | final Fitness firstFittness = context.calculateFitness(); | 154 | final Fitness firstFittness = context.calculateFitness(); |
144 | 155 | ||
145 | final ObjectiveComparatorHelper objectiveComparatorHelper = context.getObjectiveComparatorHelper(); | 156 | //final ObjectiveComparatorHelper objectiveComparatorHelper = context.getObjectiveComparatorHelper(); |
146 | final Object[] firstTrajectory = context.getTrajectory().toArray(new Object[0]); | 157 | final Object[] firstTrajectory = context.getTrajectory().toArray(new Object[0]); |
147 | TrajectoryWithFitness currentTrajectoryWithFittness = new TrajectoryWithFitness(firstTrajectory, firstFittness); | 158 | TrajectoryWithFitness currentTrajectoryWithFittness = new TrajectoryWithFitness(firstTrajectory, firstFittness); |
148 | trajectoryFit.put(currentTrajectoryWithFittness, Double.MAX_VALUE); | 159 | trajectoryFit.put(currentTrajectoryWithFittness, Double.MAX_VALUE); |
@@ -151,7 +162,6 @@ public class HillClimbingOnRealisticMetricStrategyForModelGeneration implements | |||
151 | 162 | ||
152 | //if(configuration) | 163 | //if(configuration) |
153 | visualiseCurrentState(); | 164 | visualiseCurrentState(); |
154 | |||
155 | // the two is the True and False node generated at the beginning of the generation | 165 | // the two is the True and False node generated at the beginning of the generation |
156 | int targetSize = configuration.typeScopes.maxNewElements + 2; | 166 | int targetSize = configuration.typeScopes.maxNewElements + 2; |
157 | int count = 0; | 167 | int count = 0; |
@@ -193,26 +203,19 @@ public class HillClimbingOnRealisticMetricStrategyForModelGeneration implements | |||
193 | double draw = Math.random(); | 203 | double draw = Math.random(); |
194 | count++; | 204 | count++; |
195 | 205 | ||
196 | // output statistics | ||
197 | //System.out.println("KS"); | ||
198 | System.out.println("NA distance: " + heuristics.getNADistance()); | 206 | System.out.println("NA distance: " + heuristics.getNADistance()); |
199 | System.out.println("MPC distance: " + heuristics.getMPCDistance()); | 207 | System.out.println("MPC distance: " + heuristics.getMPCDistance()); |
200 | System.out.println("Out degree distance:" + heuristics.getOutDegreeDistance()); | 208 | System.out.println("Out degree distance:" + heuristics.getOutDegreeDistance()); |
201 | System.out.println("NodeType :" + heuristics.getNodeTypeDistance()); | 209 | System.out.println("NodeType :" + heuristics.getNodeTypeDistance()); |
202 | System.out.println("Transition :" + heuristics.getNodeTypePercentage("Transition")); | 210 | System.out.println("Edge :" + heuristics.edgeTypeDistance); |
203 | System.out.println("FinalState :" + heuristics.getNodeTypePercentage("FinalState")); | 211 | |
212 | // System.out.println("FinalState :" + heuristics.getNodeTypePercentage("FinalState")); | ||
204 | 213 | ||
205 | // MetricDistanceGroup lsHeuristic = metricDistance.calculateMetricDistance(model); | ||
206 | // System.out.println("LS"); | ||
207 | // System.out.println("NA distance: " + lsHeuristic.getNADistance()); | ||
208 | // System.out.println("MPC distance: " + lsHeuristic.getMPCDistance()); | ||
209 | // System.out.println("Out degree distance:" + lsHeuristic.getOutDegreeDistance()); | ||
210 | |||
211 | //TODO: the number of activations to be checked should be configurasble | 214 | //TODO: the number of activations to be checked should be configurasble |
212 | if(activationIds.size() > 50) { | 215 | if(activationIds.size() > 50) { |
213 | activationIds = activationIds.subList(0, 50); | 216 | activationIds = activationIds.subList(0, 50); |
214 | } | 217 | } |
215 | 218 | ||
216 | valueMap = sortWithWeight(activationIds, model.getNewElements().size()); | 219 | valueMap = sortWithWeight(activationIds, model.getNewElements().size()); |
217 | lastState = context.getCurrentStateId(); | 220 | lastState = context.getCurrentStateId(); |
218 | while (!isInterrupted && !configuration.progressMonitor.isCancelled() && activationIds.size() > 0) { | 221 | while (!isInterrupted && !configuration.progressMonitor.isCancelled() && activationIds.size() > 0) { |
@@ -222,26 +225,19 @@ public class HillClimbingOnRealisticMetricStrategyForModelGeneration implements | |||
222 | logger.debug("Executing new activation: " + nextActivation); | 225 | logger.debug("Executing new activation: " + nextActivation); |
223 | context.executeAcitvationId(nextActivation); | 226 | context.executeAcitvationId(nextActivation); |
224 | visualiseCurrentState(); | 227 | visualiseCurrentState(); |
225 | |||
226 | boolean consistencyCheckResult = checkConsistency(currentTrajectoryWithFittness); | 228 | boolean consistencyCheckResult = checkConsistency(currentTrajectoryWithFittness); |
227 | if(consistencyCheckResult == true) { continue mainLoop; } | 229 | if(consistencyCheckResult == true) { continue mainLoop; } |
228 | 230 | ||
229 | int currentSize = model.getNewElements().size(); | 231 | int currentSize = model.getNewElements().size(); |
230 | int targetDiff = targetSize - currentSize; | 232 | int targetDiff = targetSize - currentSize; |
231 | boolean shouldFinish = targetDiff <= 0; | ||
232 | 233 | ||
233 | // does not allow must violations | 234 | // does not allow must violations |
234 | // if((getNumberOfViolations(mustMatchers) > 0|| getNumberOfViolations(mayMatchers) > targetDiff) && !shouldFinish) { | 235 | if((getNumberOfViolations(mustMatchers) > 0|| getNumberOfViolations(mayMatchers) > targetDiff) && !allowMustViolation) { |
235 | // context.backtrack(); | 236 | context.backtrack(); |
236 | // }else { | 237 | }else { |
237 | final Fitness nextFitness = context.calculateFitness(); | 238 | final Fitness nextFitness = context.calculateFitness(); |
238 | 239 | ||
239 | // the only hard objectives are the size | 240 | // the only hard objectives are configured in the config file |
240 | if(shouldFinish) { | ||
241 | System.out.println("Solution Found!!"); | ||
242 | System.out.println("# violations: " + (getNumberOfViolations(mustMatchers))); | ||
243 | nextFitness.setSatisifiesHardObjectives(true); | ||
244 | } | ||
245 | checkForSolution(nextFitness); | 241 | checkForSolution(nextFitness); |
246 | 242 | ||
247 | if (context.getDepth() > configuration.searchSpaceConstraints.maxDepth) { | 243 | if (context.getDepth() > configuration.searchSpaceConstraints.maxDepth) { |
@@ -261,20 +257,23 @@ public class HillClimbingOnRealisticMetricStrategyForModelGeneration implements | |||
261 | trajectoiresToExplore.add(nextTrajectoryWithFittness); | 257 | trajectoiresToExplore.add(nextTrajectoryWithFittness); |
262 | 258 | ||
263 | //Currently, just go to the next state without considering the value of trajectory | 259 | //Currently, just go to the next state without considering the value of trajectory |
264 | int compare = objectiveComparatorHelper.compare(currentTrajectoryWithFittness.fitness, | 260 | currentTrajectoryWithFittness = nextTrajectoryWithFittness; |
265 | nextTrajectoryWithFittness.fitness); | 261 | continue mainLoop; |
266 | if (compare < 0) { | 262 | |
267 | logger.debug("Better fitness, moving on: " + nextFitness); | 263 | // int compare = objectiveComparatorHelper.compare(currentTrajectoryWithFittness.fitness, |
268 | currentTrajectoryWithFittness = nextTrajectoryWithFittness; | 264 | // nextTrajectoryWithFittness.fitness); |
269 | continue mainLoop; | 265 | // if (compare < 0) { |
270 | } else if (compare == 0) { | 266 | // logger.debug("Better fitness, moving on: " + nextFitness); |
271 | logger.debug("Equally good fitness, moving on: " + nextFitness); | 267 | // currentTrajectoryWithFittness = nextTrajectoryWithFittness; |
272 | currentTrajectoryWithFittness = nextTrajectoryWithFittness; | 268 | // continue mainLoop; |
273 | continue mainLoop; | 269 | // } else if (compare == 0) { |
274 | } else { | 270 | // logger.debug("Equally good fitness, moving on: " + nextFitness); |
275 | logger.debug("Worse fitness."); | 271 | // currentTrajectoryWithFittness = nextTrajectoryWithFittness; |
276 | currentTrajectoryWithFittness = nextTrajectoryWithFittness; | 272 | // continue mainLoop; |
277 | continue mainLoop; | 273 | // } else { |
274 | // logger.debug("Worse fitness."); | ||
275 | // currentTrajectoryWithFittness = nextTrajectoryWithFittness; | ||
276 | // continue mainLoop; | ||
278 | } | 277 | } |
279 | } | 278 | } |
280 | } | 279 | } |
@@ -282,7 +281,7 @@ public class HillClimbingOnRealisticMetricStrategyForModelGeneration implements | |||
282 | trajectoiresToExplore.remove(currentTrajectoryWithFittness); | 281 | trajectoiresToExplore.remove(currentTrajectoryWithFittness); |
283 | currentTrajectoryWithFittness = null; | 282 | currentTrajectoryWithFittness = null; |
284 | context.backtrack(); | 283 | context.backtrack(); |
285 | //} | 284 | // } |
286 | 285 | ||
287 | logger.info("Interrupted."); | 286 | logger.info("Interrupted."); |
288 | } | 287 | } |
@@ -327,10 +326,33 @@ public class HillClimbingOnRealisticMetricStrategyForModelGeneration implements | |||
327 | private double calculateCurrentStateValue(int factor, int violation) { | 326 | private double calculateCurrentStateValue(int factor, int violation) { |
328 | PartialInterpretation model = (PartialInterpretation) context.getModel(); | 327 | PartialInterpretation model = (PartialInterpretation) context.getModel(); |
329 | MetricDistanceGroup g = metricDistance.calculateMetricDistanceKS(model); | 328 | MetricDistanceGroup g = metricDistance.calculateMetricDistanceKS(model); |
330 | |||
331 | double consistenceWeights = 5 * factor / (configuration.typeScopes.maxNewElements + 2) * (1- 1.0/(1+violation)); | ||
332 | 329 | ||
333 | return(100.0 *(g.getNodeTypeDistance()) + 5*(g.getNADistance() + g.getMPCDistance() +g.getOutDegreeDistance()) + consistenceWeights); | 330 | if(configuration.realisticGuidance == RealisticGuidance.MPC) { |
331 | return g.getMPCDistance(); | ||
332 | }else if(configuration.realisticGuidance == RealisticGuidance.NodeActivity) { | ||
333 | return g.getNADistance(); | ||
334 | }else if(configuration.realisticGuidance == RealisticGuidance.OutDegree) { | ||
335 | return g.getOutDegreeDistance(); | ||
336 | }else if(configuration.realisticGuidance == RealisticGuidance.NodeType) { | ||
337 | return g.getNodeTypeDistance(); | ||
338 | }else if(configuration.realisticGuidance == RealisticGuidance.Composite) { | ||
339 | double consistenceWeights = 5 * factor / (configuration.typeScopes.maxNewElements + 2) * (1- 1.0/(1+violation)); | ||
340 | |||
341 | if(domain == Domain.Yakindumm) { | ||
342 | return(100.0 *(g.getNodeTypeDistance()) + 5*(g.getNADistance() + 5*g.getMPCDistance() +g.getOutDegreeDistance()) + consistenceWeights); | ||
343 | }else { | ||
344 | return 10*(g.getNodeTypeDistance()) + 5*(g.getNADistance() + g.getMPCDistance() +2*g.getOutDegreeDistance()) + consistenceWeights; | ||
345 | } | ||
346 | |||
347 | }else if(configuration.realisticGuidance == RealisticGuidance.Composite_Without_Violations) { | ||
348 | if(domain == Domain.Yakindumm) { | ||
349 | return 100.0 *(g.getNodeTypeDistance()) + 5*(g.getNADistance() + g.getMPCDistance() +g.getOutDegreeDistance()); | ||
350 | }else { | ||
351 | return 10*(g.getNodeTypeDistance()) + 5*(g.getNADistance() + g.getMPCDistance() + 2*g.getOutDegreeDistance()); | ||
352 | } | ||
353 | }else { | ||
354 | return violation; | ||
355 | } | ||
334 | } | 356 | } |
335 | 357 | ||
336 | private int getNumberOfViolations(Collection<ViatraQueryMatcher<? extends IPatternMatch>> matchers) { | 358 | private int getNumberOfViolations(Collection<ViatraQueryMatcher<? extends IPatternMatch>> matchers) { |
@@ -392,6 +414,8 @@ public class HillClimbingOnRealisticMetricStrategyForModelGeneration implements | |||
392 | 414 | ||
393 | private void checkForSolution(final Fitness fittness) { | 415 | private void checkForSolution(final Fitness fittness) { |
394 | if (fittness.isSatisifiesHardObjectives()) { | 416 | if (fittness.isSatisifiesHardObjectives()) { |
417 | System.out.println("Solution Found!!"); | ||
418 | System.out.println("# violations: " + (getNumberOfViolations(mustMatchers))); | ||
395 | if (solutionStoreWithDiversityDescriptor.isDifferent(context)) { | 419 | if (solutionStoreWithDiversityDescriptor.isDifferent(context)) { |
396 | solutionStoreWithCopy.newSolution(context); | 420 | solutionStoreWithCopy.newSolution(context); |
397 | solutionStoreWithDiversityDescriptor.newSolution(context); | 421 | solutionStoreWithDiversityDescriptor.newSolution(context); |