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, 28 insertions, 17 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 d9f6f2aa..6c13e760 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 | |||
@@ -190,16 +190,17 @@ public class HillClimbingOnRealisticMetricStrategyForModelGeneration implements | |||
190 | } | 190 | } |
191 | 191 | ||
192 | //Output intermediate model | 192 | //Output intermediate model |
193 | if(model.getNewElements().size() >= 10) { | 193 | if(model.getNewElements().size() > 0) { |
194 | epsilon = 1.0/count; | 194 | epsilon = 1.0/count; |
195 | draw = Math.random(); | 195 | draw = Math.random(); |
196 | count++; | 196 | count++; |
197 | // cut off the trajectory for bad graph | 197 | // cut off the trajectory for bad graph |
198 | System.out.println("KS"); | 198 | //System.out.println("KS"); |
199 | System.out.println("NA distance: " + heuristics.getNADistance()); | 199 | System.out.println("NA distance: " + heuristics.getNADistance()); |
200 | System.out.println("MPC distance: " + heuristics.getMPCDistance()); | 200 | System.out.println("MPC distance: " + heuristics.getMPCDistance()); |
201 | System.out.println("Out degree distance:" + heuristics.getOutDegreeDistance()); | 201 | System.out.println("Out degree distance:" + heuristics.getOutDegreeDistance()); |
202 | 202 | System.out.println("Exit :" + heuristics.getNodeTypePercentage("Exit")); | |
203 | |||
203 | // MetricDistanceGroup lsHeuristic = metricDistance.calculateMetricDistance(model); | 204 | // MetricDistanceGroup lsHeuristic = metricDistance.calculateMetricDistance(model); |
204 | // System.out.println("LS"); | 205 | // System.out.println("LS"); |
205 | // System.out.println("NA distance: " + lsHeuristic.getNADistance()); | 206 | // System.out.println("NA distance: " + lsHeuristic.getNADistance()); |
@@ -208,7 +209,11 @@ public class HillClimbingOnRealisticMetricStrategyForModelGeneration implements | |||
208 | 209 | ||
209 | //check for next value when doing greedy move | 210 | //check for next value when doing greedy move |
210 | 211 | ||
211 | valueMap = sortWithWeight(activationIds, currentTrajectoryWithFittness.trajectory.length+1); | 212 | if(activationIds.size() > 50) { |
213 | activationIds = activationIds.subList(0, 50); | ||
214 | } | ||
215 | |||
216 | valueMap = sortWithWeight(activationIds, model.getNewElements().size()); | ||
212 | } | 217 | } |
213 | lastState = context.getCurrentStateId(); | 218 | lastState = context.getCurrentStateId(); |
214 | while (!isInterrupted && !configuration.progressMonitor.isCancelled() && activationIds.size() > 0) { | 219 | while (!isInterrupted && !configuration.progressMonitor.isCancelled() && activationIds.size() > 0) { |
@@ -224,7 +229,8 @@ public class HillClimbingOnRealisticMetricStrategyForModelGeneration implements | |||
224 | 229 | ||
225 | boolean consistencyCheckResult = checkConsistency(currentTrajectoryWithFittness); | 230 | boolean consistencyCheckResult = checkConsistency(currentTrajectoryWithFittness); |
226 | if(consistencyCheckResult == true) { continue mainLoop; } | 231 | if(consistencyCheckResult == true) { continue mainLoop; } |
227 | 232 | boolean shouldFinish = model.getNewElements().size() >= configuration.typeScopes.maxNewElements + 2; | |
233 | |||
228 | /* if (context.isCurrentStateAlreadyTraversed()) { | 234 | /* if (context.isCurrentStateAlreadyTraversed()) { |
229 | // logger.info("The new state is already visited."); | 235 | // logger.info("The new state is already visited."); |
230 | // context.backtrack(); | 236 | // context.backtrack(); |
@@ -232,22 +238,22 @@ public class HillClimbingOnRealisticMetricStrategyForModelGeneration implements | |||
232 | logger.debug("Global contraint is not satisifed."); | 238 | logger.debug("Global contraint is not satisifed."); |
233 | context.backtrack(); | 239 | context.backtrack(); |
234 | } else*/// { | 240 | } else*/// { |
235 | /*if(getNumberOfViolations(mustMatchers) > 0) { | 241 | if(getNumberOfViolations(mustMatchers) > 0 && !shouldFinish) { |
236 | context.backtrack(); | 242 | context.backtrack(); |
237 | }else*/ if(model.getNewElements().size() > 90 && heuristics.getNADistance() + heuristics.getMPCDistance() + heuristics.getOutDegreeDistance() > 0.3) { | 243 | }else if(model.getNewElements().size() > 90 && heuristics.getNADistance() + heuristics.getMPCDistance() + heuristics.getOutDegreeDistance() > 5.3) { |
238 | context.backtrack(); | 244 | context.backtrack(); |
239 | }else if(model.getNewElements().size() > 70 && heuristics.getNADistance() + heuristics.getMPCDistance() + heuristics.getOutDegreeDistance() > 0.45) { | 245 | }else if(model.getNewElements().size() > 70 && heuristics.getNADistance() + heuristics.getMPCDistance() + heuristics.getOutDegreeDistance() > 5.45) { |
240 | context.backtrack(); | 246 | context.backtrack(); |
241 | } else if(model.getNewElements().size() > 50 && heuristics.getNADistance() + heuristics.getMPCDistance() + heuristics.getOutDegreeDistance() > 0.60) { | 247 | } else if(model.getNewElements().size() > 50 && heuristics.getNADistance() + heuristics.getMPCDistance() + heuristics.getOutDegreeDistance() > 5.60) { |
242 | context.backtrack(); | 248 | context.backtrack(); |
243 | } else if(model.getNewElements().size() > 30 && heuristics.getNADistance() + heuristics.getMPCDistance() + heuristics.getOutDegreeDistance() > 0.70) { | 249 | } else if(model.getNewElements().size() > 30 && heuristics.getNADistance() + heuristics.getMPCDistance() + heuristics.getOutDegreeDistance() > 5.70) { |
244 | context.backtrack(); | 250 | context.backtrack(); |
245 | }else { | 251 | }else { |
246 | final Fitness nextFitness = context.calculateFitness(); | 252 | final Fitness nextFitness = context.calculateFitness(); |
247 | 253 | ||
248 | // the only hard objectives are the size | 254 | // the only hard objectives are the size |
249 | 255 | ||
250 | if(model.getNewElements().size() >= configuration.typeScopes.maxNewElements + 2) { | 256 | if(shouldFinish) { |
251 | System.out.println("Solution Found!!"); | 257 | System.out.println("Solution Found!!"); |
252 | System.out.println("# violations: " + (getNumberOfViolations(mustMatchers) + getNumberOfViolations(mayMatchers))); | 258 | System.out.println("# violations: " + (getNumberOfViolations(mustMatchers) + getNumberOfViolations(mayMatchers))); |
253 | nextFitness.setSatisifiesHardObjectives(true); | 259 | nextFitness.setSatisifiesHardObjectives(true); |
@@ -269,8 +275,9 @@ public class HillClimbingOnRealisticMetricStrategyForModelGeneration implements | |||
269 | int step = nextTrajectoryWithFittness.trajectory.length; | 275 | int step = nextTrajectoryWithFittness.trajectory.length; |
270 | int violation = getNumberOfViolations(mustMatchers) + getNumberOfViolations(mayMatchers); | 276 | int violation = getNumberOfViolations(mustMatchers) + getNumberOfViolations(mayMatchers); |
271 | metricDistance.getLinearModel().feedData(context.getCurrentStateId(), metricDistance.calculateFeature(step, violation), calculateCurrentStateValue(step, violation), lastState); | 277 | metricDistance.getLinearModel().feedData(context.getCurrentStateId(), metricDistance.calculateFeature(step, violation), calculateCurrentStateValue(step, violation), lastState); |
278 | double value = calculateCurrentStateValue(step, violation); | ||
272 | 279 | ||
273 | trajectoryFit.put(nextTrajectoryWithFittness, calculateCurrentStateValue(step, violation)); | 280 | trajectoryFit.put(nextTrajectoryWithFittness, value); |
274 | trajectoiresToExplore.add(nextTrajectoryWithFittness); | 281 | trajectoiresToExplore.add(nextTrajectoryWithFittness); |
275 | 282 | ||
276 | int compare = objectiveComparatorHelper.compare(currentTrajectoryWithFittness.fitness, | 283 | int compare = objectiveComparatorHelper.compare(currentTrajectoryWithFittness.fitness, |
@@ -311,19 +318,21 @@ public class HillClimbingOnRealisticMetricStrategyForModelGeneration implements | |||
311 | // do hill climbing | 318 | // do hill climbing |
312 | for(Object id : activationIds) { | 319 | for(Object id : activationIds) { |
313 | context.executeAcitvationId(id); | 320 | context.executeAcitvationId(id); |
314 | int violation = getNumberOfViolations(mayMatchers) + getNumberOfViolations(mustMatchers); | 321 | int violation = getNumberOfViolations(mayMatchers); |
315 | valueMap.put(id, calculateFutureStateValue(factor, violation)); | 322 | valueMap.put(id, calculateFutureStateValue(factor, violation)); |
316 | context.backtrack(); | 323 | context.backtrack(); |
317 | } | 324 | } |
318 | 325 | ||
326 | activationIds.removeIf(li -> valueMap.get(li) >= 10000); | ||
319 | Collections.sort(activationIds, Comparator.comparing(li -> valueMap.get(li))); | 327 | Collections.sort(activationIds, Comparator.comparing(li -> valueMap.get(li))); |
320 | return valueMap; | 328 | return valueMap; |
321 | } | 329 | } |
322 | 330 | ||
323 | private double calculateFutureStateValue(int step, int violation) { | 331 | private double calculateFutureStateValue(int step, int violation) { |
324 | double currentValue = calculateCurrentStateValue(step, violation); | 332 | double currentValue = calculateCurrentStateValue(step, violation); |
325 | if(step > 40) { | 333 | |
326 | double[] toPredict = metricDistance.calculateFeature(200, violation); | 334 | if(step > 10 && currentValue < 10000) { |
335 | double[] toPredict = metricDistance.calculateFeature(100, violation); | ||
327 | try { | 336 | try { |
328 | return metricDistance.getLinearModel().getPredictionForNextDataSample(metricDistance.calculateFeature(step, violation), currentValue, toPredict); | 337 | return metricDistance.getLinearModel().getPredictionForNextDataSample(metricDistance.calculateFeature(step, violation), currentValue, toPredict); |
329 | }catch(IllegalArgumentException e) { | 338 | }catch(IllegalArgumentException e) { |
@@ -337,10 +346,12 @@ public class HillClimbingOnRealisticMetricStrategyForModelGeneration implements | |||
337 | private double calculateCurrentStateValue(int factor, int violation) { | 346 | private double calculateCurrentStateValue(int factor, int violation) { |
338 | PartialInterpretation model = (PartialInterpretation) context.getModel(); | 347 | PartialInterpretation model = (PartialInterpretation) context.getModel(); |
339 | MetricDistanceGroup g = metricDistance.calculateMetricDistanceKS(model); | 348 | MetricDistanceGroup g = metricDistance.calculateMetricDistanceKS(model); |
340 | 349 | if(g.getNodeTypePercentage("Exit") > 0.0) { | |
350 | return 10000; | ||
351 | } | ||
341 | double consistenceWeights = 1- 1.0/(1+violation); | 352 | double consistenceWeights = 1- 1.0/(1+violation); |
342 | 353 | ||
343 | return( /*/ Math.log(factor)*/(g.getNADistance() + g.getMPCDistance() + g.getOutDegreeDistance()) + consistenceWeights); | 354 | return( 5.0 *(g.getNADistance() + g.getMPCDistance() + g.getOutDegreeDistance()) + consistenceWeights); |
344 | } | 355 | } |
345 | 356 | ||
346 | private int getNumberOfViolations(Collection<ViatraQueryMatcher<? extends IPatternMatch>> matchers) { | 357 | private int getNumberOfViolations(Collection<ViatraQueryMatcher<? extends IPatternMatch>> matchers) { |