use of com.sri.ai.praise.core.representation.interfacebased.polytope.api.Polytope in project aic-praise by aic-sri-international.
the class Polytopes method getEquivalentAtomicPolytopeOn.
/**
* Takes a polytope in which the only free variable is a given query variable,
* and returns a single equivalent {@link AtomicPolytope}.
*
* @return
*/
public static AtomicPolytope getEquivalentAtomicPolytopeOn(Variable query, Polytope polytope) {
myAssert(polytope.getFreeVariables().size() == 1 && polytope.getFreeVariables().contains(query), () -> "getEquivalentIntensionalConvexHullOfFactorsOn must receive polytope whose only free variable is " + query + ", but instead got <" + polytope + "> with free variables " + polytope.getFreeVariables());
AtomicPolytope result;
final List<? extends AtomicPolytope> nonIdentityAtomicPolytopes = getNonIdentityAtomicPolytopes(list(polytope));
Simplex simplexOnVariableIfAny = (Simplex) getFirstSatisfyingPredicateOrNull(nonIdentityAtomicPolytopes, p -> isSimplexOn(p, query));
boolean thereIsSimplexOnQuerySoItDominates = simplexOnVariableIfAny != null;
if (thereIsSimplexOnQuerySoItDominates) {
result = simplexOnVariableIfAny;
} else {
// all nonIdentityAtomicPolytopes are intensional convex hulls, or otherwise we would have simplexes on non-query variables and the query would not be the only free variable
result = mergeIntensionalConvexHulls(nonIdentityAtomicPolytopes);
}
return result;
}
use of com.sri.ai.praise.core.representation.interfacebased.polytope.api.Polytope 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.praise.core.representation.interfacebased.polytope.api.Polytope in project aic-praise by aic-sri-international.
the class Polytopes method boxPolytopes.
private static Polytope boxPolytopes(List<Polytope> toBeBoxed) {
LinkedList<Polytope> boxedPolytopes = list();
for (Polytope p : toBeBoxed) {
if (p instanceof IntensionalConvexHullOfFactors && !((IntensionalConvexHullOfFactors) p).getIndices().isEmpty() && ((IntensionalConvexHullOfFactors) p).getFactor() instanceof TableFactor) {
// Box boxedP = TableFactorBoxBuilder.makeTableBox((IntensionalConvexHullOfFactors) p);
Box boxedP = Box.boxIt((IntensionalConvexHullOfFactors) p);
boxedPolytopes.add(boxedP);
} else {
boxedPolytopes.add(p);
}
}
Polytope result = Polytope.multiply(boxedPolytopes);
return result;
}
use of com.sri.ai.praise.core.representation.interfacebased.polytope.api.Polytope in project aic-praise by aic-sri-international.
the class Polytopes method multiplyListOfAlreadyMultipledNonIdentityAtomicPolytopesWithANewOne.
public static Polytope multiplyListOfAlreadyMultipledNonIdentityAtomicPolytopesWithANewOne(Collection<? extends AtomicPolytope> nonIdentityAtomicPolytopes, AtomicPolytope nonIdentityAtomicAnother) {
List<AtomicPolytope> resultNonIdentityAtomicPolytopes = list();
boolean anotherAlreadyIncorporated = false;
for (AtomicPolytope nonIdentityAtomicPolytope : nonIdentityAtomicPolytopes) {
anotherAlreadyIncorporated = takeNextPolytopeInList(nonIdentityAtomicPolytope, nonIdentityAtomicAnother, resultNonIdentityAtomicPolytopes, anotherAlreadyIncorporated);
}
includeAnotherByItselfIfMultiplicationsFailed(nonIdentityAtomicAnother, anotherAlreadyIncorporated, resultNonIdentityAtomicPolytopes);
Polytope result = makePolytopeFromListOfNonIdentityAtomicPolytopes(resultNonIdentityAtomicPolytopes);
return result;
}
use of com.sri.ai.praise.core.representation.interfacebased.polytope.api.Polytope in project aic-praise by aic-sri-international.
the class AbstractAEBPTreeNode method messageSent.
// ----------- Message -----------
@Override
public Polytope messageSent(Predicate<Polytope> boxIt) {
if (!needToUpdateTheApproximation) {
return currentApproximation;
}
needToUpdateTheApproximation = false;
Polytope product = computeProductOfFactorAtRootTimesTheIncomingMessages(boxIt);
// Collection<? extends Variable> allFreeVariablesInProduct = product.getFreeVariables();
List<? extends Variable> variablesToBeSummedOut = getVariablesToBeSummedOut();
Polytope summedOutPolytope = Polytopes.sumOut(variablesToBeSummedOut, product);
Polytope result = // = Polytopes.BoxAPolytopeAccordingToCriteria(summedOutPolytope, criteriaToBoxAPolytope);
Polytopes.BoxAPolytope(summedOutPolytope, boxIt);
currentApproximation = result;
return result;
}
Aggregations