Search in sources :

Example 6 with SingleVariableConstraint

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

the class ConjoinedContext method conjoinSpecializedForConstraintsIfApplicable.

/**
 * Returns the result of conjoining a formula and this context under a given context if a specialized routine is available,
 * or null otherwise.
 */
private Context conjoinSpecializedForConstraintsIfApplicable(Expression formula, Context context) {
    Context result;
    if (formula instanceof SingleVariableConstraint) {
        SingleVariableConstraint formulaAsSingleVariableConstraint = (SingleVariableConstraint) formula;
        boolean formulaIsSingleVariableConstraintOnNewVariable = checkIfFormulaISingleVariableConstraintOnNewVariable(formulaAsSingleVariableConstraint);
        if (formulaIsSingleVariableConstraintOnNewVariable) {
            result = conjoinWithSingleVariableConstraintOnANewVariable(formulaAsSingleVariableConstraint, context);
        } else {
            // otherwise we won't be able to use the single variable constraint structure in any special way
            result = null;
        }
    } else if (formula instanceof ConjoinedContext) {
        ConjoinedContext formulaAsMultiVariableConstraint = (ConjoinedContext) formula;
        result = conjoinWithMultiVariableContextWithCheckedProperty(formulaAsMultiVariableConstraint, context);
    } else {
        result = null;
    }
    return result;
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint)

Example 7 with SingleVariableConstraint

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

the class LinearRealArithmeticTheoryTest method runSummationTest.

private void runSummationTest(Expression variable, String constraintString, String bodyString, Expression expected, Simplifier simplifier, Context context) {
    Expression typeExpression = context.getTypeExpressionOfRegisteredSymbol(variable);
    runQuantifierTest(variable, constraintString, bodyString, expected, "summation for " + bodyString, (SingleVariableConstraint c, Expression b) -> new SummationOnLinearRealArithmeticAndPolynomialStepSolver(new DefaultSingleQuantifierEliminationProblem(new Sum(), variable, typeExpression, c, b)), context);
}
Also used : SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Expression(com.sri.ai.expresso.api.Expression) SummationOnLinearRealArithmeticAndPolynomialStepSolver(com.sri.ai.grinder.theory.linearrealarithmetic.SummationOnLinearRealArithmeticAndPolynomialStepSolver) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem) Sum(com.sri.ai.grinder.group.Sum)

Example 8 with SingleVariableConstraint

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

the class SingleQuantifierEliminationForIndexFreeBody method bodyTimesNumberOfIndexValues.

private Expression bodyTimesNumberOfIndexValues() {
    SingleVariableConstraint singleVariableConstraint = (SingleVariableConstraint) getConstraint();
    Expression modelCount = singleVariableConstraint.modelCount(context);
    Expression result = bodyTimes(modelCount);
    return result;
}
Also used : SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Expression(com.sri.ai.expresso.api.Expression)

Example 9 with SingleVariableConstraint

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

the class CompoundTheory method makeSingleVariableConstraintAfterBookkeeping.

@Override
public SingleVariableConstraint makeSingleVariableConstraintAfterBookkeeping(Expression variable, Context context) {
    Theory theoryForVariable = getTheory(variable, context);
    SingleVariableConstraint result;
    if (theoryForVariable != null) {
        result = theoryForVariable.makeSingleVariableConstraintAfterBookkeeping(variable, context);
    } else {
        result = null;
    }
    return result;
}
Also used : SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Theory(com.sri.ai.grinder.api.Theory) AbstractTheory(com.sri.ai.grinder.core.constraint.AbstractTheory)

Example 10 with SingleVariableConstraint

use of com.sri.ai.grinder.api.SingleVariableConstraint 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

SingleVariableConstraint (com.sri.ai.grinder.api.SingleVariableConstraint)22 Expression (com.sri.ai.expresso.api.Expression)14 Context (com.sri.ai.grinder.api.Context)12 Theory (com.sri.ai.grinder.api.Theory)6 DefaultSingleQuantifierEliminationProblem (com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem)6 Beta (com.google.common.annotations.Beta)5 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)5 Type (com.sri.ai.expresso.api.Type)5 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)5 Constraint (com.sri.ai.grinder.api.Constraint)5 IncompleteMultiVariableConstraint (com.sri.ai.grinder.core.constraint.IncompleteMultiVariableConstraint)5 Simplifier (com.sri.ai.grinder.rewriter.api.Simplifier)5 BinaryFunction (com.sri.ai.util.base.BinaryFunction)5 Collection (java.util.Collection)5 TRUE (com.sri.ai.expresso.helper.Expressions.TRUE)4 ZERO (com.sri.ai.expresso.helper.Expressions.ZERO)4 Expressions.getVariablesBeingReferenced (com.sri.ai.expresso.helper.Expressions.getVariablesBeingReferenced)4 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)4 SubExpressionsDepthFirstIterator (com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator)4 SingleQuantifierEliminationProblem (com.sri.ai.grinder.api.SingleQuantifierEliminationProblem)4