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("Real"), parse("Y"), parse("Real"), parse("Z"), parse("Real"), parse("W"), parse("Integer")));
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 = true;
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);
// Must be linear
expression = parse("X^2 >= 0");
expected = false;
runIsLiteralTest(expression, expected, context);
expression = parse("X*Y >= 0");
expected = false;
runIsLiteralTest(expression, expected, context);
expression = parse("X*X - Y >= 0");
expected = false;
runIsLiteralTest(expression, expected, context);
expression = parse("2*X -3*Y + 3 >= 0");
expected = true;
runIsLiteralTest(expression, expected, context);
// Variables must be real
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("2*f() > 0");
expected = false;
runIsLiteralTest(expression, expected, context);
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class LinearRealArithmeticTheoryTest method runQuantifierTest.
private void runQuantifierTest(Expression variable, String constraintString, String bodyString, Expression expected, String computedFunction, BinaryFunction<SingleVariableConstraint, Expression, ExpressionStepSolver> stepSolverMaker, Context context) {
Expression body = parse(bodyString);
Function<SingleVariableConstraint, ExpressionStepSolver> stepSolverMakerFromConstraint = c -> stepSolverMaker.apply(c, body);
runTest(variable, constraintString, expected, computedFunction, stepSolverMakerFromConstraint, context);
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class TupleRewriterTest method testTupleValuedFreeVariablesSimplifier.
@Test
public void testTupleValuedFreeVariablesSimplifier() {
Context tupleTheoryContext = new TrueContext(new CompoundTheory(new DifferenceArithmeticTheory(false, false), new TupleTheory()));
TupleType nTupleType = new TupleType(new IntegerInterval(1, 10), new IntegerInterval(1, 10));
tupleTheoryContext = (Context) GrinderUtil.extendRegistryWith(map("N", nTupleType.toString()), Arrays.asList(nTupleType), tupleTheoryContext);
TupleValuedFreeVariablesSimplifier simplifier = new TupleValuedFreeVariablesSimplifier();
Expression expression = parse("sum( {{ (on X in 1..10) if N = (2, X) then 2 else 3 }} )");
Expression simplified = simplifier.apply(expression, tupleTheoryContext);
Assert.assertEquals(parse("if get(N, 1) = 2 then 29 else 30"), simplified);
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method testSymbolicVsBruteForceComputationForAssignment.
private static void testSymbolicVsBruteForceComputationForAssignment(Map<Expression, Expression> assignment, Theory theory, String problemDescription, Expression symbolicSolution, BinaryFunction<BruteForceCommonInterpreter, Context, Expression> fromInterpreterAndContextWithAssignmentToBruteForceSolution, Context context) throws Error {
BruteForceCommonInterpreter interpreter = new BruteForceCommonInterpreter();
Context extendedContext = Assignment.extendAssignments(assignment, context);
Expression bruteForceResultUnderAssignment = fromInterpreterAndContextWithAssignmentToBruteForceSolution.apply(interpreter, extendedContext);
Expression symbolicResultUnderAssignment = interpreter.apply(symbolicSolution, extendedContext);
output("Under free variables assignment " + assignment);
output("Symbolic result becomes " + symbolicResultUnderAssignment);
output("Brute force result becomes " + bruteForceResultUnderAssignment + "\n");
if (!symbolicResultUnderAssignment.equals(bruteForceResultUnderAssignment)) {
throw new Error("Failure in testing of " + problemDescription + "\n" + "Symbolic solution: " + symbolicSolution + "\n" + "Under assignment to free variables: " + assignment + "\n" + "Value of symbolic solution : " + symbolicResultUnderAssignment + "\n" + "Value of brute force computation: " + bruteForceResultUnderAssignment + "\n" + "Context : " + extendedContext + "\n");
}
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method testMultiVariableConstraints.
/**
* Given a theory and a number <code>n</code> of multi-variable constraint tests,
* generates <code>n</code> formulas in the theory
* and see if those detected as unsatisfiable by the corresponding solver
* are indeed unsatisfiable (checked by brute force).
* Throws an {@link Error} with the failure description if a test fails.
* @param theoryTestingSupport
* @param numberOfTests
* @param maxNumberOfLiterals
* @param outputCount
*/
public static void testMultiVariableConstraints(boolean testAgainstBruteForce, TheoryTestingSupport theoryTestingSupport, long numberOfTests, int maxNumberOfLiterals, boolean outputCount) {
NullaryFunction<Constraint> makeInitialConstraint = () -> new DefaultMultiVariableConstraint(theoryTestingSupport.getTheory());
Context context = theoryTestingSupport.makeContextWithTestingInformation();
Function<Constraint, Expression> makeRandomLiteral = c -> theoryTestingSupport.makeRandomLiteral(context);
// DefaultMultiVariableConstraint is incomplete
TestRunner tester = SGDPLLTTester::testIncompleteSatisfiability;
runTesterGivenOnSuccessiveConjunctionsOfLiterals("incomplete satisfiability", tester, numberOfTests, maxNumberOfLiterals, testAgainstBruteForce, theoryTestingSupport, makeInitialConstraint, makeRandomLiteral, outputCount, context);
}
Aggregations