From fc505b6b171a2d54c3bad6078031b028b55131d3 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Sun, 14 Jul 2019 00:56:19 +0200 Subject: Polyhedron abstraction with Z3 for cardinality propagation --- .../META-INF/MANIFEST.MF | 4 +- .../ModelGenerationMethodProvider.xtend | 24 ++- .../logic2viatra/ScopePropagator.xtend | 156 ---------------- .../cardinality/PolyhedronScopePropagator.xtend | 153 +++++++++++++++ .../cardinality/PolyhedronSolver.xtend | 115 ++++++++++++ .../logic2viatra/cardinality/ScopePropagator.xtend | 149 +++++++++++++++ .../cardinality/Z3PolyhedronSolver.xtend | 206 +++++++++++++++++++++ .../rules/RefinementRuleProvider.xtend | 2 +- 8 files changed, 650 insertions(+), 159 deletions(-) delete mode 100644 Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/ScopePropagator.xtend create mode 100644 Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/PolyhedronScopePropagator.xtend create mode 100644 Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/PolyhedronSolver.xtend create mode 100644 Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/ScopePropagator.xtend create mode 100644 Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/Z3PolyhedronSolver.xtend (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra') diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/META-INF/MANIFEST.MF b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/META-INF/MANIFEST.MF index b2ee3981..37495e50 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/META-INF/MANIFEST.MF +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/META-INF/MANIFEST.MF @@ -4,6 +4,7 @@ Bundle-Name: Logic2viatra Bundle-SymbolicName: hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatraquery;singleton:=true Bundle-Version: 1.0.0.qualifier Export-Package: hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra, + hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality, hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.interval, hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.interval.aggregators, hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns, @@ -22,7 +23,8 @@ Require-Bundle: hu.bme.mit.inf.dslreasoner.logic.model;bundle-version="1.0.0", com.google.inject;bundle-version="3.0.0", org.eclipse.xtext;bundle-version="2.10.0", org.eclipse.viatra.transformation.runtime.emf;bundle-version="1.5.0", - org.eclipse.xtext.xbase;bundle-version="2.10.0" + org.eclipse.xtext.xbase;bundle-version="2.10.0", + com.microsoft.z3;bundle-version="4.8.5" Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Import-Package: org.apache.log4j Automatic-Module-Name: hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatraquery diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/ModelGenerationMethodProvider.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/ModelGenerationMethodProvider.xtend index b6918294..0040dbcd 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/ModelGenerationMethodProvider.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/ModelGenerationMethodProvider.xtend @@ -4,6 +4,11 @@ import com.google.common.collect.ImmutableMap import hu.bme.mit.inf.dslreasoner.logic.model.builder.DocumentationLevel import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem import hu.bme.mit.inf.dslreasoner.viatra2logic.viatra2logicannotations.TransfomedViatraQuery +import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.PolyhedronScopePropagator +import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.ScopePropagator +import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.ScopePropagatorStrategy +import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.Z3PolyhedronSolver +import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns.GeneratedPatterns import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns.ModalPatternQueries import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns.PatternProvider import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.rules.GoalConstraintProvider @@ -63,7 +68,7 @@ class ModelGenerationMethodProvider { ReasonerWorkspace workspace, boolean nameNewElements, TypeInferenceMethod typeInferenceMethod, - ScopePropagator scopePropagator, + ScopePropagatorStrategy scopePropagatorStrategy, DocumentationLevel debugLevel ) { val statistics = new ModelGenerationStatistics @@ -74,6 +79,8 @@ class ModelGenerationMethodProvider { val queries = patternProvider.generateQueries(logicProblem, emptySolution, statistics, existingQueries, workspace, typeInferenceMethod, writeFiles) + val scopePropagator = createScopePropagator(scopePropagatorStrategy, emptySolution, queries) + scopePropagator.propagateAllScopeConstraints val // LinkedHashMap, BatchTransformationRule>> objectRefinementRules = refinementRuleProvider.createObjectRefinementRules(queries, scopePropagator, nameNewElements, statistics) @@ -104,4 +111,19 @@ class ModelGenerationMethodProvider { queries.allQueries ) } + + private def createScopePropagator(ScopePropagatorStrategy scopePropagatorStrategy, + PartialInterpretation emptySolution, GeneratedPatterns queries) { + switch (scopePropagatorStrategy) { + case BasicTypeHierarchy: + new ScopePropagator(emptySolution) + case PolyhedralTypeHierarchy: { + val types = queries.refineObjectQueries.keySet.map[newType].toSet + val solver = new Z3PolyhedronSolver + new PolyhedronScopePropagator(emptySolution, types, solver) + } + default: + throw new IllegalArgumentException("Unknown scope propagator strategy: " + scopePropagatorStrategy) + } + } } diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/ScopePropagator.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/ScopePropagator.xtend deleted file mode 100644 index 38633c07..00000000 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/ScopePropagator.xtend +++ /dev/null @@ -1,156 +0,0 @@ -package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra - -import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation -import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialTypeInterpratation -import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.Scope -import java.util.HashMap -import java.util.Map -import java.util.Set -import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialComplexTypeInterpretation -import java.util.HashSet - -class ScopePropagator { - PartialInterpretation partialInterpretation - Map type2Scope - - val Map> superScopes - val Map> subScopes - - public new(PartialInterpretation p) { - partialInterpretation = p - type2Scope = new HashMap - for(scope : p.scopes) { - type2Scope.put(scope.targetTypeInterpretation,scope) - } - - superScopes = new HashMap - subScopes = new HashMap - for(scope : p.scopes) { - superScopes.put(scope,new HashSet) - subScopes.put(scope,new HashSet) - } - - for(scope : p.scopes) { - val target = scope.targetTypeInterpretation - if(target instanceof PartialComplexTypeInterpretation) { - val supertypeInterpretations = target.supertypeInterpretation - for(supertypeInterpretation : supertypeInterpretations) { - val supertypeScope = type2Scope.get(supertypeInterpretation) - superScopes.get(scope).add(supertypeScope) - subScopes.get(supertypeScope).add(scope) - } - } - } - } - - def public propagateAllScopeConstraints() { - var boolean hadChanged - do{ - hadChanged = false - for(superScopeEntry : superScopes.entrySet) { - val sub = superScopeEntry.key - hadChanged = propagateLowerLimitUp(sub,partialInterpretation) || hadChanged - hadChanged = propagateUpperLimitDown(sub,partialInterpretation) || hadChanged - for(sup: superScopeEntry.value) { - hadChanged = propagateLowerLimitUp(sub,sup) || hadChanged - hadChanged = propagateUpperLimitDown(sub,sup) || hadChanged - } - } - } while(hadChanged) -// println('''All constraints are propagated.''') - } - - def public propagateAdditionToType(PartialTypeInterpratation t) { -// println('''Adding to «(t as PartialComplexTypeInterpretation).interpretationOf.name»''') - val targetScope = type2Scope.get(t) - targetScope.removeOne - val sups = superScopes.get(targetScope) - sups.forEach[removeOne] - if(this.partialInterpretation.minNewElements > 0) { - this.partialInterpretation.minNewElements = this.partialInterpretation.minNewElements-1 - } - if(this.partialInterpretation.maxNewElements > 0) { - this.partialInterpretation.maxNewElements = this.partialInterpretation.maxNewElements-1 - } else if(this.partialInterpretation.maxNewElements === 0) { - throw new IllegalArgumentException('''Inconsistent object creation: lower node limit is 0!''') - } - -// subScopes.get(targetScope).forEach[propagateUpperLimitDown(it,targetScope)] -// for(sup: sups) { -// subScopes.get(sup).forEach[propagateUpperLimitDown(it,sup)] -// } -// for(scope : type2Scope.values) { -// propagateUpperLimitDown(scope,partialInterpretation) -// } - - propagateAllScopeConstraints - -// println('''Target Scope: «targetScope.minNewElements» - «targetScope.maxNewElements»''') -// println(''' «this.partialInterpretation.minNewElements» - «this.partialInterpretation.maxNewElements»''') -// this.partialInterpretation.scopes.forEach[println(''' «(it.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name»: «it.minNewElements»-«it.maxNewElements»''')] -// println('''All constraints are propagated upon increasing «(t as PartialComplexTypeInterpretation).interpretationOf.name»''') - } - - private def propagateLowerLimitUp(Scope subScope, Scope superScope) { - if(subScope.minNewElements>superScope.minNewElements) { -// println(''' -// «(subScope.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name» -> «(superScope.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name» -// superScope.minNewElements «superScope.minNewElements» = subScope.minNewElements «subScope.minNewElements» -// ''') - superScope.minNewElements = subScope.minNewElements - return true - } else { - return false - } - } - - private def propagateUpperLimitDown(Scope subScope, Scope superScope) { - if(superScope.maxNewElements>=0 && (superScope.maxNewElements «(superScope.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name» -// subScope.maxNewElements «subScope.maxNewElements» = superScope.maxNewElements «superScope.maxNewElements» -// ''') - subScope.maxNewElements = superScope.maxNewElements - return true - } else { - return false - } - } - - private def propagateLowerLimitUp(Scope subScope, PartialInterpretation p) { - if(subScope.minNewElements>p.minNewElements) { -// println(''' -// «(subScope.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name» -> nodes -// p.minNewElements «p.minNewElements» = subScope.minNewElements «subScope.minNewElements» -// ''') - p.minNewElements = subScope.minNewElements - return true - } else { - return false - } - } - - private def propagateUpperLimitDown(Scope subScope, PartialInterpretation p) { - if(p.maxNewElements>=0 && (p.maxNewElements nodes -// subScope.maxNewElements «subScope.maxNewElements» = p.maxNewElements «p.maxNewElements» -// ''') - subScope.maxNewElements = p.maxNewElements - return true - } else { - return false - } - } - private def removeOne(Scope scope) { - if(scope.maxNewElements===0) { - throw new IllegalArgumentException('''Inconsistent object creation: «scope.targetTypeInterpretation»''') - } else if(scope.maxNewElements>0) { - scope.maxNewElements= scope.maxNewElements-1 - } - if(scope.minNewElements>0) { - scope.minNewElements= scope.minNewElements-1 - } - } -} - \ No newline at end of file diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/PolyhedronScopePropagator.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/PolyhedronScopePropagator.xtend new file mode 100644 index 00000000..8f210ffb --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/PolyhedronScopePropagator.xtend @@ -0,0 +1,153 @@ +package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality + +import com.google.common.collect.ImmutableList +import com.google.common.collect.ImmutableMap +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type +import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialComplexTypeInterpretation +import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation +import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialPrimitiveInterpretation +import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.Scope +import java.util.ArrayDeque +import java.util.HashMap +import java.util.HashSet +import java.util.Map +import java.util.Set + +class PolyhedronScopePropagator extends ScopePropagator { + val Map scopeBounds + val LinearConstraint topLevelBounds + val PolyhedronSaturationOperator operator + + new(PartialInterpretation p, Set possibleNewDynamicTypes, PolyhedronSolver solver) { + super(p) + val instanceCounts = possibleNewDynamicTypes.toInvertedMap[new Dimension(name, 0, null)] + val primitiveDimensions = new HashMap + val constraintsBuilder = ImmutableList.builder + val scopeBoundsBuilder = ImmutableMap.builder + // Dimensions for instantiable types were created according to the type analysis, + // but for any possible primitive types, we create them on demand, + // as there is no Type directly associated with a PartialPrimitiveInterpretation. + for (scope : p.scopes) { + switch (targetTypeInterpretation : scope.targetTypeInterpretation) { + PartialPrimitiveInterpretation: { + val dimension = primitiveDimensions.computeIfAbsent(targetTypeInterpretation) [ interpretation | + new Dimension(interpretation.eClass.name, 0, null) + ] + scopeBoundsBuilder.put(scope, dimension) + } + PartialComplexTypeInterpretation: { + val complexType = targetTypeInterpretation.interpretationOf + val dimensions = findSubtypeDimensions(complexType, instanceCounts) + switch (dimensions.size) { + case 0: + if (scope.minNewElements > 0) { + throw new IllegalArgumentException("Found scope for " + complexType.name + + ", but the type cannot be instantiated") + } + case 1: + scopeBoundsBuilder.put(scope, dimensions.head) + default: { + val constraint = new LinearConstraint(dimensions.toInvertedMap[1], null, null) + constraintsBuilder.add(constraint) + scopeBoundsBuilder.put(scope, constraint) + } + } + } + default: + throw new IllegalArgumentException("Unknown PartialTypeInterpretation: " + targetTypeInterpretation) + } + } + val allDimensions = ImmutableList.builder.addAll(instanceCounts.values).addAll(primitiveDimensions.values).build + scopeBounds = scopeBoundsBuilder.build + topLevelBounds = new LinearConstraint(allDimensions.toInvertedMap[1], null, null) + constraintsBuilder.add(topLevelBounds) + val expressionsToSaturate = ImmutableList.builder.addAll(scopeBounds.values).add(topLevelBounds).build + val polyhedron = new Polyhedron(allDimensions, constraintsBuilder.build, expressionsToSaturate) + operator = solver.createSaturationOperator(polyhedron) + } + + private def findSubtypeDimensions(Type type, Map instanceCounts) { + val subtypes = new HashSet + val dimensions = new HashSet + val stack = new ArrayDeque + stack.addLast(type) + while (!stack.empty) { + val subtype = stack.removeLast + if (subtypes.add(subtype)) { + val dimension = instanceCounts.get(subtype) + if (dimension !== null) { + dimensions.add(dimension) + } + stack.addAll(subtype.subtypes) + } + } + dimensions + } + + override void propagateAllScopeConstraints() { + populatePolyhedronFromScope() + val result = operator.saturate() + if (result == PolyhedronSaturationResult.EMPTY) { + throw new IllegalStateException("Scope bounds cannot be satisfied") + } else { + populateScopesFromPolyhedron() + if (result != PolyhedronSaturationResult.SATURATED) { + super.propagateAllScopeConstraints() + } + } + } + + private def populatePolyhedronFromScope() { + topLevelBounds.lowerBound = partialInterpretation.minNewElements + if (partialInterpretation.maxNewElements >= 0) { + topLevelBounds.upperBound = partialInterpretation.maxNewElements + } + for (pair : scopeBounds.entrySet) { + val scope = pair.key + val bounds = pair.value + bounds.lowerBound = scope.minNewElements + if (scope.maxNewElements >= 0) { + bounds.upperBound = scope.maxNewElements + } + } + } + + private def populateScopesFromPolyhedron() { + checkFiniteBounds(topLevelBounds) + if (partialInterpretation.minNewElements > topLevelBounds.lowerBound) { + throw new IllegalArgumentException('''Lower bound of «topLevelBounds» smaller than top-level scope: «partialInterpretation.minNewElements»''') + } else if (partialInterpretation.minNewElements != topLevelBounds.lowerBound) { + partialInterpretation.minNewElements = topLevelBounds.lowerBound + } + if (partialInterpretation.maxNewElements >= 0 && + partialInterpretation.maxNewElements < topLevelBounds.upperBound) { + throw new IllegalArgumentException('''Upper bound of «topLevelBounds» larger than top-level scope: «partialInterpretation.maxNewElements»''') + } else if (partialInterpretation.maxNewElements != topLevelBounds.upperBound) { + partialInterpretation.maxNewElements = topLevelBounds.upperBound + } + for (pair : scopeBounds.entrySet) { + val scope = pair.key + val bounds = pair.value + checkFiniteBounds(bounds) + if (scope.minNewElements > bounds.lowerBound) { + throw new IllegalArgumentException('''Lower bound of «bounds» smaller than «scope.targetTypeInterpretation» scope: «scope.minNewElements»''') + } else if (scope.minNewElements != bounds.lowerBound) { + scope.minNewElements = bounds.lowerBound + } + if (scope.maxNewElements >= 0 && scope.maxNewElements < bounds.upperBound) { + throw new IllegalArgumentException('''Upper bound of «bounds» larger than «scope.targetTypeInterpretation» scope: «scope.maxNewElements»''') + } else if (scope.maxNewElements != bounds.upperBound) { + scope.maxNewElements = bounds.upperBound + } + } + } + + private def checkFiniteBounds(LinearBoundedExpression bounds) { + if (bounds.lowerBound === null) { + throw new IllegalArgumentException("Infinite lower bound: " + bounds) + } + if (bounds.upperBound === null) { + throw new IllegalArgumentException("Infinite upper bound: " + bounds) + } + } +} diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/PolyhedronSolver.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/PolyhedronSolver.xtend new file mode 100644 index 00000000..08bf25b9 --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/PolyhedronSolver.xtend @@ -0,0 +1,115 @@ +package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality + +import java.util.List +import java.util.Map +import org.eclipse.xtend.lib.annotations.Accessors +import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor + +interface PolyhedronSolver { + def PolyhedronSaturationOperator createSaturationOperator(Polyhedron polyhedron) +} + +enum PolyhedronSaturationResult { + SATURATED, + EMPTY, + UNKNOWN +} + +interface PolyhedronSaturationOperator extends AutoCloseable { + def Polyhedron getPolyhedron() + + def PolyhedronSaturationResult saturate() +} + +@FinalFieldsConstructor +@Accessors +class Polyhedron { + /** + * The list of dimensions (variables) for this polyhedron. + * + * This list must not be modified after the polyhedron was created. + * However, lower and upper bounds of the dimensions may be changed. + * + * Names of dimensions in this list are assumed to be unique. + */ + val List dimensions + + /** + * The list of constraints defining this polyhedron. + * + * The list and its elements may be freely modified. + */ + val List constraints + + /** + * The list of constraints that should be saturated (tightened) + * when a {@link PolyhedronSaturationOperator} is invoked. + * + * This list may be freely modified. + * + * Place all dimensions and constraints here to saturate all the bounds. + */ + val List expressionsToSaturate + + override toString() ''' + Dimensions: + «FOR dimension : dimensions» + «dimension» + «ENDFOR» + Constraints: + «FOR constraint : constraints» + «constraint» + «ENDFOR» +««« Saturate: +««« «FOR expression : expressionsToSaturate» +««« «IF expression instanceof Dimension»dimension«ELSEIF expression instanceof LinearConstraint»constraint«ELSE»unknown«ENDIF» «expression» +««« «ENDFOR» + ''' + +} + +@Accessors +abstract class LinearBoundedExpression { + var Integer lowerBound + var Integer upperBound +} + +@Accessors +class Dimension extends LinearBoundedExpression { + val String name + + @FinalFieldsConstructor + new() { + } + + new(String name, Integer lowerBound, Integer upperBound) { + this(name) + this.lowerBound = lowerBound + this.upperBound = upperBound + } + + override toString() { + '''«IF lowerBound !== null»«lowerBound» <= «ENDIF»«name»«IF upperBound !== null» <= «upperBound»«ENDIF»''' + } + +} + +@Accessors +class LinearConstraint extends LinearBoundedExpression { + val Map coefficients + + @FinalFieldsConstructor + new() { + } + + new(Map coefficients, Integer lowerBound, Integer upperBound) { + this(coefficients) + this.lowerBound = lowerBound + this.upperBound = upperBound + } + + override toString() { + '''«IF lowerBound !== null»«lowerBound» <= «ENDIF»«FOR pair : coefficients.entrySet SEPARATOR " + "»«IF pair.value != 1»«pair.value» * «ENDIF»«pair.key.name»«ENDFOR»«IF upperBound !== null» <= «upperBound»«ENDIF»''' + } + +} diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/ScopePropagator.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/ScopePropagator.xtend new file mode 100644 index 00000000..c8fb3409 --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/ScopePropagator.xtend @@ -0,0 +1,149 @@ +package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality + +import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialComplexTypeInterpretation +import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation +import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialTypeInterpratation +import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.Scope +import java.util.HashMap +import java.util.HashSet +import java.util.Map +import java.util.Set +import org.eclipse.xtend.lib.annotations.Accessors + +enum ScopePropagatorStrategy { + BasicTypeHierarchy, + PolyhedralTypeHierarchy +} + +class ScopePropagator { + @Accessors(PROTECTED_GETTER) PartialInterpretation partialInterpretation + Map type2Scope + + val Map> superScopes + val Map> subScopes + + new(PartialInterpretation p) { + partialInterpretation = p + type2Scope = new HashMap + for (scope : p.scopes) { + type2Scope.put(scope.targetTypeInterpretation, scope) + } + + superScopes = new HashMap + subScopes = new HashMap + for (scope : p.scopes) { + superScopes.put(scope, new HashSet) + subScopes.put(scope, new HashSet) + } + + for (scope : p.scopes) { + val target = scope.targetTypeInterpretation + if (target instanceof PartialComplexTypeInterpretation) { + val supertypeInterpretations = target.supertypeInterpretation + for (supertypeInterpretation : supertypeInterpretations) { + val supertypeScope = type2Scope.get(supertypeInterpretation) + superScopes.get(scope).add(supertypeScope) + subScopes.get(supertypeScope).add(scope) + } + } + } + } + + def propagateAllScopeConstraints() { + var boolean hadChanged + do { + hadChanged = false + for (superScopeEntry : superScopes.entrySet) { + val sub = superScopeEntry.key + hadChanged = propagateLowerLimitUp(sub, partialInterpretation) || hadChanged + hadChanged = propagateUpperLimitDown(sub, partialInterpretation) || hadChanged + for (sup : superScopeEntry.value) { + hadChanged = propagateLowerLimitUp(sub, sup) || hadChanged + hadChanged = propagateUpperLimitDown(sub, sup) || hadChanged + } + } + } while (hadChanged) + } + + def propagateAdditionToType(PartialTypeInterpratation t) { +// println('''Adding to «(t as PartialComplexTypeInterpretation).interpretationOf.name»''') + val targetScope = type2Scope.get(t) + targetScope.removeOne + val sups = superScopes.get(targetScope) + sups.forEach[removeOne] + if (this.partialInterpretation.minNewElements > 0) { + this.partialInterpretation.minNewElements = this.partialInterpretation.minNewElements - 1 + } + if (this.partialInterpretation.maxNewElements > 0) { + this.partialInterpretation.maxNewElements = this.partialInterpretation.maxNewElements - 1 + } else if (this.partialInterpretation.maxNewElements === 0) { + throw new IllegalArgumentException('''Inconsistent object creation: lower node limit is 0!''') + } + propagateAllScopeConstraints + +// println('''Target Scope: «targetScope.minNewElements» - «targetScope.maxNewElements»''') +// println(''' «this.partialInterpretation.minNewElements» - «this.partialInterpretation.maxNewElements»''') +// this.partialInterpretation.scopes.forEach[println(''' «(it.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name»: «it.minNewElements»-«it.maxNewElements»''')] +// println('''All constraints are propagated upon increasing «(t as PartialComplexTypeInterpretation).interpretationOf.name»''') + } + + private def propagateLowerLimitUp(Scope subScope, Scope superScope) { + if (subScope.minNewElements > superScope.minNewElements) { + superScope.minNewElements = subScope.minNewElements + return true + } else { + return false + } + } + + private def propagateUpperLimitDown(Scope subScope, Scope superScope) { + if (superScope.maxNewElements >= 0 && + (superScope.maxNewElements < subScope.maxNewElements || subScope.maxNewElements < 0)) { +// println(''' +// «(subScope.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name» -> «(superScope.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name» +// subScope.maxNewElements «subScope.maxNewElements» = superScope.maxNewElements «superScope.maxNewElements» +// ''') + subScope.maxNewElements = superScope.maxNewElements + return true + } else { + return false + } + } + + private def propagateLowerLimitUp(Scope subScope, PartialInterpretation p) { + if (subScope.minNewElements > p.minNewElements) { +// println(''' +// «(subScope.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name» -> nodes +// p.minNewElements «p.minNewElements» = subScope.minNewElements «subScope.minNewElements» +// ''') + p.minNewElements = subScope.minNewElements + return true + } else { + return false + } + } + + private def propagateUpperLimitDown(Scope subScope, PartialInterpretation p) { + if (p.maxNewElements >= 0 && (p.maxNewElements < subScope.maxNewElements || subScope.maxNewElements < 0)) { +// println(''' +// «(subScope.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name» -> nodes +// subScope.maxNewElements «subScope.maxNewElements» = p.maxNewElements «p.maxNewElements» +// ''') + subScope.maxNewElements = p.maxNewElements + return true + } else { + return false + } + } + + private def removeOne(Scope scope) { + if (scope.maxNewElements === 0) { + throw new IllegalArgumentException('''Inconsistent object creation: «scope.targetTypeInterpretation»''') + } else if (scope.maxNewElements > 0) { + scope.maxNewElements = scope.maxNewElements - 1 + } + if (scope.minNewElements > 0) { + scope.minNewElements = scope.minNewElements - 1 + } + } +} diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/Z3PolyhedronSolver.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/Z3PolyhedronSolver.xtend new file mode 100644 index 00000000..f1a84f2d --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/Z3PolyhedronSolver.xtend @@ -0,0 +1,206 @@ +package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality + +import com.microsoft.z3.ArithExpr +import com.microsoft.z3.Context +import com.microsoft.z3.Expr +import com.microsoft.z3.IntNum +import com.microsoft.z3.Optimize +import com.microsoft.z3.Status +import com.microsoft.z3.Symbol +import java.util.Map +import org.eclipse.xtend.lib.annotations.Accessors +import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor + +class Z3PolyhedronSolver implements PolyhedronSolver { + val boolean lpRelaxation + + @FinalFieldsConstructor + new() { + } + + new() { + this(true) + } + + override createSaturationOperator(Polyhedron polyhedron) { + new Z3SaturationOperator(polyhedron, lpRelaxation) + } +} + +class Z3SaturationOperator implements PolyhedronSaturationOperator { + static val INFINITY_SYMBOL_NAME = "oo" + static val MULT_SYMBOL_NAME = "*" + + extension val Context context + val Symbol infinitySymbol + val Symbol multSymbol + @Accessors val Polyhedron polyhedron + val Map variables + + new(Polyhedron polyhedron, boolean lpRelaxation) { + context = new Context + infinitySymbol = context.mkSymbol(INFINITY_SYMBOL_NAME) + multSymbol = context.mkSymbol(MULT_SYMBOL_NAME) + this.polyhedron = polyhedron + variables = polyhedron.dimensions.toInvertedMap [ dimension | + val name = dimension.name + if (lpRelaxation) { + mkRealConst(name) + } else { + mkIntConst(name) + } + ] + } + + override saturate() { + val status = doSaturate() + convertStatusToSaturationResult(status) + } + + private def convertStatusToSaturationResult(Status status) { + switch (status) { + case SATISFIABLE: + PolyhedronSaturationResult.SATURATED + case UNSATISFIABLE: + PolyhedronSaturationResult.EMPTY + case UNKNOWN: + PolyhedronSaturationResult.UNKNOWN + default: + throw new IllegalArgumentException("Unknown Status: " + status) + } + } + + private def doSaturate() { + for (expressionToSaturate : polyhedron.expressionsToSaturate) { + val expr = expressionToSaturate.toExpr + val lowerResult = saturateLowerBound(expr, expressionToSaturate) + if (lowerResult != Status.SATISFIABLE) { + return lowerResult + } + val upperResult = saturateUpperBound(expr, expressionToSaturate) + if (upperResult != Status.SATISFIABLE) { + return upperResult + } + } + Status.SATISFIABLE + } + + private def saturateLowerBound(ArithExpr expr, LinearBoundedExpression expressionToSaturate) { + val optimize = prepareOptimize + val handle = optimize.MkMinimize(expr) + val status = optimize.Check() + if (status == Status.SATISFIABLE) { + val value = switch (resultExpr : handle.lower) { + IntNum: + resultExpr.getInt() + default: + if (isNegativeInfinity(resultExpr)) { + null + } else { + throw new IllegalArgumentException("Integer result expected, got: " + resultExpr) + } + } + expressionToSaturate.lowerBound = value + } + status + } + + private def saturateUpperBound(ArithExpr expr, LinearBoundedExpression expressionToSaturate) { + val optimize = prepareOptimize + val handle = optimize.MkMaximize(expr) + val status = optimize.Check() + if (status == Status.SATISFIABLE) { + val value = switch (resultExpr : handle.upper) { + IntNum: + resultExpr.getInt() + default: + if (isPositiveInfinity(resultExpr)) { + null + } else { + throw new IllegalArgumentException("Integer result expected, got: " + resultExpr) + } + } + expressionToSaturate.upperBound = value + } + status + } + + private def isPositiveInfinity(Expr expr) { + expr.app && expr.getFuncDecl.name == infinitySymbol + } + + private def isNegativeInfinity(Expr expr) { + // Negative infinity is represented as (* (- 1) oo) + if (!expr.app || expr.getFuncDecl.name != multSymbol || expr.numArgs != 2) { + return false + } + isPositiveInfinity(expr.args.get(1)) + } + + private def prepareOptimize() { + val optimize = mkOptimize() + assertConstraints(optimize) + optimize + } + + private def assertConstraints(Optimize it) { + for (pair : variables.entrySet) { + assertBounds(pair.value, pair.key) + } + for (constraint : polyhedron.constraints) { + val expr = createLinearCombination(constraint.coefficients) + assertBounds(expr, constraint) + } + } + + private def assertBounds(Optimize it, ArithExpr expression, LinearBoundedExpression bounds) { + val lowerBound = bounds.lowerBound + val upperBound = bounds.upperBound + if (lowerBound == upperBound) { + if (lowerBound === null) { + return + } + Assert(mkEq(expression, mkInt(lowerBound))) + } else { + if (lowerBound !== null) { + Assert(mkGe(expression, mkInt(lowerBound))) + } + if (upperBound !== null) { + Assert(mkLe(expression, mkInt(upperBound))) + } + } + } + + private def toExpr(LinearBoundedExpression linearBoundedExpression) { + switch (linearBoundedExpression) { + Dimension: variables.get(linearBoundedExpression) + LinearConstraint: createLinearCombination(linearBoundedExpression.coefficients) + default: throw new IllegalArgumentException("Unknown linear bounded expression:" + linearBoundedExpression) + } + } + + private def createLinearCombination(Map coefficients) { + val size = coefficients.size + val array = newArrayOfSize(size) + var int i = 0 + for (pair : coefficients.entrySet) { + val variable = variables.get(pair.key) + if (variable === null) { + throw new IllegalArgumentException("Unknown dimension: " + pair.key.name) + } + val coefficient = pair.value + val term = if (coefficient == 1) { + variable + } else { + mkMul(mkInt(coefficient), variable) + } + array.set(i, term) + i++ + } + mkAdd(array) + } + + override close() throws Exception { + context.close() + } +} diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/rules/RefinementRuleProvider.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/rules/RefinementRuleProvider.xtend index 20d24b77..5fefa551 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/rules/RefinementRuleProvider.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/rules/RefinementRuleProvider.xtend @@ -6,7 +6,7 @@ import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Relation import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.ModelGenerationStatistics -import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.ScopePropagator +import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.ScopePropagator import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns.GeneratedPatterns import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns.ObjectCreationPrecondition import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialComplexTypeInterpretation -- cgit v1.2.3-54-g00ecf