Search in sources :

Example 11 with FunctionIterator

use of com.sri.ai.util.collect.FunctionIterator 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)

Example 12 with FunctionIterator

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

the class Expressions method makeSingleIndexPathsIteratorFromTo.

/**
 * Makes iterator over paths with a single index, starting from <code>start</code> to <code>end</code> exclusive.
 */
public static FunctionIterator<Integer, List<Integer>> makeSingleIndexPathsIteratorFromTo(int start, int end) {
    IntegerIterator integerIterator = new IntegerIterator(start, end);
    FunctionIterator<Integer, List<Integer>> result = new FunctionIterator<Integer, List<Integer>>(integerIterator, INTEGER_SINGLETON_LIST_MAKER);
    return result;
}
Also used : FunctionIterator(com.sri.ai.util.collect.FunctionIterator) Util.mapIntoList(com.sri.ai.util.Util.mapIntoList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) IntegerIterator(com.sri.ai.util.collect.IntegerIterator)

Aggregations

FunctionIterator (com.sri.ai.util.collect.FunctionIterator)12 NestedIterator (com.sri.ai.util.collect.NestedIterator)11 ArrayList (java.util.ArrayList)11 Iterator (java.util.Iterator)11 Beta (com.google.common.annotations.Beta)10 Function (com.google.common.base.Function)10 Expression (com.sri.ai.expresso.api.Expression)10 Expressions.apply (com.sri.ai.expresso.helper.Expressions.apply)10 Util (com.sri.ai.util.Util)10 Util.arrayList (com.sri.ai.util.Util.arrayList)10 Util.arrayListFrom (com.sri.ai.util.Util.arrayListFrom)10 Util.in (com.sri.ai.util.Util.in)10 Util.list (com.sri.ai.util.Util.list)10 PairOf (com.sri.ai.util.base.PairOf)10 PairOf.makePairOf (com.sri.ai.util.base.PairOf.makePairOf)10 CartesianProductIterator (com.sri.ai.util.collect.CartesianProductIterator)10 FunctionIterator.functionIterator (com.sri.ai.util.collect.FunctionIterator.functionIterator)10 PairOfElementsInListIterator (com.sri.ai.util.collect.PairOfElementsInListIterator)10 Symbol (com.sri.ai.expresso.api.Symbol)8 Expressions (com.sri.ai.expresso.helper.Expressions)8