Search in sources :

Example 1 with SingleQuantifierEliminationProblem

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

the class SGDPLLTTester method makeProblem.

private static Expression makeProblem(Collection<Expression> indices, Constraint constraint, Expression body, AssociativeCommutativeGroup group, Context context) {
    Expression currentBody = body;
    boolean firstIndex = true;
    for (Expression index : indices) {
        Expression indexType = GrinderUtil.getTypeExpressionOfExpression(index, context);
        Expression constraintOnThisIndex = firstIndex ? constraint : TRUE;
        SingleQuantifierEliminationProblem problem = new DefaultSingleQuantifierEliminationProblem(group, index, indexType, constraintOnThisIndex, currentBody);
        currentBody = problem.toExpression();
        firstIndex = false;
    }
    return currentBody;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) SingleQuantifierEliminationProblem(com.sri.ai.grinder.api.SingleQuantifierEliminationProblem) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem)

Example 2 with SingleQuantifierEliminationProblem

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

the class AbstractSingleQuantifierEliminationStepSolver method step.

@Override
public Step step(Context context) {
    Step result;
    Context contextForBody = getContextForBody(context);
    if (contextForBody.isContradiction()) {
        result = new Solution(getGroup().additiveIdentityElement());
    } else {
        ExpressionLiteralSplitterStepSolver bodyStepSolver = getInitialBodyStepSolver(context.getTheory());
        ExpressionLiteralSplitterStepSolver.Step bodyStep = bodyStepSolver.step(contextForBody);
        // Check (**) in this file to see where this happens
        if (!bodyStep.itDepends()) {
            ExpressionLiteralSplitterStepSolver evaluatorStepSolver = context.getTheory().makeEvaluatorStepSolver(bodyStep.getValue());
            bodyStep = evaluatorStepSolver.step(context);
        }
        if (bodyStep.itDepends()) {
            // "intercept" literals containing the index and split the quantifier based on it
            if (isSubExpressionOf(getIndex(), bodyStep.getSplitterLiteral())) {
                Expression literalOnIndex = bodyStep.getSplitterLiteral();
                result = resultIfLiteralContainsIndex(literalOnIndex, bodyStep, contextForBody, context);
            } else {
                // not on index, just pass the expression on which we depend on, but with appropriate sub-step solvers (this, for now)
                AbstractSingleQuantifierEliminationStepSolver ifTrue = clone();
                AbstractSingleQuantifierEliminationStepSolver ifFalse = clone();
                ifTrue.initialBodyEvaluationStepSolver = bodyStep.getStepSolverForWhenSplitterIsTrue();
                ifFalse.initialBodyEvaluationStepSolver = bodyStep.getStepSolverForWhenSplitterIsFalse();
                ifTrue.initialContextForBody = bodyStep.getContextSplittingWhenSplitterIsLiteral().getContextAndLiteral();
                ifFalse.initialContextForBody = bodyStep.getContextSplittingWhenSplitterIsLiteral().getContextAndLiteralNegation();
                // to compute the result's constraint splitting,
                // we cannot directly re-use bodyStep.getConstraintSplitting() because it was not obtained from
                // the context it is returning to,
                // but from the context conjoined with the index constraint.
                // In order to provide two contexts to work with the sequel step solvers,
                // we calculate the splittings here.
                // TODO: In the future, we expect it possible to efficiently extract the contextForBody component relative
                // to the original context only, excluding the index.
                ContextSplitting split = new ContextSplitting(bodyStep.getSplitterLiteral(), context);
                result = new ItDependsOn(bodyStep.getSplitterLiteral(), split, ifTrue, ifFalse);
            }
        } else {
            // body is already literal free
            Expression literalFreeBody = bodyStep.getValue();
            result = eliminateQuantifierForLiteralFreeBody(literalFreeBody, context);
            boolean solutionToQuantifiedLiteralFreeBodyIsNotConditionalItself = !result.itDepends();
            if (solutionToQuantifiedLiteralFreeBodyIsNotConditionalItself) {
                IntegrationRecording.registerGroupIntegration(problem, literalFreeBody, result, context);
            }
        }
    }
    if (context.getGlobalObject(BRUTE_FORCE_CHECKING_OF_NON_CONDITIONAL_PROBLEMS) != null) {
        if (!result.itDepends()) {
            Expression indexType = context.getTypeExpressionOfRegisteredSymbol(getIndex());
            SingleQuantifierEliminationProblem problem = new DefaultSingleQuantifierEliminationProblem(getGroup(), getIndex(), indexType, getIndexConstraint(), getBody());
            Expression problemExpression = problem.toExpression();
            Set<Expression> freeVariables = Expressions.freeVariables(problemExpression, context);
            AssignmentMapsIterator assignments = new AssignmentMapsIterator(freeVariables, context);
            for (Map<Expression, Expression> assignment : in(assignments)) {
                BruteForceCommonInterpreter bruteForceCommonInterpreter = new BruteForceCommonInterpreter();
                Context extendedContext = Assignment.extendAssignments(assignment, context);
                // Only go on if the assignment satisfies the context:
                if (bruteForceCommonInterpreter.apply(context, extendedContext).equals(Expressions.TRUE)) {
                    Expression bruteForceResult = bruteForceCommonInterpreter.apply(problemExpression, extendedContext);
                    Expression resultGivenAssignment = bruteForceCommonInterpreter.apply(result.getValue(), extendedContext);
                    Expression evaluatedProblem = bruteForceCommonInterpreter.apply(problemExpression, extendedContext);
                    if (!bruteForceResult.equals(resultGivenAssignment)) {
                        String message = "Disagreement on " + problemExpression + "\nunder " + assignment + ".\n" + "Context: " + context + ".\n" + "Evaluated problem: " + evaluatedProblem + ".\n" + "Brute force says " + bruteForceResult + ", symbolic says " + resultGivenAssignment;
                        println(message);
                        throw new Error(message);
                    } else {
                        String message = "Agreement on " + problemExpression + "\nunder " + assignment + ".\n" + "Context: " + context + ".\n" + "Evaluated problem: " + evaluatedProblem + ".\n" + "Brute force says " + bruteForceResult + ", symbolic says " + resultGivenAssignment;
                        println(message);
                    }
                }
            }
        }
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.api.Context) AssignmentMapsIterator(com.sri.ai.grinder.helper.AssignmentMapsIterator) BruteForceCommonInterpreter(com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter) Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver) SingleQuantifierEliminationProblem(com.sri.ai.grinder.api.SingleQuantifierEliminationProblem) ContextSplitting(com.sri.ai.grinder.core.constraint.ContextSplitting)

Example 3 with SingleQuantifierEliminationProblem

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

the class SummationOnDifferenceArithmeticAndPolynomialStepSolverTest method runTest.

private void runTest(Expression variable, String constraintString, Expression body, Expression expected, Context context) {
    Theory theory = context.getTheory();
    SingleVariableDifferenceArithmeticConstraint constraint = new SingleVariableDifferenceArithmeticConstraint(variable, true, theory);
    constraint = (SingleVariableDifferenceArithmeticConstraint) constraint.conjoin(parse(constraintString), context);
    Expression typeExpression = context.getTypeExpressionOfRegisteredSymbol(variable);
    SingleQuantifierEliminationProblem problem = new DefaultSingleQuantifierEliminationProblem(new Sum(), variable, typeExpression, constraint, body);
    ExpressionStepSolver stepSolver = new SummationOnDifferenceArithmeticAndPolynomialStepSolver(problem);
    Expression actual = stepSolver.solve(context);
    expected = simplify(expected, context);
    System.out.println("sum({{ (on " + variable + " in " + GrinderUtil.getTypeExpressionOfExpression(variable, context) + ") " + body + " : " + constraintString + " }} = " + actual + "\n");
    if (!expected.equals(actual)) {
        Expression difference = apply(MINUS, expected, actual);
        Expression differenceResult = simplify(difference, context);
        if (!differenceResult.equals(ZERO)) {
            System.err.println("Expressions are not equal and even difference is not zero");
            System.err.println("Expected: " + expected);
            System.err.println("Actual: " + actual);
            System.err.println("Difference: " + differenceResult);
            fail("Expressions are not equal and even difference is not zero. Expected: " + expected + ", actual: " + actual);
        }
    }
// TODO: correctness test against grounding
}
Also used : SummationOnDifferenceArithmeticAndPolynomialStepSolver(com.sri.ai.grinder.theory.differencearithmetic.SummationOnDifferenceArithmeticAndPolynomialStepSolver) Theory(com.sri.ai.grinder.api.Theory) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) Expression(com.sri.ai.expresso.api.Expression) ExpressionStepSolver(com.sri.ai.grinder.api.ExpressionStepSolver) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem) SingleQuantifierEliminationProblem(com.sri.ai.grinder.api.SingleQuantifierEliminationProblem) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem) Sum(com.sri.ai.grinder.group.Sum) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint)

Example 4 with SingleQuantifierEliminationProblem

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

the class SummationOnDifferenceArithmeticAndPolynomialStepSolver method makeWithNewIndexConstraint.

@Override
protected SummationOnDifferenceArithmeticAndPolynomialStepSolver makeWithNewIndexConstraint(SingleVariableConstraint newIndexConstraint) {
    SingleQuantifierEliminationProblem newProblem = getProblem().makeWithNewIndexConstraint(newIndexConstraint);
    SummationOnDifferenceArithmeticAndPolynomialStepSolver result = new SummationOnDifferenceArithmeticAndPolynomialStepSolver(newProblem);
    return result;
}
Also used : SingleQuantifierEliminationProblem(com.sri.ai.grinder.api.SingleQuantifierEliminationProblem)

Example 5 with SingleQuantifierEliminationProblem

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

the class SummationOnLinearRealArithmeticAndPolynomialStepSolver method makeWithNewIndexConstraint.

@Override
protected SummationOnLinearRealArithmeticAndPolynomialStepSolver makeWithNewIndexConstraint(SingleVariableConstraint newIndexConstraint) {
    SingleQuantifierEliminationProblem newProblem = getProblem().makeWithNewIndexConstraint(newIndexConstraint);
    SummationOnLinearRealArithmeticAndPolynomialStepSolver result = new SummationOnLinearRealArithmeticAndPolynomialStepSolver(newProblem);
    return result;
}
Also used : SingleQuantifierEliminationProblem(com.sri.ai.grinder.api.SingleQuantifierEliminationProblem)

Aggregations

SingleQuantifierEliminationProblem (com.sri.ai.grinder.api.SingleQuantifierEliminationProblem)8 Expression (com.sri.ai.expresso.api.Expression)3 DefaultSingleQuantifierEliminationProblem (com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem)2 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)1 Context (com.sri.ai.grinder.api.Context)1 ExpressionLiteralSplitterStepSolver (com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver)1 ExpressionStepSolver (com.sri.ai.grinder.api.ExpressionStepSolver)1 Theory (com.sri.ai.grinder.api.Theory)1 ContextSplitting (com.sri.ai.grinder.core.constraint.ContextSplitting)1 Sum (com.sri.ai.grinder.group.Sum)1 AssignmentMapsIterator (com.sri.ai.grinder.helper.AssignmentMapsIterator)1 BruteForceCommonInterpreter (com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter)1 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)1 SingleVariableDifferenceArithmeticConstraint (com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint)1 SummationOnDifferenceArithmeticAndPolynomialStepSolver (com.sri.ai.grinder.theory.differencearithmetic.SummationOnDifferenceArithmeticAndPolynomialStepSolver)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1