Search in sources :

Example 6 with NestedIterator

use of com.sri.ai.util.collect.NestedIterator in project aic-expresso by aic-sri-international.

the class AbstractSingleVariableNumericConstraintFeasibilityRegionStepSolver method makeUpperBoundsAndStrictness.

/**
 * A method setting {@link #upperBoundsIncludingImplicitOnes} and {@link #fromUpperBoundsIncludingImplicitOnesToStrictness}
 * from constraint and variable's type.
 * @param context
 */
protected void makeUpperBoundsAndStrictness(Context context) {
    AbstractSingleVariableConstraint abstractSingleVariableConstraint = (AbstractSingleVariableConstraint) constraint;
    FunctionIterator<Expression, Pair<Expression, Boolean>> upperBoundsFromPositiveNormalizedAtomsIterator = functionIterator(predicateIterator(abstractSingleVariableConstraint.getPositiveNormalizedAtoms(), e -> e.hasFunctor(LESS_THAN)), // strict
    e -> processExplicitUpperBoundAndStrictnessPair(e.get(1), true, context));
    FunctionIterator<Expression, Pair<Expression, Boolean>> upperBoundsFromNegativeNormalizedAtomsIterator = functionIterator(predicateIterator(abstractSingleVariableConstraint.getNegativeNormalizedAtoms(), // not (X > Y) <=> X <= Y, so Y is a non-strict upper bound
    e -> e.hasFunctor(GREATER_THAN)), // non-strict
    e -> processExplicitUpperBoundAndStrictnessPair(e.get(1), false, context));
    Pair<Expression, Boolean> typeUpperBound = getTypeUpperBoundAndStrictness(context);
    Iterator<Pair<Expression, Boolean>> upperBoundsAndStrictnessIterator = new NestedIterator<>(upperBoundsFromPositiveNormalizedAtomsIterator, upperBoundsFromNegativeNormalizedAtomsIterator, typeUpperBound);
    upperBoundsIncludingImplicitOnes = arrayList();
    fromUpperBoundsIncludingImplicitOnesToStrictness = map();
    for (Pair<Expression, Boolean> boundAndStrictness : in(upperBoundsAndStrictnessIterator)) {
        Expression bound = boundAndStrictness.first;
        upperBoundsIncludingImplicitOnes.add(bound);
        Boolean strictness = boundAndStrictness.second;
        Boolean previousStrictness = fromUpperBoundsIncludingImplicitOnesToStrictness.get(bound);
        if (previousStrictness == null || (!previousStrictness && strictness)) {
            // if no strictness information so far, store current one; otherwise, only need to change it if previous occurrences were non-strict and this one is strict
            fromUpperBoundsIncludingImplicitOnesToStrictness.put(bound, strictness);
        }
    }
}
Also used : AbstractExpressionWithPropagatedLiteralsStepSolver(com.sri.ai.grinder.core.solver.AbstractExpressionWithPropagatedLiteralsStepSolver) Expressions(com.sri.ai.expresso.helper.Expressions) NestedIterator(com.sri.ai.util.collect.NestedIterator) INFINITY(com.sri.ai.expresso.helper.Expressions.INFINITY) PairOf(com.sri.ai.util.base.PairOf) Expression(com.sri.ai.expresso.api.Expression) EQUALITY(com.sri.ai.grinder.library.FunctorConstants.EQUALITY) CartesianProductIterator(com.sri.ai.util.collect.CartesianProductIterator) PairOf.makePairOf(com.sri.ai.util.base.PairOf.makePairOf) ArrayList(java.util.ArrayList) Util.in(com.sri.ai.util.Util.in) Util.map(com.sri.ai.util.Util.map) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver) AbstractSingleVariableConstraint(com.sri.ai.grinder.core.constraint.AbstractSingleVariableConstraint) Symbol(com.sri.ai.expresso.api.Symbol) Equality(com.sri.ai.grinder.library.Equality) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) TrueContext(com.sri.ai.grinder.core.TrueContext) ConstantExpressionStepSolver(com.sri.ai.grinder.theory.base.ConstantExpressionStepSolver) Map(java.util.Map) AbstractSingleVariableDifferenceArithmeticConstraintFeasibilityRegionStepSolver(com.sri.ai.grinder.theory.differencearithmetic.AbstractSingleVariableDifferenceArithmeticConstraintFeasibilityRegionStepSolver) Context(com.sri.ai.grinder.api.Context) Pair.pair(com.sri.ai.util.base.Pair.pair) ConstantStepSolver(com.sri.ai.grinder.theory.base.ConstantStepSolver) Util.arrayList(com.sri.ai.util.Util.arrayList) FunctionIterator(com.sri.ai.util.collect.FunctionIterator) Pair(com.sri.ai.util.base.Pair) PredicateIterator.predicateIterator(com.sri.ai.util.collect.PredicateIterator.predicateIterator) Util.arrayListFrom(com.sri.ai.util.Util.arrayListFrom) LiteralStepSolver(com.sri.ai.grinder.theory.base.LiteralStepSolver) LESS_THAN_OR_EQUAL_TO(com.sri.ai.grinder.library.FunctorConstants.LESS_THAN_OR_EQUAL_TO) Function(com.google.common.base.Function) Iterator(java.util.Iterator) Util.iterator(com.sri.ai.util.Util.iterator) Util.list(com.sri.ai.util.Util.list) MINUS_INFINITY(com.sri.ai.expresso.helper.Expressions.MINUS_INFINITY) MaximumExpressionStepSolver(com.sri.ai.grinder.helper.MaximumExpressionStepSolver) GREATER_THAN(com.sri.ai.grinder.library.FunctorConstants.GREATER_THAN) Beta(com.google.common.annotations.Beta) GREATER_THAN_OR_EQUAL_TO(com.sri.ai.grinder.library.FunctorConstants.GREATER_THAN_OR_EQUAL_TO) StepSolver(com.sri.ai.grinder.api.StepSolver) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) FunctionIterator.functionIterator(com.sri.ai.util.collect.FunctionIterator.functionIterator) LESS_THAN(com.sri.ai.grinder.library.FunctorConstants.LESS_THAN) Util(com.sri.ai.util.Util) FunctorConstants(com.sri.ai.grinder.library.FunctorConstants) PairOfElementsInListIterator(com.sri.ai.util.collect.PairOfElementsInListIterator) Expression(com.sri.ai.expresso.api.Expression) NestedIterator(com.sri.ai.util.collect.NestedIterator) AbstractSingleVariableConstraint(com.sri.ai.grinder.core.constraint.AbstractSingleVariableConstraint) Pair(com.sri.ai.util.base.Pair)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)6 NestedIterator (com.sri.ai.util.collect.NestedIterator)6 Beta (com.google.common.annotations.Beta)4 Function (com.google.common.base.Function)4 Symbol (com.sri.ai.expresso.api.Symbol)4 Expressions (com.sri.ai.expresso.helper.Expressions)4 INFINITY (com.sri.ai.expresso.helper.Expressions.INFINITY)4 MINUS_INFINITY (com.sri.ai.expresso.helper.Expressions.MINUS_INFINITY)4 Expressions.apply (com.sri.ai.expresso.helper.Expressions.apply)4 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)4 Util (com.sri.ai.util.Util)4 Util.arrayList (com.sri.ai.util.Util.arrayList)4 Util.arrayListFrom (com.sri.ai.util.Util.arrayListFrom)4 Util.in (com.sri.ai.util.Util.in)4 Util.iterator (com.sri.ai.util.Util.iterator)4 Util.list (com.sri.ai.util.Util.list)4 Util.map (com.sri.ai.util.Util.map)4 Pair (com.sri.ai.util.base.Pair)4 Pair.pair (com.sri.ai.util.base.Pair.pair)4 PairOf (com.sri.ai.util.base.PairOf)4