Search in sources :

Example 1 with SingleVariableConstraint

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

the class DefaultMultiVariableConstraint method conjoinWithLiteral.

@Override
public DefaultMultiVariableConstraint conjoinWithLiteral(Expression literal, Context context) {
    DefaultMultiVariableConstraint result;
    Expression variable = getSomeVariableFor(literal, context);
    if (variable == null) {
        // the literal has no variables.
        Expression literalEvaluation = getTheory().simplify(literal, context);
        if (literalEvaluation.equals(TRUE)) {
            result = this;
        } else {
            result = makeContradiction();
        }
    } else {
        SingleVariableConstraint singleVariableConstraint = getConstraintFor(variable, context);
        SingleVariableConstraint newSingleVariableConstraint = singleVariableConstraint.conjoin(literal, context);
        if (newSingleVariableConstraint.isContradiction()) {
            result = makeContradiction();
        } else if (newSingleVariableConstraint == singleVariableConstraint) {
            result = this;
        } else {
            Map<Expression, SingleVariableConstraint> newFromVariableToItsConstraint = new LinkedHashMap<>(fromVariableToItsConstraint);
            newFromVariableToItsConstraint.put(variable, newSingleVariableConstraint);
            result = makeWithNewFromVariableToItsConstraint(newFromVariableToItsConstraint);
        }
    }
    return result;
}
Also used : SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Expression(com.sri.ai.expresso.api.Expression) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with SingleVariableConstraint

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

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

the class IncompleteMultiVariableConstraint method conjoinLiteralForVariable.

private IncompleteMultiVariableConstraint conjoinLiteralForVariable(Expression literal, Expression variable, Context context) {
    IncompleteMultiVariableConstraint result;
    SingleVariableConstraint singleVariableConstraint = getConstraintFor(variable, context);
    SingleVariableConstraint newSingleVariableConstraint = singleVariableConstraint.conjoin(literal, context);
    if (newSingleVariableConstraint.isContradiction()) {
        result = makeContradiction();
    } else if (newSingleVariableConstraint == singleVariableConstraint) {
        result = this;
    } else {
        result = makeCopyWithNewSingleVariableConstraintForVariable(variable, newSingleVariableConstraint);
    }
    return result;
}
Also used : SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint)

Example 4 with SingleVariableConstraint

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

the class ConjoinedContext method makeNewConjoinedContextFromNewHeadAndNewTail.

private Context makeNewConjoinedContextFromNewHeadAndNewTail(Pair<SingleVariableConstraint, Context> newHeadAndNewTail, Context context) {
    SingleVariableConstraint newHead = newHeadAndNewTail.first;
    Context newTail = newHeadAndNewTail.second;
    Context result;
    if (newHead == head && newTail == tail) {
        // in case nothing changed
        result = this;
    } else {
        result = makeAndCheck(getTheory(), newHead, newTail, propertyCheckerStepSolverMaker, context);
    }
    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 5 with SingleVariableConstraint

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

the class AbstractSingleQuantifierEliminationStepSolver method makeSubProblemStepSolver.

protected ExpressionStepSolver makeSubProblemStepSolver(boolean splitterValue, ExpressionLiteralSplitterStepSolver.Step bodyStep, Constraint newIndexConstraint) {
    SingleVariableConstraint newIndexConstraintAsSingleVariableConstraint = (SingleVariableConstraint) newIndexConstraint;
    AbstractSingleQuantifierEliminationStepSolver result = makeWithNewIndexConstraint(newIndexConstraintAsSingleVariableConstraint);
    result.initialBodyEvaluationStepSolver = bodyStep.getStepSolverForWhenSplitterIs(splitterValue);
    return result;
}
Also used : SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint)

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