Search in sources :

Example 16 with Context

use of com.sri.ai.grinder.sgdpllt.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.sgdpllt.api.Context) SingleVariableConstraint(com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint) Expression(com.sri.ai.expresso.api.Expression)

Example 17 with Context

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

the class AbstractQuantifierEliminationStepSolver method step.

@Override
public Step step(Context context) {
    Step result;
    Context contextForBody = getContextForBody(context);
    if (contextForBody.isContradiction()) {
        result = new Solution(group.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 = 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)
                AbstractQuantifierEliminationStepSolver ifTrue = clone();
                AbstractQuantifierEliminationStepSolver 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
            result = eliminateQuantifierForLiteralFreeBodyAndSingleVariableConstraint(indexConstraint, bodyStep.getValue(), context);
        }
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) ContextSplitting(com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting)

Example 18 with Context

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

the class AbstractIterativeMultiIndexQuantifierElimination method extendAssignments.

/**
	 * Sets the value assignment to a given expression in the binding mechanism stored in the context.
	 * @param newAssignment
	 * @param context
	 * @return
	 */
public static Context extendAssignments(Map<Expression, Expression> newAssignments, Context context) {
    @SuppressWarnings("unchecked") Map<Expression, Expression> assignments = (Map<Expression, Expression>) context.getGlobalObject(ASSIGNMENTS_GLOBAL_OBJECTS_KEY);
    Map<Expression, Expression> extendedAssignments;
    if (assignments == null) {
        extendedAssignments = newAssignments;
    } else {
        extendedAssignments = new StackedHashMap<>(newAssignments, assignments);
    }
    Context result = context.putGlobalObject(ASSIGNMENTS_GLOBAL_OBJECTS_KEY, extendedAssignments);
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) Expression(com.sri.ai.expresso.api.Expression) StackedHashMap(com.sri.ai.util.collect.StackedHashMap) Map(java.util.Map)

Example 19 with Context

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

the class MultiVariableContextWithCheckedProperty method putGlobalObject.

@Override
public MultiVariableContextWithCheckedProperty putGlobalObject(Object key, Object value) {
    MultiVariableContextWithCheckedProperty result = clone();
    Context newTail = tail.putGlobalObject(key, value);
    result.tail = newTail;
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context)

Example 20 with Context

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

Aggregations

Context (com.sri.ai.grinder.sgdpllt.api.Context)105 Expression (com.sri.ai.expresso.api.Expression)80 Test (org.junit.Test)48 TheoryTestingSupport (com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport)36 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)30 Type (com.sri.ai.expresso.api.Type)28 DifferenceArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory)28 PropositionalTheory (com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory)22 EqualityTheory (com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory)20 CompoundTheory (com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)18 Constraint (com.sri.ai.grinder.sgdpllt.api.Constraint)17 Map (java.util.Map)17 Beta (com.google.common.annotations.Beta)16 CompleteMultiVariableContext (com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext)16 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)14 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)14 LinearRealArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory)14 FunctionType (com.sri.ai.expresso.type.FunctionType)13 Util (com.sri.ai.util.Util)13 SingleVariableConstraint (com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint)12