use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class AbstractQuantifierEliminationStepSolver method step.
@Override
public Step step(Context context) {
Step result;
Context contextForBody = getContextForBody(context);
if (contextForBody.isContradiction()) {
result = new Solution(group.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 = 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)
AbstractQuantifierEliminationStepSolver ifTrue = clone();
AbstractQuantifierEliminationStepSolver 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
result = eliminateQuantifierForLiteralFreeBodyAndSingleVariableConstraint(indexConstraint, bodyStep.getValue(), context);
}
}
return result;
}
use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class AbstractIterativeMultiIndexQuantifierElimination method extendAssignments.
/**
* Sets the value assignment to a given expression in the binding mechanism stored in the context.
* @param newAssignment
* @param context
* @return
*/
public static Context extendAssignments(Map<Expression, Expression> newAssignments, Context context) {
@SuppressWarnings("unchecked") Map<Expression, Expression> assignments = (Map<Expression, Expression>) context.getGlobalObject(ASSIGNMENTS_GLOBAL_OBJECTS_KEY);
Map<Expression, Expression> extendedAssignments;
if (assignments == null) {
extendedAssignments = newAssignments;
} else {
extendedAssignments = new StackedHashMap<>(newAssignments, assignments);
}
Context result = context.putGlobalObject(ASSIGNMENTS_GLOBAL_OBJECTS_KEY, extendedAssignments);
return result;
}
use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class SampleMultiIndexQuantifierEliminator method solve.
@Override
public Expression solve(AssociativeCommutativeGroup group, List<Expression> indices, Expression indicesCondition, Expression body, Context context) {
Expression result = null;
// Check if we want to sample
if (indices.size() == 1) {
// SetOfI = {{ (on I in Domain) I : Condition }}
Pair<Rational, Boolean> measureSetOfIAndSample = computeMeasureAndDetermineIfShouldSample(indices.get(0), indicesCondition, group.additiveIdentityElement(), context);
Rational measureSetOfI = measureSetOfIAndSample.first;
Boolean sample = measureSetOfIAndSample.second;
if (sample) {
// Quantifier({{ (on I in Samples) Head }} )
// NOTE: we are using the indices[2] with 2nd arg=TRUE so that the sampling logic can determine when it should activate
// in makeAssignmentsInterator() and makeSummand().
Expression sampleGroupSum = super.solve(group, Arrays.asList(indices.get(0), Expressions.TRUE), indicesCondition, body, context);
// Average = Quantifier( {{ (on I in Samples) Head }}) / n
Expression average = group.addNTimes(sampleGroupSum, Division.make(Expressions.ONE, Expressions.makeSymbol(sampleSizeN)), context);
// return Average * | SetOfI |
result = group.addNTimes(average, Expressions.makeSymbol(measureSetOfI), context);
}
}
if (result == null) {
result = super.solve(group, indices, indicesCondition, body, context);
}
return result;
}
use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class Associative method associateWhenSureOperatorIsAssociative.
/** A static version of associate when check for associative operator is done, with predicate indicating whether argument of same functor is to be associated. */
public static Expression associateWhenSureOperatorIsAssociative(Expression expression) {
Predicate<Expression> alwaysTrue = Predicates.alwaysTrue();
Expression result = associateWhenSureOperatorIsAssociative(expression, alwaysTrue);
return result;
}
use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class ContextDependentExpressionProblemSolver method solve.
/**
* Returns the solution for a problem using a step solver.
* @param stepSolver
* @param context
* @return
*/
public Expression solve(ExpressionLiteralSplitterStepSolver stepSolver, Context context) {
if (interrupted) {
throw new Error("Solver interrupted.");
}
Expression result;
ExpressionLiteralSplitterStepSolver.Step step = stepSolver.step(context);
if (step.itDepends()) {
Expression splitter = step.getSplitter();
ContextSplitting split = (ContextSplitting) step.getContextSplittingWhenSplitterIsLiteral();
myAssert(() -> split.isUndefined(), () -> "Undefined " + ContextSplitting.class + " result value: " + split.getResult());
Expression subSolution1 = solve(step.getStepSolverForWhenSplitterIsTrue(), split.getConstraintAndLiteral());
Expression subSolution2 = solve(step.getStepSolverForWhenSplitterIsFalse(), split.getConstraintAndLiteralNegation());
result = IfThenElse.make(splitter, subSolution1, subSolution2, true);
} else {
result = step.getValue();
}
return result;
}
Aggregations