Search in sources :

Example 1 with PredicateIterator

use of com.sri.ai.util.collect.PredicateIterator 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 2 with PredicateIterator

use of com.sri.ai.util.collect.PredicateIterator 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.getTypeExpressionOfExpression(getConstraint().getVariable(), context);
            Type type = context.getTypeFromTypeExpression(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) Equality(com.sri.ai.grinder.library.Equality) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) DISEQUALITY(com.sri.ai.grinder.library.FunctorConstants.DISEQUALITY) Context(com.sri.ai.grinder.api.Context) 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) Util.arrayListFrom(com.sri.ai.util.Util.arrayListFrom) Function(com.google.common.base.Function) Type(com.sri.ai.expresso.api.Type) AbstractBooleanWithPropagatedLiteralsRequiringPropagatedLiteralsAndCNFToBeSatisfiedStepSolver(com.sri.ai.grinder.core.solver.AbstractBooleanWithPropagatedLiteralsRequiringPropagatedLiteralsAndCNFToBeSatisfiedStepSolver) Iterator(java.util.Iterator) Util.list(com.sri.ai.util.Util.list) Set(java.util.Set) 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)

Aggregations

Beta (com.google.common.annotations.Beta)2 Function (com.google.common.base.Function)2 Expression (com.sri.ai.expresso.api.Expression)2 Type (com.sri.ai.expresso.api.Type)2 TRUE (com.sri.ai.expresso.helper.Expressions.TRUE)2 Expressions.apply (com.sri.ai.expresso.helper.Expressions.apply)2 Expressions.zipApply (com.sri.ai.expresso.helper.Expressions.zipApply)2 GrinderUtil (com.sri.ai.grinder.helper.GrinderUtil)2 Util (com.sri.ai.util.Util)2 Util.arrayList (com.sri.ai.util.Util.arrayList)2 Util.arrayListFrom (com.sri.ai.util.Util.arrayListFrom)2 Util.in (com.sri.ai.util.Util.in)2 Util.list (com.sri.ai.util.Util.list)2 Util.thereExists (com.sri.ai.util.Util.thereExists)2 Util.toLinkedHashSet (com.sri.ai.util.Util.toLinkedHashSet)2 PairOf (com.sri.ai.util.base.PairOf)2 PairOf.makePairOf (com.sri.ai.util.base.PairOf.makePairOf)2 CartesianProductIterator (com.sri.ai.util.collect.CartesianProductIterator)2 FunctionIterator (com.sri.ai.util.collect.FunctionIterator)2 FunctionIterator.functionIterator (com.sri.ai.util.collect.FunctionIterator.functionIterator)2