Search in sources :

Example 41 with TableVariable

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable 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 42 with TableVariable

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable 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 43 with TableVariable

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable 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 44 with TableVariable

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable 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)

Example 45 with TableVariable

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

the class Box method makeTableFactorBox.

private static TableFactor makeTableFactorBox(TableFactor phiMin, TableFactor phiMax) {
    ArrayList<TableVariable> variables = Util.arrayList(TABLE_BOX_VARIABLE);
    variables.addAll(phiMax.getVariables());
    ArrayList<Double> entries = new ArrayList<>(phiMin.getEntries());
    entries.addAll(phiMax.getEntries());
    TableFactor result = new TableFactor(variables, entries);
    return result;
}
Also used : TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)

Aggregations

TableVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)49 TableFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor)34 ArrayList (java.util.ArrayList)17 Test (org.junit.Test)13 Util.mapIntoArrayList (com.sri.ai.util.Util.mapIntoArrayList)10 Variable (com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable)8 LinkedHashMap (java.util.LinkedHashMap)8 List (java.util.List)8 TableFactor.copyToSubTableFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor.copyToSubTableFactor)7 TableFactorNetwork (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactorNetwork)6 Pair (com.sri.ai.util.base.Pair)6 Expression (com.sri.ai.expresso.api.Expression)4 Factor (com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor)3 DefaultDatapoint (com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint)3 DefaultDataset (com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset)3 TableBayesianNode (com.sri.ai.praise.learning.parameterlearning.representation.table.TableBayesianNode)3 Util (com.sri.ai.util.Util)3 Util.arrayList (com.sri.ai.util.Util.arrayList)3 File (java.io.File)3 LinkedList (java.util.LinkedList)3