Search in sources :

Example 31 with Expression

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;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) ContextSplitting(com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting)

Example 32 with Expression

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;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) Expression(com.sri.ai.expresso.api.Expression) StackedHashMap(com.sri.ai.util.collect.StackedHashMap) Map(java.util.Map)

Example 33 with Expression

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;
}
Also used : Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression)

Example 34 with Expression

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;
}
Also used : Expression(com.sri.ai.expresso.api.Expression)

Example 35 with Expression

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;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) ContextSplitting(com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)1392 Test (org.junit.Test)259 ArrayList (java.util.ArrayList)196 Context (com.sri.ai.grinder.api.Context)187 Type (com.sri.ai.expresso.api.Type)124 TrueContext (com.sri.ai.grinder.core.TrueContext)113 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)100 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)91 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)90 Context (com.sri.ai.grinder.sgdpllt.api.Context)87 Theory (com.sri.ai.grinder.api.Theory)78 Map (java.util.Map)78 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)71 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)68 List (java.util.List)68 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)63 CommonTheory (com.sri.ai.grinder.application.CommonTheory)55 LinkedHashMap (java.util.LinkedHashMap)55 LinkedHashSet (java.util.LinkedHashSet)54 Pair (com.sri.ai.util.base.Pair)52