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());
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations