Search in sources :

Example 1 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 : LESS_THAN_OR_EQUAL_TO(com.sri.ai.grinder.sgdpllt.library.FunctorConstants.LESS_THAN_OR_EQUAL_TO) 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) 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) Symbol(com.sri.ai.expresso.api.Symbol) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) ConstantStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.ConstantStepSolver) Map(java.util.Map) Pair.pair(com.sri.ai.util.base.Pair.pair) AbstractExpressionWithPropagatedLiteralsStepSolver(com.sri.ai.grinder.sgdpllt.core.solver.AbstractExpressionWithPropagatedLiteralsStepSolver) Util.arrayList(com.sri.ai.util.Util.arrayList) FunctionIterator(com.sri.ai.util.collect.FunctionIterator) LiteralStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.LiteralStepSolver) Pair(com.sri.ai.util.base.Pair) Equality(com.sri.ai.grinder.sgdpllt.library.Equality) PredicateIterator.predicateIterator(com.sri.ai.util.collect.PredicateIterator.predicateIterator) Util.arrayListFrom(com.sri.ai.util.Util.arrayListFrom) 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) AbstractSingleVariableConstraint(com.sri.ai.grinder.sgdpllt.core.constraint.AbstractSingleVariableConstraint) EQUALITY(com.sri.ai.grinder.sgdpllt.library.FunctorConstants.EQUALITY) Context(com.sri.ai.grinder.sgdpllt.api.Context) GREATER_THAN_OR_EQUAL_TO(com.sri.ai.grinder.sgdpllt.library.FunctorConstants.GREATER_THAN_OR_EQUAL_TO) Beta(com.google.common.annotations.Beta) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) StepSolver(com.sri.ai.grinder.sgdpllt.api.StepSolver) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) FunctionIterator.functionIterator(com.sri.ai.util.collect.FunctionIterator.functionIterator) ConstantExpressionStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.ConstantExpressionStepSolver) LESS_THAN(com.sri.ai.grinder.sgdpllt.library.FunctorConstants.LESS_THAN) AbstractSingleVariableDifferenceArithmeticConstraintFeasibilityRegionStepSolver(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.AbstractSingleVariableDifferenceArithmeticConstraintFeasibilityRegionStepSolver) MaximumExpressionStepSolver(com.sri.ai.grinder.sgdpllt.helper.MaximumExpressionStepSolver) Util(com.sri.ai.util.Util) PairOfElementsInListIterator(com.sri.ai.util.collect.PairOfElementsInListIterator) FunctorConstants(com.sri.ai.grinder.sgdpllt.library.FunctorConstants) GREATER_THAN(com.sri.ai.grinder.sgdpllt.library.FunctorConstants.GREATER_THAN) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) Expression(com.sri.ai.expresso.api.Expression) NestedIterator(com.sri.ai.util.collect.NestedIterator) AbstractSingleVariableConstraint(com.sri.ai.grinder.sgdpllt.core.constraint.AbstractSingleVariableConstraint) Pair(com.sri.ai.util.base.Pair)

Example 2 with FunctionIterator

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

the class SatisfiabilityOfSingleVariableEqualityConstraintStepSolver method getPropagatedCNFBesidesPropagatedLiterals.

@Override
protected Iterable<Iterable<Expression>> getPropagatedCNFBesidesPropagatedLiterals(Context context) {
    if (!variableIsBoundToUniquelyNamedConstant(context)) {
        // the following logic only holds if the variable is not bound to a uniquely named constants,
        // since that eliminates all disequalities to other uniquely named constants as redundant
        long variableDomainSize = getConstraint().getVariableTypeSize(context);
        if (variableDomainSize >= 0 && getConstraint().numberOfDisequals() >= variableDomainSize) {
            // the following procedure can be very expensive but the condition above will rarely be satisfied
            ArrayList<Expression> variableDisequals = getVariableDisequals(context);
            Set<Expression> uniquelyNamedConstantDisequals = getUniquelyNamedConstantDisequals(context);
            Expression typeExpression = GrinderUtil.getTypeExpression(getConstraint().getVariable(), context);
            Type type = context.getType(typeExpression);
            ArrayList<Expression> remainingUniquelyNamedConstants = arrayListFrom(new PredicateIterator<>(type.iterator(), c -> !uniquelyNamedConstantDisequals.contains(c)));
            CartesianProductIterator<ArrayList<Expression>> subsetOfVariableDisequalsAndRemainingConstantsPermutationIterator = new CartesianProductIterator<ArrayList<Expression>>(() -> new SubsetsOfKIterator<Expression>(variableDisequals, remainingUniquelyNamedConstants.size()), () -> new PermutationIterator<Expression>(remainingUniquelyNamedConstants));
            FunctionIterator<ArrayList<ArrayList<Expression>>, Iterable<Expression>> clausesIterator = FunctionIterator.make(subsetOfVariableDisequalsAndRemainingConstantsPermutationIterator, (ArrayList<ArrayList<Expression>> subsetAndPermutation) -> clauseNegatingAssignmentOfSubsetOfVariablesToParticularPermutationOfRemainingConstants(subsetAndPermutation));
            Iterable<Iterable<Expression>> clauses = in(clausesIterator);
            return clauses;
        }
    }
    // otherwise, nothing is implied.
    return list();
}
Also used : PredicateIterator(com.sri.ai.util.collect.PredicateIterator) PermutationIterator(com.sri.ai.util.collect.PermutationIterator) NestedIterator(com.sri.ai.util.collect.NestedIterator) PairOf(com.sri.ai.util.base.PairOf) Expression(com.sri.ai.expresso.api.Expression) CartesianProductIterator(com.sri.ai.util.collect.CartesianProductIterator) PairOf.makePairOf(com.sri.ai.util.base.PairOf.makePairOf) ArrayList(java.util.ArrayList) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) SubsetsOfKIterator(com.sri.ai.util.collect.SubsetsOfKIterator) Util.in(com.sri.ai.util.Util.in) Expressions.zipApply(com.sri.ai.expresso.helper.Expressions.zipApply) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) Util.thereExists(com.sri.ai.util.Util.thereExists) Util.arrayList(com.sri.ai.util.Util.arrayList) FunctionIterator(com.sri.ai.util.collect.FunctionIterator) LinkedHashSet(java.util.LinkedHashSet) Equality(com.sri.ai.grinder.sgdpllt.library.Equality) Util.arrayListFrom(com.sri.ai.util.Util.arrayListFrom) Function(com.google.common.base.Function) Type(com.sri.ai.expresso.api.Type) Iterator(java.util.Iterator) DISEQUALITY(com.sri.ai.grinder.sgdpllt.library.FunctorConstants.DISEQUALITY) Util.list(com.sri.ai.util.Util.list) Set(java.util.Set) AbstractBooleanWithPropagatedLiteralsRequiringPropagatedLiteralsAndCNFToBeSatisfiedStepSolver(com.sri.ai.grinder.sgdpllt.core.solver.AbstractBooleanWithPropagatedLiteralsRequiringPropagatedLiteralsAndCNFToBeSatisfiedStepSolver) Context(com.sri.ai.grinder.sgdpllt.api.Context) Beta(com.google.common.annotations.Beta) Util.toLinkedHashSet(com.sri.ai.util.Util.toLinkedHashSet) List(java.util.List) FunctionIterator.functionIterator(com.sri.ai.util.collect.FunctionIterator.functionIterator) Util(com.sri.ai.util.Util) PairOfElementsInListIterator(com.sri.ai.util.collect.PairOfElementsInListIterator) TRUE(com.sri.ai.expresso.helper.Expressions.TRUE) ArrayList(java.util.ArrayList) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) CartesianProductIterator(com.sri.ai.util.collect.CartesianProductIterator)

Example 3 with FunctionIterator

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

the class DefaultCompoundSyntaxTree method toStringWithoutCaching.

@Override
public String toStringWithoutCaching() {
    String rootTreeString = getRootTree().toString();
    if (!(getRootTree() instanceof SyntaxLeaf)) {
        rootTreeString = "(" + rootTreeString + ")";
    }
    Iterator stringOfSubTrees = new FunctionIterator<SyntaxTree, String>(new ToStringWithoutCaching(), getImmediateSubTrees());
    return rootTreeString + "(" + Util.join(", ", stringOfSubTrees) + ")";
}
Also used : SyntaxLeaf(com.sri.ai.expresso.api.SyntaxLeaf) FunctionIterator(com.sri.ai.util.collect.FunctionIterator) Iterator(java.util.Iterator) NestedIterator(com.sri.ai.util.collect.NestedIterator) FunctionIterator(com.sri.ai.util.collect.FunctionIterator)

Example 4 with FunctionIterator

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

the class AbstractSingleVariableNumericConstraintFeasibilityRegionStepSolver method getDisequals.

protected ArrayList<Expression> getDisequals() {
    if (disequals == null) {
        AbstractSingleVariableConstraint abstractSingleVariableConstraint = (AbstractSingleVariableConstraint) constraint;
        Iterator<Expression> disequalsIterator = functionIterator(predicateIterator(abstractSingleVariableConstraint.getNegativeNormalizedAtoms(), // negative equality is disequality
        e -> e.hasFunctor(FunctorConstants.EQUALITY)), e -> e.get(1));
        disequals = arrayListFrom(disequalsIterator);
    }
    return disequals;
}
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) AbstractSingleVariableConstraint(com.sri.ai.grinder.core.constraint.AbstractSingleVariableConstraint)

Example 5 with FunctionIterator

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

the class AbstractSingleVariableNumericConstraintFeasibilityRegionStepSolver method getEquals.

protected ArrayList<Expression> getEquals() {
    if (equals == null) {
        AbstractSingleVariableConstraint abstractSingleVariableConstraint = (AbstractSingleVariableConstraint) constraint;
        Iterator<Expression> equalsIterator = functionIterator(predicateIterator(abstractSingleVariableConstraint.getPositiveNormalizedAtoms(), e -> e.hasFunctor(EQUALITY)), e -> e.get(1));
        equals = arrayListFrom(equalsIterator);
    }
    return equals;
}
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) AbstractSingleVariableConstraint(com.sri.ai.grinder.core.constraint.AbstractSingleVariableConstraint)

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