Search in sources :

Example 1 with IntensionalConvexHullOfFactors

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));
}
Also used : Iterator(java.util.Iterator) BiFunction(java.util.function.BiFunction) TableFactor.getCartesianProduct(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor.getCartesianProduct) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) Variable(com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Util.in(com.sri.ai.util.Util.in) List(java.util.List) TableFactor.numEntries(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor.numEntries) Util.arrayListFilledWith(com.sri.ai.util.Util.arrayListFilledWith) IntensionalConvexHullOfFactors(com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.IntensionalConvexHullOfFactors) TABLE_BOX_VARIABLE(com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.box.TableBoxVariable.TABLE_BOX_VARIABLE) Util(com.sri.ai.util.Util) Util.println(com.sri.ai.util.Util.println) TableFactor.copyToSubTableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor.copyToSubTableFactor) Util.arrayList(com.sri.ai.util.Util.arrayList) LinkedHashSet(java.util.LinkedHashSet) Pair(com.sri.ai.util.base.Pair) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) TableFactor.copyToSubTableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor.copyToSubTableFactor) IntensionalConvexHullOfFactors(com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.IntensionalConvexHullOfFactors) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with IntensionalConvexHullOfFactors

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");
}
Also used : Polytope(com.sri.ai.praise.core.representation.interfacebased.polytope.api.Polytope) ExactBP(com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.fulltime.core.ExactBP) Util.list(com.sri.ai.util.Util.list) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) AEBPRPlotting(com.sri.ai.util.rplot.AEBPRPlotting) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) Polytope(com.sri.ai.praise.core.representation.interfacebased.polytope.api.Polytope) File(java.io.File) AnytimeExactBP(com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.anytime.rodrigo.AnytimeExactBP) Variable(com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) ArrayList(java.util.ArrayList) TestCases(com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.anytime.gabriel.TestCases) List(java.util.List) TableFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactorNetwork) ExactBPNode(com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.fulltime.api.ExactBPNode) Predicate(com.google.common.base.Predicate) AEBP(com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.anytime.gabriel.AEBP) EditableFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.api.EditableFactorNetwork) AEBPTestingDataFrame(com.sri.ai.util.rplot.dataframe.AEBPTestingDataFrame) IntensionalConvexHullOfFactors(com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.IntensionalConvexHullOfFactors) Util.println(com.sri.ai.util.Util.println) LinkedHashSet(java.util.LinkedHashSet) AssignmentsIterator(com.sri.ai.grinder.helper.AssignmentsIterator) IntensionalConvexHullOfFactors(com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.IntensionalConvexHullOfFactors) AEBP(com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.anytime.gabriel.AEBP)

Example 3 with IntensionalConvexHullOfFactors

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());
}
Also used : ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) IntensionalConvexHullOfFactors(com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.IntensionalConvexHullOfFactors) Test(org.junit.Test)

Example 4 with IntensionalConvexHullOfFactors

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;
}
Also used : IntensionalConvexHullOfFactors(com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.IntensionalConvexHullOfFactors) ConstantFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.ConstantFactor) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor)

Example 5 with IntensionalConvexHullOfFactors

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;
}
Also used : Simplex(com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.Simplex) Variable(com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable) IntensionalConvexHullOfFactors(com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.IntensionalConvexHullOfFactors) HashSet(java.util.HashSet)

Aggregations

IntensionalConvexHullOfFactors (com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.IntensionalConvexHullOfFactors)16 Factor (com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor)9 ConstantFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.ConstantFactor)7 AtomicPolytope (com.sri.ai.praise.core.representation.interfacebased.polytope.api.AtomicPolytope)5 Variable (com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable)4 Polytope (com.sri.ai.praise.core.representation.interfacebased.polytope.api.Polytope)4 Polytopes.identityPolytope (com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.Polytopes.identityPolytope)3 ArrayList (java.util.ArrayList)3 TableFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor)2 TableVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)2 Simplex (com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.Simplex)2 Util.println (com.sri.ai.util.Util.println)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 Predicate (com.google.common.base.Predicate)1 AssignmentsIterator (com.sri.ai.grinder.helper.AssignmentsIterator)1 AEBP (com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.anytime.gabriel.AEBP)1 TestCases (com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.anytime.gabriel.TestCases)1 AnytimeExactBP (com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.anytime.rodrigo.AnytimeExactBP)1 ExactBPNode (com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.fulltime.api.ExactBPNode)1