Search in sources :

Example 36 with TheoryTestingSupport

use of com.sri.ai.grinder.tester.TheoryTestingSupport 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 37 with TheoryTestingSupport

use of com.sri.ai.grinder.tester.TheoryTestingSupport 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 38 with TheoryTestingSupport

use of com.sri.ai.grinder.tester.TheoryTestingSupport 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 39 with TheoryTestingSupport

use of com.sri.ai.grinder.tester.TheoryTestingSupport 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)

Example 40 with TheoryTestingSupport

use of com.sri.ai.grinder.tester.TheoryTestingSupport in project aic-expresso by aic-sri-international.

the class CompoundTheoryTestingSupport method aggregateTestingInformation.

// 
// 
private void aggregateTestingInformation() throws Error {
    Map<String, Type> variableNamesAndTypesForTesting = new LinkedHashMap<>();
    for (TheoryTestingSupport theoryTestingSupport : getSubConstraintTheoryTestingSupports()) {
        Set<Entry<String, Type>> variableNamesAndTypeNameEntries = theoryTestingSupport.getVariableNamesAndTypesForTesting().entrySet();
        for (Map.Entry<String, Type> variableNameAndTypeName : variableNamesAndTypeNameEntries) {
            String variableName = variableNameAndTypeName.getKey();
            Type type = variableNameAndTypeName.getValue();
            if (variableNamesAndTypesForTesting.containsKey(variableName)) {
                variableName = primedUntilUnique(variableName, s -> variableNamesAndTypesForTesting.containsKey(s.toString()));
            }
            variableNamesAndTypesForTesting.put(variableName, type);
        }
    }
    setVariableNamesAndTypesForTesting(variableNamesAndTypesForTesting);
}
Also used : AbstractTheoryTestingSupport(com.sri.ai.grinder.core.constraint.AbstractTheoryTestingSupport) Type(com.sri.ai.expresso.api.Type) Collection(java.util.Collection) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) Set(java.util.Set) Util.check(com.sri.ai.util.Util.check) Random(java.util.Random) Expression(com.sri.ai.expresso.api.Expression) Beta(com.google.common.annotations.Beta) LinkedHashMap(java.util.LinkedHashMap) FunctionType(com.sri.ai.expresso.type.FunctionType) Util.map(com.sri.ai.util.Util.map) Theory(com.sri.ai.grinder.api.Theory) Expressions.primedUntilUnique(com.sri.ai.expresso.helper.Expressions.primedUntilUnique) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) Entry(java.util.Map.Entry) Context(com.sri.ai.grinder.api.Context) Type(com.sri.ai.expresso.api.Type) FunctionType(com.sri.ai.expresso.type.FunctionType) Entry(java.util.Map.Entry) AbstractTheoryTestingSupport(com.sri.ai.grinder.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

TheoryTestingSupport (com.sri.ai.grinder.tester.TheoryTestingSupport)49 Test (org.junit.Test)42 Context (com.sri.ai.grinder.api.Context)36 Expression (com.sri.ai.expresso.api.Expression)26 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)22 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)20 PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)17 FunctionType (com.sri.ai.expresso.type.FunctionType)13 Type (com.sri.ai.expresso.api.Type)12 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)11 StepSolver (com.sri.ai.grinder.api.StepSolver)10 UnificationStepSolver (com.sri.ai.grinder.theory.base.UnificationStepSolver)10 AbstractTheoryTestingSupport (com.sri.ai.grinder.core.constraint.AbstractTheoryTestingSupport)9 LinearRealArithmeticTheory (com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory)9 LinkedHashMap (java.util.LinkedHashMap)7 Constraint (com.sri.ai.grinder.api.Constraint)6 CompleteMultiVariableContext (com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext)6 Ignore (org.junit.Ignore)5 Categorical (com.sri.ai.expresso.type.Categorical)4 TrueContext (com.sri.ai.grinder.core.TrueContext)4