use of com.sri.ai.grinder.theory.equality.EqualityTheory in project aic-expresso by aic-sri-international.
the class NumberOfDistinctExpressionsStepSolverTest method test.
@Test
public void test() {
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new EqualityTheory(true, true));
Context context = theoryTestingSupport.makeContextWithTestingInformation();
String contextString = "X != Y and X != a and X != b and Y != b";
List<String> elementsStrings = list("X", "Y", "a", "b", "c");
context = context.conjoin(parse(contextString), context);
ArrayList<Expression> list = mapIntoArrayList(elementsStrings, Expressions::parse);
NumberOfDistinctExpressionsStepSolver stepSolver = new NumberOfDistinctExpressionsStepSolver(list);
Step step = stepSolver.step(context);
assertEquals(true, step.itDepends());
assertEquals(parse("X = c"), step.getSplitter());
ExpressionLiteralSplitterStepSolver stepSolverIfXEqualsC = step.getStepSolverForWhenSplitterIs(true);
ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromC = step.getStepSolverForWhenSplitterIs(false);
// if X = c, the number of distinct values can be 3 or 4, depending on whether Y = a, or Y = b
step = stepSolverIfXEqualsC.step(context);
assertEquals(true, step.itDepends());
assertEquals(parse("Y = a"), step.getSplitter());
ExpressionLiteralSplitterStepSolver stepSolverIfXEqualsCAndYEqualsA = step.getStepSolverForWhenSplitterIs(true);
ExpressionLiteralSplitterStepSolver stepSolverIfXEqualsCAndYIsDifferentFromA = step.getStepSolverForWhenSplitterIs(false);
// if X = c and Y = a, the number of distinct values is 3 (a, b, c)
step = stepSolverIfXEqualsCAndYEqualsA.step(context);
assertEquals(false, step.itDepends());
assertEquals(parse("3"), step.getValue());
// if X = c and Y != a, the number of distinct values is 3 or 4, depending on Y = c
step = stepSolverIfXEqualsCAndYIsDifferentFromA.step(context);
assertEquals(true, step.itDepends());
assertEquals(parse("Y = c"), step.getSplitter());
ExpressionLiteralSplitterStepSolver stepSolverIfXEqualsCAndYIsDifferentFromAAndYEqualsC = step.getStepSolverForWhenSplitterIs(true);
ExpressionLiteralSplitterStepSolver stepSolverIfXEqualsCAndYIsDifferentFromAAndYIsDifferentFromC = step.getStepSolverForWhenSplitterIs(false);
// if X = c and Y != a and Y = c, the number of distinct values is 3
step = stepSolverIfXEqualsCAndYIsDifferentFromAAndYEqualsC.step(context);
assertEquals(false, step.itDepends());
assertEquals(parse("3"), step.getValue());
// if X = c and Y != a and Y != c, the number of distinct values is 4
step = stepSolverIfXEqualsCAndYIsDifferentFromAAndYIsDifferentFromC.step(context);
assertEquals(false, step.itDepends());
assertEquals(parse("4"), step.getValue());
// if X = c and Y = a, the number of distinct values is 3 (a, b, c)
step = stepSolverIfXEqualsCAndYEqualsA.step(context);
assertEquals(false, step.itDepends());
assertEquals(parse("3"), step.getValue());
// using again just to make sure it produces the same result
step = stepSolverIfXEqualsCAndYEqualsA.step(context);
assertEquals(false, step.itDepends());
assertEquals(parse("3"), step.getValue());
// if X != c, the number of distinct value will now depend on Y = a
step = stepSolverIfXIsDifferentFromC.step(context);
assertEquals(true, step.itDepends());
assertEquals(parse("Y = a"), step.getSplitter());
// using again just to make sure it produces the same result
step = stepSolverIfXIsDifferentFromC.step(context);
assertEquals(true, step.itDepends());
assertEquals(parse("Y = a"), step.getSplitter());
// if X != c, the number of distinct values can be 4 or 5, depending on whether Y = a, or Y = b
step = stepSolverIfXIsDifferentFromC.step(context);
assertEquals(true, step.itDepends());
assertEquals(parse("Y = a"), step.getSplitter());
ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromCAndYEqualsA = step.getStepSolverForWhenSplitterIs(true);
ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromCAndYIsDifferentFromA = step.getStepSolverForWhenSplitterIs(false);
step = stepSolverIfXIsDifferentFromCAndYEqualsA.step(context);
assertEquals(false, step.itDepends());
assertEquals(parse("4"), step.getValue());
// if however Y != a, limit will depend on Y = c
step = stepSolverIfXIsDifferentFromCAndYIsDifferentFromA.step(context);
assertEquals(true, step.itDepends());
assertEquals(parse("Y = c"), step.getSplitter());
ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromCAndYIsDifferentFromAAndYIsEqualToC = step.getStepSolverForWhenSplitterIs(true);
ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromCAndYIsDifferentFromAAndYIsDifferentFromC = step.getStepSolverForWhenSplitterIs(false);
// if Y = c, then there are 4 distinct values
step = stepSolverIfXIsDifferentFromCAndYIsDifferentFromAAndYIsEqualToC.step(context);
assertEquals(false, step.itDepends());
assertEquals(parse("4"), step.getValue());
// if Y != c, then Y is also unique and the number of distinct values is 5
step = stepSolverIfXIsDifferentFromCAndYIsDifferentFromAAndYIsDifferentFromC.step(context);
assertEquals(false, step.itDepends());
assertEquals(parse("5"), step.getValue());
}
use of com.sri.ai.grinder.theory.equality.EqualityTheory 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;
}
use of com.sri.ai.grinder.theory.equality.EqualityTheory 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()));
Constraint context = theoryTestingSupport.makeContextWithTestingInformation();
for (Expression literal : And.getConjuncts(parse(conjunction))) {
context = context.conjoin(literal, theoryTestingSupport.makeContextWithTestingInformation());
}
assertEquals(expected, context);
}
use of com.sri.ai.grinder.theory.equality.EqualityTheory in project aic-expresso by aic-sri-international.
the class CompoundTheoryWithDifferenceArithmeticTest method basicTests.
@Test
public void basicTests() {
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
Expression condition = parse("X = Y and Y = X and P and not Q and P and X = a and X != b");
Context context = theoryTestingSupport.makeContextWithTestingInformation();
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);
// nested indices
Expression expression = parse("sum({{(on I in 1..2, J in 2..3) sum({{ (on I in 1..10, J in 1..2) I + J : I != J }}) }})");
context = new TrueContext(theoryTestingSupport.getTheory());
expected = parse("536");
Expression actual = theoryTestingSupport.getTheory().evaluate(expression, context);
println(actual);
assertEquals(expected, actual);
}
use of com.sri.ai.grinder.theory.equality.EqualityTheory in project aic-expresso by aic-sri-international.
the class SymbolicShell method makeTheory.
private static Theory makeTheory() {
Theory theory = new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, false), new LinearRealArithmeticTheory(false, false), new TupleTheory(), new PropositionalTheory(), new BruteForceFunctionTheory());
theory = new BruteForceFallbackTheory(theory);
return theory;
}
Aggregations