Search in sources :

Example 26 with TheoryTestingSupport

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

the class SummationOnDifferenceArithmeticAndPolynomialStepSolverTest method polynomialBodyTest.

@Test
public void polynomialBodyTest() {
    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 - I + 1");
    constraintString = "true";
    expected = parse("25");
    runTest(variable, constraintString, body, expected, context);
    constraintString = "I != 3";
    expected = parse("18");
    runTest(variable, constraintString, body, expected, context);
    constraintString = "I < 3";
    expected = parse("5");
    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.sgdpllt.api.Context) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) Test(org.junit.Test)

Example 27 with TheoryTestingSupport

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

the class ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolverTest method test.

@Test
public void test() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new DifferenceArithmeticTheory(true, true));
    theoryTestingSupport.setVariableNamesAndTypesForTesting(map("I", new IntegerInterval(0, 4), "J", new IntegerInterval(0, 4), "K", new IntegerInterval(0, 4)));
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    Expression variable;
    String constraintString;
    Expression expected;
    variable = parse("I");
    constraintString = "true";
    expected = parse("aboveAndUpTo(-1, 4)");
    runTest(variable, constraintString, expected, context);
    variable = parse("I");
    constraintString = "false";
    expected = parse("{}");
    runTest(variable, constraintString, expected, context);
    variable = parse("I");
    constraintString = "I < 3 and J > I";
    expected = parse("if 3 > J then if 0 < J then aboveAndUpTo(-1, J - 1) else {} else aboveAndUpTo(-1, 2)");
    runTest(variable, constraintString, expected, context);
    variable = parse("I");
    constraintString = "I < 3 and J > I and I != 2";
    expected = parse("if 3 > J then if 0 < J then aboveAndUpTo(-1, J - 1) else {} else aboveAndUpTo(-1, 2) - { 2 }");
    runTest(variable, constraintString, expected, context);
    variable = parse("I");
    constraintString = "I < 3 and J > I and I != 2 and I != K";
    expected = parse("if 3 > J then if 0 < J then if K + 1 <= J then aboveAndUpTo(-1, J - 1) - { K } else aboveAndUpTo(-1, J - 1) else {} else if K <= 2 then if 2 = K then aboveAndUpTo(-1, 2) - { K } else aboveAndUpTo(-1, 2) - { 2, K } else aboveAndUpTo(-1, 2) - { 2 }");
    runTest(variable, constraintString, expected, context);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) Test(org.junit.Test)

Example 28 with TheoryTestingSupport

use of com.sri.ai.grinder.sgdpllt.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.sgdpllt.theory.equality.SingleVariableEqualityConstraint) CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) Context(com.sri.ai.grinder.sgdpllt.api.Context) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) SingleVariableEqualityConstraint(com.sri.ai.grinder.sgdpllt.theory.equality.SingleVariableEqualityConstraint) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) Test(org.junit.Test) AbstractTheoryIncludingEqualityTest(com.sri.ai.test.grinder.sgdpllt.theory.base.AbstractTheoryIncludingEqualityTest)

Example 29 with TheoryTestingSupport

use of com.sri.ai.grinder.sgdpllt.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 : CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) Context(com.sri.ai.grinder.sgdpllt.api.Context) CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) FALSE(com.sri.ai.expresso.helper.Expressions.FALSE) Categorical(com.sri.ai.expresso.type.Categorical) EQUIVALENCE(com.sri.ai.grinder.sgdpllt.library.FunctorConstants.EQUIVALENCE) HashMap(java.util.HashMap) Expression(com.sri.ai.expresso.api.Expression) GrinderUtil.universallyQuantifyFreeVariables(com.sri.ai.grinder.helper.GrinderUtil.universallyQuantifyFreeVariables) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) Util.map(com.sri.ai.util.Util.map) SGDPLLTTester(com.sri.ai.grinder.sgdpllt.tester.SGDPLLTTester) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) And(com.sri.ai.grinder.sgdpllt.library.boole.And) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) Util.arrayList(com.sri.ai.util.Util.arrayList) Type(com.sri.ai.expresso.api.Type) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) Test(org.junit.Test) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) Context(com.sri.ai.grinder.sgdpllt.api.Context) Sum(com.sri.ai.grinder.sgdpllt.group.Sum) AbstractTheoryIncludingEqualityTest(com.sri.ai.test.grinder.sgdpllt.theory.base.AbstractTheoryIncludingEqualityTest) Beta(com.google.common.annotations.Beta) Max(com.sri.ai.grinder.sgdpllt.group.Max) SingleVariableEqualityConstraint(com.sri.ai.grinder.sgdpllt.theory.equality.SingleVariableEqualityConstraint) Assert(org.junit.Assert) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) SingleVariableEqualityConstraint(com.sri.ai.grinder.sgdpllt.theory.equality.SingleVariableEqualityConstraint) Expression(com.sri.ai.expresso.api.Expression) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier)

Example 30 with TheoryTestingSupport

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

the class LinearRealArithmeticTheoryTest method testSummation.

@Test
public void testSummation() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new LinearRealArithmeticTheory(true, true));
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    Simplifier simplifier = (e, c) -> theoryTestingSupport.getTheory().simplify(e, c);
    Expression variable;
    String constraintString;
    String bodyString;
    Expression expected;
    variable = parse("X");
    constraintString = "true";
    bodyString = "1";
    expected = parse("4");
    runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
    variable = parse("X");
    constraintString = "X < 3 and X != 2";
    bodyString = "1";
    expected = parse("3");
    runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
    variable = parse("X");
    constraintString = "X < Y and X != 2";
    bodyString = "1";
    expected = parse("if Y > 0 then Y else 0");
    runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
    variable = parse("X");
    constraintString = "X < 3 and X != 2";
    bodyString = "Y";
    expected = parse("3*Y");
    runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
    variable = parse("X");
    constraintString = "X < 3 and X != 2";
    bodyString = "X";
    expected = parse("4.5");
    runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
    variable = parse("X");
    constraintString = "X < 3 and X != 2 and X = 2";
    bodyString = "Y";
    expected = parse("0");
    runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
    variable = parse("X");
    constraintString = "X < Y and X != 2";
    bodyString = "X";
    expected = parse("if Y > 0 then 0.5*Y^2 else 0");
    runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
    variable = parse("X");
    constraintString = "X < Y and X != 2";
    bodyString = "Y";
    expected = parse("if Y > 0 then Y^2 else 0");
    runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
    variable = parse("X");
    constraintString = "X < Y and X != 2";
    bodyString = "X + Y";
    expected = parse("if Y > 0 then 1.5*Y^2 else 0");
    runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
//		variable = parse("X");
//		constraintString = "X < Y and X != 2";
//		bodyString = "X^2 + Y";
//		expected = parse("if Y > 0 then 0.333333333*Y^3 + Y^2 else 0");
//		runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver) BinaryFunction(com.sri.ai.util.base.BinaryFunction) ExpressionStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionStepSolver) Function(com.google.common.base.Function) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) Random(java.util.Random) Test(org.junit.Test) Expression(com.sri.ai.expresso.api.Expression) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) Context(com.sri.ai.grinder.sgdpllt.api.Context) LinearRealArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory) Beta(com.google.common.annotations.Beta) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) SummationOnLinearRealArithmeticAndPolynomialStepSolver(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SummationOnLinearRealArithmeticAndPolynomialStepSolver) Assert.assertEquals(org.junit.Assert.assertEquals) SatisfiabilityOfSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SatisfiabilityOfSingleVariableLinearRealArithmeticConstraintStepSolver) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) LinearRealArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory) Simplifier(com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier) Test(org.junit.Test)

Aggregations

TheoryTestingSupport (com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport)50 Test (org.junit.Test)42 Context (com.sri.ai.grinder.sgdpllt.api.Context)36 Expression (com.sri.ai.expresso.api.Expression)27 DifferenceArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory)23 EqualityTheory (com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory)21 PropositionalTheory (com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory)18 FunctionType (com.sri.ai.expresso.type.FunctionType)13 Type (com.sri.ai.expresso.api.Type)12 CompoundTheory (com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)12 StepSolver (com.sri.ai.grinder.sgdpllt.api.StepSolver)10 UnificationStepSolver (com.sri.ai.grinder.sgdpllt.theory.base.UnificationStepSolver)10 AbstractTheoryTestingSupport (com.sri.ai.grinder.sgdpllt.core.constraint.AbstractTheoryTestingSupport)9 LinearRealArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory)9 Constraint (com.sri.ai.grinder.sgdpllt.api.Constraint)7 LinkedHashMap (java.util.LinkedHashMap)7 CompleteMultiVariableContext (com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext)6 Ignore (org.junit.Ignore)6 Categorical (com.sri.ai.expresso.type.Categorical)4 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)4