use of com.sri.ai.grinder.api.SingleQuantifierEliminationProblem in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method makeProblem.
private static Expression makeProblem(Collection<Expression> indices, Constraint constraint, Expression body, AssociativeCommutativeGroup group, Context context) {
Expression currentBody = body;
boolean firstIndex = true;
for (Expression index : indices) {
Expression indexType = GrinderUtil.getTypeExpressionOfExpression(index, context);
Expression constraintOnThisIndex = firstIndex ? constraint : TRUE;
SingleQuantifierEliminationProblem problem = new DefaultSingleQuantifierEliminationProblem(group, index, indexType, constraintOnThisIndex, currentBody);
currentBody = problem.toExpression();
firstIndex = false;
}
return currentBody;
}
use of com.sri.ai.grinder.api.SingleQuantifierEliminationProblem 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.api.SingleQuantifierEliminationProblem in project aic-expresso by aic-sri-international.
the class SummationOnDifferenceArithmeticAndPolynomialStepSolverTest method runTest.
private void runTest(Expression variable, String constraintString, Expression body, Expression expected, Context context) {
Theory theory = context.getTheory();
SingleVariableDifferenceArithmeticConstraint constraint = new SingleVariableDifferenceArithmeticConstraint(variable, true, theory);
constraint = (SingleVariableDifferenceArithmeticConstraint) constraint.conjoin(parse(constraintString), context);
Expression typeExpression = context.getTypeExpressionOfRegisteredSymbol(variable);
SingleQuantifierEliminationProblem problem = new DefaultSingleQuantifierEliminationProblem(new Sum(), variable, typeExpression, constraint, body);
ExpressionStepSolver stepSolver = new SummationOnDifferenceArithmeticAndPolynomialStepSolver(problem);
Expression actual = stepSolver.solve(context);
expected = simplify(expected, context);
System.out.println("sum({{ (on " + variable + " in " + GrinderUtil.getTypeExpressionOfExpression(variable, context) + ") " + body + " : " + constraintString + " }} = " + actual + "\n");
if (!expected.equals(actual)) {
Expression difference = apply(MINUS, expected, actual);
Expression differenceResult = simplify(difference, context);
if (!differenceResult.equals(ZERO)) {
System.err.println("Expressions are not equal and even difference is not zero");
System.err.println("Expected: " + expected);
System.err.println("Actual: " + actual);
System.err.println("Difference: " + differenceResult);
fail("Expressions are not equal and even difference is not zero. Expected: " + expected + ", actual: " + actual);
}
}
// TODO: correctness test against grounding
}
use of com.sri.ai.grinder.api.SingleQuantifierEliminationProblem in project aic-expresso by aic-sri-international.
the class SummationOnDifferenceArithmeticAndPolynomialStepSolver method makeWithNewIndexConstraint.
@Override
protected SummationOnDifferenceArithmeticAndPolynomialStepSolver makeWithNewIndexConstraint(SingleVariableConstraint newIndexConstraint) {
SingleQuantifierEliminationProblem newProblem = getProblem().makeWithNewIndexConstraint(newIndexConstraint);
SummationOnDifferenceArithmeticAndPolynomialStepSolver result = new SummationOnDifferenceArithmeticAndPolynomialStepSolver(newProblem);
return result;
}
use of com.sri.ai.grinder.api.SingleQuantifierEliminationProblem in project aic-expresso by aic-sri-international.
the class SummationOnLinearRealArithmeticAndPolynomialStepSolver method makeWithNewIndexConstraint.
@Override
protected SummationOnLinearRealArithmeticAndPolynomialStepSolver makeWithNewIndexConstraint(SingleVariableConstraint newIndexConstraint) {
SingleQuantifierEliminationProblem newProblem = getProblem().makeWithNewIndexConstraint(newIndexConstraint);
SummationOnLinearRealArithmeticAndPolynomialStepSolver result = new SummationOnLinearRealArithmeticAndPolynomialStepSolver(newProblem);
return result;
}
Aggregations