Search in sources :

Example 91 with Context

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

the class CompoundTheoryWithoutDifferenceArithmeticTest method runCompleteSatisfiabilityTest.

/**
 * @param conjunction
 * @param expected
 */
private void runCompleteSatisfiabilityTest(String conjunction, Expression expected, Map<String, Type> variableNamesAndTypesForTesting) {
    TheoryTestingSupport equalityTheoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new EqualityTheory(true, true));
    equalityTheoryTestingSupport.setVariableNamesAndTypesForTesting(variableNamesAndTypesForTesting);
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), equalityTheoryTestingSupport, TheoryTestingSupport.make(makeRandom(), new PropositionalTheory()));
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    Constraint constraint = new CompleteMultiVariableContext(theoryTestingSupport.getTheory(), context);
    for (Expression literal : And.getConjuncts(parse(conjunction))) {
        constraint = constraint.conjoin(literal, context);
    }
    assertEquals(expected, constraint);
}
Also used : CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) Constraint(com.sri.ai.grinder.api.Constraint) Expression(com.sri.ai.expresso.api.Expression) AbstractTheoryTestingSupport(com.sri.ai.grinder.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory)

Example 92 with Context

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

the class SummationOnDifferenceArithmeticAndPolynomialStepSolverTest method polynomialBodyWithADifferentVariableTest.

@Test
public void polynomialBodyWithADifferentVariableTest() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new DifferenceArithmeticTheory(true, true));
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    Expression variable;
    String constraintString;
    Expression body;
    Expression expected;
    variable = parse("I");
    body = parse("I^2 - J + 1");
    constraintString = "true";
    expected = parse("-(5*J) + 35");
    runTest(variable, constraintString, body, expected, context);
    constraintString = "I != 3";
    expected = parse("-(4*J) + 25");
    runTest(variable, constraintString, body, expected, context);
    constraintString = "I < 3";
    expected = parse("-(3*J) + 8");
    runTest(variable, constraintString, body, expected, context);
    constraintString = "false";
    expected = parse("0");
    runTest(variable, constraintString, body, expected, context);
    constraintString = "I > 3 and I < 3";
    expected = parse("0");
    runTest(variable, constraintString, body, expected, context);
    constraintString = "I > 1 and I < 3 and I != 2";
    expected = parse("0");
    runTest(variable, constraintString, body, expected, context);
}
Also used : Context(com.sri.ai.grinder.api.Context) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) Test(org.junit.Test)

Example 93 with Context

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

the class SummationOnDifferenceArithmeticAndPolynomialStepSolverTest method polynomialBodyAndConstraintWithADifferentVariableTest.

@Test
public void polynomialBodyAndConstraintWithADifferentVariableTest() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new DifferenceArithmeticTheory(true, true));
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    Expression variable;
    String constraintString;
    Expression body;
    Expression expected;
    variable = parse("I");
    body = parse("I^2 - J + 1");
    constraintString = "I != J";
    expected = parse("-(J^2) -(4*J) + 34");
    runTest(variable, constraintString, body, expected, context);
    constraintString = "I <= J and I != J";
    expected = parse("(1/3 * J ^ 3 - 1.5 * J ^ 2) + 7/6 * J");
    runTest(variable, constraintString, body, expected, context);
    constraintString = "I < J and I > J";
    expected = parse("0");
    runTest(variable, constraintString, body, expected, context);
}
Also used : Context(com.sri.ai.grinder.api.Context) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) Test(org.junit.Test)

Example 94 with Context

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

the class AbstractEqualityConstraintTest method testSatisfiabilitySpecialCases.

@Test
public void testSatisfiabilitySpecialCases() {
    String conjunction;
    // looks unsatisfiable for type size 5, but it is not
    conjunction = "X != a and X != b and X != c and X != Y and X != Z";
    TheoryTestingSupport theoryTestingSupport = makeTheoryTestingSupport();
    Constraint constraint = new SingleVariableEqualityConstraint(parse("X"), false, theoryTestingSupport.getTheory());
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    constraint = constraint.conjoinWithConjunctiveClause(parse(conjunction), context);
    // satisfiable if either Y or Z is equal to a, b, c, or each other.
    Assert.assertNotEquals(null, constraint);
}
Also used : SingleVariableEqualityConstraint(com.sri.ai.grinder.theory.equality.SingleVariableEqualityConstraint) Context(com.sri.ai.grinder.api.Context) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) SingleVariableEqualityConstraint(com.sri.ai.grinder.theory.equality.SingleVariableEqualityConstraint) Constraint(com.sri.ai.grinder.api.Constraint) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) AbstractTheoryIncludingEqualityTest(com.sri.ai.test.grinder.theory.base.AbstractTheoryIncludingEqualityTest) Test(org.junit.Test)

Example 95 with Context

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

the class AbstractEqualityConstraintTest method runCompleteSatisfiabilityTest.

/**
 * @param conjunction
 * @param expected
 */
private void runCompleteSatisfiabilityTest(String conjunction, Expression expected, TheoryTestingSupport theoryTestingSupport) {
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    Constraint constraint = new CompleteMultiVariableContext(theoryTestingSupport.getTheory(), context);
    for (Expression literal : And.getConjuncts(parse(conjunction))) {
        constraint = constraint.conjoin(literal, context);
        if (constraint.isContradiction()) {
            break;
        }
    }
    if (expected == null && !constraint.isContradiction()) {
        throw new AssertionError("Expected null but was <" + constraint + ">");
    } else if (expected != null && constraint.isContradiction()) {
        throw new AssertionError("Expected <" + expected + "> but was null");
    } else if (expected != null && !constraint.isContradiction() && !expected.equals(constraint)) {
        Simplifier interpreter = (e, c) -> theoryTestingSupport.getTheory().evaluate(e, c);
        // Simplifier interpreter = new Evaluator(theoryTestingSupport.getTheory());
        Expression equivalenceDefinition = apply(EQUIVALENCE, expected, constraint);
        Expression universallyQuantified = universallyQuantifyFreeVariables(equivalenceDefinition, context);
        Expression equivalent = interpreter.apply(universallyQuantified, context);
        if (equivalent.equals(FALSE)) {
            throw new Error("Expected <" + expected + "> but got <" + constraint + ">, which is not equivalent either");
        }
    }
}
Also used : Context(com.sri.ai.grinder.api.Context) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) FALSE(com.sri.ai.expresso.helper.Expressions.FALSE) Categorical(com.sri.ai.expresso.type.Categorical) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) HashMap(java.util.HashMap) Expression(com.sri.ai.expresso.api.Expression) GrinderUtil.universallyQuantifyFreeVariables(com.sri.ai.grinder.helper.GrinderUtil.universallyQuantifyFreeVariables) SGDPLLTTester(com.sri.ai.grinder.tester.SGDPLLTTester) And(com.sri.ai.grinder.library.boole.And) Util.map(com.sri.ai.util.Util.map) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) SingleVariableEqualityConstraint(com.sri.ai.grinder.theory.equality.SingleVariableEqualityConstraint) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) Context(com.sri.ai.grinder.api.Context) AbstractTheoryIncludingEqualityTest(com.sri.ai.test.grinder.theory.base.AbstractTheoryIncludingEqualityTest) Util.arrayList(com.sri.ai.util.Util.arrayList) Max(com.sri.ai.grinder.group.Max) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) Type(com.sri.ai.expresso.api.Type) EQUIVALENCE(com.sri.ai.grinder.library.FunctorConstants.EQUIVALENCE) Test(org.junit.Test) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) Beta(com.google.common.annotations.Beta) Sum(com.sri.ai.grinder.group.Sum) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) Assert(org.junit.Assert) Constraint(com.sri.ai.grinder.api.Constraint) SingleVariableEqualityConstraint(com.sri.ai.grinder.theory.equality.SingleVariableEqualityConstraint) Constraint(com.sri.ai.grinder.api.Constraint) Expression(com.sri.ai.expresso.api.Expression) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier)

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