Search in sources :

Example 1 with Constraint

use of com.sri.ai.grinder.api.Constraint in project aic-expresso by aic-sri-international.

the class DefaultMultiVariableConstraint method conjoin.

@Override
public Constraint conjoin(Expression formula, Context context) {
    myAssert(() -> context.isLiteral(formula) || formula instanceof Constraint, () -> this.getClass() + " currently only supports conjoining with literals and constraints, but received " + formula);
    Constraint result;
    if (formula instanceof Constraint) {
        result = conjoinWithConjunctiveClause(formula, context);
    } else {
        result = conjoinWithLiteral(formula, context);
    }
    return result;
}
Also used : SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Constraint(com.sri.ai.grinder.api.Constraint)

Example 2 with Constraint

use of com.sri.ai.grinder.api.Constraint in project aic-expresso by aic-sri-international.

the class AbstractSGVET method solve.

@Override
public Expression solve(MultiQuantifierEliminationProblem problem, Context context) {
    AssociativeCommutativeGroup group = problem.getGroup();
    List<Expression> indices = problem.getIndices();
    Expression indicesConstraint = problem.getConstraint();
    Expression body = problem.getBody();
    checkInterrupted();
    // Make sure body is simplified and quantifier-free.
    // context.getTheory().evaluate(body, context);
    Expression simplifiedBody = body;
    Expression result;
    if (getDebug()) {
        System.out.println("SGVE(T) input: " + simplifiedBody);
        System.out.println("Width        : " + width(simplifiedBody, context));
    }
    AssociativeCommutativeSemiRing semiRing = (AssociativeCommutativeSemiRing) group;
    Partition partition;
    if (indices.size() < 1) {
        partition = null;
    } else {
        Expression factoredConditionalsExpression = factoredConditionalsWithAbsorbingElseClause(semiRing, simplifiedBody, context);
        partition = pickPartition(semiRing, factoredConditionalsExpression, indices, context);
    }
    if (partition == null) {
        if (basicOutput) {
            System.out.println("No partition");
        }
        result = subSolver.solve(group, indices, indicesConstraint, simplifiedBody, context);
    } else {
        Expression indexSubProblemExpression = product(semiRing, partition.expressionsOnIndexAndNot.first, context);
        if (basicOutput) {
            System.out.println("Eliminating: " + getFirst(partition.index));
            System.out.println("From       : " + indexSubProblemExpression);
            System.out.println("Width      : " + width(indexSubProblemExpression, context) + " out of " + indices.size() + " indices");
        }
        // We now invoke the subsolver for summing the index out of the factors it is in.
        // Ideally, we would reuse the current constraint, but the set of index has changed and the current constraint may
        // use an internal representation that depends on its previous set of indices.
        // In the future, we should try to re-use that internal representation and re-index it appropriately, but for now
        // we rewrite the program in a way that the current constraint becomes a part of the input expression.
        // This will be equivalent to using it as a constraint, but will cause the constraint to be re-built.
        // BTW, the call to "project" below will also re-context the constraint for the same reason: re-indexing.
        // In the future it should also re-use the representation.
        // The following transformation is:  sum_C E   =   sum_{true} if C then E else 0
        Expression indexSubProblemExpressionWithConstraint = IfThenElse.make(indicesConstraint, indexSubProblemExpression, semiRing.multiplicativeAbsorbingElement());
        Expression indexSubProblemSolution = subSolver.extendContextAndSolve(group, partition.index, indexSubProblemExpressionWithConstraint, context);
        if (basicOutput) {
            System.out.println("Solution   : " + indexSubProblemSolution + "\n");
        }
        partition.expressionsOnIndexAndNot.second.add(indexSubProblemSolution);
        Expression remainingSubProblemExpression = product(semiRing, partition.expressionsOnIndexAndNot.second, context);
        // the constraint is already represented in indexSubProblemSolution
        Constraint constraintOnRemainingIndices = context;
        result = solve(group, partition.remainingIndices, constraintOnRemainingIndices, remainingSubProblemExpression, context);
        result = semiRing.multiply(result, context);
    }
    return result;
}
Also used : AssociativeCommutativeSemiRing(com.sri.ai.grinder.group.AssociativeCommutativeSemiRing) Expression(com.sri.ai.expresso.api.Expression) Constraint(com.sri.ai.grinder.api.Constraint) AssociativeCommutativeGroup(com.sri.ai.grinder.group.AssociativeCommutativeGroup)

Example 3 with Constraint

use of com.sri.ai.grinder.api.Constraint in project aic-expresso by aic-sri-international.

the class CompoundTheoryWithDifferenceArithmeticTest method runCompleteSatisfiabilityTest.

/**
 * @param conjunction
 * @param expected
 */
private void runCompleteSatisfiabilityTest(String conjunction, Expression expected, Map<String, Type> variableNamesAndTypesForTesting) {
    TheoryTestingSupport equalityTheoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new EqualityTheory(true, true));
    equalityTheoryTestingSupport.setVariableNamesAndTypesForTesting(variableNamesAndTypesForTesting);
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), equalityTheoryTestingSupport, TheoryTestingSupport.make(makeRandom(), new PropositionalTheory()));
    Constraint context = theoryTestingSupport.makeContextWithTestingInformation();
    for (Expression literal : And.getConjuncts(parse(conjunction))) {
        context = context.conjoin(literal, theoryTestingSupport.makeContextWithTestingInformation());
    }
    assertEquals(expected, context);
}
Also used : EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) Constraint(com.sri.ai.grinder.api.Constraint) Expression(com.sri.ai.expresso.api.Expression) AbstractTheoryTestingSupport(com.sri.ai.grinder.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory)

Example 4 with Constraint

use of com.sri.ai.grinder.api.Constraint in project aic-expresso by aic-sri-international.

the class SGDPLLTTester method testCompleteMultiVariableConstraints.

/**
 * Given a theory and a number <code>n</code> of multi-variable constraint tests,
 * generates <code>n</code> formulas in the theory
 * and see if those detected as unsatisfiable by the corresponding solver
 * are indeed unsatisfiable (checked by brute force).
 * Throws an {@link Error} with the failure description if a test fails.
 * @param theoryTestingSupport
 * @param numberOfTests
 * @param maxNumberOfLiterals
 * @param outputCount
 */
public static void testCompleteMultiVariableConstraints(boolean testAgainstBruteForce, TheoryTestingSupport theoryTestingSupport, long numberOfTests, int maxNumberOfLiterals, boolean outputCount) {
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    NullaryFunction<Constraint> makeInitialConstraint = () -> context;
    Function<Constraint, Expression> makeRandomLiteral = c -> theoryTestingSupport.makeRandomLiteral(context);
    // CompleteMultiVariableContext is complete
    TestRunner tester = SGDPLLTTester::testCompleteSatisfiability;
    runTesterGivenOnSuccessiveConjunctionsOfLiterals("complete satisfiability", tester, numberOfTests, maxNumberOfLiterals, testAgainstBruteForce, theoryTestingSupport, makeInitialConstraint, makeRandomLiteral, outputCount, context);
}
Also used : Context(com.sri.ai.grinder.api.Context) BinaryFunction(com.sri.ai.util.base.BinaryFunction) Util.removeFromSetNonDestructively(com.sri.ai.util.Util.removeFromSetNonDestructively) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) Expression(com.sri.ai.expresso.api.Expression) Sets(com.sri.ai.grinder.library.set.Sets) Function(java.util.function.Function) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) And(com.sri.ai.grinder.library.boole.And) Util.in(com.sri.ai.util.Util.in) AssociativeCommutativeGroup(com.sri.ai.grinder.group.AssociativeCommutativeGroup) BruteForceCommonInterpreter(com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) ThereExists(com.sri.ai.grinder.library.boole.ThereExists) Context(com.sri.ai.grinder.api.Context) ZERO(com.sri.ai.expresso.helper.Expressions.ZERO) SingleQuantifierEliminationProblem(com.sri.ai.grinder.api.SingleQuantifierEliminationProblem) LinkedHashSet(java.util.LinkedHashSet) Type(com.sri.ai.expresso.api.Type) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem) NullaryFunction(com.sri.ai.util.base.NullaryFunction) Util.join(com.sri.ai.util.Util.join) Expressions.getVariablesBeingReferenced(com.sri.ai.expresso.helper.Expressions.getVariablesBeingReferenced) Collection(java.util.Collection) Util.list(com.sri.ai.util.Util.list) Set(java.util.Set) IncompleteMultiVariableConstraint(com.sri.ai.grinder.core.constraint.IncompleteMultiVariableConstraint) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) AssignmentMapsIterator(com.sri.ai.grinder.helper.AssignmentMapsIterator) Assignment(com.sri.ai.grinder.interpreter.Assignment) Beta(com.google.common.annotations.Beta) Theory(com.sri.ai.grinder.api.Theory) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) Util(com.sri.ai.util.Util) Util.pickKElementsWithoutReplacement(com.sri.ai.util.Util.pickKElementsWithoutReplacement) TRUE(com.sri.ai.expresso.helper.Expressions.TRUE) Constraint(com.sri.ai.grinder.api.Constraint) IncompleteMultiVariableConstraint(com.sri.ai.grinder.core.constraint.IncompleteMultiVariableConstraint) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Constraint(com.sri.ai.grinder.api.Constraint) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression)

Example 5 with Constraint

use of com.sri.ai.grinder.api.Constraint in project aic-expresso by aic-sri-international.

the class SGDPLLTTester method runModelCountingTestForSingleVariableConstraint.

private static void runModelCountingTestForSingleVariableConstraint(Expression variable, Collection<Expression> literals, Constraint constraint, boolean testAgainstBruteForce, Theory theory, Context context) {
    Expression literalsConjunction = And.make(literals);
    String problemDescription = "model counting of " + literalsConjunction + " for variable " + variable;
    output("Problem: " + problemDescription);
    Simplifier symbolicSolver = (e, p) -> computeModelCountBySolver((SingleVariableConstraint) e, p);
    SingleVariableConstraint singleVariableConstraint = (SingleVariableConstraint) constraint;
    Expression symbolicSolution = symbolicSolver.apply(singleVariableConstraint, context);
    if (Util.thereExists(new SubExpressionsDepthFirstIterator(symbolicSolution), e -> e instanceof QuantifiedExpression || Sets.isIntensionalSet(e))) {
        throw new Error("Symbolic solution is not quantifier-free: " + symbolicSolution);
    }
    output("Symbolic result: " + symbolicSolution);
    if (testAgainstBruteForce) {
        if (singleVariableConstraint.isContradiction()) {
            if (!symbolicSolution.equals(ZERO)) {
                throw new Error("Constraint is contradiction, but symbolic solver does not produce 0, but instead " + symbolicSolution);
            }
        } else {
            Expression testingVariable = singleVariableConstraint.getVariable();
            Set<Expression> allVariables = getVariablesBeingReferenced(singleVariableConstraint, context);
            Collection<? extends Expression> otherVariables = removeFromSetNonDestructively(allVariables, v -> v.equals(testingVariable));
            BinaryFunction<BruteForceCommonInterpreter, Context, Expression> fromInterpreterAndContextWithAssignmentToOtherVariablesToBruteForceSolution = (interpreter, contextWithAssignmentToOtherVariables) -> bruteForceModelCounterForVariableGivenInterpreterAndAssignmentToOtherVariables(variable, literalsConjunction, interpreter, theory, contextWithAssignmentToOtherVariables);
            testSymbolicVsBruteForceComputationForEachAssignment(theory, problemDescription, otherVariables, symbolicSolution, fromInterpreterAndContextWithAssignmentToOtherVariablesToBruteForceSolution, context);
        }
    } else {
        output("Skipping test againt brute-force.");
    }
}
Also used : BinaryFunction(com.sri.ai.util.base.BinaryFunction) Util.removeFromSetNonDestructively(com.sri.ai.util.Util.removeFromSetNonDestructively) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) Expression(com.sri.ai.expresso.api.Expression) Sets(com.sri.ai.grinder.library.set.Sets) Function(java.util.function.Function) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) And(com.sri.ai.grinder.library.boole.And) Util.in(com.sri.ai.util.Util.in) AssociativeCommutativeGroup(com.sri.ai.grinder.group.AssociativeCommutativeGroup) BruteForceCommonInterpreter(com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) ThereExists(com.sri.ai.grinder.library.boole.ThereExists) Context(com.sri.ai.grinder.api.Context) ZERO(com.sri.ai.expresso.helper.Expressions.ZERO) SingleQuantifierEliminationProblem(com.sri.ai.grinder.api.SingleQuantifierEliminationProblem) LinkedHashSet(java.util.LinkedHashSet) Type(com.sri.ai.expresso.api.Type) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem) NullaryFunction(com.sri.ai.util.base.NullaryFunction) Util.join(com.sri.ai.util.Util.join) Expressions.getVariablesBeingReferenced(com.sri.ai.expresso.helper.Expressions.getVariablesBeingReferenced) Collection(java.util.Collection) Util.list(com.sri.ai.util.Util.list) Set(java.util.Set) IncompleteMultiVariableConstraint(com.sri.ai.grinder.core.constraint.IncompleteMultiVariableConstraint) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) AssignmentMapsIterator(com.sri.ai.grinder.helper.AssignmentMapsIterator) Assignment(com.sri.ai.grinder.interpreter.Assignment) Beta(com.google.common.annotations.Beta) Theory(com.sri.ai.grinder.api.Theory) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) Util(com.sri.ai.util.Util) Util.pickKElementsWithoutReplacement(com.sri.ai.util.Util.pickKElementsWithoutReplacement) TRUE(com.sri.ai.expresso.helper.Expressions.TRUE) Constraint(com.sri.ai.grinder.api.Constraint) Context(com.sri.ai.grinder.api.Context) BruteForceCommonInterpreter(com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator)

Aggregations

Constraint (com.sri.ai.grinder.api.Constraint)22 Expression (com.sri.ai.expresso.api.Expression)16 SingleVariableConstraint (com.sri.ai.grinder.api.SingleVariableConstraint)15 Context (com.sri.ai.grinder.api.Context)11 IncompleteMultiVariableConstraint (com.sri.ai.grinder.core.constraint.IncompleteMultiVariableConstraint)10 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)9 AssociativeCommutativeGroup (com.sri.ai.grinder.group.AssociativeCommutativeGroup)9 LinkedHashSet (java.util.LinkedHashSet)9 Beta (com.google.common.annotations.Beta)8 Type (com.sri.ai.expresso.api.Type)8 TRUE (com.sri.ai.expresso.helper.Expressions.TRUE)8 ZERO (com.sri.ai.expresso.helper.Expressions.ZERO)8 Expressions.getVariablesBeingReferenced (com.sri.ai.expresso.helper.Expressions.getVariablesBeingReferenced)8 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)8 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)8 SubExpressionsDepthFirstIterator (com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator)8 SingleQuantifierEliminationProblem (com.sri.ai.grinder.api.SingleQuantifierEliminationProblem)8 Theory (com.sri.ai.grinder.api.Theory)8 DefaultSingleQuantifierEliminationProblem (com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem)8 AssignmentMapsIterator (com.sri.ai.grinder.helper.AssignmentMapsIterator)8