Search in sources :

Example 56 with Context

use of com.sri.ai.grinder.api.Context 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.api.Context) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) FunctionType(com.sri.ai.expresso.type.FunctionType) 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 57 with Context

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

Example 58 with Context

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

the class CompoundTheoryWithDifferenceArithmeticTest 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 : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) 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 59 with Context

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

the class CompoundTheoryWithoutDifferenceArithmeticTest method basicTests.

@Test
public void basicTests() {
    TheoryTestingSupport theoryTestingSupport = makeTheoryTestingSupport();
    Expression condition = parse("X = Y and Y = X and P and not Q and P and X = a and X != b");
    Context context = theoryTestingSupport.extendWithTestingInformation(new TrueContext(theoryTestingSupport.getTheory()));
    Constraint constraint = new CompleteMultiVariableContext(theoryTestingSupport.getTheory(), context);
    constraint = constraint.conjoin(condition, context);
    Expression expected = parse("(Y = a) and not Q and P and (X = Y)");
    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) Expression(com.sri.ai.expresso.api.Expression) Constraint(com.sri.ai.grinder.api.Constraint) AbstractTheoryTestingSupport(com.sri.ai.grinder.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) TrueContext(com.sri.ai.grinder.core.TrueContext) Test(org.junit.Test) AbstractTheoryTest(com.sri.ai.test.grinder.theory.base.AbstractTheoryTest)

Example 60 with Context

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

the class BasicTest method isLiteralTests.

@Test
public void isLiteralTests() {
    Expression expression;
    boolean expected;
    Context context = new TrueContext();
    context = context.makeCloneWithAdditionalRegisteredSymbolsAndTypes(map(parse("X"), parse("Integer"), parse("Y"), parse("Integer"), parse("Z"), parse("Integer"), parse("W"), parse("Real")));
    // Difference arithmetic literals include up to two integer variables with coefficient
    // -1 or +1 (each no more than once) and an optional numeric constant.
    expression = parse("1 >= 0");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X >= 0");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("-X >= 0");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X - Y >= 0");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X + Y >= 0");
    expected = false;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X - Y + 3 >= 0");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X - Y + 3 + Z >= Z");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X - X + X - Y + Y - Z + 1 - 5 >= 0");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    // Final variables coefficients must be 1 or -1
    expression = parse("X >= -X");
    expected = false;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X - Y >= -X");
    expected = false;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X + Y >= -X");
    expected = false;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X - Y + 3 >= -X");
    expected = false;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X - Y + 3 + Z >= Z - X");
    expected = false;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X - X + X - Y + Y - Z + 1 - 5 >= -X");
    expected = false;
    runIsLiteralTest(expression, expected, context);
    // Final variables coefficients must be 1 or -1
    // Coefficients may be present in the initial expression
    expression = parse("X*2 >= 0");
    expected = false;
    runIsLiteralTest(expression, expected, context);
    expression = parse("2*X - Y >= 0");
    expected = false;
    runIsLiteralTest(expression, expected, context);
    expression = parse("2*X - Y - X >= 0");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("2*X - Y >= X");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X + Y >= 2*Y");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("3*X - Y + 3 >= X");
    expected = false;
    runIsLiteralTest(expression, expected, context);
    expression = parse("X - Y + 3 + 2*Z >= 2*Z");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("2*X - 2*X + X - 2*Y + 2*Y - 2*Z + 1 - 5 >= -Z");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    // Variables must be integer
    expression = parse("2*X - 2*X + X - 2*W + 2*W - 2*Z + 1 - 5 >= -Z");
    expected = true;
    runIsLiteralTest(expression, expected, context);
    expression = parse("W - X >= 0");
    expected = false;
    runIsLiteralTest(expression, expected, context);
    // Sides should be polynomials
    expression = parse("f()*2 > 0");
    expected = false;
    runIsLiteralTest(expression, expected, context);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Expression(com.sri.ai.expresso.api.Expression) TrueContext(com.sri.ai.grinder.core.TrueContext) Test(org.junit.Test)

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