diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf')
4 files changed, 373 insertions, 393 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 fd70688b..e00c864a 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 | |||
@@ -44,28 +44,17 @@ class ViatraReasoner extends LogicReasoner{ | |||
44 | val extension LogicresultFactory factory = LogicresultFactory.eINSTANCE | 44 | val extension LogicresultFactory factory = LogicresultFactory.eINSTANCE |
45 | val WF2ObjectiveConverter wf2ObjectiveConverter = new WF2ObjectiveConverter | 45 | val WF2ObjectiveConverter wf2ObjectiveConverter = new WF2ObjectiveConverter |
46 | 46 | ||
47 | val LogicReasoner inconsistencyDetector | ||
48 | |||
49 | public new() { | ||
50 | this.inconsistencyDetector=null | ||
51 | } | ||
52 | |||
53 | public new(LogicReasoner inconsistencyDetector) { | ||
54 | this.inconsistencyDetector = inconsistencyDetector | ||
55 | } | ||
56 | |||
57 | public static var Collection<? extends IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> allPatterns = null | ||
58 | //public static var List<SortedMap<String, Integer>> additionalMatches = null | ||
59 | 47 | ||
60 | override solve(LogicProblem problem, LogicSolverConfiguration configuration, ReasonerWorkspace workspace) throws LogicReasonerException { | 48 | override solve(LogicProblem problem, LogicSolverConfiguration configuration, ReasonerWorkspace workspace) throws LogicReasonerException { |
61 | val viatraConfig = configuration.asConfig | 49 | val viatraConfig = configuration.asConfig |
62 | val DesignSpaceExplorer dse = new DesignSpaceExplorer(); | ||
63 | 50 | ||
64 | /* | 51 | if(viatraConfig.debugCongiguration.logging) { |
65 | DesignSpaceExplorer.turnOnLogging(DseLoggingLevel.VERBOSE_FULL) | 52 | DesignSpaceExplorer.turnOnLogging(DseLoggingLevel.VERBOSE_FULL) |
66 | /*/ | 53 | } else { |
67 | DesignSpaceExplorer.turnOnLogging(DseLoggingLevel.WARN) | 54 | DesignSpaceExplorer.turnOnLogging(DseLoggingLevel.WARN) |
68 | //*/ | 55 | } |
56 | |||
57 | val DesignSpaceExplorer dse = new DesignSpaceExplorer(); | ||
69 | 58 | ||
70 | dse.addMetaModelPackage(LogiclanguagePackage.eINSTANCE) | 59 | dse.addMetaModelPackage(LogiclanguagePackage.eINSTANCE) |
71 | dse.addMetaModelPackage(LogicproblemPackage.eINSTANCE) | 60 | dse.addMetaModelPackage(LogicproblemPackage.eINSTANCE) |
@@ -86,7 +75,6 @@ class ViatraReasoner extends LogicReasoner{ | |||
86 | viatraConfig.nameNewElements, | 75 | viatraConfig.nameNewElements, |
87 | viatraConfig.typeInferenceMethod | 76 | viatraConfig.typeInferenceMethod |
88 | ) | 77 | ) |
89 | allPatterns = method.allPatterns | ||
90 | 78 | ||
91 | dse.addObjective(new ModelGenerationCompositeObjective( | 79 | dse.addObjective(new ModelGenerationCompositeObjective( |
92 | new ScopeObjective, | 80 | new ScopeObjective, |
@@ -95,7 +83,7 @@ class ViatraReasoner extends LogicReasoner{ | |||
95 | )) | 83 | )) |
96 | 84 | ||
97 | dse.addGlobalConstraint(wf2ObjectiveConverter.createInvalidationObjective(method.invalidWF)) | 85 | dse.addGlobalConstraint(wf2ObjectiveConverter.createInvalidationObjective(method.invalidWF)) |
98 | for(additionalConstraint : configuration.asConfig.additionalGlobalConstraints) { | 86 | for(additionalConstraint : viatraConfig.searchSpaceConstraints.additionalGlobalConstraints) { |
99 | dse.addGlobalConstraint(additionalConstraint.apply(method)) | 87 | dse.addGlobalConstraint(additionalConstraint.apply(method)) |
100 | } | 88 | } |
101 | 89 | ||
@@ -121,10 +109,7 @@ class ViatraReasoner extends LogicReasoner{ | |||
121 | } | 109 | } |
122 | 110 | ||
123 | val strategy = new BestFirstStrategyForModelGeneration( | 111 | val strategy = new BestFirstStrategyForModelGeneration( |
124 | workspace,inconsistencyDetector, | 112 | workspace,viatraConfig) |
125 | viatraConfig.inconsistencDetectorConfiguration, | ||
126 | viatraConfig.diversityRequirement/*, | ||
127 | method.allPatterns*/) | ||
128 | 113 | ||
129 | val transformationTime = System.nanoTime - transformationStartTime | 114 | val transformationTime = System.nanoTime - transformationStartTime |
130 | val solverStartTime = System.nanoTime | 115 | val solverStartTime = System.nanoTime |
@@ -162,7 +147,7 @@ class ViatraReasoner extends LogicReasoner{ | |||
162 | it.name = "StateCoderTime" it.value = (statecoderFinal.runtime/1000000) as int | 147 | it.name = "StateCoderTime" it.value = (statecoderFinal.runtime/1000000) as int |
163 | ] | 148 | ] |
164 | it.entries += createIntStatisticEntry => [ | 149 | it.entries += createIntStatisticEntry => [ |
165 | it.name = "StateCoderFailCount" it.value = strategy.numberOfStatecoderFaiL | 150 | it.name = "StateCoderFailCount" it.value = strategy.numberOfStatecoderFail |
166 | ] | 151 | ] |
167 | it.entries += createIntStatisticEntry => [ | 152 | it.entries += createIntStatisticEntry => [ |
168 | it.name = "SolutionCopyTime" it.value = (strategy.solutionStoreWithCopy.sumRuntime/1000000) as int | 153 | it.name = "SolutionCopyTime" it.value = (strategy.solutionStoreWithCopy.sumRuntime/1000000) as int |
@@ -204,10 +189,6 @@ class ViatraReasoner extends LogicReasoner{ | |||
204 | } | 189 | } |
205 | } | 190 | } |
206 | } | 191 | } |
207 | |||
208 | /*private def simpleWeithts(List<IObjective> objectives) { | ||
209 | objectives.map[1.0].toList | ||
210 | }*/ | ||
211 | 192 | ||
212 | private def dispatch long runtime(NeighbourhoodBasedStateCoderFactory sc) { | 193 | private def dispatch long runtime(NeighbourhoodBasedStateCoderFactory sc) { |
213 | sc.sumStatecoderRuntime | 194 | sc.sumStatecoderRuntime |
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 d638dd71..f32078a2 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 | |||
@@ -1,15 +1,17 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner | 1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner |
2 | 2 | ||
3 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicReasoner | ||
3 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicSolverConfiguration | 4 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicSolverConfiguration |
4 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration | 5 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration |
5 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDeclaration | 6 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDeclaration |
6 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.ModelGenerationMethod | ||
7 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.TypeInferenceMethod | 7 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.TypeInferenceMethod |
8 | import java.util.LinkedList | 8 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretationVisualiser |
9 | import java.util.List | ||
10 | import java.util.Set | 9 | import java.util.Set |
11 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PQuery | 10 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PQuery |
11 | import java.util.LinkedList | ||
12 | import java.util.List | ||
12 | import org.eclipse.xtext.xbase.lib.Functions.Function1 | 13 | import org.eclipse.xtext.xbase.lib.Functions.Function1 |
14 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.ModelGenerationMethod | ||
13 | 15 | ||
14 | public enum StateCoderStrategy { | 16 | public enum StateCoderStrategy { |
15 | Neighbourhood, NeighbourhoodWithEquivalence, IDBased, DefinedByDiversity | 17 | Neighbourhood, NeighbourhoodWithEquivalence, IDBased, DefinedByDiversity |
@@ -17,26 +19,59 @@ public enum StateCoderStrategy { | |||
17 | 19 | ||
18 | class ViatraReasonerConfiguration extends LogicSolverConfiguration{ | 20 | class ViatraReasonerConfiguration extends LogicSolverConfiguration{ |
19 | public var Iterable<PQuery> existingQueries | 21 | public var Iterable<PQuery> existingQueries |
20 | public var LogicSolverConfiguration inconsistencDetectorConfiguration = null | ||
21 | public var List<Function1<ModelGenerationMethod,ModelGenerationMethodBasedGlobalConstraint>> additionalGlobalConstraints = new LinkedList | ||
22 | int maxDepth = -1 | ||
23 | |||
24 | public var TypeInferenceMethod typeInferenceMethod = TypeInferenceMethod.Generic | ||
25 | 22 | ||
26 | public var nameNewElements = false | 23 | public var nameNewElements = false |
27 | public var StateCoderStrategy stateCoderStrategy = StateCoderStrategy.Neighbourhood | 24 | public var StateCoderStrategy stateCoderStrategy = StateCoderStrategy.Neighbourhood |
25 | public var TypeInferenceMethod typeInferenceMethod = TypeInferenceMethod.PreliminaryAnalysis | ||
26 | /** | ||
27 | * Once per 1/randomBacktrackChance the search selects a random state. | ||
28 | */ | ||
29 | public var int randomBacktrackChance = 20; | ||
28 | 30 | ||
29 | /** | 31 | /** |
30 | * Describes the required diversity between the solutions. | 32 | * Describes the required diversity between the solutions. |
31 | * Null means that the solutions have to have different state codes only. | 33 | * Null means that the solutions have to have different state codes only. |
32 | */ | 34 | */ |
33 | public var DiversityDescriptor diversityRequirement = null | 35 | public var DiversityDescriptor diversityRequirement = new DiversityDescriptor |
36 | /** | ||
37 | * A logic solver that able to check the consistency of an intermediate solution. | ||
38 | * Null means that no solver is called. | ||
39 | */ | ||
40 | public var InternalConsistencyCheckerConfiguration internalConsistencyCheckerConfiguration = new InternalConsistencyCheckerConfiguration | ||
41 | /** | ||
42 | * Configuration for debugging support. | ||
43 | */ | ||
44 | public var DebugConfiguration debugCongiguration = new DebugConfiguration | ||
45 | /** | ||
46 | * Configuration for cutting search space. | ||
47 | */ | ||
48 | public var SearchSpaceConstraint searchSpaceConstraints = new SearchSpaceConstraint | ||
34 | } | 49 | } |
35 | 50 | ||
36 | public class DiversityDescriptor { | 51 | public class DiversityDescriptor { |
37 | public var int range = -1 | 52 | public var ensureDiversity = false |
53 | public static val FixPointRange = -1 | ||
54 | public var int range = FixPointRange | ||
38 | public var int parallels = Integer.MAX_VALUE | 55 | public var int parallels = Integer.MAX_VALUE |
39 | public var int maxNumber = Integer.MAX_VALUE | 56 | public var int maxNumber = Integer.MAX_VALUE |
40 | public var Set<TypeDeclaration> relevantTypes | 57 | public var Set<TypeDeclaration> relevantTypes = null |
41 | public var Set<RelationDeclaration> relevantRelations | 58 | public var Set<RelationDeclaration> relevantRelations = null |
59 | } | ||
60 | |||
61 | public class DebugConfiguration { | ||
62 | public var logging = false | ||
63 | public var PartialInterpretationVisualiser partialInterpretatioVisualiser = null; | ||
64 | public var partalInterpretationVisualisationFrequency = 1 | ||
65 | } | ||
66 | |||
67 | public class InternalConsistencyCheckerConfiguration { | ||
68 | public var LogicReasoner internalIncosnsitencyDetector = null | ||
69 | public var LogicSolverConfiguration internalInconsistencDetectorConfiguration = null | ||
70 | public var incternalConsistencyCheckingFrequency = 1 | ||
71 | } | ||
72 | |||
73 | public class SearchSpaceConstraint { | ||
74 | public static val UNLIMITED_MAXDEPTH = Integer.MAX_VALUE | ||
75 | public var int maxDepth = UNLIMITED_MAXDEPTH | ||
76 | public var List<Function1<ModelGenerationMethod, ModelGenerationMethodBasedGlobalConstraint>> additionalGlobalConstraints = new LinkedList | ||
42 | } \ No newline at end of file | 77 | } \ No newline at end of file |
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 862ba245..346ed886 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 | |||
@@ -11,397 +11,339 @@ package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse; | |||
11 | 11 | ||
12 | import java.util.ArrayList; | 12 | import java.util.ArrayList; |
13 | import java.util.Arrays; | 13 | import java.util.Arrays; |
14 | import java.util.Collection; | ||
15 | import java.util.Collections; | 14 | import java.util.Collections; |
16 | import java.util.Comparator; | 15 | import java.util.Comparator; |
17 | import java.util.Iterator; | 16 | import java.util.Iterator; |
18 | import java.util.LinkedList; | ||
19 | import java.util.List; | 17 | import java.util.List; |
20 | import java.util.PriorityQueue; | 18 | import java.util.PriorityQueue; |
21 | import java.util.Random; | 19 | import java.util.Random; |
22 | import java.util.SortedMap; | ||
23 | import java.util.TreeMap; | ||
24 | import java.util.TreeSet; | ||
25 | 20 | ||
26 | import org.apache.log4j.Logger; | 21 | import org.apache.log4j.Logger; |
22 | import org.eclipse.emf.ecore.util.EcoreUtil; | ||
27 | import org.eclipse.viatra.dse.api.strategy.interfaces.IStrategy; | 23 | import org.eclipse.viatra.dse.api.strategy.interfaces.IStrategy; |
28 | import org.eclipse.viatra.dse.base.ThreadContext; | 24 | import org.eclipse.viatra.dse.base.ThreadContext; |
29 | import org.eclipse.viatra.dse.objectives.Fitness; | 25 | import org.eclipse.viatra.dse.objectives.Fitness; |
30 | import org.eclipse.viatra.dse.objectives.ObjectiveComparatorHelper; | 26 | import org.eclipse.viatra.dse.objectives.ObjectiveComparatorHelper; |
31 | import org.eclipse.viatra.dse.solutionstore.SolutionStore; | 27 | import org.eclipse.viatra.dse.solutionstore.SolutionStore; |
32 | import org.eclipse.viatra.query.runtime.api.IPatternMatch; | 28 | import org.eclipse.xtend.lib.annotations.AccessorType; |
33 | import org.eclipse.viatra.query.runtime.api.IQuerySpecification; | 29 | import org.eclipse.xtend.lib.annotations.Accessors; |
34 | import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher; | ||
35 | import org.eclipse.viatra.query.runtime.exception.ViatraQueryException; | ||
36 | 30 | ||
37 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicReasoner; | 31 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicReasoner; |
38 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicSolverConfiguration; | 32 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicSolverConfiguration; |
33 | import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem; | ||
34 | import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.InconsistencyResult; | ||
35 | import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.LogicResult; | ||
39 | import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.ModelResult; | 36 | import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.ModelResult; |
40 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretation2logic.PartialInterpretation2Logic; | 37 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretation2logic.PartialInterpretation2Logic; |
41 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation; | 38 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation; |
42 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretation2Gml; | 39 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretation2Gml; |
40 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretationVisualisation; | ||
41 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretationVisualiser; | ||
42 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.DebugConfiguration; | ||
43 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.DiversityDescriptor; | 43 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.DiversityDescriptor; |
44 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.ViatraReasonerConfiguration; | ||
44 | import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace; | 45 | import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace; |
45 | 46 | ||
46 | /** | 47 | /** |
47 | * This exploration strategy eventually explorers the whole design space but goes in the most promising directions | 48 | * This exploration strategy eventually explorers the whole design space but |
48 | * first, based on the {@link Fitness}. | 49 | * goes in the most promising directions first, based on the {@link Fitness}. |
49 | * | 50 | * |
50 | * There are a few parameter to tune such as | 51 | * There are a few parameter to tune such as |
51 | * <ul> | 52 | * <ul> |
52 | * <li>maximum depth</li> | 53 | * <li>maximum depth</li> |
53 | * <li>continue the exploration from a state that satisfies the hard objectives (the default that it will | 54 | * <li>continue the exploration from a state that satisfies the hard objectives |
54 | * backtrack),</li> | 55 | * (the default that it will backtrack),</li> |
55 | * <li>whether to continue the exploration from the newly explored state if it is at least equally good than the | 56 | * <li>whether to continue the exploration from the newly explored state if it |
56 | * previous one or only if it is better (default is "at least equally good").</li> | 57 | * is at least equally good than the previous one or only if it is better |
58 | * (default is "at least equally good").</li> | ||
57 | * </ul> | 59 | * </ul> |
58 | * | 60 | * |
59 | * @author Andras Szabolcs Nagy | ||
60 | * | ||
61 | */ | 61 | */ |
62 | public class BestFirstStrategyForModelGeneration implements IStrategy { | 62 | public class BestFirstStrategyForModelGeneration implements IStrategy { |
63 | 63 | ||
64 | private ThreadContext context; | 64 | // Services and Configuration |
65 | private SolutionStore solutionStore; | 65 | private ThreadContext context; |
66 | private SolutionStoreWithCopy solutionStoreWithCopy; | 66 | private ReasonerWorkspace workspace; |
67 | private SolutionStoreWithDiversityDescriptor solutionStoreWithDiversityDescriptor; | 67 | private ViatraReasonerConfiguration configuration; |
68 | private int numberOfStatecoderFail=0; | 68 | private PartialInterpretation2Logic partialInterpretation2Logic = new PartialInterpretation2Logic(); |
69 | //Collection<? extends IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> additionalPatterns = null; | 69 | private Comparator<TrajectoryWithFitness> comparator; |
70 | //List<ViatraQueryMatcher<? extends IPatternMatch>> additionalMatchers = new LinkedList<ViatraQueryMatcher<? extends IPatternMatch>>(); | 70 | private Logger logger = Logger.getLogger(IStrategy.class); |
71 | 71 | ||
72 | private int maxDepth = Integer.MAX_VALUE; | 72 | // Running |
73 | private boolean isInterrupted = false; | 73 | private PriorityQueue<TrajectoryWithFitness> trajectoiresToExplore; |
74 | //private boolean backTrackIfSolution = true; | 74 | private SolutionStore solutionStore; |
75 | private boolean onlyBetterFirst = false; | 75 | private SolutionStoreWithCopy solutionStoreWithCopy; |
76 | 76 | private SolutionStoreWithDiversityDescriptor solutionStoreWithDiversityDescriptor; | |
77 | 77 | private boolean isInterrupted = false; | |
78 | private PriorityQueue<TrajectoryWithFitness> trajectoiresToExplore; | 78 | private ModelResult modelResultByInternalSolver = null; |
79 | private Logger logger = Logger.getLogger(IStrategy.class); | 79 | private Random random = new Random(); |
80 | 80 | ||
81 | private static class TrajectoryWithFitness { | 81 | // Statistics |
82 | 82 | private int numberOfStatecoderFail = 0; | |
83 | public Object[] trajectory; | 83 | private int numberOfPrintedModel = 0; |
84 | public Fitness fitness; | 84 | private int numberOfSolverCalls = 0; |
85 | 85 | ||
86 | public TrajectoryWithFitness(Object[] trajectory, Fitness fitness) { | 86 | public BestFirstStrategyForModelGeneration( |
87 | super(); | 87 | ReasonerWorkspace workspace, |
88 | this.trajectory = trajectory; | 88 | ViatraReasonerConfiguration configuration) |
89 | this.fitness = fitness; | 89 | { |
90 | } | 90 | this.workspace = workspace; |
91 | 91 | this.configuration = configuration; | |
92 | @Override | 92 | } |
93 | public String toString() { | 93 | |
94 | return Arrays.toString(trajectory) + fitness.toString(); | 94 | public SolutionStoreWithCopy getSolutionStoreWithCopy() { |
95 | } | 95 | return solutionStoreWithCopy; |
96 | 96 | } | |
97 | } | 97 | public SolutionStoreWithDiversityDescriptor getSolutionStoreWithDiversityDescriptor() { |
98 | 98 | return solutionStoreWithDiversityDescriptor; | |
99 | public BestFirstStrategyForModelGeneration( | 99 | } |
100 | ReasonerWorkspace workspace, LogicReasoner reasoner, LogicSolverConfiguration configuration, DiversityDescriptor descriptor/*, | 100 | public int getNumberOfStatecoderFail() { |
101 | Collection<? extends IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> additionalPatterns*/) { | 101 | return numberOfStatecoderFail; |
102 | this.maxDepth = Integer.MAX_VALUE; | 102 | } |
103 | this.workspace = workspace; | 103 | |
104 | this.reasoner = reasoner; | 104 | @Override |
105 | this.configuration = configuration; | 105 | public void initStrategy(ThreadContext context) { |
106 | this.solutionStoreWithDiversityDescriptor = new SolutionStoreWithDiversityDescriptor(descriptor); | 106 | this.context = context; |
107 | //this.additionalPatterns = additionalPatterns; | 107 | this.solutionStore = context.getGlobalContext().getSolutionStore(); |
108 | } | 108 | this.solutionStoreWithCopy = new SolutionStoreWithCopy(); |
109 | 109 | this.solutionStoreWithDiversityDescriptor = new SolutionStoreWithDiversityDescriptor(configuration.diversityRequirement); | |
110 | @Override | 110 | |
111 | public void initStrategy(ThreadContext context) { | 111 | final ObjectiveComparatorHelper objectiveComparatorHelper = context.getObjectiveComparatorHelper(); |
112 | this.context = context; | 112 | this.comparator = new Comparator<TrajectoryWithFitness>() { |
113 | this.solutionStore = context.getGlobalContext().getSolutionStore(); | 113 | @Override |
114 | this.solutionStoreWithCopy = new SolutionStoreWithCopy(); | 114 | public int compare(TrajectoryWithFitness o1, TrajectoryWithFitness o2) { |
115 | 115 | return objectiveComparatorHelper.compare(o2.fitness, o1.fitness); | |
116 | |||
117 | final ObjectiveComparatorHelper objectiveComparatorHelper = context.getObjectiveComparatorHelper(); | ||
118 | |||
119 | Comparator<TrajectoryWithFitness> comparator = new Comparator<BestFirstStrategyForModelGeneration.TrajectoryWithFitness>() { | ||
120 | |||
121 | @Override | ||
122 | public int compare(TrajectoryWithFitness o1, TrajectoryWithFitness o2) { | ||
123 | return objectiveComparatorHelper.compare(o2.fitness, o1.fitness); | ||
124 | } | ||
125 | }; | ||
126 | |||
127 | |||
128 | |||
129 | trajectoiresToExplore = new PriorityQueue<TrajectoryWithFitness>(11, comparator); | ||
130 | |||
131 | } | ||
132 | |||
133 | public SolutionStoreWithCopy getSolutionStoreWithCopy() { | ||
134 | return solutionStoreWithCopy; | ||
135 | } | 116 | } |
117 | }; | ||
118 | |||
119 | trajectoiresToExplore = new PriorityQueue<TrajectoryWithFitness>(11, comparator); | ||
120 | } | ||
121 | |||
122 | @Override | ||
123 | public void explore() { | ||
124 | if (!context.checkGlobalConstraints()) { | ||
125 | logger.info("Global contraint is not satisifed in the first state. Terminate."); | ||
126 | return; | ||
127 | } | ||
128 | if (configuration.searchSpaceConstraints.maxDepth == 0) { | ||
129 | logger.info("Maximal depth is reached in the initial solution. Terminate."); | ||
130 | return; | ||
131 | } | ||
136 | 132 | ||
137 | public SolutionStoreWithDiversityDescriptor getSolutionStoreWithDiversityDescriptor() { | 133 | final Fitness firstFittness = context.calculateFitness(); |
138 | return solutionStoreWithDiversityDescriptor; | 134 | checkForSolution(firstFittness); |
139 | } | ||
140 | 135 | ||
141 | public int getNumberOfStatecoderFaiL() { | 136 | final ObjectiveComparatorHelper objectiveComparatorHelper = context.getObjectiveComparatorHelper(); |
142 | return numberOfStatecoderFail; | 137 | final Object[] firstTrajectory = context.getTrajectory().toArray(new Object[0]); |
138 | TrajectoryWithFitness currentTrajectoryWithFittness = new TrajectoryWithFitness(firstTrajectory, firstFittness); | ||
139 | trajectoiresToExplore.add(currentTrajectoryWithFittness); | ||
140 | |||
141 | mainLoop: while (!isInterrupted) { | ||
142 | |||
143 | if (currentTrajectoryWithFittness == null) { | ||
144 | if (trajectoiresToExplore.isEmpty()) { | ||
145 | logger.debug("State space is fully traversed."); | ||
146 | return; | ||
147 | } else { | ||
148 | currentTrajectoryWithFittness = selectState(); | ||
149 | if (logger.isDebugEnabled()) { | ||
150 | logger.debug("Current trajectory: " + Arrays.toString(context.getTrajectory().toArray())); | ||
151 | logger.debug("New trajectory is chosen: " + currentTrajectoryWithFittness); | ||
152 | } | ||
153 | context.getDesignSpaceManager().executeTrajectoryWithMinimalBacktrackWithoutStateCoding(currentTrajectoryWithFittness.trajectory); | ||
154 | } | ||
155 | } | ||
156 | |||
157 | // visualiseCurrentState(); | ||
158 | // boolean consistencyCheckResult = checkConsistency(currentTrajectoryWithFittness); | ||
159 | // if(consistencyCheckResult == true) { | ||
160 | // continue mainLoop; | ||
161 | // } | ||
162 | |||
163 | List<Object> activationIds = selectActivation(); | ||
164 | Iterator<Object> iterator = activationIds.iterator(); | ||
165 | |||
166 | while (!isInterrupted && iterator.hasNext()) { | ||
167 | final Object nextActivation = iterator.next(); | ||
168 | // if (!iterator.hasNext()) { | ||
169 | // logger.debug("Last untraversed activation of the state."); | ||
170 | // trajectoiresToExplore.remove(currentTrajectoryWithFittness); | ||
171 | // } | ||
172 | logger.debug("Executing new activation: " + nextActivation); | ||
173 | context.executeAcitvationId(nextActivation); | ||
174 | |||
175 | visualiseCurrentState(); | ||
176 | boolean consistencyCheckResult = checkConsistency(currentTrajectoryWithFittness); | ||
177 | if(consistencyCheckResult == true) { continue mainLoop; } | ||
178 | |||
179 | if (context.isCurrentStateAlreadyTraversed()) { | ||
180 | logger.info("The new state is already visited."); | ||
181 | context.backtrack(); | ||
182 | } else if (!context.checkGlobalConstraints()) { | ||
183 | logger.debug("Global contraint is not satisifed."); | ||
184 | context.backtrack(); | ||
185 | } else { | ||
186 | final Fitness nextFitness = context.calculateFitness(); | ||
187 | checkForSolution(nextFitness); | ||
188 | if (context.getDepth() > configuration.searchSpaceConstraints.maxDepth) { | ||
189 | logger.debug("Reached max depth."); | ||
190 | context.backtrack(); | ||
191 | continue; | ||
192 | } | ||
193 | |||
194 | TrajectoryWithFitness nextTrajectoryWithFittness = new TrajectoryWithFitness( | ||
195 | context.getTrajectory().toArray(), nextFitness); | ||
196 | trajectoiresToExplore.add(nextTrajectoryWithFittness); | ||
197 | |||
198 | int compare = objectiveComparatorHelper.compare(currentTrajectoryWithFittness.fitness, | ||
199 | nextTrajectoryWithFittness.fitness); | ||
200 | if (compare < 0) { | ||
201 | logger.debug("Better fitness, moving on: " + nextFitness); | ||
202 | currentTrajectoryWithFittness = nextTrajectoryWithFittness; | ||
203 | continue mainLoop; | ||
204 | } else if (compare == 0) { | ||
205 | logger.debug("Equally good fitness, moving on: " + nextFitness); | ||
206 | currentTrajectoryWithFittness = nextTrajectoryWithFittness; | ||
207 | continue mainLoop; | ||
208 | } else { | ||
209 | logger.debug("Worse fitness."); | ||
210 | currentTrajectoryWithFittness = null; | ||
211 | continue mainLoop; | ||
212 | } | ||
213 | } | ||
143 | } | 214 | } |
144 | 215 | ||
145 | @Override | 216 | logger.debug("State is fully traversed."); |
146 | public void explore() { | 217 | trajectoiresToExplore.remove(currentTrajectoryWithFittness); |
147 | 218 | currentTrajectoryWithFittness = null; | |
148 | /*if(this.additionalPatterns!=null) { | 219 | |
149 | for (IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> additionalPattern : additionalPatterns) { | 220 | } |
221 | logger.info("Interrupted."); | ||
222 | } | ||
223 | |||
224 | private List<Object> selectActivation() { | ||
225 | List<Object> activationIds; | ||
226 | try { | ||
227 | activationIds = new ArrayList<Object>(context.getUntraversedActivationIds()); | ||
228 | Collections.shuffle(activationIds); | ||
229 | } catch (NullPointerException e) { | ||
230 | numberOfStatecoderFail++; | ||
231 | activationIds = Collections.emptyList(); | ||
232 | } | ||
233 | return activationIds; | ||
234 | } | ||
235 | |||
236 | private void checkForSolution(final Fitness fittness) { | ||
237 | if (fittness.isSatisifiesHardObjectives()) { | ||
238 | if (solutionStoreWithDiversityDescriptor.isDifferent(context)) { | ||
239 | solutionStoreWithCopy.newSolution(context); | ||
240 | solutionStoreWithDiversityDescriptor.newSolution(context); | ||
241 | solutionStore.newSolution(context); | ||
242 | |||
243 | logger.debug("Found a solution."); | ||
244 | } | ||
245 | } | ||
246 | } | ||
247 | |||
248 | @Override | ||
249 | public void interruptStrategy() { | ||
250 | isInterrupted = true; | ||
251 | } | ||
252 | |||
253 | |||
254 | private TrajectoryWithFitness selectState() { | ||
255 | int randomNumber = random.nextInt(configuration.randomBacktrackChance); | ||
256 | if (randomNumber == 0) { | ||
257 | int elements = trajectoiresToExplore.size(); | ||
258 | int randomElementIndex = random.nextInt(elements); | ||
259 | logger.debug("Randomly backtract to the " + randomElementIndex + " best solution..."); | ||
260 | Iterator<TrajectoryWithFitness> iterator = trajectoiresToExplore.iterator(); | ||
261 | while (randomElementIndex != 0) { | ||
262 | iterator.next(); | ||
263 | randomElementIndex--; | ||
264 | } | ||
265 | TrajectoryWithFitness res = iterator.next(); | ||
266 | if (res == null) { | ||
267 | return trajectoiresToExplore.element(); | ||
268 | } else { | ||
269 | return res; | ||
270 | } | ||
271 | } else { | ||
272 | return trajectoiresToExplore.element(); | ||
273 | } | ||
274 | } | ||
275 | |||
276 | public void visualiseCurrentState() { | ||
277 | PartialInterpretationVisualiser partialInterpretatioVisualiser = configuration.debugCongiguration.partialInterpretatioVisualiser; | ||
278 | if(partialInterpretatioVisualiser != null) { | ||
279 | PartialInterpretation p = (PartialInterpretation) (context.getModel()); | ||
280 | int id = ++numberOfPrintedModel; | ||
281 | if (id % configuration.debugCongiguration.partalInterpretationVisualisationFrequency == 0) { | ||
282 | PartialInterpretationVisualisation visualisation = partialInterpretatioVisualiser.visualiseConcretization(p); | ||
283 | visualisation.writeToFile(workspace, String.format("state%09d", id)); | ||
284 | } | ||
285 | } | ||
286 | } | ||
287 | |||
288 | protected boolean checkConsistency(TrajectoryWithFitness t) { | ||
289 | LogicReasoner internalIncosnsitencyDetector = configuration.internalConsistencyCheckerConfiguration.internalIncosnsitencyDetector; | ||
290 | if (internalIncosnsitencyDetector!= null) { | ||
291 | int id = ++numberOfSolverCalls; | ||
292 | if (id % configuration.internalConsistencyCheckerConfiguration.incternalConsistencyCheckingFrequency == 0) { | ||
150 | try { | 293 | try { |
151 | this.additionalMatchers.add(additionalPattern.getMatcher(context.getQueryEngine())); | 294 | PartialInterpretation interpretation = (PartialInterpretation) (context.getModel()); |
152 | } catch (ViatraQueryException e) { | 295 | PartialInterpretation copied = EcoreUtil.copy(interpretation); |
153 | // TODO Auto-generated catch block | 296 | this.partialInterpretation2Logic.transformPartialIntepretation2Logic(copied.getProblem(), copied); |
297 | LogicProblem newProblem = copied.getProblem(); | ||
298 | |||
299 | this.configuration.typeScopes.maxNewElements = interpretation.getMaxNewElements(); | ||
300 | this.configuration.typeScopes.minNewElements = interpretation.getMinNewElements(); | ||
301 | LogicResult result = internalIncosnsitencyDetector.solve(newProblem, configuration, workspace); | ||
302 | if (result instanceof InconsistencyResult) { | ||
303 | logger.debug("Solver found an Inconsistency!"); | ||
304 | removeSubtreeFromQueue(t); | ||
305 | return true; | ||
306 | } else if (result instanceof ModelResult) { | ||
307 | logger.debug("Solver found a model!"); | ||
308 | solutionStore.newSolution(context); | ||
309 | |||
310 | this.modelResultByInternalSolver = (ModelResult) result; | ||
311 | return true; | ||
312 | } else { | ||
313 | logger.debug("Failed consistency check."); | ||
314 | return false; | ||
315 | } | ||
316 | } catch (Exception e) { | ||
317 | logger.debug("Problem with internal consistency checking: "+e.getMessage()); | ||
154 | e.printStackTrace(); | 318 | e.printStackTrace(); |
319 | return false; | ||
155 | } | 320 | } |
156 | } | 321 | } |
157 | }*/ | 322 | |
158 | 323 | } | |
159 | final ObjectiveComparatorHelper objectiveComparatorHelper = context.getObjectiveComparatorHelper(); | 324 | return false; |
160 | 325 | } | |
161 | boolean globalConstraintsAreSatisfied = context.checkGlobalConstraints(); | 326 | |
162 | if (!globalConstraintsAreSatisfied) { | 327 | protected void removeSubtreeFromQueue(TrajectoryWithFitness t) { |
163 | logger.info("Global contraint is not satisifed in the first state. Terminate."); | 328 | PriorityQueue<TrajectoryWithFitness> previous = this.trajectoiresToExplore; |
164 | return; | 329 | this.trajectoiresToExplore = new PriorityQueue<>(this.comparator); |
165 | } | 330 | for (TrajectoryWithFitness trajectoryWithFitness : previous) { |
166 | 331 | if (!containsAsSubstring(trajectoryWithFitness.trajectory, t.trajectory)) { | |
167 | final Fitness firstFittness = context.calculateFitness(); | 332 | this.trajectoiresToExplore.add(trajectoryWithFitness); |
168 | if (firstFittness.isSatisifiesHardObjectives()) { | 333 | } else { |
169 | context.newSolution(); | 334 | logger.debug("State has been excluded due to inherent inconsistency"); |
170 | logger.info("First state is a solution. Terminate."); | 335 | } |
171 | return; | 336 | } |
172 | } | 337 | } |
173 | 338 | ||
174 | if (maxDepth == 0) { | 339 | private boolean containsAsSubstring(Object[] full, Object[] substring) { |
175 | return; | 340 | if (substring.length > full.length) { |
176 | } | 341 | return false; |
177 | 342 | } else if (substring.length == full.length) { | |
178 | final Object[] firstTrajectory = context.getTrajectory().toArray(new Object[0]); | 343 | return Arrays.equals(full, substring); |
179 | TrajectoryWithFitness currentTrajectoryWithFittness = new TrajectoryWithFitness(firstTrajectory, firstFittness); | 344 | } else { |
180 | trajectoiresToExplore.add(currentTrajectoryWithFittness); | 345 | Object[] part = Arrays.copyOfRange(full, 0, substring.length); |
181 | 346 | return Arrays.equals(part, substring); | |
182 | mainLoop: while (!isInterrupted) { | 347 | } |
183 | 348 | } | |
184 | if (currentTrajectoryWithFittness == null) { | ||
185 | if (trajectoiresToExplore.isEmpty()) { | ||
186 | logger.debug("State space is fully traversed."); | ||
187 | return; | ||
188 | } else { | ||
189 | currentTrajectoryWithFittness = selectRandomState();// trajectoiresToExplore.element(); | ||
190 | if (logger.isDebugEnabled()) { | ||
191 | logger.debug("Current trajectory: " + Arrays.toString(context.getTrajectory().toArray())); | ||
192 | logger.debug("New trajectory is chosen: " + currentTrajectoryWithFittness); | ||
193 | } | ||
194 | // context.backtrackUntilRoot(); | ||
195 | // context.executeTrajectoryWithoutStateCoding(currentTrajectoryWithFittness.trajectory); | ||
196 | context.getDesignSpaceManager().executeTrajectoryWithMinimalBacktrackWithoutStateCoding(currentTrajectoryWithFittness.trajectory); | ||
197 | } | ||
198 | |||
199 | } | ||
200 | |||
201 | List<Object> activationIds; | ||
202 | try { | ||
203 | activationIds = new ArrayList<Object>(context.getUntraversedActivationIds()); | ||
204 | Collections.shuffle(activationIds); | ||
205 | } catch (NullPointerException e) { | ||
206 | numberOfStatecoderFail++; | ||
207 | activationIds = Collections.emptyList(); | ||
208 | } | ||
209 | |||
210 | Iterator<Object> iterator = activationIds.iterator(); | ||
211 | |||
212 | // writeCurrentState(); | ||
213 | // boolean consistencyCheckResult = checkConsistency(currentTrajectoryWithFittness); | ||
214 | // if(consistencyCheckResult == true) { | ||
215 | // continue mainLoop; | ||
216 | // } | ||
217 | |||
218 | while (!isInterrupted && iterator.hasNext()) { | ||
219 | final Object nextActivation = iterator.next(); | ||
220 | if (!iterator.hasNext()) { | ||
221 | logger.debug("Last untraversed activation of the state."); | ||
222 | trajectoiresToExplore.remove(currentTrajectoryWithFittness); | ||
223 | } | ||
224 | |||
225 | if (logger.isDebugEnabled()) { | ||
226 | logger.debug("Executing new activation: " + nextActivation); | ||
227 | } | ||
228 | context.executeAcitvationId(nextActivation); | ||
229 | |||
230 | // writeCurrentState(); | ||
231 | |||
232 | if (context.isCurrentStateAlreadyTraversed()) { | ||
233 | logger.info("The new state is already visited."); | ||
234 | context.backtrack(); | ||
235 | } else if (!context.checkGlobalConstraints()) { | ||
236 | logger.debug("Global contraint is not satisifed."); | ||
237 | context.backtrack(); | ||
238 | } else { | ||
239 | final Fitness nextFitness = context.calculateFitness(); | ||
240 | if (nextFitness.isSatisifiesHardObjectives()) { | ||
241 | if(solutionStoreWithDiversityDescriptor.isDifferent(context)) { | ||
242 | /*SortedMap<String, Integer> x = new TreeMap<String, Integer>(); | ||
243 | for(ViatraQueryMatcher<? extends IPatternMatch> additioanMatcher : this.additionalMatchers) { | ||
244 | x.put(additioanMatcher.getPatternName(),additioanMatcher.countMatches()); | ||
245 | }*/ | ||
246 | |||
247 | solutionStoreWithCopy.newSolution(context/*,x*/); | ||
248 | solutionStoreWithDiversityDescriptor.newSolution(context); | ||
249 | solutionStore.newSolution(context); | ||
250 | |||
251 | logger.debug("Found a solution."); | ||
252 | } | ||
253 | } | ||
254 | if (context.getDepth() > maxDepth) { | ||
255 | logger.debug("Reached max depth."); | ||
256 | context.backtrack(); | ||
257 | continue; | ||
258 | } | ||
259 | |||
260 | TrajectoryWithFitness nextTrajectoryWithFittness = new TrajectoryWithFitness( | ||
261 | context.getTrajectory().toArray(), nextFitness); | ||
262 | trajectoiresToExplore.add(nextTrajectoryWithFittness); | ||
263 | |||
264 | int compare = objectiveComparatorHelper.compare(currentTrajectoryWithFittness.fitness, | ||
265 | nextTrajectoryWithFittness.fitness); | ||
266 | if (compare < 0) { | ||
267 | logger.debug("Better fitness, moving on: " + nextFitness); | ||
268 | currentTrajectoryWithFittness = nextTrajectoryWithFittness; | ||
269 | continue mainLoop; | ||
270 | } else if (compare == 0) { | ||
271 | if (onlyBetterFirst) { | ||
272 | logger.debug("Equally good fitness, backtrack: " + nextFitness); | ||
273 | context.backtrack(); | ||
274 | continue; | ||
275 | } else { | ||
276 | logger.debug("Equally good fitness, moving on: " + nextFitness); | ||
277 | currentTrajectoryWithFittness = nextTrajectoryWithFittness; | ||
278 | continue mainLoop; | ||
279 | } | ||
280 | } else { | ||
281 | logger.debug("Worse fitness."); | ||
282 | // context.backtrack(); | ||
283 | currentTrajectoryWithFittness = null; | ||
284 | continue mainLoop; | ||
285 | } | ||
286 | } | ||
287 | } | ||
288 | |||
289 | logger.debug("State is fully traversed."); | ||
290 | trajectoiresToExplore.remove(currentTrajectoryWithFittness); | ||
291 | currentTrajectoryWithFittness = null; | ||
292 | |||
293 | } | ||
294 | logger.info("Interrupted."); | ||
295 | } | ||
296 | |||
297 | @Override | ||
298 | public void interruptStrategy() { | ||
299 | isInterrupted = true; | ||
300 | } | ||
301 | |||
302 | Random random = new Random(); | ||
303 | private TrajectoryWithFitness selectRandomState() { | ||
304 | int randomNumber = random.nextInt(100); | ||
305 | if(randomNumber < 5) { | ||
306 | int elements = trajectoiresToExplore.size(); | ||
307 | int randomElementIndex = random.nextInt(elements); | ||
308 | logger.debug("Randomly backtract to the " + randomElementIndex + " best solution..."); | ||
309 | Iterator<TrajectoryWithFitness> iterator = trajectoiresToExplore.iterator(); | ||
310 | while(randomElementIndex!=0) { | ||
311 | iterator.next(); | ||
312 | randomElementIndex--; | ||
313 | } | ||
314 | TrajectoryWithFitness res = iterator.next(); | ||
315 | if(res == null) { | ||
316 | return trajectoiresToExplore.element(); | ||
317 | } else { | ||
318 | return res; | ||
319 | } | ||
320 | } else { | ||
321 | return trajectoiresToExplore.element(); | ||
322 | } | ||
323 | } | ||
324 | |||
325 | private PartialInterpretation2Logic partialInterpretation2Logic = new PartialInterpretation2Logic(); | ||
326 | private LogicReasoner reasoner; | ||
327 | private PartialInterpretation2Gml partialInterpretation2Gml = new PartialInterpretation2Gml(); | ||
328 | private ReasonerWorkspace workspace; | ||
329 | private LogicSolverConfiguration configuration; | ||
330 | int numberOfPrintedModel = 0; | ||
331 | public ModelResult modelResultByTheSolver = null; | ||
332 | public void writeCurrentState() { | ||
333 | PartialInterpretation p = (PartialInterpretation)(context.getModel()); | ||
334 | int id= ++numberOfPrintedModel; | ||
335 | if(id%100 == 1) { | ||
336 | String text = this.partialInterpretation2Gml.transform(p); | ||
337 | this.workspace.writeText(id+".gml", text); | ||
338 | this.workspace.writeModel(p, id+".partialinterpretation"); | ||
339 | logger.debug("State "+id+" is saved."); | ||
340 | } | ||
341 | } | ||
342 | |||
343 | // int numberOfSolverCalls = 0; | ||
344 | // | ||
345 | // protected boolean checkConsistency(TrajectoryWithFitness t) { | ||
346 | // if (reasoner != null) { | ||
347 | // int id = ++numberOfSolverCalls; | ||
348 | // if (id % 100 == 1) { | ||
349 | // try { | ||
350 | // PartialInterpretation interpretation = (PartialInterpretation) (context.getModel()); | ||
351 | // PartialInterpretation copied = EcoreUtil.copy(interpretation); | ||
352 | // this.partialInterpretation2Logic.transformPartialIntepretation2Logic(copied.getProblem(), copied); | ||
353 | // LogicProblem newProblem = copied.getProblem(); | ||
354 | // | ||
355 | // this.configuration.typeScopes.maxNewElements = interpretation.getMaxNewElements(); | ||
356 | // this.configuration.typeScopes.minNewElements = interpretation.getMinNewElements(); | ||
357 | // LogicResult result = reasoner.solve(newProblem, configuration, workspace); | ||
358 | // if (result instanceof InconsistencyResult) { | ||
359 | // logger.debug("Solver found an Inconsistency!"); | ||
360 | // removeSubtreeFromQueue(t); | ||
361 | // return true; | ||
362 | // } else if (result instanceof ModelResult) { | ||
363 | // logger.debug("Solver found a model!"); | ||
364 | // solutionStore.newSolution(context); | ||
365 | |||
366 | // modelResultByTheSolver = (ModelResult) result; | ||
367 | // return true; | ||
368 | // } else { | ||
369 | // logger.debug("Failed consistency check."); | ||
370 | // return false; | ||
371 | // } | ||
372 | // } catch (Exception e) { | ||
373 | // // TODO Auto-generated catch block | ||
374 | // e.printStackTrace(); | ||
375 | // return false; | ||
376 | // } | ||
377 | // } | ||
378 | // | ||
379 | // } | ||
380 | // return false; | ||
381 | // } | ||
382 | // | ||
383 | // protected void removeSubtreeFromQueue(TrajectoryWithFitness t) { | ||
384 | // PriorityQueue<TrajectoryWithFitness> previous = this.trajectoiresToExplore; | ||
385 | // this.trajectoiresToExplore = new PriorityQueue<>(this.comparator); | ||
386 | // for (TrajectoryWithFitness trajectoryWithFitness : previous) { | ||
387 | // if(! containsAsSubstring(trajectoryWithFitness.trajectory,t.trajectory)) { | ||
388 | // this.trajectoiresToExplore.add(trajectoryWithFitness); | ||
389 | // } else { | ||
390 | // logger.debug("State has been excluded due to inherent inconsistency"); | ||
391 | // } | ||
392 | // } | ||
393 | // } | ||
394 | // | ||
395 | // private boolean containsAsSubstring(Object[] full, Object[] substring) { | ||
396 | // if(substring.length > full.length) { | ||
397 | // return false; | ||
398 | // } else if(substring.length == full.length) { | ||
399 | // return Arrays.equals(full, substring); | ||
400 | // } else { | ||
401 | // Object[] part = Arrays.copyOfRange(full, 0, substring.length); | ||
402 | // return Arrays.equals(part, substring); | ||
403 | // } | ||
404 | // } | ||
405 | // | ||
406 | |||
407 | } | 349 | } |
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/TrajectoryWithFitness.java b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/TrajectoryWithFitness.java new file mode 100644 index 00000000..a7a6ab36 --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/TrajectoryWithFitness.java | |||
@@ -0,0 +1,22 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse; | ||
2 | |||
3 | import java.util.Arrays; | ||
4 | |||
5 | import org.eclipse.viatra.dse.objectives.Fitness; | ||
6 | |||
7 | class TrajectoryWithFitness { | ||
8 | |||
9 | public Object[] trajectory; | ||
10 | public Fitness fitness; | ||
11 | |||
12 | public TrajectoryWithFitness(Object[] trajectory, Fitness fitness) { | ||
13 | super(); | ||
14 | this.trajectory = trajectory; | ||
15 | this.fitness = fitness; | ||
16 | } | ||
17 | |||
18 | @Override | ||
19 | public String toString() { | ||
20 | return Arrays.toString(trajectory) + fitness.toString(); | ||
21 | } | ||
22 | } \ No newline at end of file | ||