aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf
diff options
context:
space:
mode:
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf')
-rw-r--r--Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasoner.xtend70
-rw-r--r--Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasonerConfiguration.xtend16
-rw-r--r--Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/HillClimbingOnRealisticMetricStrategyForModelGeneration.java658
-rw-r--r--Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/SolutionCopier.xtend2
4 files changed, 713 insertions, 33 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasoner.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasoner.xtend
index 8e992741..a6fa627d 100644
--- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasoner.xtend
+++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasoner.xtend
@@ -9,6 +9,7 @@ import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem
9import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicproblemPackage 9import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicproblemPackage
10import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.LogicresultFactory 10import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.LogicresultFactory
11import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.ModelResult 11import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.ModelResult
12import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.ScopePropagator
12import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.ScopePropagatorStrategy 13import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.ScopePropagatorStrategy
13import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.PartialInterpretationInitialiser 14import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.PartialInterpretationInitialiser
14import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation 15import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation
@@ -18,7 +19,7 @@ import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.sta
18import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.statecoder.NeighbourhoodBasedHashStateCoderFactory 19import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.statecoder.NeighbourhoodBasedHashStateCoderFactory
19import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.statecoder.PairwiseNeighbourhoodBasedStateCoderFactory 20import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.statecoder.PairwiseNeighbourhoodBasedStateCoderFactory
20import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.BasicScopeGlobalConstraint 21import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.BasicScopeGlobalConstraint
21import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.BestFirstStrategyForModelGeneration 22import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.HillClimbingOnRealisticMetricStrategyForModelGeneration
22import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.InconsistentScopeGlobalConstraint 23import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.InconsistentScopeGlobalConstraint
23import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.LoggerSolutionFoundHandler 24import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.LoggerSolutionFoundHandler
24import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.ModelGenerationCompositeObjective 25import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.ModelGenerationCompositeObjective
@@ -34,6 +35,7 @@ import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace
34import java.util.List 35import java.util.List
35import java.util.Map 36import java.util.Map
36import org.eclipse.emf.ecore.EObject 37import org.eclipse.emf.ecore.EObject
38import org.eclipse.emf.ecore.util.EcoreUtil
37import org.eclipse.viatra.dse.api.DesignSpaceExplorer 39import org.eclipse.viatra.dse.api.DesignSpaceExplorer
38import org.eclipse.viatra.dse.api.DesignSpaceExplorer.DseLoggingLevel 40import org.eclipse.viatra.dse.api.DesignSpaceExplorer.DseLoggingLevel
39import org.eclipse.viatra.dse.solutionstore.SolutionStore 41import org.eclipse.viatra.dse.solutionstore.SolutionStore
@@ -69,6 +71,11 @@ class ViatraReasoner extends LogicReasoner {
69 } 71 }
70 72
71 emptySolution.problemConainer = problem 73 emptySolution.problemConainer = problem
74 val emptySolutionCopy = EcoreUtil.copy(emptySolution)
75
76 val ScopePropagator scopePropagator = new ScopePropagator(emptySolution)
77 scopePropagator.propagateAllScopeConstraints
78
72 var BasicScopeGlobalConstraint basicScopeGlobalConstraint = null 79 var BasicScopeGlobalConstraint basicScopeGlobalConstraint = null
73 if (viatraConfig.scopePropagatorStrategy == ScopePropagatorStrategy.None) { 80 if (viatraConfig.scopePropagatorStrategy == ScopePropagatorStrategy.None) {
74 basicScopeGlobalConstraint = new BasicScopeGlobalConstraint(emptySolution) 81 basicScopeGlobalConstraint = new BasicScopeGlobalConstraint(emptySolution)
@@ -147,9 +154,8 @@ class ViatraReasoner extends LogicReasoner {
147 for (rule : method.objectRefinementRules) { 154 for (rule : method.objectRefinementRules) {
148 dse.addTransformationRule(rule) 155 dse.addTransformationRule(rule)
149 } 156 }
150 157
151 val strategy = new BestFirstStrategyForModelGeneration(workspace, viatraConfig, method, solutionSaver, 158 val strategy = new HillClimbingOnRealisticMetricStrategyForModelGeneration(workspace,viatraConfig,method)
152 numericSolver)
153 viatraConfig.progressMonitor.workedForwardTransformation 159 viatraConfig.progressMonitor.workedForwardTransformation
154 val transformationFinished = System.nanoTime 160 val transformationFinished = System.nanoTime
155 val transformationTime = transformationFinished - transformationStartTime 161 val transformationTime = transformationFinished - transformationStartTime
@@ -257,34 +263,34 @@ class ViatraReasoner extends LogicReasoner {
257 it.name = "ScopePropagationsSolverCalls" 263 it.name = "ScopePropagationsSolverCalls"
258 it.value = method.statistics.scopePropagatorSolverInvocations 264 it.value = method.statistics.scopePropagatorSolverInvocations
259 ] 265 ]
260 it.entries += createIntStatisticEntry => [ 266// it.entries += createIntStatisticEntry => [
261 it.name = "NumericalSolverSumTime" 267// it.name = "NumericalSolverSumTime"
262 it.value = (strategy.numericSolver.runtime / 1000000) as int 268// it.value = (strategy.numericSolver.runtime / 1000000) as int
263 ] 269// ]
264 it.entries += createIntStatisticEntry => [ 270// it.entries += createIntStatisticEntry => [
265 it.name = "NumericalSolverProblemFormingTime" 271// it.name = "NumericalSolverProblemFormingTime"
266 it.value = (strategy.numericSolver.solverFormingProblem / 1000000) as int 272// it.value = (strategy.numericSolver.solverFormingProblem / 1000000) as int
267 ] 273// ]
268 it.entries += createIntStatisticEntry => [ 274// it.entries += createIntStatisticEntry => [
269 it.name = "NumericalSolverSolvingTime" 275// it.name = "NumericalSolverSolvingTime"
270 it.value = (strategy.numericSolver.solverSolvingProblem / 1000000) as int 276// it.value = (strategy.numericSolver.solverSolvingProblem / 1000000) as int
271 ] 277// ]
272 it.entries += createIntStatisticEntry => [ 278// it.entries += createIntStatisticEntry => [
273 it.name = "NumericalSolverInterpretingSolution" 279// it.name = "NumericalSolverInterpretingSolution"
274 it.value = (strategy.numericSolver.solverSolution / 1000000) as int 280// it.value = (strategy.numericSolver.solverSolution / 1000000) as int
275 ] 281// ]
276 it.entries += createIntStatisticEntry => [ 282// it.entries += createIntStatisticEntry => [
277 it.name = "NumericalSolverCachingTime" 283// it.name = "NumericalSolverCachingTime"
278 it.value = (strategy.numericSolver.cachingTime / 1000000) as int 284// it.value = (strategy.numericSolver.cachingTime / 1000000) as int
279 ] 285// ]
280 it.entries += createIntStatisticEntry => [ 286// it.entries += createIntStatisticEntry => [
281 it.name = "NumericalSolverCallNumber" 287// it.name = "NumericalSolverCallNumber"
282 it.value = strategy.numericSolver.numberOfSolverCalls 288// it.value = strategy.numericSolver.numberOfSolverCalls
283 ] 289// ]
284 it.entries += createIntStatisticEntry => [ 290// it.entries += createIntStatisticEntry => [
285 it.name = "NumericalSolverCachedAnswerNumber" 291// it.name = "NumericalSolverCachedAnswerNumber"
286 it.value = strategy.numericSolver.numberOfCachedSolverCalls 292// it.value = strategy.numericSolver.numberOfCachedSolverCalls
287 ] 293// ]
288 if (diversityChecker.active) { 294 if (diversityChecker.active) {
289 it.entries += createIntStatisticEntry => [ 295 it.entries += createIntStatisticEntry => [
290 it.name = "SolutionDiversityCheckTime" 296 it.name = "SolutionDiversityCheckTime"
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasonerConfiguration.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasonerConfiguration.xtend
index fbe6da9d..e73a52b6 100644
--- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasonerConfiguration.xtend
+++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasonerConfiguration.xtend
@@ -71,6 +71,13 @@ class ViatraReasonerConfiguration extends LogicSolverConfiguration {
71 public var unfinishedWFWeight = 1 71 public var unfinishedWFWeight = 1
72 public var calculateObjectCreationCosts = false 72 public var calculateObjectCreationCosts = false
73 73
74 public var RealisticGuidance realisticGuidance = RealisticGuidance.Composite;
75
76 public var isWFOptional = false;
77
78 public var allowMustViolations = false;
79
80 public var String domain = '';
74 public var ScopePropagatorStrategy scopePropagatorStrategy = new ScopePropagatorStrategy.Polyhedral( 81 public var ScopePropagatorStrategy scopePropagatorStrategy = new ScopePropagatorStrategy.Polyhedral(
75 PolyhedralScopePropagatorConstraints.Relational, PolyhedralScopePropagatorSolver.Clp) 82 PolyhedralScopePropagatorConstraints.Relational, PolyhedralScopePropagatorSolver.Clp)
76// public var ScopePropagatorStrategy scopePropagatorStrategy = ScopePropagatorStrategy.BasicTypeHierarchy 83// public var ScopePropagatorStrategy scopePropagatorStrategy = ScopePropagatorStrategy.BasicTypeHierarchy
@@ -109,6 +116,15 @@ class SearchSpaceConstraint {
109 public var List<Function1<ModelGenerationMethod, ModelGenerationMethodBasedGlobalConstraint>> additionalGlobalConstraints = new LinkedList 116 public var List<Function1<ModelGenerationMethod, ModelGenerationMethodBasedGlobalConstraint>> additionalGlobalConstraints = new LinkedList
110} 117}
111 118
119public enum RealisticGuidance{
120 MPC,
121 NodeActivity,
122 OutDegree,
123 NodeType,
124 Composite,
125 Composite_Without_Violations,
126 Violations
127}
112class CostObjectiveConfiguration { 128class CostObjectiveConfiguration {
113 public var List<CostObjectiveElementConfiguration> elements = newArrayList 129 public var List<CostObjectiveElementConfiguration> elements = newArrayList
114 public var ObjectiveKind kind 130 public var ObjectiveKind kind
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
new file mode 100644
index 00000000..d9f84b36
--- /dev/null
+++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/HillClimbingOnRealisticMetricStrategyForModelGeneration.java
@@ -0,0 +1,658 @@
1package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse;
2
3import java.util.ArrayList;
4import java.util.Arrays;
5import java.util.Collection;
6import java.util.Collections;
7import java.util.Comparator;
8import java.util.HashMap;
9import java.util.HashSet;
10import java.util.Iterator;
11import java.util.LinkedList;
12import java.util.List;
13import java.util.Map;
14import java.util.PriorityQueue;
15import java.util.Random;
16import java.util.Set;
17
18import org.apache.log4j.Logger;
19import org.eclipse.emf.ecore.EObject;
20import org.eclipse.emf.ecore.util.EcoreUtil;
21import org.eclipse.viatra.dse.api.strategy.interfaces.IStrategy;
22import org.eclipse.viatra.dse.base.ThreadContext;
23import org.eclipse.viatra.dse.objectives.Fitness;
24import org.eclipse.viatra.dse.objectives.ObjectiveComparatorHelper;
25import org.eclipse.viatra.dse.solutionstore.SolutionStore;
26import org.eclipse.viatra.query.runtime.api.IPatternMatch;
27import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine;
28import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher;
29
30import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.app.Domain;
31import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.app.MetricDistanceGroup;
32import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.app.PartialInterpretationMetricDistance;
33import hu.bme.mit.inf.dslreasoner.logic.model.builder.DocumentationLevel;
34import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicReasoner;
35import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem;
36import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.InconsistencyResult;
37import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.LogicResult;
38import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.ModelResult;
39import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretation2logic.PartialInterpretation2Logic;
40import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation;
41import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.statecoder.NeighbourhoodBasedPartialInterpretationStateCoder;
42import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretationVisualisation;
43import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretationVisualiser;
44import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ModelGenerationMethod;
45import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.RealisticGuidance;
46import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ViatraReasonerConfiguration;
47import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace;
48
49public class HillClimbingOnRealisticMetricStrategyForModelGeneration implements IStrategy {
50 // Services and Configuration
51 private ThreadContext context;
52 private ReasonerWorkspace workspace;
53 private ViatraReasonerConfiguration configuration;
54 private ModelGenerationMethod method;
55 private PartialInterpretation2Logic partialInterpretation2Logic = new PartialInterpretation2Logic();
56 private Comparator<TrajectoryWithFitness> comparator;
57 private Logger logger = Logger.getLogger(IStrategy.class);
58 public NumericSolver numericSolver = null;
59
60 // Running
61 private PriorityQueue<TrajectoryWithFitness> trajectoiresToExplore;
62 private SolutionStore solutionStore;
63 private SolutionStoreWithCopy solutionStoreWithCopy;
64 private SolutionStoreWithDiversityDescriptor solutionStoreWithDiversityDescriptor;
65 private volatile boolean isInterrupted = false;
66 private ModelResult modelResultByInternalSolver = null;
67 private Random random = new Random();
68
69 // matchers for detecting the number of violations
70 private Collection<ViatraQueryMatcher<? extends IPatternMatch>> mustMatchers;
71 private Collection<ViatraQueryMatcher<? extends IPatternMatch>> mayMatchers;
72
73 // Encode the used activations of a particular state
74 private Map<Object, List<Object>> stateAndActivations;
75 private boolean allowMustViolation;
76 private Domain domain;
77 int targetSize;
78 public ActivationSelector activationSelector = new EvenActivationSelector(random);
79 // Statistics
80 private int numberOfStatecoderFail = 0;
81 private int numberOfPrintedModel = 0;
82 private int numberOfSolverCalls = 0;
83 private PartialInterpretationMetricDistance metricDistance;
84 private double currentStateValue = Double.MAX_VALUE;
85 private double currentNodeTypeDistance = 1;
86 private int numNodesToGenerate = 0;
87 public long explorationStarted = 0;
88 public long globalConstraintEvaluationTime = 0;
89 public long fitnessCalculationTime = 0;
90
91 public HillClimbingOnRealisticMetricStrategyForModelGeneration(
92 ReasonerWorkspace workspace,
93 ViatraReasonerConfiguration configuration,
94 ModelGenerationMethod method)
95 {
96 this.workspace = workspace;
97 this.configuration = configuration;
98 this.method = method;
99 }
100
101 public SolutionStoreWithCopy getSolutionStoreWithCopy() {
102 return solutionStoreWithCopy;
103 }
104 public SolutionStoreWithDiversityDescriptor getSolutionStoreWithDiversityDescriptor() {
105 return solutionStoreWithDiversityDescriptor;
106 }
107 public int getNumberOfStatecoderFail() {
108 return numberOfStatecoderFail;
109 }
110
111 public long getForwardTime() {
112 return context.getDesignSpaceManager().getForwardTime();
113 }
114 public long getBacktrackingTime() {
115 return context.getDesignSpaceManager().getBacktrackingTime();
116 }
117
118 @Override
119 public void initStrategy(ThreadContext context) {
120 this.context = context;
121 this.solutionStore = context.getGlobalContext().getSolutionStore();
122 domain = Domain.valueOf(configuration.domain);
123
124 ViatraQueryEngine engine = context.getQueryEngine();
125// // TODO: visualisation
126 mustMatchers = new LinkedList<ViatraQueryMatcher<? extends IPatternMatch>>();
127 mayMatchers = new LinkedList<ViatraQueryMatcher<? extends IPatternMatch>>();
128
129 // manully restict the number of super types of one class
130 this.method.getInvalidWF().forEach(a ->{
131 ViatraQueryMatcher<? extends IPatternMatch> matcher = a.getMatcher(engine);
132 mustMatchers.add(matcher);
133 });
134
135 this.method.getUnfinishedWF().forEach(a ->{
136 ViatraQueryMatcher<? extends IPatternMatch> matcher = a.getMatcher(engine);
137 mayMatchers.add(matcher);
138 });
139
140
141 //set up comparator
142 final ObjectiveComparatorHelper objectiveComparatorHelper = context.getObjectiveComparatorHelper();
143 this.comparator = new Comparator<TrajectoryWithFitness>() {
144 @Override
145 public int compare(TrajectoryWithFitness o1, TrajectoryWithFitness o2) {
146 return objectiveComparatorHelper.compare(o2.fitness, o1.fitness);
147 }
148 };
149
150 this.solutionStoreWithCopy = new SolutionStoreWithCopy();
151 this.solutionStoreWithDiversityDescriptor = new SolutionStoreWithDiversityDescriptor(configuration.diversityRequirement);
152
153 trajectoiresToExplore = new PriorityQueue<TrajectoryWithFitness>(11, comparator);
154 stateAndActivations = new HashMap<Object, List<Object>>();
155 metricDistance = new PartialInterpretationMetricDistance(domain);
156
157 //set whether allows must violations during the realistic generation
158 allowMustViolation = configuration.allowMustViolations;
159 targetSize = configuration.typeScopes.maxNewElements + 2;
160 //this.numericSolver = new NumericSolver(method, this.configuration.runIntermediateNumericalConsistencyChecks, false);
161 }
162
163 @Override
164 public void explore() {
165 this.explorationStarted=System.nanoTime();
166 if (!checkGlobalConstraints()) {
167 logger.info("Global contraint is not satisifed in the first state. Terminate.");
168 return;
169 }
170 if (configuration.searchSpaceConstraints.maxDepth == 0) {
171 logger.info("Maximal depth is reached in the initial solution. Terminate.");
172 return;
173 }
174
175 final Fitness firstFittness = context.calculateFitness();
176
177 //final ObjectiveComparatorHelper objectiveComparatorHelper = context.getObjectiveComparatorHelper();
178 final Object[] firstTrajectory = context.getTrajectory().toArray(new Object[0]);
179 TrajectoryWithFitness currentTrajectoryWithFittness = new TrajectoryWithFitness(firstTrajectory, firstFittness);
180 trajectoiresToExplore.add(currentTrajectoryWithFittness);
181 Object lastState = null;
182
183 //if(configuration)
184 visualiseCurrentState();
185 // the two is the True and False node generated at the beginning of the generation
186 int count = 0;
187 mainLoop: while (!isInterrupted && !configuration.progressMonitor.isCancelled()) {
188
189 if (currentTrajectoryWithFittness == null) {
190 if (trajectoiresToExplore.isEmpty()) {
191 logger.debug("State space is fully traversed.");
192 return;
193 } else {
194 currentTrajectoryWithFittness = selectState();
195 if (logger.isDebugEnabled()) {
196 logger.debug("Current trajectory: " + Arrays.toString(context.getTrajectory().toArray()));
197 logger.debug("New trajectory is chosen: " + currentTrajectoryWithFittness);
198 }
199 context.getDesignSpaceManager().executeTrajectoryWithMinimalBacktrackWithoutStateCoding(currentTrajectoryWithFittness.trajectory);
200
201 // reset the regression for this trajectory
202 metricDistance.getLinearModel().resetRegression(context.getCurrentStateId());
203 }
204 }
205
206 List<Object> activationIds = selectActivation();
207 PartialInterpretation model = (PartialInterpretation) context.getModel();
208 System.out.println(model.getNewElements().size());
209 System.out.println("# violations: " + getNumberOfViolations(mayMatchers));
210
211 Map<Object, Double> valueMap = new HashMap<Object,Double>();
212
213 //init epsilon and draw
214 MetricDistanceGroup heuristics = metricDistance.calculateMetricDistanceKS(model);
215
216 if(!stateAndActivations.containsKey(context.getCurrentStateId())) {
217 stateAndActivations.put(context.getCurrentStateId(), new ArrayList<Object>());
218 }
219
220 // calculate values for epsilon greedy
221 double epsilon = 1.0/count;
222 double draw = Math.random();
223 count++;
224 this.currentNodeTypeDistance = heuristics.getNodeTypeDistance();
225 numNodesToGenerate = model.getMaxNewElements();
226 System.out.println("NA distance: " + heuristics.getNADistance());
227 System.out.println("MPC distance: " + heuristics.getMPCDistance());
228 System.out.println("Out degree distance:" + heuristics.getOutDegreeDistance());
229 System.out.println("NodeType :" + currentNodeTypeDistance);
230
231
232 //TODO: the number of activations to be checked should be configurasble
233 if(activationIds.size() > 50) {
234 activationIds = activationIds.subList(0, 50);
235 }
236
237 valueMap = sortWithWeight(activationIds);
238 lastState = context.getCurrentStateId();
239 while (!isInterrupted && !configuration.progressMonitor.isCancelled() && activationIds.size() > 0) {
240 final Object nextActivation = drawWithEpsilonProbabilty(activationIds, valueMap, epsilon, draw);
241
242 stateAndActivations.get(context.getCurrentStateId()).add(nextActivation);
243 logger.debug("Executing new activation: " + nextActivation);
244 context.executeAcitvationId(nextActivation);
245 visualiseCurrentState();
246 boolean consistencyCheckResult = checkConsistency(currentTrajectoryWithFittness);
247 if(consistencyCheckResult == true) { continue mainLoop; }
248
249// if (context.isCurrentStateAlreadyTraversed()) {
250// logger.info("The new state is already visited.");
251// context.backtrack();
252// } else if (!checkGlobalConstraints()) {
253// logger.debug("Global contraint is not satisifed.");
254// context.backtrack();
255// }
256
257 int currentSize = model.getNewElements().size();
258 int targetDiff = targetSize - currentSize;
259 boolean shouldFinish = currentSize >= targetSize;
260
261 // does not allow must violations
262 if((getNumberOfViolations(mustMatchers) > 0|| getNumberOfViolations(mayMatchers) > targetDiff) && !allowMustViolation && !shouldFinish) {
263 context.backtrack();
264 }else {
265 final Fitness nextFitness = context.calculateFitness();
266
267 // the only hard objectives are configured in the config file
268 checkForSolution(nextFitness);
269
270 if (context.getDepth() > configuration.searchSpaceConstraints.maxDepth) {
271 logger.debug("Reached max depth.");
272 context.backtrack();
273 continue;
274 }
275
276 //Record value for current trajectory
277 TrajectoryWithFitness nextTrajectoryWithFittness = new TrajectoryWithFitness(
278 context.getTrajectory().toArray(), nextFitness);
279 int nodeSize = ((PartialInterpretation) context.getModel()).getNewElements().size();
280 int violation = getNumberOfViolations(mayMatchers);
281 double currentValue = calculateCurrentStateValue(nodeSize, violation);
282 metricDistance.getLinearModel().feedData(context.getCurrentStateId(), metricDistance.calculateFeature(nodeSize, violation), currentValue, lastState);
283 trajectoiresToExplore.add(nextTrajectoryWithFittness);
284 currentStateValue = currentValue;
285 //Currently, just go to the next state without considering the value of trajectory
286 currentTrajectoryWithFittness = nextTrajectoryWithFittness;
287 continue mainLoop;
288
289 }
290 }
291 logger.debug("State is fully traversed.");
292 trajectoiresToExplore.remove(currentTrajectoryWithFittness);
293 currentTrajectoryWithFittness = null;
294 context.backtrack();
295 }
296 logger.info("Interrupted.");
297 }
298
299 private boolean checkGlobalConstraints() {
300 long start = System.nanoTime();
301 boolean result = context.checkGlobalConstraints();
302 globalConstraintEvaluationTime += System.nanoTime() - start;
303 return result;
304 }
305
306 private Fitness calculateFitness() {
307 long start = System.nanoTime();
308 Fitness fitness = context.calculateFitness();
309 fitnessCalculationTime += System.nanoTime() - start;
310 return fitness;
311 }
312
313 /**
314 *
315 * @param activationIds
316 * @return: activation to value map
317 */
318 private Map<Object, Double> sortWithWeight(List<Object> activationIds){
319 Map<Object, Double> valueMap = new HashMap<Object, Double>();
320 Object currentId = context.getCurrentStateId();
321 // check for next states
322 for(Object id : activationIds) {
323 context.executeAcitvationId(id);
324 int violation = getNumberOfViolations(mayMatchers);
325
326 if(!allowMustViolation && getNumberOfViolations(mustMatchers) > 0) {
327 valueMap.put(id, Double.MAX_VALUE);
328 stateAndActivations.get(currentId).add(id);
329 }else {
330 valueMap.put(id, calculateFutureStateValue(violation));
331 }
332
333
334
335 context.backtrack();
336 }
337
338 //remove all the elements having large distance
339 Collections.sort(activationIds, Comparator.comparing(li -> valueMap.get(li)));
340 return valueMap;
341 }
342
343 private double calculateFutureStateValue(int violation) {
344 long start = System.nanoTime();
345 int nodeSize = ((PartialInterpretation) context.getModel()).getNewElements().size();
346 double currentValue = calculateCurrentStateValue(nodeSize,violation);
347 double[] toPredict = metricDistance.calculateFeature(100, violation);
348 if(Math.abs(currentValue - currentStateValue) < 0.001) {
349 this.method.getStatistics().addMetricCalculationTime(System.nanoTime() - start);
350 return Double.MAX_VALUE;
351 }
352 try {
353 this.method.getStatistics().addMetricCalculationTime(System.nanoTime() - start);
354 return metricDistance.getLinearModel().getPredictionForNextDataSample(metricDistance.calculateFeature(nodeSize, violation), currentValue, toPredict);
355 }catch(IllegalArgumentException e) {
356 this.method.getStatistics().addMetricCalculationTime(System.nanoTime() - start);
357 return currentValue;
358 }
359
360 }
361 private double calculateCurrentStateValue(int factor, int violation) {
362 PartialInterpretation model = (PartialInterpretation) context.getModel();
363 MetricDistanceGroup g = metricDistance.calculateMetricDistanceKS(model);
364 if(configuration.realisticGuidance == RealisticGuidance.MPC) {
365 return g.getMPCDistance();
366 }else if(configuration.realisticGuidance == RealisticGuidance.NodeActivity) {
367 return g.getNADistance();
368 }else if(configuration.realisticGuidance == RealisticGuidance.OutDegree) {
369 return g.getOutDegreeDistance();
370 }else if(configuration.realisticGuidance == RealisticGuidance.NodeType) {
371 return g.getNodeTypeDistance();
372 }else if(configuration.realisticGuidance == RealisticGuidance.Composite) {
373 double consistenceWeights = 5 * factor / (configuration.typeScopes.maxNewElements + 2) * (1- 1.0/(1+violation));
374 if(domain == Domain.Yakindumm) {
375 double unfinishFactor = 50 * (1 - (double)factor / targetSize);
376 double nodeTypeFactor = g.getNodeTypeDistance();
377 double normalFactor = 5;
378 if(currentNodeTypeDistance <= 0.05 || numNodesToGenerate == 1) {
379 nodeTypeFactor = 0;
380 normalFactor = 100;
381 unfinishFactor = 0;
382 }
383
384 return 100*(nodeTypeFactor) + normalFactor*(2*g.getNADistance() + g.getMPCDistance() + 2*g.getOutDegreeDistance()) + normalFactor / 5*consistenceWeights + unfinishFactor;
385 }else if (domain == Domain.Ecore) {
386 double unfinishFactor = 100 * (1 - (double)factor / targetSize);
387 double nodeTypeFactor = g.getNodeTypeDistance();
388 double normalFactor = 5;
389 if(currentNodeTypeDistance <= 0.12 || numNodesToGenerate == 1) {
390 nodeTypeFactor = 0;
391 normalFactor = 100;
392 unfinishFactor *= 0.5;
393 }
394
395 return 100*(nodeTypeFactor) + normalFactor*(2*g.getNADistance() + g.getMPCDistance() + 2*g.getOutDegreeDistance()) + normalFactor / 5*consistenceWeights + unfinishFactor;
396 }else {
397 double unfinishFactor = context.calculateFitness().get("CompositeUnfinishednessObjective");
398 double nodeTypeFactor = g.getNodeTypeDistance();
399 double normalFactor = 5;
400 if(currentNodeTypeDistance <= 0.05 || numNodesToGenerate == 1) {
401 nodeTypeFactor = 0;
402 normalFactor = 100;
403 //unfinishFactor *= 0.5;
404 }
405
406 return 100*(nodeTypeFactor) + normalFactor*(2*g.getNADistance() + 2*g.getMPCDistance() + 2*g.getOutDegreeDistance()) + normalFactor / 5*consistenceWeights + unfinishFactor;
407 }
408
409 }else if(configuration.realisticGuidance == RealisticGuidance.Composite_Without_Violations) {
410 if(domain == Domain.Yakindumm) {
411 double unfinishFactor = 50 * (1 - (double)factor / targetSize);
412 double nodeTypeFactor = g.getNodeTypeDistance();
413 double normalFactor = 5;
414 if(currentNodeTypeDistance <= 0.05 || numNodesToGenerate == 1) {
415 nodeTypeFactor = 0;
416 normalFactor = 100;
417 unfinishFactor = 0;
418 }
419
420 return 100*(nodeTypeFactor) + normalFactor*(2*g.getNADistance() + g.getMPCDistance() + 2*g.getOutDegreeDistance()) + unfinishFactor;
421 }else if (domain == Domain.Github) {
422 double unfinishFactor = 100 * (1 - (double)factor / targetSize);
423 double nodeTypeFactor = g.getNodeTypeDistance();
424 double normalFactor = 5;
425 if(currentNodeTypeDistance <= 0.12 || numNodesToGenerate == 1) {
426 nodeTypeFactor = 0;
427 normalFactor = 100;
428 unfinishFactor *= 0.5;
429 }
430
431 return 100*(nodeTypeFactor) + normalFactor*(2*g.getNADistance() + g.getMPCDistance() + 2*g.getOutDegreeDistance()) + unfinishFactor;
432 } else {
433 double unfinishFactor = 100 * (1 - (double)factor / targetSize);
434 double nodeTypeFactor = g.getNodeTypeDistance();
435 double normalFactor = 5;
436 if(currentNodeTypeDistance <= 0.20 || numNodesToGenerate == 1) {
437 nodeTypeFactor = 0;
438 normalFactor = 100;
439 unfinishFactor *= 0.5;
440 }
441
442 return 100*(nodeTypeFactor) + normalFactor*(2*g.getNADistance() + g.getMPCDistance() + 2*g.getOutDegreeDistance()) + unfinishFactor;
443 }
444 }else {
445 return violation;
446 }
447 }
448
449 private int getNumberOfViolations(Collection<ViatraQueryMatcher<? extends IPatternMatch>> matchers) {
450 int violations = matchers.stream().mapToInt(m -> m.countMatches()).sum();
451
452 return violations;
453 }
454
455 // Modified epsilon greedy choose for action based on value function
456 // with probability epsilon, choose the state with probability based on the weight
457 // with probability 1 - epsilon, choose the best state
458 // epsilon should decay w.r.t. time
459 private Object drawWithEpsilonProbabilty(List<Object> activationIds, Map<Object, Double> valueMap, double epsilon, double currentDraw) {
460 if(activationIds.size() <= 0) {
461 return null;
462 }
463
464 // if epsilon is smaller than current draw, return greedy choice
465 if(epsilon < currentDraw) {
466 return activationIds.remove(0);
467 }else {
468 //else return draw with probability of the weights
469 //find the sum of all 1-weights: the smaller the better
470 double sum = valueMap.values().stream().mapToDouble(d->1).sum();
471 double rand = Math.random() * sum;
472 double iterator = 0.0;
473 Object idToReturn = null;
474
475 // draw an item with probability
476 for(Object o : valueMap.keySet()) {
477 iterator += (1);
478 if(rand < iterator) {
479 idToReturn = o;
480 break;
481 }
482 }
483
484 //delete the item from the list
485 activationIds.remove(idToReturn);
486 valueMap.remove(idToReturn);
487 return idToReturn;
488 }
489 }
490
491 private List<Object> selectActivation() {
492 List<Object> activationIds;
493 try {
494 activationIds = this.activationSelector.randomizeActivationIDs(context.getUntraversedActivationIds());
495 } catch (NullPointerException e) {
496 numberOfStatecoderFail++;
497 activationIds = Collections.emptyList();
498 }
499 return activationIds;
500 }
501
502 private void checkForSolution(final Fitness fittness) {
503 if (fittness.isSatisifiesHardObjectives()) {
504 logger.debug("Solution Found!!");
505 logger.debug("# violations: " + (getNumberOfViolations(mustMatchers)));
506 if (solutionStoreWithDiversityDescriptor.isDifferent(context)) {
507 solutionStoreWithCopy.newSolution(context);
508 solutionStoreWithDiversityDescriptor.newSolution(context);
509 solutionStore.newSolution(context);
510 configuration.progressMonitor.workedModelFound(configuration.solutionScope.numberOfRequiredSolutions);
511
512 logger.debug("Found a solution.");
513 }
514 }
515 }
516
517 public List<String> times = new LinkedList<String>();
518 private void saveTimes() {
519 long forwardTime = context.getDesignSpaceManager().getForwardTime()/1000000;
520 long backtrackingTime = context.getDesignSpaceManager().getBacktrackingTime()/1000000;
521 long statecoderTime = ((NeighbourhoodBasedPartialInterpretationStateCoder)this.context.getStateCoder()).getStatecoderRuntime()/1000000;
522 long solutionCopy = solutionStoreWithCopy.getSumRuntime()/1000000;
523 long activationSelection = this.activationSelector.getRuntime()/1000000;
524// long numericalSolverSumTime = this.numericSolver.getRuntime()/1000000;
525// long numericalSolverProblemForming = this.numericSolver.getSolverSolvingProblem()/1000000;
526// long numericalSolverSolving = this.numericSolver.getSolverSolvingProblem()/1000000;
527// long numericalSolverInterpreting = this.numericSolver.getSolverSolution()/1000000;
528 long metricCalculationTime = this.method.getStatistics().metricCalculationTime / 1000000;
529 this.times.add(
530 "(TransformationExecutionTime"+method.getStatistics().transformationExecutionTime/1000000+
531 "|ForwardTime:"+forwardTime+
532 "|Backtrackingtime:"+backtrackingTime+
533 "|StateCoderTime:"+statecoderTime+
534 "|SolutionCopyTime:"+solutionCopy+
535 "|ActivationSelectionTime:"+activationSelection+
536 //"|NumericalSolverSumTime:"+numericalSolverSumTime+
537 //"|NumericalSolverProblemFormingTime:"+numericalSolverProblemForming+
538 //"|NumericalSolverSolvingTime:"+numericalSolverSolving+
539 //"|NumericalSolverInterpretingSolution:"+numericalSolverInterpreting+
540 "|MetricCalculationTime:"+metricCalculationTime + ")"
541 );
542
543 }
544
545 @Override
546 public void interruptStrategy() {
547 isInterrupted = true;
548 }
549
550
551 private TrajectoryWithFitness selectState() {
552 int randomNumber = random.nextInt(configuration.randomBacktrackChance);
553 if (randomNumber == 0) {
554 int elements = trajectoiresToExplore.size();
555 int randomElementIndex = random.nextInt(elements);
556 logger.debug("Randomly backtract to the " + randomElementIndex + " best solution...");
557 Iterator<TrajectoryWithFitness> iterator = trajectoiresToExplore.iterator();
558 while (randomElementIndex != 0) {
559 iterator.next();
560 randomElementIndex--;
561 }
562 TrajectoryWithFitness res = iterator.next();
563 if (res == null) {
564 return trajectoiresToExplore.element();
565 } else {
566 return res;
567 }
568 } else {
569 return trajectoiresToExplore.element();
570 }
571 }
572
573// private void logCurrentStateMetric() {
574// if(this.configuration.documentationLevel != DocumentationLevel.NONE || workspace == null) {
575// return;
576// }
577//
578// PartialInterpretation interpretation = (PartialInterpretation)context.getModel(); //pattern.get("interpretation");
579// PartialInterpretationMetric.calculateMetric(interpretation, "debug/metric/" + context.getModel().hashCode(), context.getCurrentStateId().toString());
580// }
581
582 public void visualiseCurrentState() {
583 PartialInterpretationVisualiser partialInterpretatioVisualiser = configuration.debugConfiguration.partialInterpretatioVisualiser;
584 if(partialInterpretatioVisualiser != null && this.configuration.documentationLevel == DocumentationLevel.FULL && workspace != null) {
585 PartialInterpretation p = (PartialInterpretation) (context.getModel());
586 int id = ++numberOfPrintedModel;
587 if (id % configuration.debugConfiguration.partalInterpretationVisualisationFrequency == 0) {
588 PartialInterpretationVisualisation visualisation = partialInterpretatioVisualiser.visualiseConcretization(p);
589 logger.debug("Visualizing state: " + id + " (" + context.getDesignSpaceManager().getCurrentState() + ")");
590 String name = String.format("state%09d", id);
591 visualisation.writeToFile(workspace, name + ".png");
592 workspace.writeModel((EObject) context.getModel(), name + ".xmi");
593 }
594 }
595 }
596
597 protected boolean checkConsistency(TrajectoryWithFitness t) {
598 LogicReasoner internalIncosnsitencyDetector = configuration.internalConsistencyCheckerConfiguration.internalIncosnsitencyDetector;
599 if (internalIncosnsitencyDetector!= null) {
600 int id = ++numberOfSolverCalls;
601 if (id % configuration.internalConsistencyCheckerConfiguration.incternalConsistencyCheckingFrequency == 0) {
602 try {
603 PartialInterpretation interpretation = (PartialInterpretation) (context.getModel());
604 PartialInterpretation copied = EcoreUtil.copy(interpretation);
605 this.partialInterpretation2Logic.transformPartialIntepretation2Logic(copied.getProblem(), copied);
606 LogicProblem newProblem = copied.getProblem();
607
608 this.configuration.typeScopes.maxNewElements = interpretation.getMaxNewElements();
609 this.configuration.typeScopes.minNewElements = interpretation.getMinNewElements();
610 LogicResult result = internalIncosnsitencyDetector.solve(newProblem, configuration, workspace);
611 if (result instanceof InconsistencyResult) {
612 logger.debug("Solver found an Inconsistency!");
613 removeSubtreeFromQueue(t);
614 return true;
615 } else if (result instanceof ModelResult) {
616 logger.debug("Solver found a model!");
617 solutionStore.newSolution(context);
618
619 this.modelResultByInternalSolver = (ModelResult) result;
620 return true;
621 } else {
622 logger.debug("Failed consistency check.");
623 return false;
624 }
625 } catch (Exception e) {
626 logger.debug("Problem with internal consistency checking: "+e.getMessage());
627 e.printStackTrace();
628 return false;
629 }
630 }
631
632 }
633 return false;
634 }
635
636 protected void removeSubtreeFromQueue(TrajectoryWithFitness t) {
637 PriorityQueue<TrajectoryWithFitness> previous = this.trajectoiresToExplore;
638 this.trajectoiresToExplore = new PriorityQueue<>(this.comparator);
639 for (TrajectoryWithFitness trajectoryWithFitness : previous) {
640 if (!containsAsSubstring(trajectoryWithFitness.trajectory, t.trajectory)) {
641 this.trajectoiresToExplore.add(trajectoryWithFitness);
642 } else {
643 logger.debug("State has been excluded due to inherent inconsistency");
644 }
645 }
646 }
647
648 private boolean containsAsSubstring(Object[] full, Object[] substring) {
649 if (substring.length > full.length) {
650 return false;
651 } else if (substring.length == full.length) {
652 return Arrays.equals(full, substring);
653 } else {
654 Object[] part = Arrays.copyOfRange(full, 0, substring.length);
655 return Arrays.equals(part, substring);
656 }
657 }
658}
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/SolutionCopier.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/SolutionCopier.xtend
index 33b69436..888eda18 100644
--- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/SolutionCopier.xtend
+++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/SolutionCopier.xtend
@@ -43,7 +43,7 @@ class SolutionCopier {
43 totalCopierRuntime += System.nanoTime - copyStart 43 totalCopierRuntime += System.nanoTime - copyStart
44 val copierRuntime = System.nanoTime - startTime 44 val copierRuntime = System.nanoTime - startTime
45 val copiedSolution = new CopiedSolution(copiedPartialInterpretation, copier, copierRuntime) 45 val copiedSolution = new CopiedSolution(copiedPartialInterpretation, copier, copierRuntime)
46 numericSolver?.fillSolutionCopy(copiedSolution.trace) 46 //numericSolver?.fillSolutionCopy(copiedSolution.trace)
47 copiedSolutions.put(solutionId, copiedSolution) 47 copiedSolutions.put(solutionId, copiedSolution)
48 } else { 48 } else {
49 existingCopy.current = true 49 existingCopy.current = true