use of com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter in project aic-expresso by aic-sri-international.
the class BruteForceCommonInterpreterTest method test.
@Test
public void test() {
LinkedHashMap<Expression, Expression> assignment = map(parse("Two"), Expressions.TWO);
BruteForceCommonInterpreter interpreter = new BruteForceCommonInterpreter();
Context context = new TrueContext();
context = AbstractIterativeMultiIndexQuantifierElimination.extendAssignments(assignment, context);
String expression;
String expected;
expression = "max({{ (on I in 0..3) max( {{ (on J in 0..3) I + J : I != J }} ) }})";
expected = "5";
runTest(expression, expected, interpreter, context);
expression = "3 * (Two + 5 - 3)*(-10)";
expected = "-120";
runTest(expression, expected, interpreter, context);
expression = "3 * (2 + 5 - 3)*(-10)";
expected = "-120";
runTest(expression, expected, interpreter, context);
expression = "sum({{ (on x in 0..2) x }} )";
expected = "3";
runTest(expression, expected, interpreter, context);
expression = "sum({{ (on f in 0..2 -> Boolean) if f(0) then 2 else 3 }} )";
expected = "20";
runTest(expression, expected, interpreter, context);
expression = "sum({{ (on f in 0..2 -> Boolean) if f(0) and f(1) then 2 else 3 : f(2) }} )";
expected = "11";
runTest(expression, expected, interpreter, context);
expression = "sum({{ (on f in 0..2 -> Boolean) " + "if f(0) and f(1) then 2 else | f in 0..2 x 0..2 -> Boolean : f(0, 0) | " + ": f(2) }} )";
expected = "770";
runTest(expression, expected, interpreter, context);
expression = "sum({{ (on f in '->'(x(1..2), Boolean), g in '->'(x(1..2), Boolean)) if f(1) and g(2) then 2 else 3 : f(2) }} )";
expected = "22";
runTest(expression, expected, interpreter, context);
expression = "| f in '->'(x(0..2, 0..2), Boolean) : f(0, 0) |";
expected = "256";
runTest(expression, expected, interpreter, context);
expression = "| f in 0..2 x 0..2 -> Boolean : f(0, 0) |";
expected = "256";
runTest(expression, expected, interpreter, context);
expression = "| f in 0..2 x 0..2 -> Boolean, g in 0..2 -> Boolean : f(0, 0) |";
expected = "2048";
runTest(expression, expected, interpreter, context);
expression = "sum( {{ (on p in Boolean) if p then 1 else 2 }} )";
expected = "3";
runTest(expression, expected, interpreter, context);
}
use of com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter in project aic-expresso by aic-sri-international.
the class SampleCommonInterpreterTest method bruteForceResult.
private Expression bruteForceResult(String expression) {
BruteForceCommonInterpreter bruteForceInterpreter = new BruteForceCommonInterpreter();
Expression result = bruteForceInterpreter.apply(Expressions.parse(expression), context);
return result;
}
use of com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method runGroupProblemSolvingTestGivenConstraintAndProblem.
/**
* @param problem
* @param indices
* @param constraint
* @param body
* @param testAgainstBruteForce
* @param theoryTestingSupport
* @param context
* @throws Error
*/
public static void runGroupProblemSolvingTestGivenConstraintAndProblem(Expression problem, Collection<Expression> indices, Constraint constraint, Expression body, boolean testAgainstBruteForce, TheoryTestingSupport theoryTestingSupport, Context context) throws Error {
Theory theory = theoryTestingSupport.getTheory();
Collection<Expression> freeVariables = getFreeVariableMinusIndices(indices, constraint, body, context);
String problemDescription = problem.toString();
output(problemDescription);
Simplifier symbolicInterpreter = (e, c) -> theory.evaluate(e, c);
long start = System.currentTimeMillis();
Expression symbolicSolution = symbolicInterpreter.apply(problem, context);
long time = System.currentTimeMillis() - start;
output("Symbolic solution: " + symbolicSolution);
output("Computed in " + time + " ms");
if (Util.thereExists(new SubExpressionsDepthFirstIterator(symbolicSolution), e -> e instanceof QuantifiedExpression || Sets.isIntensionalSet(e))) {
throw new Error("Symbolic solution is not quantifier-free: " + symbolicSolution);
}
if (testAgainstBruteForce) {
BinaryFunction<BruteForceCommonInterpreter, Context, Expression> bruteForceSolutionGivenInterpreterAndContextWithAssignmentToOtherVariables = (i, c) -> i.apply(problem, c);
testSymbolicVsBruteForceComputationForEachAssignment(theory, problemDescription, freeVariables, symbolicSolution, bruteForceSolutionGivenInterpreterAndContextWithAssignmentToOtherVariables, context);
// A more elegant approach would be to create a "for all free variables : symbolic = problem" expression
// and solve it by brute force instead of using testSymbolicVsBruteForceComputation
// which replicates the brute force interpreter to some extent.
// The reason we do not do this is simply due to the fact that the brute force interpreter would return "false"
// in case of failure, without indicating which assignment failed, which is very useful for debugging.
// If interpreters, and in fact the whole framework, provided proofs of its calculations,
// then we could simply use the more elegant approach.
} else {
output("Skipping test againt brute-force.");
}
}
use of com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter 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 = AbstractIterativeMultiIndexQuantifierElimination.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.sgdpllt.interpreter.BruteForceCommonInterpreter in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method isSatisfiableByBruteForce.
/**
* Determines whether a formula is satisfiable by adding existential quantifiers for each of its variables
* (according to the theory provided) and evaluating it.
* @param formula
* @param theoryTestingSupport
* @param context
* @return whether the formula is satisfiable.
*/
public static boolean isSatisfiableByBruteForce(Expression formula, TheoryTestingSupport theoryTestingSupport, Context context) {
Map<String, Type> variableNamesAndTypesForTesting = theoryTestingSupport.getVariableNamesAndTypesForTesting();
Expression quantifiedFormula = formula;
Collection<Expression> variables = theoryTestingSupport.getTheory().getVariablesIn(formula, context);
for (Expression variable : variables) {
Expression typeNameExpression = parse(variableNamesAndTypesForTesting.get(variable).toString());
quantifiedFormula = ThereExists.make(IndexExpressions.makeIndexExpression(variable, typeNameExpression), quantifiedFormula);
}
Expression evaluation = new BruteForceCommonInterpreter().apply(quantifiedFormula, context);
boolean result = evaluation.equals(TRUE);
return result;
}
Aggregations