use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class MultiVariableContextWithCheckedProperty method conjoinSpecializedForConstraintsIfApplicable.
/**
* Returns a pair indicating whether specialized conjoin for constraints applies to this case and,
* if so, provides the result of this conjoining.
* @param formula
* @param context
* @return
*/
private Pair<Boolean, Context> conjoinSpecializedForConstraintsIfApplicable(Expression formula, Context context) {
Pair<Boolean, Context> result;
if (formula instanceof SingleVariableConstraint) {
SingleVariableConstraint formulaAsSingleVariableConstraint = (SingleVariableConstraint) formula;
Expression variable = formulaAsSingleVariableConstraint.getVariable();
// TODO: this forces expression representation to be generated, which can be expensive. Better write a method that checks it on the constraint representation itself
boolean variableAlreadyConstrainedInThis = contains(this, variable);
if (!variableAlreadyConstrainedInThis) {
// if the variable is new to this constraint, we can simply tack on its constraint on it.
Context newContext = makeAndCheck(getTheory(), formulaAsSingleVariableConstraint, this, contextDependentProblemStepSolverMaker, context);
result = pair(true, newContext);
} else {
// otherwise we won't be able to use the single variable constraint structure in any special way
result = pair(false, null);
}
} else if (formula instanceof MultiVariableContextWithCheckedProperty) {
MultiVariableContextWithCheckedProperty formulaAsMultiVariableConstraint = (MultiVariableContextWithCheckedProperty) formula;
// if formula is itself a MultiVariableContextWithCheckedProperty,
// we conjoin its two known parts individually.
// Their own inner structure will also be efficiently exploited by these conjunctions.
Context conjunction = this;
if (formulaAsMultiVariableConstraint.tail != null) {
conjunction = conjunction.conjoin(formulaAsMultiVariableConstraint.tail, context);
}
if (formulaAsMultiVariableConstraint.head != null) {
conjunction = conjunction.conjoin(formulaAsMultiVariableConstraint.head, context);
}
result = pair(true, conjunction);
} else {
// the formula does not have a recognizable structure we can exploit
result = pair(false, null);
}
return result;
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class MultiVariableContextWithCheckedProperty method makeAndCheck.
/**
* Creates a new {@link Context} from a {@link SingleVariableConstraint} and a {@link Context},
* by either returning a contradiction if either is contradictory,
* or a new {@link MultiVariableContextWithCheckedProperty} otherwise.
* @param theory
* @param head
* @param tail
* @param context
* @return
*/
public static Context makeAndCheck(Theory theory, SingleVariableConstraint head, Context tail, ContextDependentProblemStepSolverMaker contextDependentProblemStepSolverMaker, Context context) {
Context result;
if (head.isContradiction() || tail.isContradiction()) {
result = tail.makeContradiction();
} else {
MultiVariableContextWithCheckedProperty uncheckedMultiVariableConstraintWithCheckedProperty = new MultiVariableContextWithCheckedProperty(tail.getTheory(), head, tail, contextDependentProblemStepSolverMaker);
result = uncheckedMultiVariableConstraintWithCheckedProperty.check(context);
}
return result;
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class MultiVariableContextWithCheckedProperty method makeCloneWithAdditionalRegisteredSymbolsAndTypes.
@Override
public MultiVariableContextWithCheckedProperty makeCloneWithAdditionalRegisteredSymbolsAndTypes(Map<Expression, Expression> indicesAndTypes) {
MultiVariableContextWithCheckedProperty result = clone();
Context newTail = tail.makeCloneWithAdditionalRegisteredSymbolsAndTypes(indicesAndTypes);
result.tail = newTail;
return result;
}
use of com.sri.ai.grinder.api.Context 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.Context in project aic-expresso by aic-sri-international.
the class AssignmentsSamplingIterator method makeContextExtendedBySampledValue.
private Context makeContextExtendedBySampledValue(Expression sampledValue) {
Assignment sampledAssignment = makeIndexAssignment(sampledValue);
Context contextExtendedWithSampledAssignment = sampledAssignment.extend(context);
return contextExtendedWithSampledAssignment;
}
Aggregations