Search in sources :

Example 16 with Context

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

the class MultiVariableContextWithCheckedProperty method conjoinSpecializedForConstraintsIfApplicable.

/**
 * Returns a pair indicating whether specialized conjoin for constraints applies to this case and,
 * if so, provides the result of this conjoining.
 * @param formula
 * @param context
 * @return
 */
private Pair<Boolean, Context> conjoinSpecializedForConstraintsIfApplicable(Expression formula, Context context) {
    Pair<Boolean, Context> result;
    if (formula instanceof SingleVariableConstraint) {
        SingleVariableConstraint formulaAsSingleVariableConstraint = (SingleVariableConstraint) formula;
        Expression variable = formulaAsSingleVariableConstraint.getVariable();
        // TODO: this forces expression representation to be generated, which can be expensive. Better write a method that checks it on the constraint representation itself
        boolean variableAlreadyConstrainedInThis = contains(this, variable);
        if (!variableAlreadyConstrainedInThis) {
            // if the variable is new to this constraint, we can simply tack on its constraint on it.
            Context newContext = makeAndCheck(getTheory(), formulaAsSingleVariableConstraint, this, contextDependentProblemStepSolverMaker, context);
            result = pair(true, newContext);
        } else {
            // otherwise we won't be able to use the single variable constraint structure in any special way
            result = pair(false, null);
        }
    } else if (formula instanceof MultiVariableContextWithCheckedProperty) {
        MultiVariableContextWithCheckedProperty formulaAsMultiVariableConstraint = (MultiVariableContextWithCheckedProperty) formula;
        // if formula is itself a MultiVariableContextWithCheckedProperty,
        // we conjoin its two known parts individually.
        // Their own inner structure will also be efficiently exploited by these conjunctions.
        Context conjunction = this;
        if (formulaAsMultiVariableConstraint.tail != null) {
            conjunction = conjunction.conjoin(formulaAsMultiVariableConstraint.tail, context);
        }
        if (formulaAsMultiVariableConstraint.head != null) {
            conjunction = conjunction.conjoin(formulaAsMultiVariableConstraint.head, context);
        }
        result = pair(true, conjunction);
    } else {
        // the formula does not have a recognizable structure we can exploit
        result = pair(false, null);
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.api.Context) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Expression(com.sri.ai.expresso.api.Expression)

Example 17 with Context

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

the class MultiVariableContextWithCheckedProperty method makeAndCheck.

/**
 * Creates a new {@link Context} from a {@link SingleVariableConstraint} and a {@link Context},
 * by either returning a contradiction if either is contradictory,
 * or a new {@link MultiVariableContextWithCheckedProperty} otherwise.
 * @param theory
 * @param head
 * @param tail
 * @param context
 * @return
 */
public static Context makeAndCheck(Theory theory, SingleVariableConstraint head, Context tail, ContextDependentProblemStepSolverMaker contextDependentProblemStepSolverMaker, Context context) {
    Context result;
    if (head.isContradiction() || tail.isContradiction()) {
        result = tail.makeContradiction();
    } else {
        MultiVariableContextWithCheckedProperty uncheckedMultiVariableConstraintWithCheckedProperty = new MultiVariableContextWithCheckedProperty(tail.getTheory(), head, tail, contextDependentProblemStepSolverMaker);
        result = uncheckedMultiVariableConstraintWithCheckedProperty.check(context);
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.api.Context)

Example 18 with Context

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

the class MultiVariableContextWithCheckedProperty method makeCloneWithAdditionalRegisteredSymbolsAndTypes.

@Override
public MultiVariableContextWithCheckedProperty makeCloneWithAdditionalRegisteredSymbolsAndTypes(Map<Expression, Expression> indicesAndTypes) {
    MultiVariableContextWithCheckedProperty result = clone();
    Context newTail = tail.makeCloneWithAdditionalRegisteredSymbolsAndTypes(indicesAndTypes);
    result.tail = newTail;
    return result;
}
Also used : Context(com.sri.ai.grinder.api.Context)

Example 19 with Context

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

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

the class AssignmentsSamplingIterator method makeContextExtendedBySampledValue.

private Context makeContextExtendedBySampledValue(Expression sampledValue) {
    Assignment sampledAssignment = makeIndexAssignment(sampledValue);
    Context contextExtendedWithSampledAssignment = sampledAssignment.extend(context);
    return contextExtendedWithSampledAssignment;
}
Also used : Assignment(com.sri.ai.grinder.interpreter.Assignment) Context(com.sri.ai.grinder.api.Context)

Aggregations

Context (com.sri.ai.grinder.api.Context)132 Expression (com.sri.ai.expresso.api.Expression)100 Test (org.junit.Test)50 TrueContext (com.sri.ai.grinder.core.TrueContext)40 TheoryTestingSupport (com.sri.ai.grinder.tester.TheoryTestingSupport)36 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)32 Type (com.sri.ai.expresso.api.Type)31 Theory (com.sri.ai.grinder.api.Theory)24 PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)23 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)22 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)21 Set (java.util.Set)20 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)19 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)19 CompleteMultiVariableContext (com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext)16 Map (java.util.Map)16 Beta (com.google.common.annotations.Beta)15 Constraint (com.sri.ai.grinder.api.Constraint)15 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)14 SingleVariableConstraint (com.sri.ai.grinder.api.SingleVariableConstraint)14