use of com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.IntensionalConvexHullOfFactors in project aic-praise by aic-sri-international.
the class TableFactorBoxBuilder method main.
public static void main(String[] args) {
TableVariable A = new TableVariable("A", 2);
TableVariable B = new TableVariable("B", 2);
TableVariable C = new TableVariable("C", 2);
TableVariable D = new TableVariable("D", 2);
TableFactor fABCD = new TableFactor(arrayList(A, B, C, D), arrayList(0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5));
TableFactor fBoxABCD = new TableFactor(arrayList(TABLE_BOX_VARIABLE, A, B, C, D), arrayList(0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 10., 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 11.0, 11.1, 11.2, 11.3, 11.4, 11.5));
ArrayList<TableVariable> list = arrayList(A, B, C, D, TABLE_BOX_VARIABLE);
TableFactor fABCDBox = new TableFactor(list);
LinkedHashMap<TableVariable, Integer> map = new LinkedHashMap<>();
for (ArrayList<Integer> instantiation : in(getCartesianProduct(list))) {
addValuesToMapFromVariableToInstantiation(list, instantiation, map);
fABCDBox.setEntryFor(map, fBoxABCD.getEntryFor(map));
}
println(fABCD);
println(fBoxABCD);
println(fABCDBox);
Pair<TableFactor, TableFactor> pair = divideABoxFactorIntoTwoHalves(fBoxABCD);
println("Lower box: " + pair.first);
println("Higher box: " + pair.second);
pair = divideABoxFactorIntoTwoHalves(fABCDBox);
println("Lower box: " + pair.first);
println("Higher box: " + pair.second);
println(maxOrMinOut(fABCD, arrayList(A, B), arrayList(C, D), (newValue, oldValue) -> (newValue < oldValue)));
println(maxOrMinOut(fABCD, arrayList(A, B), arrayList(C, D), (newValue, oldValue) -> (newValue > oldValue)));
println("BoxFactor" + makeABoxFactorHavingTheBoxesExtremes(fABCD, fABCD));
// -----------------------------------------------
IntensionalConvexHullOfFactors ICHOF = new IntensionalConvexHullOfFactors(Util.list(TABLE_BOX_VARIABLE, A, B), fABCDBox);
println(ICHOF);
println(makeTableBox(ICHOF));
TableFactor fCBDA = copyFactorInDifferentOrder(arrayList(D, B, TABLE_BOX_VARIABLE, C, A), fABCDBox);
ICHOF = new IntensionalConvexHullOfFactors(Util.list(A, TABLE_BOX_VARIABLE, B), fCBDA);
println(makeTableBox(ICHOF));
}
use of com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.IntensionalConvexHullOfFactors in project aic-praise by aic-sri-international.
the class AnytimeExactBPTest3 method solveGabrielWithBoxing.
public static AEBPTestingDataFrame solveGabrielWithBoxing(Variable query, EditableFactorNetwork factorNetwork, long maximunTimeInSeconds, String PGMName) {
println("\nSolving with Gabriel's Anytime - boxing\n");
Predicate<Polytope> criteria = p -> ((IntensionalConvexHullOfFactors) p).getIndices().size() > 10;
AEBP aebp = new AEBP(factorNetwork, query, criteria, true);
/*while(aebp.hasNext()) {
Polytope p = aebp.next();
println(p);
}*/
return AEBPSolver.solve(aebp, query, maximunTimeInSeconds, 0, PGMName, "Boxing");
}
use of com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.IntensionalConvexHullOfFactors in project aic-praise by aic-sri-international.
the class PolytopeTest method testGetEquivalentAtomicPolytope.
@Test
public void testGetEquivalentAtomicPolytope() {
product = Polytope.multiply(list(simplexU));
actual = Polytopes.getEquivalentAtomicPolytopeOn(u, product);
println("Atomic polytope on u equivalent to " + product + ": " + actual);
expected = simplexU;
assertEquals(expected, actual);
product = Polytope.multiply(list(simplexU, convexHullUV));
actual = Polytopes.getEquivalentAtomicPolytopeOn(u, product);
println("Atomic polytope on u equivalent to " + product + ": " + actual);
expected = simplexU;
assertEquals(expected, actual);
product = Polytope.multiply(list(simplexU, convexHullUFreeVBound, convexHullVW));
actual = Polytopes.getEquivalentAtomicPolytopeOn(u, product);
println("Atomic polytope on u equivalent to " + product + ": " + actual);
expected = simplexU;
assertEquals(expected, actual);
product = Polytope.multiply(list(simplexU, convexHullUV));
actual = Polytopes.getEquivalentAtomicPolytopeOn(u, product);
println("Atomic polytope on u equivalent to " + product + ": " + actual);
expected = simplexU;
assertEquals(expected, actual);
product = Polytope.multiply(list(simplexU, convexHullUFreeVBound));
actual = Polytopes.getEquivalentAtomicPolytopeOn(u, product);
println("Atomic polytope on u equivalent to " + product + ": " + actual);
expected = simplexU;
assertEquals(expected, actual);
try {
product = Polytope.multiply(list(simplexU, convexHullUBoundVFree));
actual = Polytopes.getEquivalentAtomicPolytopeOn(u, product);
fail("Should have failed because V is free in polytope but query is U");
} catch (AssertionError e) {
if (!e.getMessage().contains("free variables")) {
fail("Should have complained about free variables");
}
}
product = Polytope.multiply(list(convexHullUFreeVBound, convexHullVW));
actual = Polytopes.getEquivalentAtomicPolytopeOn(u, product);
println("Atomic polytope on u equivalent to " + product + ": " + actual);
ExpressionFactor expectedExpressionFactor = new DefaultExpressionFactor(parse("if U then if V then if W then 8 else 10 else 15 else if V then if W then 12 else 15 else 15"), context);
expected = new IntensionalConvexHullOfFactors(list(v, w), expectedExpressionFactor);
println(expected.toString());
println(actual.toString());
// factor are compared by reference, not value
assertEquals(expected.toString(), actual.toString());
}
use of com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.IntensionalConvexHullOfFactors in project aic-praise by aic-sri-international.
the class GradientAnytimeExactBP method createNormalizedMaxFactor.
private Factor createNormalizedMaxFactor(Collection<? extends Variable> rootIndices) {
Approximation<Factor> rootApproximation = getCurrentApproximation();
IntensionalConvexHullOfFactors rootConvexHull = transformApproximationToConvexHullOrThrowsErrorIfNotPossible(rootApproximation);
Factor rootFactor = rootConvexHull.getFactor();
Factor maxFactor = rootFactor.max(rootIndices);
Factor normalizedMaxFactor = maxFactor.normalize();
return normalizedMaxFactor;
}
use of com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.IntensionalConvexHullOfFactors in project aic-praise by aic-sri-international.
the class GradientAnytimeExactBP method getIndices.
private static Set<? extends Variable> getIndices(AtomicPolytope subAtomicPolytope) {
Set<Variable> result = new HashSet<>();
if (subAtomicPolytope instanceof Simplex) {
Simplex subSimplex = (Simplex) subAtomicPolytope;
result.add(subSimplex.getVariable());
} else if (subAtomicPolytope instanceof IntensionalConvexHullOfFactors) {
IntensionalConvexHullOfFactors subConvexHull = (IntensionalConvexHullOfFactors) subAtomicPolytope;
result.addAll(subConvexHull.getIndices());
} else {
throw new Error("New unsupported type of atomic polytope added");
}
return result;
}
Aggregations