use of com.sri.ai.util.collect.CartesianProductIterator 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();
}
use of com.sri.ai.util.collect.CartesianProductIterator in project aic-expresso by aic-sri-international.
the class Bounds method boundProduct.
/**
* Computes the product of each term of a list of bounds
* @param theory
* @param context
* @param listOfBounds
* @return bound resulting from the product of bounds
*/
public static Expression boundProduct(Theory theory, Context context, Expression... listOfBounds) {
ArrayList<NullaryFunction<Iterator<Expression>>> iteratorForBoundList = mapIntoArrayList(listOfBounds, bound -> () -> getElements(bound).iterator());
Iterator<ArrayList<Expression>> cartesianProduct = new CartesianProductIterator<Expression>(iteratorForBoundList);
ArrayList<Expression> resultList = new ArrayList<>();
for (ArrayList<Expression> element : in(cartesianProduct)) {
Expression product = apply("*", element);
Expression evaluation = theory.evaluate(product, context);
resultList.add(evaluation);
}
Expression result = new DefaultExtensionalUniSet(resultList);
// Updating extreme points
result = updateExtremes(result, theory, context);
return result;
}
use of com.sri.ai.util.collect.CartesianProductIterator 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();
}
use of com.sri.ai.util.collect.CartesianProductIterator in project aic-praise by aic-sri-international.
the class Polytopes method IntensionalConvexHullToListOfFactors.
public static List<TableFactor> IntensionalConvexHullToListOfFactors(IntensionalConvexHullOfFactors polytope) {
TableFactor factor = (TableFactor) polytope.getFactor();
List<TableVariable> indexes = mapIntoArrayList(polytope.getIndices(), v -> (TableVariable) v);
List<List<Integer>> listOfListOfValues = mapIntoList(indexes, v -> mapIntoList(v.getValues(), o -> (Integer) o));
List<NullaryFunction<Iterator<Integer>>> iteratorForListOfVariableValues = mapIntoList(listOfListOfValues, element -> () -> element.iterator());
Iterator<ArrayList<Integer>> cartesianProduct = new CartesianProductIterator<Integer>(iteratorForListOfVariableValues);
List<TableFactor> result = new LinkedList<>();
for (List<Integer> instantiations : in(cartesianProduct)) {
result.add(TableFactor.copyToSubTableFactor(factor, indexes, instantiations));
}
return result;
}
use of com.sri.ai.util.collect.CartesianProductIterator in project aic-expresso by aic-sri-international.
the class DefaultExtensionalBound method boundProduct.
public static DefaultExtensionalBound boundProduct(Theory theory, Context context, Bound... listOfBounds) {
if (listOfBounds.length == 0) {
DefaultExtensionalBound singletonWithNumberOne = new DefaultExtensionalBound(parse("1"));
return singletonWithNumberOne;
}
ArrayList<NullaryFunction<Iterator<Expression>>> iteratorForBoundList = mapIntoArrayList(listOfBounds, bound -> () -> getElements(bound).iterator());
Iterator<ArrayList<Expression>> cartesianProduct = new CartesianProductIterator<Expression>(iteratorForBoundList);
if (!cartesianProduct.hasNext()) {
DefaultExtensionalBound singletonWithNumberOne = new DefaultExtensionalBound(parse("1"));
println("One of the bounds on the list is { }, which is an error");
return singletonWithNumberOne;
}
ArrayList<Expression> resultList = new ArrayList<>();
for (ArrayList<? extends Expression> element : in(cartesianProduct)) {
if (element == null || element.get(0) == null) {
return null;
}
Expression product = apply("*", element);
Expression evaluation = theory.evaluate(product, context);
resultList.add(evaluation);
}
DefaultExtensionalBound result = new DefaultExtensionalBound(resultList);
// Updating extreme points
result = updateExtremes(result, theory, context);
return result;
}
Aggregations