use of com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method runModelCountingTestForSingleVariableConstraint.
private static void runModelCountingTestForSingleVariableConstraint(Expression variable, Collection<Expression> literals, Constraint constraint, boolean testAgainstBruteForce, Theory theory, Context context) {
Expression literalsConjunction = And.make(literals);
String problemDescription = "model counting of " + literalsConjunction + " for variable " + variable;
output("Problem: " + problemDescription);
Simplifier symbolicSolver = (e, p) -> computeModelCountBySolver((SingleVariableConstraint) e, p);
SingleVariableConstraint singleVariableConstraint = (SingleVariableConstraint) constraint;
Expression symbolicSolution = symbolicSolver.apply(singleVariableConstraint, context);
if (Util.thereExists(new SubExpressionsDepthFirstIterator(symbolicSolution), e -> e instanceof QuantifiedExpression || Sets.isIntensionalSet(e))) {
throw new Error("Symbolic solution is not quantifier-free: " + symbolicSolution);
}
output("Symbolic result: " + symbolicSolution);
if (testAgainstBruteForce) {
if (singleVariableConstraint.isContradiction()) {
if (!symbolicSolution.equals(ZERO)) {
throw new Error("Constraint is contradiction, but symbolic solver does not produce 0, but instead " + symbolicSolution);
}
} else {
Expression testingVariable = singleVariableConstraint.getVariable();
Set<Expression> allVariables = getVariablesBeingReferenced(singleVariableConstraint, context);
Collection<Expression> otherVariables = removeFromSetNonDestructively(allVariables, v -> v.equals(testingVariable));
BinaryFunction<BruteForceCommonInterpreter, Context, Expression> fromInterpreterAndContextWithAssignmentToOtherVariablesToBruteForceSolution = (interpreter, contextWithAssignmentToOtherVariables) -> bruteForceModelCounterForVariableGivenInterpreterAndAssignmentToOtherVariables(variable, literalsConjunction, interpreter, theory, contextWithAssignmentToOtherVariables);
testSymbolicVsBruteForceComputationForEachAssignment(theory, problemDescription, otherVariables, symbolicSolution, fromInterpreterAndContextWithAssignmentToOtherVariablesToBruteForceSolution, context);
}
} else {
output("Skipping test againt brute-force.");
}
}
use of com.sri.ai.grinder.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.interpreter.BruteForceCommonInterpreter in project aic-expresso by aic-sri-international.
the class AbstractSingleQuantifierEliminationStepSolver method step.
@Override
public Step step(Context context) {
Step result;
Context contextForBody = getContextForBody(context);
if (contextForBody.isContradiction()) {
result = new Solution(getGroup().additiveIdentityElement());
} else {
ExpressionLiteralSplitterStepSolver bodyStepSolver = getInitialBodyStepSolver(context.getTheory());
ExpressionLiteralSplitterStepSolver.Step bodyStep = bodyStepSolver.step(contextForBody);
// Check (**) in this file to see where this happens
if (!bodyStep.itDepends()) {
ExpressionLiteralSplitterStepSolver evaluatorStepSolver = context.getTheory().makeEvaluatorStepSolver(bodyStep.getValue());
bodyStep = evaluatorStepSolver.step(context);
}
if (bodyStep.itDepends()) {
// "intercept" literals containing the index and split the quantifier based on it
if (isSubExpressionOf(getIndex(), bodyStep.getSplitterLiteral())) {
Expression literalOnIndex = bodyStep.getSplitterLiteral();
result = resultIfLiteralContainsIndex(literalOnIndex, bodyStep, contextForBody, context);
} else {
// not on index, just pass the expression on which we depend on, but with appropriate sub-step solvers (this, for now)
AbstractSingleQuantifierEliminationStepSolver ifTrue = clone();
AbstractSingleQuantifierEliminationStepSolver ifFalse = clone();
ifTrue.initialBodyEvaluationStepSolver = bodyStep.getStepSolverForWhenSplitterIsTrue();
ifFalse.initialBodyEvaluationStepSolver = bodyStep.getStepSolverForWhenSplitterIsFalse();
ifTrue.initialContextForBody = bodyStep.getContextSplittingWhenSplitterIsLiteral().getContextAndLiteral();
ifFalse.initialContextForBody = bodyStep.getContextSplittingWhenSplitterIsLiteral().getContextAndLiteralNegation();
// to compute the result's constraint splitting,
// we cannot directly re-use bodyStep.getConstraintSplitting() because it was not obtained from
// the context it is returning to,
// but from the context conjoined with the index constraint.
// In order to provide two contexts to work with the sequel step solvers,
// we calculate the splittings here.
// TODO: In the future, we expect it possible to efficiently extract the contextForBody component relative
// to the original context only, excluding the index.
ContextSplitting split = new ContextSplitting(bodyStep.getSplitterLiteral(), context);
result = new ItDependsOn(bodyStep.getSplitterLiteral(), split, ifTrue, ifFalse);
}
} else {
// body is already literal free
Expression literalFreeBody = bodyStep.getValue();
result = eliminateQuantifierForLiteralFreeBody(literalFreeBody, context);
boolean solutionToQuantifiedLiteralFreeBodyIsNotConditionalItself = !result.itDepends();
if (solutionToQuantifiedLiteralFreeBodyIsNotConditionalItself) {
IntegrationRecording.registerGroupIntegration(problem, literalFreeBody, result, context);
}
}
}
if (context.getGlobalObject(BRUTE_FORCE_CHECKING_OF_NON_CONDITIONAL_PROBLEMS) != null) {
if (!result.itDepends()) {
Expression indexType = context.getTypeExpressionOfRegisteredSymbol(getIndex());
SingleQuantifierEliminationProblem problem = new DefaultSingleQuantifierEliminationProblem(getGroup(), getIndex(), indexType, getIndexConstraint(), getBody());
Expression problemExpression = problem.toExpression();
Set<Expression> freeVariables = Expressions.freeVariables(problemExpression, context);
AssignmentMapsIterator assignments = new AssignmentMapsIterator(freeVariables, context);
for (Map<Expression, Expression> assignment : in(assignments)) {
BruteForceCommonInterpreter bruteForceCommonInterpreter = new BruteForceCommonInterpreter();
Context extendedContext = Assignment.extendAssignments(assignment, context);
// Only go on if the assignment satisfies the context:
if (bruteForceCommonInterpreter.apply(context, extendedContext).equals(Expressions.TRUE)) {
Expression bruteForceResult = bruteForceCommonInterpreter.apply(problemExpression, extendedContext);
Expression resultGivenAssignment = bruteForceCommonInterpreter.apply(result.getValue(), extendedContext);
Expression evaluatedProblem = bruteForceCommonInterpreter.apply(problemExpression, extendedContext);
if (!bruteForceResult.equals(resultGivenAssignment)) {
String message = "Disagreement on " + problemExpression + "\nunder " + assignment + ".\n" + "Context: " + context + ".\n" + "Evaluated problem: " + evaluatedProblem + ".\n" + "Brute force says " + bruteForceResult + ", symbolic says " + resultGivenAssignment;
println(message);
throw new Error(message);
} else {
String message = "Agreement on " + problemExpression + "\nunder " + assignment + ".\n" + "Context: " + context + ".\n" + "Evaluated problem: " + evaluatedProblem + ".\n" + "Brute force says " + bruteForceResult + ", symbolic says " + resultGivenAssignment;
println(message);
}
}
}
}
}
return result;
}
use of com.sri.ai.grinder.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.interpreter.BruteForceCommonInterpreter in project aic-expresso by aic-sri-international.
the class BasicTest method debug.
private void debug(Expression problem) {
Theory theory = new DifferenceArithmeticTheory(true, true);
Context context = new TrueContext(theory);
context = context.putGlobalObject(BRUTE_FORCE_CHECKING_OF_NON_CONDITIONAL_PROBLEMS, "Yes");
Expression symbolicSolution = theory.evaluate(problem, context);
println(symbolicSolution);
BruteForceInterpreter bruteForceInterpreter = new BruteForceCommonInterpreter();
Expression bruteForceSolution = bruteForceInterpreter.apply(problem, context);
println(bruteForceSolution);
Assert.assertEquals(bruteForceSolution, symbolicSolution);
}
Aggregations