Search in sources :

Example 36 with DifferenceArithmeticTheory

use of com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory in project aic-expresso by aic-sri-international.

the class CompoundTheoryWithDifferenceArithmeticTest method makeTheoryTestingSupport.

@Override
protected TheoryTestingSupport makeTheoryTestingSupport() {
    TheoryTestingSupport result = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
    // using different testing variables and types to test distribution of testing information
    // to sub constraint theories.
    Categorical booleanType = BOOLEAN_TYPE;
    Categorical dogsType = new Categorical("Dogs", 4, arrayList(parse("fido"), parse("rex")));
    IntegerInterval oneTwoThree = new IntegerInterval(1, 3);
    Map<String, Type> variablesAndTypes = map("F", booleanType, "G", booleanType, "R", dogsType, "S", dogsType, "T", oneTwoThree, "U", oneTwoThree);
    result.setVariableNamesAndTypesForTesting(variablesAndTypes);
    return result;
}
Also used : EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) Type(com.sri.ai.expresso.api.Type) AbstractTheoryTestingSupport(com.sri.ai.grinder.sgdpllt.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) Categorical(com.sri.ai.expresso.type.Categorical) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)

Example 37 with DifferenceArithmeticTheory

use of com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory in project aic-expresso by aic-sri-international.

the class UnificationStepSolverTest method differenceArithmeticTest.

@Test
public void differenceArithmeticTest() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(seededRandom, new DifferenceArithmeticTheory(true, true));
    // NOTE: passing explicit FunctionTypes will prevent the general variables' argument types being randomly changed.
    theoryTestingSupport.setVariableNamesAndTypesForTesting(map("I", TESTING_INTEGER_INTERVAL_TYPE, "J", TESTING_INTEGER_INTERVAL_TYPE, "K", TESTING_INTEGER_INTERVAL_TYPE, "unary_dar", new FunctionType(TESTING_INTEGER_INTERVAL_TYPE, TESTING_INTEGER_INTERVAL_TYPE), "binary_dar", new FunctionType(TESTING_INTEGER_INTERVAL_TYPE, TESTING_INTEGER_INTERVAL_TYPE, TESTING_INTEGER_INTERVAL_TYPE)));
    Context rootContext = theoryTestingSupport.makeContextWithTestingInformation();
    UnificationStepSolver unificationStepSolver = new UnificationStepSolver(parse("unary_dar(I)"), parse("unary_dar(I)"));
    StepSolver.Step<Boolean> step = unificationStepSolver.step(rootContext);
    Assert.assertEquals(false, step.itDepends());
    Assert.assertEquals(true, step.getValue());
    unificationStepSolver = new UnificationStepSolver(parse("unary_dar(I)"), parse("unary_dar(J)"));
    step = unificationStepSolver.step(rootContext);
    Assert.assertEquals(true, step.itDepends());
    Assert.assertEquals(Expressions.parse("I = J"), step.getSplitter());
    Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsTrue().step(rootContext).itDepends());
    Assert.assertEquals(true, step.getStepSolverForWhenSplitterIsTrue().step(rootContext).getValue());
    Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsFalse().step(rootContext).itDepends());
    Assert.assertEquals(false, step.getStepSolverForWhenSplitterIsFalse().step(rootContext).getValue());
    Context localTestContext = rootContext.conjoinWithConjunctiveClause(parse("I = 0 and J = 1"), rootContext);
    step = unificationStepSolver.step(localTestContext);
    Assert.assertEquals(false, step.itDepends());
    Assert.assertEquals(false, step.getValue());
    unificationStepSolver = new UnificationStepSolver(parse("unary_dar(I)"), parse("unary_dar(0)"));
    localTestContext = rootContext.conjoinWithConjunctiveClause(parse("I = 0"), rootContext);
    step = unificationStepSolver.step(localTestContext);
    Assert.assertEquals(false, step.itDepends());
    Assert.assertEquals(true, step.getValue());
    localTestContext = rootContext.conjoinWithConjunctiveClause(parse("I = 1"), rootContext);
    step = unificationStepSolver.step(localTestContext);
    Assert.assertEquals(false, step.itDepends());
    Assert.assertEquals(false, step.getValue());
    unificationStepSolver = new UnificationStepSolver(parse("binary_dar(I, unary_dar(I))"), parse("binary_dar(unary_dar(J), J)"));
    step = unificationStepSolver.step(rootContext);
    Assert.assertEquals(true, step.itDepends());
    Assert.assertEquals(Expressions.parse("I = unary_dar(J)"), step.getSplitter());
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) FunctionType(com.sri.ai.expresso.type.FunctionType) UnificationStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.UnificationStepSolver) StepSolver(com.sri.ai.grinder.sgdpllt.api.StepSolver) UnificationStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.UnificationStepSolver) Test(org.junit.Test)

Example 38 with DifferenceArithmeticTheory

use of com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory in project aic-expresso by aic-sri-international.

the class UnificationStepSolverTest method advancedDifferenceArithmeticTest.

@Ignore("TODO - context implementation currently does not support these more advanced/indirect comparisons")
@Test
public void advancedDifferenceArithmeticTest() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(seededRandom, new DifferenceArithmeticTheory(true, true));
    // NOTE: passing explicit FunctionTypes will prevent the general variables' argument types being randomly changed.
    theoryTestingSupport.setVariableNamesAndTypesForTesting(map("I", TESTING_INTEGER_INTERVAL_TYPE, "J", TESTING_INTEGER_INTERVAL_TYPE, "K", TESTING_INTEGER_INTERVAL_TYPE, "unary_dar/1", new FunctionType(TESTING_INTEGER_INTERVAL_TYPE, TESTING_INTEGER_INTERVAL_TYPE), "binary_dar/2", new FunctionType(TESTING_INTEGER_INTERVAL_TYPE, TESTING_INTEGER_INTERVAL_TYPE, TESTING_INTEGER_INTERVAL_TYPE)));
    Context rootContext = theoryTestingSupport.makeContextWithTestingInformation();
    UnificationStepSolver unificationStepSolver = new UnificationStepSolver(parse("binary_dar(I, unary_dar(I))"), parse("binary_dar(unary_dar(J), J)"));
    Context localTestContext = rootContext.conjoinWithConjunctiveClause(parse("I = 0 and J = 1 and unary_dar(J) = 0 and unary_dar(I) = 1"), rootContext);
    StepSolver.Step<Boolean> step = unificationStepSolver.step(localTestContext);
    Assert.assertEquals(false, step.itDepends());
    Assert.assertEquals(true, step.getValue());
    localTestContext = rootContext.conjoinWithConjunctiveClause(parse("I = 1 and J = 1 and unary_dar(J) = 0 and unary_dar(I) = 1"), rootContext);
    step = unificationStepSolver.step(localTestContext);
    Assert.assertEquals(false, step.itDepends());
    Assert.assertEquals(false, step.getValue());
    localTestContext = rootContext.conjoinWithConjunctiveClause(parse("I = 0 and J = 1 and unary_dar(1) = 0 and unary_dar(0) = 1"), rootContext);
    step = unificationStepSolver.step(localTestContext);
    Assert.assertEquals(false, step.itDepends());
    Assert.assertEquals(true, step.getValue());
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) FunctionType(com.sri.ai.expresso.type.FunctionType) UnificationStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.UnificationStepSolver) StepSolver(com.sri.ai.grinder.sgdpllt.api.StepSolver) UnificationStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.UnificationStepSolver) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 39 with DifferenceArithmeticTheory

use of com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory in project aic-expresso by aic-sri-international.

the class EvaluationTest method testEvaluationOfGroupOperationsOnSets.

@Test
public void testEvaluationOfGroupOperationsOnSets() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
    Map<String, Type> variablesAndTypes = new LinkedHashMap<>(theoryTestingSupport.getVariableNamesAndTypesForTesting());
    Type booleanType = variablesAndTypes.get("P");
    variablesAndTypes.put("S", booleanType);
    variablesAndTypes.put("T", booleanType);
    variablesAndTypes.put("U", booleanType);
    theoryTestingSupport.setVariableNamesAndTypesForTesting(variablesAndTypes);
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    String expressionString;
    Expression expected;
    expressionString = "sum( {{ (on I in 1..10) 3 : I != 4 and P }} )";
    expected = parse("if P then 27 else 0");
    runTest(expressionString, expected, context);
    expressionString = "sum( {{ (on ) 3 : I != 4 and P }} )";
    expected = parse("if I != 4 then if P then 3 else 0 else 0");
    runTest(expressionString, expected, context);
    expressionString = "sum( {{ (on ) 3 : P and not P }} )";
    expected = parse("0");
    runTest(expressionString, expected, context);
    expressionString = "sum( {{ (on I in 1..10, J in 1..2) 3 : I != 4 }} )";
    expected = parse("54");
    runTest(expressionString, expected, context);
    expressionString = "sum( {{ (on I in 1..10, P in Boolean) 3 : I != 4 }} )";
    expected = parse("54");
    runTest(expressionString, expected, context);
    expressionString = "max( {{ (on I in 1..10) 3 : I != 4 and P }} )";
    expected = parse("if P then 3 else -infinity");
    runTest(expressionString, expected, context);
    expressionString = "max( {{ (on ) 3 : I != 4 and P }} )";
    expected = parse("if I != 4 then if P then 3 else -infinity else -infinity");
    runTest(expressionString, expected, context);
    expressionString = "max( {{ (on ) 3 : P and not P }} )";
    expected = parse("-infinity");
    runTest(expressionString, expected, context);
    expressionString = "max( {{ (on I in 1..10, J in 1..2) 3 : I != 4 }} )";
    expected = parse("3");
    runTest(expressionString, expected, context);
    expressionString = "max( {{ (on I in 1..10, P in Boolean) 3 : I != 4 }} )";
    expected = parse("3");
    runTest(expressionString, expected, context);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 40 with DifferenceArithmeticTheory

use of com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory in project aic-expresso by aic-sri-international.

the class EvaluationTest method testEvaluationOfFunctionApplications.

@Test
public void testEvaluationOfFunctionApplications() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
    Map<String, Type> variablesAndTypes = new LinkedHashMap<>(theoryTestingSupport.getVariableNamesAndTypesForTesting());
    Type booleanType = variablesAndTypes.get("P");
    variablesAndTypes.put("S", booleanType);
    variablesAndTypes.put("T", booleanType);
    variablesAndTypes.put("U", booleanType);
    theoryTestingSupport.setVariableNamesAndTypesForTesting(variablesAndTypes);
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    String expressionString;
    Expression expected;
    expressionString = "0";
    expected = parse("0");
    runTest(expressionString, expected, context);
    expressionString = "I > J";
    expected = parse("I > J");
    runTest(expressionString, expected, context);
    expressionString = "I > J and I < J";
    expected = parse("false");
    runTest(expressionString, expected, context);
    expressionString = "(if I > J then 1 else 2) + (if I <= J then 30 else 40)";
    expected = parse("if I > J then 41 else 32");
    runTest(expressionString, expected, context);
    expressionString = "(if I > J then 1 else 2) + (if I <= J then 3 else 4)";
    expected = parse("5");
    runTest(expressionString, expected, context);
    expressionString = "(if I > J then if P or Q then 1 else 2 else 5) + (if I <= J then 3 else if not Q then 4 else -3)";
    expected = parse("if I > J then if P then if not Q then 5 else -2 else if Q then -2 else 6 else 8");
    runTest(expressionString, expected, context);
    expressionString = "(if I > J then if P or X = a or Y != b then 1 else 2 else 5) + (if I <= J then 3 else if not (X != a or Y = c and Q) then 4 else -3)";
    expected = parse("if I > J then if P then if X != a then -2 else if Y = c then if Q then -2 else 5 else 5 else if X = a then if Y = c then if Q then -2 else 5 else 5 else if Y != b then -2 else -1 else 8");
    runTest(expressionString, expected, context);
    expressionString = "if P and Q and R then 1 else 0";
    expected = parse("if P then if Q then if R then 1 else 0 else 0 else 0");
    runTest(expressionString, expected, context);
    expressionString = "if P and Q and R and S and T and U then 1 else 0";
    expected = parse("if P then if Q then if R then if S then if T then if U then 1 else 0 else 0 else 0 else 0 else 0 else 0");
    runTest(expressionString, expected, context);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

DifferenceArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory)46 CompoundTheory (com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)34 Context (com.sri.ai.grinder.sgdpllt.api.Context)28 Expression (com.sri.ai.expresso.api.Expression)25 Test (org.junit.Test)25 PropositionalTheory (com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory)24 TheoryTestingSupport (com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport)23 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)22 EqualityTheory (com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory)20 TupleTheory (com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory)16 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)15 Before (org.junit.Before)11 Type (com.sri.ai.expresso.api.Type)8 LinearRealArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory)8 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)7 FunctionType (com.sri.ai.expresso.type.FunctionType)5 LinkedHashMap (java.util.LinkedHashMap)5 StepSolver (com.sri.ai.grinder.sgdpllt.api.StepSolver)4 Rewriter (com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter)4 Categorical (com.sri.ai.expresso.type.Categorical)3