1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner
import com.google.common.collect.ImmutableList
import com.google.common.collect.Lists
import hu.bme.mit.inf.dslreasoner.logic.model.builder.DocumentationLevel
import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicReasoner
import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicReasonerException
import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicSolverConfiguration
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.LogiclanguagePackage
import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem
import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicproblemPackage
import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.LogicresultFactory
import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.ModelResult
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.ModelGenerationMethodProvider
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.ScopePropagatorStrategy
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.PartialInterpretationInitialiser
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialinterpretationPackage
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.statecoder.AbstractNeighbourhoodBasedStateCoderFactory
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.statecoder.IdentifierBasedStateCoderFactory
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.statecoder.NeighbourhoodBasedHashStateCoderFactory
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.statecoder.PairwiseNeighbourhoodBasedStateCoderFactory
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.BasicScopeGlobalConstraint
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.BestFirstStrategyForModelGeneration
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.DiversityChecker
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.InconsistentScopeGlobalConstraint
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.LoggerSolutionFoundHandler
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.ModelGenerationCompositeObjective
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.NumericSolver
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.PartialModelAsLogicInterpretation
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.PunishSizeObjective
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.ScopeObjective
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.SurelyViolatedObjectiveGlobalConstraint
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.UnfinishedMultiplicityObjective
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.ViatraReasonerSolutionSaver
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.WF2ObjectiveConverter
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.ObjectiveKind
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.ThreeValuedCostElement
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.ThreeValuedCostObjective
import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace
import java.util.List
import java.util.Map
import org.eclipse.emf.ecore.EObject
import org.eclipse.viatra.dse.api.DesignSpaceExplorer
import org.eclipse.viatra.dse.api.DesignSpaceExplorer.DseLoggingLevel
import org.eclipse.viatra.dse.solutionstore.SolutionStore
import org.eclipse.viatra.dse.statecode.IStateCoderFactory
class ViatraReasoner extends LogicReasoner {
val PartialInterpretationInitialiser initialiser = new PartialInterpretationInitialiser()
val ModelGenerationMethodProvider modelGenerationMethodProvider = new ModelGenerationMethodProvider
val extension LogicresultFactory factory = LogicresultFactory.eINSTANCE
val WF2ObjectiveConverter wf2ObjectiveConverter = new WF2ObjectiveConverter
override solve(LogicProblem problem, LogicSolverConfiguration configuration,
ReasonerWorkspace workspace) throws LogicReasonerException {
val viatraConfig = configuration.asConfig
if (viatraConfig.documentationLevel == DocumentationLevel.FULL) {
DesignSpaceExplorer.turnOnLogging(DseLoggingLevel.VERBOSE_FULL)
} else {
DesignSpaceExplorer.turnOnLogging(DseLoggingLevel.WARN)
}
val DesignSpaceExplorer dse = new DesignSpaceExplorer();
dse.addMetaModelPackage(LogiclanguagePackage.eINSTANCE)
dse.addMetaModelPackage(LogicproblemPackage.eINSTANCE)
dse.addMetaModelPackage(PartialinterpretationPackage.eINSTANCE)
val transformationStartTime = System.nanoTime
val emptySolution = initialiser.initialisePartialInterpretation(problem, viatraConfig.typeScopes).output
if ((viatraConfig.documentationLevel == DocumentationLevel::FULL ||
viatraConfig.documentationLevel == DocumentationLevel::NORMAL) && workspace !== null) {
workspace.writeModel(emptySolution, "init.partialmodel")
}
emptySolution.problemConainer = problem
var BasicScopeGlobalConstraint basicScopeGlobalConstraint = null
if (viatraConfig.scopePropagatorStrategy == ScopePropagatorStrategy.None) {
basicScopeGlobalConstraint = new BasicScopeGlobalConstraint(emptySolution)
emptySolution.scopes.clear
}
val method = modelGenerationMethodProvider.createModelGenerationMethod(
problem,
emptySolution,
workspace,
viatraConfig.nameNewElements,
viatraConfig.typeInferenceMethod,
viatraConfig.calculateObjectCreationCosts,
viatraConfig.scopePropagatorStrategy,
viatraConfig.hints,
viatraConfig.unitPropagationPatternGenerators,
viatraConfig.documentationLevel
)
val compositeObjective = new ModelGenerationCompositeObjective(
basicScopeGlobalConstraint ?: new ScopeObjective,
method.unfinishedMultiplicities.map[new UnfinishedMultiplicityObjective(it)],
wf2ObjectiveConverter.createCompletenessObjective(method.unfinishedWF),
viatraConfig
)
dse.addObjective(compositeObjective)
if (viatraConfig.punishSize != PunishSizeStrategy.NONE) {
val punishSizeStrategy = switch (viatraConfig.punishSize) {
case SMALLER_IS_BETTER: ObjectiveKind.LOWER_IS_BETTER
case LARGER_IS_BETTER: ObjectiveKind.HIGHER_IS_BETTER
default: throw new IllegalArgumentException("Unknown PunishSizeStrategy: " + viatraConfig.punishSize)
}
val punishObjective = new PunishSizeObjective(punishSizeStrategy, compositeObjective.level + 1)
dse.addObjective(punishObjective)
}
val extremalObjectives = Lists.newArrayListWithExpectedSize(viatraConfig.costObjectives.size)
for (entry : viatraConfig.costObjectives.indexed) {
val objectiveName = '''costObjective«entry.key»'''
val objectiveConfig = entry.value
val elementsBuilder = ImmutableList.builder
for (elementConfig : objectiveConfig.elements) {
val relationName = elementConfig.patternQualifiedName
val modalQueries = method.modalRelationQueries.get(relationName)
if (modalQueries === null) {
throw new IllegalArgumentException("Unknown relation: " + relationName)
}
elementsBuilder.add(new ThreeValuedCostElement(
modalQueries.currentQuery,
modalQueries.mayQuery,
modalQueries.mustQuery,
elementConfig.weight
))
}
val costElements = elementsBuilder.build
val costObjective = new ThreeValuedCostObjective(objectiveName, costElements, objectiveConfig.kind,
objectiveConfig.threshold, 3)
dse.addObjective(costObjective)
if (objectiveConfig.findExtremum) {
extremalObjectives += costObjective
}
}
val numberOfRequiredSolutions = configuration.solutionScope.numberOfRequiredSolutions
val solutionStore = if (extremalObjectives.empty) {
new SolutionStore(numberOfRequiredSolutions)
} else {
new SolutionStore()
}
solutionStore.registerSolutionFoundHandler(new LoggerSolutionFoundHandler(viatraConfig))
val diversityChecker = DiversityChecker.of(viatraConfig.diversityRequirement)
val numericSolver = new NumericSolver(method, viatraConfig.runIntermediateNumericalConsistencyChecks, false)
val solutionSaver = new ViatraReasonerSolutionSaver(newArrayList(extremalObjectives), numberOfRequiredSolutions,
diversityChecker, numericSolver)
val solutionCopier = solutionSaver.solutionCopier
solutionStore.withSolutionSaver(solutionSaver)
dse.solutionStore = solutionStore
dse.addGlobalConstraint(wf2ObjectiveConverter.createInvalidationGlobalConstraint(method.invalidWF))
dse.addGlobalConstraint(new SurelyViolatedObjectiveGlobalConstraint(solutionSaver))
dse.addGlobalConstraint(new InconsistentScopeGlobalConstraint)
if (basicScopeGlobalConstraint !== null) {
dse.addGlobalConstraint(basicScopeGlobalConstraint)
}
for (additionalConstraint : viatraConfig.searchSpaceConstraints.additionalGlobalConstraints) {
dse.addGlobalConstraint(additionalConstraint.apply(method))
}
dse.setInitialModel(emptySolution, false)
val IStateCoderFactory statecoder = switch (viatraConfig.stateCoderStrategy) {
case Neighbourhood:
new NeighbourhoodBasedHashStateCoderFactory
case PairwiseNeighbourhood:
new PairwiseNeighbourhoodBasedStateCoderFactory
default:
new IdentifierBasedStateCoderFactory
}
dse.stateCoderFactory = statecoder
dse.maxNumberOfThreads = 1
for (rule : method.relationRefinementRules) {
dse.addTransformationRule(rule)
}
for (rule : method.objectRefinementRules) {
dse.addTransformationRule(rule)
}
val strategy = new BestFirstStrategyForModelGeneration(workspace, viatraConfig, method, solutionSaver, numericSolver)
viatraConfig.progressMonitor.workedForwardTransformation
val transformationFinished = System.nanoTime
val transformationTime = transformationFinished - transformationStartTime
val solverStartTime = System.nanoTime
var boolean stoppedByTimeout
try {
stoppedByTimeout = dse.startExplorationWithTimeout(strategy, configuration.runtimeLimit * 1000);
} catch (NullPointerException npe) {
stoppedByTimeout = false
}
val solverTime = System.nanoTime - solverStartTime
viatraConfig.progressMonitor.workedSearchFinished
// additionalMatches = strategy.solutionStoreWithCopy.additionalMatches
val statistics = createStatistics => [
// it.solverTime = viatraConfig.runtimeLimit
it.solverTime = (solverTime / 1000000) as int
it.transformationTime = (transformationTime / 1000000) as int
for (pair : solutionCopier.getAllCopierRuntimes(true).indexed) {
it.entries += createIntStatisticEntry => [
it.name = '''Solution«pair.key»FoundAt'''
it.value = (pair.value / 1000000) as int
]
}
for(x: 0..<strategy.times.size) {
it.entries += createStringStatisticEntry => [
it.name = '''Solution«x+1»DetailedStatistics'''
it.value = strategy.times.get(x)
]
}
it.entries += createIntStatisticEntry => [
it.name = "ExplorationInitializationTime" it.value = ((strategy.explorationStarted-transformationFinished)/1000000) as int
]
it.entries += createIntStatisticEntry => [
it.name = "TransformationExecutionTime"
it.value = (method.statistics.transformationExecutionTime / 1000000) as int
]
it.entries += createIntStatisticEntry => [
it.name = "ScopePropagationTime"
it.value = (method.statistics.scopePropagationTime / 1000000) as int
]
it.entries += createIntStatisticEntry => [
it.name = "TypeAnalysisTime"
it.value = (method.statistics.preliminaryTypeAnalisisTime / 1000000) as int
]
it.entries += createIntStatisticEntry => [
it.name = "StateCoderTime"
it.value = (statecoder.runtime / 1000000) as int
]
it.entries += createIntStatisticEntry => [
it.name = "StateCoderFailCount"
it.value = strategy.numberOfStatecoderFail
]
it.entries += createIntStatisticEntry => [
it.name = "SolutionCopyTime"
it.value = (solutionCopier.getTotalCopierRuntime / 1000000) as int
]
it.entries += createIntStatisticEntry => [
it.name = "States"
it.value = dse.numberOfStates as int
]
it.entries += createIntStatisticEntry => [
it.name = "ForwardTime" it.value = (strategy.forwardTime/1000000) as int
]
it.entries += createIntStatisticEntry => [
it.name = "BacktrackingTime" it.value = (strategy.backtrackingTime/1000000) as int
]
it.entries += createIntStatisticEntry => [
it.name = "GlobalConstraintEvaluationTime" it.value = (strategy.globalConstraintEvaluationTime/1000000) as int
]
it.entries += createIntStatisticEntry => [
it.name = "FitnessCalculationTime" it.value = (strategy.fitnessCalculationTime/1000000) as int
]
it.entries += createIntStatisticEntry => [
it.name = "SolutionCopyTime" it.value = (solutionSaver.totalCopierRuntime/1000000) as int
]
it.entries += createIntStatisticEntry => [
it.name = "ActivationSelectionTime" it.value = (strategy.activationSelector.runtime/1000000) as int
]
it.entries += createIntStatisticEntry => [
it.name = "Decisions"
it.value = method.statistics.decisionsTried
]
it.entries += createIntStatisticEntry => [
it.name = "Transformations"
it.value = method.statistics.transformationInvocations
]
it.entries += createIntStatisticEntry => [
it.name = "ScopePropagations"
it.value = method.statistics.scopePropagatorInvocations
]
it.entries += createIntStatisticEntry => [
it.name = "ScopePropagationsSolverCalls"
it.value = method.statistics.scopePropagatorSolverInvocations
]
it.entries += createIntStatisticEntry => [
it.name = "NumericalSolverSumTime" it.value = (strategy.numericSolver.runtime/1000000) as int
]
it.entries += createIntStatisticEntry => [
it.name = "NumericalSolverProblemFormingTime" it.value = (strategy.numericSolver.solverFormingProblem/1000000) as int
]
it.entries += createIntStatisticEntry => [
it.name = "NumericalSolverSolvingTime" it.value = (strategy.numericSolver.solverSolvingProblem/1000000) as int
]
it.entries += createIntStatisticEntry => [
it.name = "NumericalSolverInterpretingSolution" it.value = (strategy.numericSolver.solverSolution/1000000) as int
]
it.entries += createIntStatisticEntry => [
it.name = "NumericalSolverCachingTime" it.value = (strategy.numericSolver.cachingTime/1000000) as int
]
it.entries += createIntStatisticEntry => [
it.name = "NumericalSolverCallNumber" it.value = strategy.numericSolver.numberOfSolverCalls
]
it.entries += createIntStatisticEntry => [
it.name = "NumericalSolverCachedAnswerNumber" it.value = strategy.numericSolver.numberOfCachedSolverCalls
]
if(diversityChecker.active) {
it.entries += createIntStatisticEntry => [
it.name = "SolutionDiversityCheckTime"
it.value = (diversityChecker.totalRuntime / 1000000) as int
]
it.entries += createRealStatisticEntry => [
it.name = "SolutionDiversitySuccessRate"
it.value = diversityChecker.successRate
]
}
]
viatraConfig.progressMonitor.workedBackwardTransformationFinished
if (stoppedByTimeout) {
return createInsuficientResourcesResult => [
it.problem = problem
it.resourceName = "time"
it.representation += solutionCopier.getPartialInterpretations(true)
it.statistics = statistics
]
} else {
if (solutionStore.solutions.empty) {
return createInconsistencyResult => [
it.problem = problem
it.representation += solutionCopier.getPartialInterpretations(true)
it.statistics = statistics
]
} else {
return createModelResult => [
it.problem = problem
it.trace = solutionCopier.getTraces(true)
it.representation += solutionCopier.getPartialInterpretations(true)
it.statistics = statistics
]
}
}
}
private def dispatch long runtime(AbstractNeighbourhoodBasedStateCoderFactory sc) {
sc.sumStatecoderRuntime
}
private def dispatch long runtime(IdentifierBasedStateCoderFactory sc) {
sc.sumStatecoderRuntime
}
override getInterpretations(ModelResult modelResult) {
val indexes = 0 ..< modelResult.representation.size
val traces = modelResult.trace as List<Map<EObject, EObject>>;
val res = indexes.map [ i |
new PartialModelAsLogicInterpretation(modelResult.representation.get(i) as PartialInterpretation,
traces.get(i))
].toList
return res
}
private def ViatraReasonerConfiguration asConfig(LogicSolverConfiguration configuration) {
if (configuration instanceof ViatraReasonerConfiguration) {
return configuration
} else
throw new IllegalArgumentException('''Wrong configuration. Expected: «ViatraReasonerConfiguration.name», but got: «configuration.class.name»"''')
}
}
|