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