Search in sources :

Example 11 with CompoundTheory

use of com.sri.ai.grinder.theory.compound.CompoundTheory in project aic-expresso by aic-sri-international.

the class ExhaustiveTest method runTest.

private void runTest(Rewriter rewriter, Expression initial, Expression expected, Map<Expression, Expression> symbolsAndTypes) {
    CompoundTheory theory = new CompoundTheory(new PropositionalTheory(), new DifferenceArithmeticTheory(false, true));
    Context context = new TrueContext(theory);
    context = context.makeCloneWithAdditionalRegisteredSymbolsAndTypes(symbolsAndTypes);
    Rewriter exhaustive = new Exhaustive(rewriter);
    Expression solution = exhaustive.apply(initial, context);
    System.out.println("Solution: " + solution);
    assertEquals(expected, solution);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Expression(com.sri.ai.expresso.api.Expression) Exhaustive(com.sri.ai.grinder.rewriter.core.Exhaustive) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) Rewriter(com.sri.ai.grinder.rewriter.api.Rewriter) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) TrueContext(com.sri.ai.grinder.core.TrueContext)

Example 12 with CompoundTheory

use of com.sri.ai.grinder.theory.compound.CompoundTheory in project aic-expresso by aic-sri-international.

the class EvaluationTest method testEvaluationOfQuantifiersOverFunctions.

@Test
public void testEvaluationOfQuantifiersOverFunctions() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory(), new BruteForceFunctionTheory()));
    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 f in 0..2 -> Boolean)  if f(0) and f(1) then 2 else 3  :  f(2) }} )";
    // 2+3+3+3
    expected = parse("11");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in 1..2 -> Boolean, g in 1..2 -> Boolean)  if f(1) and g(2) then 2 else 3  :  f(2) }} )";
    expected = parse("22");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in 1..2 -> Boolean, g in 1..2 -> Boolean)  if f(1) and g(2) then 2 else 3 }} )";
    expected = parse("44");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in 1..2 -> Boolean, g in 1..2 -> Boolean)  if f(1) then 2 else 3 }} )";
    expected = parse("40");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in 1..2 -> Boolean)  if f(1) then 2 else 3 }} )";
    expected = parse("10");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in 1..2 -> 1..2)  if f(1) = 1 then 2 else 3 }} )";
    expected = parse("10");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in 1..2 -> 1..2)  f(1) }} )";
    expected = parse("6");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in '->'(1..2))  f() }} )";
    expected = parse("3");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in '->'(1..2))  f }} )";
    expected = parse("(lambda : 1) + (lambda : 2)");
    runTest(expressionString, expected, context);
    expressionString = "product( {{ (on f in 0..2 -> Boolean)  if f(0) and f(1) then 2 else 3  :  f(2) }} )";
    // 2*3*3*3
    expected = parse("54");
    runTest(expressionString, expected, context);
    expressionString = "max( {{ (on f in 0..2 -> Boolean)  if f(0) and f(1) then 2 else 3  :  f(2) }} )";
    expected = parse("3");
    runTest(expressionString, expected, context);
    expressionString = "| f in 0..2 -> Boolean : f(0) |";
    expected = parse("4");
    runTest(expressionString, expected, context);
    expressionString = "| f in 0..2 -> Boolean : f(0) |";
    expected = parse("4");
    runTest(expressionString, expected, context);
    expressionString = "| f in 0..2 -> Boolean : f(0) |";
    expected = parse("4");
    runTest(expressionString, expected, context);
    expressionString = "| f in x(0..2, 0..2) -> Boolean : f(0, 0) |";
    expected = parse("256");
    runTest(expressionString, expected, context);
    expressionString = "| f in 0..2 x 0..2 -> Boolean : f(0, 0) |";
    expected = parse("256");
    runTest(expressionString, expected, context);
    expressionString = "for all f in 0..2 -> Boolean : f(0)";
    expected = parse("false");
    runTest(expressionString, expected, context);
    expressionString = "for all f in x(0..2) -> Boolean : (f(0) or not f(0)) and P";
    expected = parse("P");
    runTest(expressionString, expected, context);
    expressionString = "there exists f in '->'(x(0..2), Boolean) : f(0)";
    expected = parse("true");
    runTest(expressionString, expected, context);
}
Also used : Context(com.sri.ai.grinder.api.Context) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) BruteForceFunctionTheory(com.sri.ai.grinder.theory.function.BruteForceFunctionTheory) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) 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) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 13 with CompoundTheory

use of com.sri.ai.grinder.theory.compound.CompoundTheory 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.api.Context) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) 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) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 14 with CompoundTheory

use of com.sri.ai.grinder.theory.compound.CompoundTheory in project aic-expresso by aic-sri-international.

the class UnificationStepSolverTest method advancedCompositeTest.

@Ignore("TODO - context implementation currently does not support these more advanced/indirect comparisons")
@Test
public void advancedCompositeTest() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(seededRandom, new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new LinearRealArithmeticTheory(false, true), new PropositionalTheory()));
    // NOTE: passing explicit FunctionTypes will prevent the general variables' argument types being randomly changed.
    theoryTestingSupport.setVariableNamesAndTypesForTesting(map("P", BOOLEAN_TYPE, "Q", BOOLEAN_TYPE, "R", BOOLEAN_TYPE, "unary_prop/1", new FunctionType(BOOLEAN_TYPE, BOOLEAN_TYPE), "binary_prop/2", new FunctionType(BOOLEAN_TYPE, BOOLEAN_TYPE, BOOLEAN_TYPE), "S", TESTING_CATEGORICAL_TYPE, "T", TESTING_CATEGORICAL_TYPE, "U", TESTING_CATEGORICAL_TYPE, "unary_eq/1", new FunctionType(TESTING_CATEGORICAL_TYPE, TESTING_CATEGORICAL_TYPE), "binary_eq/2", new FunctionType(TESTING_CATEGORICAL_TYPE, TESTING_CATEGORICAL_TYPE, TESTING_CATEGORICAL_TYPE), "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), "X", TESTING_REAL_INTERVAL_TYPE, "Y", TESTING_REAL_INTERVAL_TYPE, "Z", TESTING_REAL_INTERVAL_TYPE, "unary_lra/1", new FunctionType(TESTING_REAL_INTERVAL_TYPE, TESTING_REAL_INTERVAL_TYPE), "binary_lra/2", new FunctionType(TESTING_REAL_INTERVAL_TYPE, TESTING_REAL_INTERVAL_TYPE, TESTING_REAL_INTERVAL_TYPE)));
    Context rootContext = theoryTestingSupport.makeContextWithTestingInformation();
    UnificationStepSolver unificationStepSolver = new UnificationStepSolver(parse("binary_prop(P, unary_prop(P))"), parse("binary_prop(unary_prop(Q), Q)"));
    Context localTestContext = rootContext.conjoinWithConjunctiveClause(parse("not P and Q and not unary_prop(Q) and unary_prop(P)"), rootContext);
    StepSolver.Step<Boolean> step = unificationStepSolver.step(localTestContext);
    Assert.assertEquals(false, step.itDepends());
    Assert.assertEquals(true, step.getValue());
    localTestContext = rootContext.conjoinWithConjunctiveClause(parse("P and Q and not unary_prop(Q) and unary_prop(P)"), rootContext);
    step = unificationStepSolver.step(localTestContext);
    Assert.assertEquals(false, step.itDepends());
    Assert.assertEquals(false, step.getValue());
}
Also used : Context(com.sri.ai.grinder.api.Context) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) FunctionType(com.sri.ai.expresso.type.FunctionType) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) StepSolver(com.sri.ai.grinder.api.StepSolver) UnificationStepSolver(com.sri.ai.grinder.theory.base.UnificationStepSolver) UnificationStepSolver(com.sri.ai.grinder.theory.base.UnificationStepSolver) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 15 with CompoundTheory

use of com.sri.ai.grinder.theory.compound.CompoundTheory in project aic-expresso by aic-sri-international.

the class BasicTest method debug.

public void debug(Expression problem, Expression expectedSolution) {
    CompoundTheory theory = new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, false), new LinearRealArithmeticTheory(false, false), new TupleTheory(), new PropositionalTheory(), new BruteForceFunctionTheory());
    Context context = new TrueContext(theory);
    context = context.makeNewContextWithAddedType(BOOLEAN_TYPE);
    context = context.makeCloneWithAdditionalRegisteredSymbolsAndTypes(map(makeSymbol("P"), makeSymbol("Boolean")));
    Expression symbolicSolution = theory.evaluate(problem, context);
    println(symbolicSolution);
    Assert.assertEquals(expectedSolution, symbolicSolution);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) BruteForceFunctionTheory(com.sri.ai.grinder.theory.function.BruteForceFunctionTheory) Expression(com.sri.ai.expresso.api.Expression) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) TupleTheory(com.sri.ai.grinder.theory.tuple.TupleTheory) TrueContext(com.sri.ai.grinder.core.TrueContext)

Aggregations

CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)37 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)36 PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)27 TrueContext (com.sri.ai.grinder.core.TrueContext)25 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)23 TupleTheory (com.sri.ai.grinder.theory.tuple.TupleTheory)19 Context (com.sri.ai.grinder.api.Context)18 Expression (com.sri.ai.expresso.api.Expression)16 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)12 LinearRealArithmeticTheory (com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory)12 Before (org.junit.Before)12 Test (org.junit.Test)12 TheoryTestingSupport (com.sri.ai.grinder.tester.TheoryTestingSupport)11 Type (com.sri.ai.expresso.api.Type)8 Theory (com.sri.ai.grinder.api.Theory)6 LinkedHashMap (java.util.LinkedHashMap)5 Rewriter (com.sri.ai.grinder.rewriter.api.Rewriter)4 FunctionType (com.sri.ai.expresso.type.FunctionType)3 Recursive (com.sri.ai.grinder.rewriter.core.Recursive)3 BruteForceFunctionTheory (com.sri.ai.grinder.theory.function.BruteForceFunctionTheory)3