Search in sources :

Example 36 with TableFactor

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor in project aic-praise by aic-sri-international.

the class BenchmarkingIsingModel method testEBPP.

private static void testEBPP(AEBPTestingDataFrame df, int nRepetitions, List<TableFactor> network, TableVariable query, String netName) {
    TableFactorNetwork factorNetwork = new TableFactorNetwork(network);
    ExactBPNode<Variable, Factor> exactBP = new ExactBP(query, factorNetwork);
    for (int i = 0; i < nRepetitions; i++) {
        println("solveWithExactBP : " + netName);
        Pair<Double, Factor> p = solveAndPrint(exactBP);
        df.addRow(// run number
        i, // iteration
        -1, // minPTrue
        ((TableFactor) p.second).getEntries().get(0), // MaxPTrue
        ((TableFactor) p.second).getEntries().get(0), // iteration time
        p.first, // total time
        p.first, // InferenceMethodUsed
        "ExactBP", // GraphicalModelName
        netName + "; query: " + query.toString());
    }
}
Also used : Variable(com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) ExactBP(com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.fulltime.core.ExactBP) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) TableFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactorNetwork)

Example 37 with TableFactor

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor in project aic-praise by aic-sri-international.

the class TableFactorBoxBuilder method makeTableBox.

public static Box makeTableBox(IntensionalConvexHullOfFactors bound) {
    ArrayList<TableVariable> notFreeVariables = new ArrayList<>(bound.getIndices().size());
    for (Variable v : bound.getIndices()) {
        notFreeVariables.add((TableVariable) v);
    }
    TableFactor factor = buildBoxFactor((TableFactor) bound.getFactor(), notFreeVariables);
    Box result = new Box(TABLE_BOX_VARIABLE, factor);
    return result;
}
Also used : Variable(com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) 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) ArrayList(java.util.ArrayList) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)

Example 38 with TableFactor

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor in project aic-praise by aic-sri-international.

the class TableFactorBoxBuilder method buildBoxFactorIfSetIsNotEmpty.

private static TableFactor buildBoxFactorIfSetIsNotEmpty(TableFactor factor, ArrayList<TableVariable> notFreeVariables) {
    LinkedHashSet<TableVariable> setOfFreeVariables = new LinkedHashSet<TableVariable>(factor.getVariables());
    setOfFreeVariables.removeAll(notFreeVariables);
    boolean initialFactorIsABox = setOfFreeVariables.contains(TABLE_BOX_VARIABLE);
    if (initialFactorIsABox) {
        setOfFreeVariables.remove(TABLE_BOX_VARIABLE);
    }
    // ArrayList<TableVariable> listOfFreeVariables = Util.arrayList(TABLE_BOX_VARIABLE);
    // listOfFreeVariables.addAll(newFactorSetOfVariables);
    ArrayList<TableVariable> listOfFreeVariables = new ArrayList<>(setOfFreeVariables);
    TableFactor newFactorLowHalf;
    TableFactor newFactorHighHalf;
    if (initialFactorIsABox) {
        Pair<TableFactor, TableFactor> factorHalves = divideABoxFactorIntoTwoHalves(factor);
        TableFactor factorLowHalf = factorHalves.first;
        TableFactor factorHighHalf = factorHalves.second;
        newFactorLowHalf = maxOrMinOut(factorLowHalf, listOfFreeVariables, notFreeVariables, (newValue, oldValue) -> newValue < oldValue);
        newFactorHighHalf = maxOrMinOut(factorHighHalf, listOfFreeVariables, notFreeVariables, (newValue, oldValue) -> (newValue > oldValue));
    } else {
        newFactorLowHalf = maxOrMinOut(factor, listOfFreeVariables, notFreeVariables, (newValue, oldValue) -> (newValue < oldValue));
        newFactorHighHalf = maxOrMinOut(factor, listOfFreeVariables, notFreeVariables, (newValue, oldValue) -> (newValue > oldValue));
    }
    TableFactor result = makeABoxFactorHavingTheBoxesExtremes(newFactorLowHalf, newFactorHighHalf);
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) 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) ArrayList(java.util.ArrayList) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)

Example 39 with TableFactor

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor in project aic-praise by aic-sri-international.

the class TableFactorBoxBuilder method maxOrMinOut.

public static TableFactor maxOrMinOut(TableFactor nonBoxfactor, ArrayList<TableVariable> freeVariables, ArrayList<TableVariable> notFreeVariables, BiFunction<Double, Double, Boolean> comparisson) {
    TableFactor result = new TableFactor(freeVariables, arrayListFilledWith(-1.0, numEntries(freeVariables)));
    for (ArrayList<Integer> notFreeVariablesInstantiation : in(getCartesianProduct(notFreeVariables))) {
        LinkedHashMap<TableVariable, Integer> mapOfInstantiations = new LinkedHashMap<>();
        addValuesToMapFromVariableToInstantiation(notFreeVariables, notFreeVariablesInstantiation, mapOfInstantiations);
        TableFactor thisInstanciation = copyToSubTableFactor(nonBoxfactor, mapOfInstantiations);
        if (normalize_) {
            thisInstanciation = thisInstanciation.normalize();
        }
        for (ArrayList<Integer> freeVariablesInstantiation : in(getCartesianProduct(freeVariables))) {
            addValuesToMapFromVariableToInstantiation(freeVariables, freeVariablesInstantiation, mapOfInstantiations);
            Double currentValue = result.getEntryFor(mapOfInstantiations);
            Double newValue = thisInstanciation.getEntryFor(mapOfInstantiations);
            if (currentValue == -1.0 || comparisson.apply(newValue, currentValue)) {
                result.setEntryFor(mapOfInstantiations, newValue);
            }
        }
    }
    return result;
}
Also used : 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) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) LinkedHashMap(java.util.LinkedHashMap)

Example 40 with TableFactor

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor in project aic-praise by aic-sri-international.

the class TableFactorBoxBuilder method divideABoxFactorIntoTwoHalves.

private static Pair<TableFactor, TableFactor> divideABoxFactorIntoTwoHalves(TableFactor factor) {
    ArrayList<TableVariable> varaibles = (ArrayList<TableVariable>) factor.getVariables();
    TableFactor minValueHalfFactor;
    TableFactor maxValueHalfFactor;
    if (varaibles.get(0).equals(TABLE_BOX_VARIABLE)) {
        println(varaibles.subList(1, varaibles.size()).getClass());
        ArrayList<TableVariable> variablesWithoutBoxVariable = new ArrayList<>(varaibles.subList(1, varaibles.size()));
        ArrayList<Double> entries = factor.getEntries();
        int len = entries.size();
        ArrayList<Double> lowHalfEntries = new ArrayList<>(entries.subList(0, len / 2));
        ArrayList<Double> highHalfEntries = new ArrayList<>(entries.subList(len / 2, len));
        minValueHalfFactor = new TableFactor(variablesWithoutBoxVariable, lowHalfEntries);
        maxValueHalfFactor = new TableFactor(variablesWithoutBoxVariable, highHalfEntries);
    } else {
        // Util.println(TABLE_BOX_VARIABLE.toString() + "should be the first variable, but wasn't");
        LinkedHashMap<TableVariable, Integer> mapOfvaluesPredetermined = new LinkedHashMap<>();
        mapOfvaluesPredetermined.put(TABLE_BOX_VARIABLE, 0);
        minValueHalfFactor = copyToSubTableFactor(factor, mapOfvaluesPredetermined);
        mapOfvaluesPredetermined = new LinkedHashMap<>();
        mapOfvaluesPredetermined.put(TABLE_BOX_VARIABLE, 1);
        maxValueHalfFactor = copyToSubTableFactor(factor, mapOfvaluesPredetermined);
    }
    Pair<TableFactor, TableFactor> result = new Pair<>(minValueHalfFactor, maxValueHalfFactor);
    return result;
}
Also used : 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) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) Pair(com.sri.ai.util.base.Pair)

Aggregations

TableFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor)52 TableVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)36 ArrayList (java.util.ArrayList)19 Test (org.junit.Test)11 Factor (com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor)10 Util.mapIntoArrayList (com.sri.ai.util.Util.mapIntoArrayList)10 List (java.util.List)9 ExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor)8 TableFactor.copyToSubTableFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor.copyToSubTableFactor)8 Pair (com.sri.ai.util.base.Pair)7 Variable (com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable)6 TableFactorNetwork (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactorNetwork)6 LinkedHashMap (java.util.LinkedHashMap)6 Expression (com.sri.ai.expresso.api.Expression)5 DefaultExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor)5 Util (com.sri.ai.util.Util)5 Util.arrayList (com.sri.ai.util.Util.arrayList)5 Util.println (com.sri.ai.util.Util.println)5 BiFunction (java.util.function.BiFunction)5 LinkedList (java.util.LinkedList)4