Search in sources :

Example 11 with Constraint

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

the class AbstractSingleVariableConstraintWithBinaryAtomsIncludingEquality method conjoinNonTrivialSignAndNormalizedAtomToConstraintWithBoundVariable.

private AbstractSingleVariableConstraintWithDependentNormalizedAtoms conjoinNonTrivialSignAndNormalizedAtomToConstraintWithBoundVariable(boolean sign, Expression normalizedAtom, Context context) {
    Constraint result;
    // first, use super's implementation to detect inconsistencies
    AbstractSingleVariableConstraintWithDependentNormalizedAtoms conjunctionWithSignAndNormalizedAtom = super.conjoinNonTrivialSignAndNormalizedAtom(sign, normalizedAtom, context);
    if (conjunctionWithSignAndNormalizedAtom == null) {
        result = makeContradiction();
    } else {
        // this assumes the original single positive normalized atom stays as the first one in the conjoined constraint
        Expression binding = getPositiveNormalizedAtoms().get(0);
        // create a fresh constraint with the binding and external literals only
        result = makeSimplification(arrayList(binding), arrayList(), getExternalLiterals());
        // apply new normalized atom after replacing constraint's variable by its value (making it an external literal)
        Expression newExternalLiteral = rewriteSignAndNormalizedAtomForValueVariableIsBoundTo(sign, normalizedAtom, binding.get(1), context);
        result = result.conjoinWithLiteral(newExternalLiteral, context);
    }
    return (AbstractSingleVariableConstraintWithDependentNormalizedAtoms) result;
// Note: a simpler, more expensive version of this method could create an empty constraint,
// conjoin it with the binding, with each external literal, and the new normalized atom, converted to external literal,
// as opposed to using makeRefinementWith with all external literals at once.
// That solution would require the application of external literals one by one, however, whereas the above just copies them all at once.
}
Also used : Constraint(com.sri.ai.grinder.api.Constraint) Expression(com.sri.ai.expresso.api.Expression) AbstractSingleVariableConstraintWithDependentNormalizedAtoms(com.sri.ai.grinder.core.constraint.AbstractSingleVariableConstraintWithDependentNormalizedAtoms)

Example 12 with Constraint

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

the class CompoundTheoryWithDifferenceArithmeticTest method basicTests.

@Test
public void basicTests() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
    Expression condition = parse("X = Y and Y = X and P and not Q and P and X = a and X != b");
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    Constraint constraint = new CompleteMultiVariableContext(theoryTestingSupport.getTheory(), context);
    constraint = constraint.conjoin(condition, context);
    Expression expected = parse("(Y = a) and not Q and P and (X = Y)");
    assertEquals(expected, constraint);
    // nested indices
    Expression expression = parse("sum({{(on I in 1..2, J in 2..3) sum({{ (on I in 1..10, J in 1..2) I + J : I != J }}) }})");
    context = new TrueContext(theoryTestingSupport.getTheory());
    expected = parse("536");
    Expression actual = theoryTestingSupport.getTheory().evaluate(expression, context);
    println(actual);
    assertEquals(expected, actual);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) Expression(com.sri.ai.expresso.api.Expression) Constraint(com.sri.ai.grinder.api.Constraint) AbstractTheoryTestingSupport(com.sri.ai.grinder.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) TrueContext(com.sri.ai.grinder.core.TrueContext) AbstractTheoryTest(com.sri.ai.test.grinder.theory.base.AbstractTheoryTest) Test(org.junit.Test)

Example 13 with Constraint

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

the class SGDPLLTTester method testMultiVariableConstraints.

/**
 * Given a theory and a number <code>n</code> of multi-variable constraint tests,
 * generates <code>n</code> formulas in the theory
 * and see if those detected as unsatisfiable by the corresponding solver
 * are indeed unsatisfiable (checked by brute force).
 * Throws an {@link Error} with the failure description if a test fails.
 * @param theoryTestingSupport
 * @param numberOfTests
 * @param maxNumberOfLiterals
 * @param outputCount
 */
public static void testMultiVariableConstraints(boolean testAgainstBruteForce, TheoryTestingSupport theoryTestingSupport, long numberOfTests, int maxNumberOfLiterals, boolean outputCount) {
    NullaryFunction<Constraint> makeInitialConstraint = () -> new DefaultMultiVariableConstraint(theoryTestingSupport.getTheory());
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    Function<Constraint, Expression> makeRandomLiteral = c -> theoryTestingSupport.makeRandomLiteral(context);
    // DefaultMultiVariableConstraint is incomplete
    TestRunner tester = SGDPLLTTester::testIncompleteSatisfiability;
    runTesterGivenOnSuccessiveConjunctionsOfLiterals("incomplete satisfiability", tester, numberOfTests, maxNumberOfLiterals, testAgainstBruteForce, theoryTestingSupport, makeInitialConstraint, makeRandomLiteral, outputCount, context);
}
Also used : Context(com.sri.ai.grinder.api.Context) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) 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) DefaultMultiVariableConstraint(com.sri.ai.grinder.core.constraint.DefaultMultiVariableConstraint) Set(java.util.Set) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) 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) DefaultMultiVariableConstraint(com.sri.ai.grinder.core.constraint.DefaultMultiVariableConstraint) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Constraint(com.sri.ai.grinder.api.Constraint) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) DefaultMultiVariableConstraint(com.sri.ai.grinder.core.constraint.DefaultMultiVariableConstraint)

Example 14 with Constraint

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

the class AbstractSingleQuantifierEliminationStepSolver method resultIfLiteralContainsIndex.

protected Step resultIfLiteralContainsIndex(Expression literal, ExpressionLiteralSplitterStepSolver.Step bodyStep, Context contextForBody, Context context) {
    // if the splitter contains the index, we must split the quantifier:
    // Quant_x:C Body  --->   (Quant_{x:C and L} Body) op (Quant_{x:C and not L} Body)
    Step result;
    Expression solutionValue;
    // Here, we need to obtain the new index constraints, for the case in which the splitter literal is true and false,
    // to create the corresponding sub-problems, solve them, and combine them.
    // However, it is important to remember that bodyStep.getContextSplittingWhenSplitterIsLiteral()
    // contains the splitting of contextForBody with the splitter literal,
    // so the information on the new index constraints is already there in some form.
    // TODO: We current don't have a Constraint-generic way to extract it, but expect to do it in the future.
    // For now, we split the index constraint separately
    ConstraintSplitting indexConstraintSplitting = new ConstraintSplitting(literal, getIndexConstraint(), context);
    Constraint indexConstraintAndLiteral = indexConstraintSplitting.getConstraintAndLiteral();
    Constraint indexConstraintAndLiteralNegation = indexConstraintSplitting.getConstraintAndLiteralNegation();
    switch(indexConstraintSplitting.getResult()) {
        case CONSTRAINT_IS_CONTRADICTORY:
            solutionValue = null;
            break;
        case LITERAL_IS_UNDEFINED:
            // (**) IF DELETING THIS MARKER, DELETE ALL THE REFERENCES TO IT IN THIS FILE
            // This is where this step solver may return a Solution with literals in it:
            // solveSubProblem uses an exhaustive solve.
            solutionValue = solveSubProblems(makeSubProblem(true, bodyStep, indexConstraintAndLiteral), makeSubProblem(false, bodyStep, indexConstraintAndLiteralNegation), context);
            break;
        case LITERAL_IS_TRUE:
            solutionValue = solveSubProblem(makeSubProblem(true, bodyStep, indexConstraintAndLiteral), context);
            break;
        case LITERAL_IS_FALSE:
            solutionValue = solveSubProblem(makeSubProblem(false, bodyStep, indexConstraintAndLiteralNegation), context);
            break;
        default:
            throw new Error("Unrecognized result for " + ConstraintSplitting.class + ": " + indexConstraintSplitting.getResult());
    }
    if (solutionValue == null) {
        result = null;
    } else {
        result = new Solution(solutionValue);
    }
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Constraint(com.sri.ai.grinder.api.Constraint) ConstraintSplitting(com.sri.ai.grinder.core.constraint.ConstraintSplitting)

Example 15 with Constraint

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

the class AbstractSingleQuantifierEliminationStepSolver method splitOnDefinedSplitter.

private Step splitOnDefinedSplitter(ExpressionLiteralSplitterStepSolver.Step bodyStep, ConstraintSplitting indexConstraintSplitting, Context context) {
    boolean splitterValue = indexConstraintSplitting.getResult() == ConstraintSplitting.Result.LITERAL_IS_TRUE;
    explain("Index literal ", bodyStep.getSplitter(), " is always " + splitterValue + " under current context, so we will solve a single sub-problem");
    Constraint indexConstrainedConjoinedWithSplitter = indexConstraintSplitting.getConstraintAndLiteralEqualTo(splitterValue);
    Step step = stepOnSubProblemIfSplitterIs(splitterValue, bodyStep, indexConstrainedConjoinedWithSplitter, context);
    return step;
}
Also used : SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Constraint(com.sri.ai.grinder.api.Constraint)

Aggregations

Constraint (com.sri.ai.grinder.api.Constraint)22 Expression (com.sri.ai.expresso.api.Expression)16 SingleVariableConstraint (com.sri.ai.grinder.api.SingleVariableConstraint)15 Context (com.sri.ai.grinder.api.Context)11 IncompleteMultiVariableConstraint (com.sri.ai.grinder.core.constraint.IncompleteMultiVariableConstraint)10 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)9 AssociativeCommutativeGroup (com.sri.ai.grinder.group.AssociativeCommutativeGroup)9 LinkedHashSet (java.util.LinkedHashSet)9 Beta (com.google.common.annotations.Beta)8 Type (com.sri.ai.expresso.api.Type)8 TRUE (com.sri.ai.expresso.helper.Expressions.TRUE)8 ZERO (com.sri.ai.expresso.helper.Expressions.ZERO)8 Expressions.getVariablesBeingReferenced (com.sri.ai.expresso.helper.Expressions.getVariablesBeingReferenced)8 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)8 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)8 SubExpressionsDepthFirstIterator (com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator)8 SingleQuantifierEliminationProblem (com.sri.ai.grinder.api.SingleQuantifierEliminationProblem)8 Theory (com.sri.ai.grinder.api.Theory)8 DefaultSingleQuantifierEliminationProblem (com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem)8 AssignmentMapsIterator (com.sri.ai.grinder.helper.AssignmentMapsIterator)8