use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor in project aic-praise by aic-sri-international.
the class ConstantFactor method add.
@Override
public Factor add(Factor another) {
Factor result = null;
if (another instanceof ConstantFactor) {
ConstantFactor anotherConstant = (ConstantFactor) another;
result = addTwoConstantFactors(anotherConstant);
} else if (another instanceof ExpressionFactor) {
ExpressionFactor anotherExpression = (ExpressionFactor) another;
result = evaluateAsFactor(Plus.make(makeSymbol(getConstant()), (Expression) another), anotherExpression.getContext());
} else if (another instanceof TableFactor) {
TableFactor anotherTable = (TableFactor) another;
result = addATableFactor(anotherTable);
} else {
throw new Error("Unknown class for another : class was " + another.getClass());
}
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 AnytimeExactBPTest3 method main.
public static void main(String[] args) {
for (Integer i : list(1, 2, 3, 4)) {
testing(TestCases.treeWithExponentialGaussianRandomEntries(10, i, 2, .3, .1), 20, new TableVariable("g_0_0", 2), "treeExpGauss" + i);
}
List<TableFactor> fs = TestCases.TableFactorALARM();
testing(fs, 500, new TableVariable("g22", 3), "alarmante");
/* for(Integer i : list(-2,-1,0,1)) {
testing((List<TableFactor>) TestCases.isingModelGridWithWeigthsAndPotetialNormalyDistributed(20, Math.pow(10, i), true),
5,
new TableVariable("10_10", 2),
"IsingNormalRandom" + i);
}
println(factors.get(27).getVariables());
for(TableFactor f : factors) {
println(f);
}
List<TableFactor> factors = TestCases.getListOfTableFactors("Seg","BN_8");
testing(factors,
5000,
factors.get(27).getVariables().get(0),
"promedas-");
*/
ArrayList<File> Files = TestCases.retrieveUAIFilesInFolder("Segmentation");
int i = 0;
for (File file : Files) {
println(i++ + "---------------" + file.getName() + "---------------");
}
for (File file : Files) {
file = Files.get(48);
println("---------------" + file.getName() + "---------------");
List<TableFactor> factors = TestCases.getListOfTableFactors("Segmentation", file.getName());
testing(factors, 20, factors.get(0).getVariables().get(0), "Segmentation-" + file.getName() + "0");
testing(factors, 200, factors.get(10).getVariables().get(0), "Segmentation-" + file.getName() + "1");
testing(factors, 20, factors.get(15).getVariables().get(0), "Segmentation-" + file.getName() + "2");
}
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor in project aic-praise by aic-sri-international.
the class AnytimeExactBPTest3 method testingALARM.
public static void testingALARM() {
ArrayList<TableFactor> factors = TestCases.TableFactorALARM();
LinkedHashSet<TableVariable> variablesInALARM = new LinkedHashSet<>();
for (TableFactor f : factors) {
variablesInALARM.addAll(f.getVariables());
}
println(variablesInALARM);
for (TableVariable v : variablesInALARM) {
testing(factors, 20, v, "Alarm-Var=" + v);
}
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor in project aic-praise by aic-sri-international.
the class AnytimeExactBPTest2 method getMaxMinProbabilityFromIntensionalConvHull.
private static ArrayList<Double> getMaxMinProbabilityFromIntensionalConvHull(IntensionalConvexHullOfFactors polytopeOnQuery, boolean maxOrMin) {
TableFactor factor = (TableFactor) polytopeOnQuery.getFactor();
TableVariable query = (TableVariable) polytopeOnQuery.getFreeVariables().iterator().next();
List<Variable> variablesInOrder = new ArrayList<>(polytopeOnQuery.getIndices());
List<List<Integer>> listOflistOfInstantiations = getListOfListOfInstantiations(variablesInOrder);
Iterator<ArrayList<Integer>> iter = getCartesianProductWithValuesOfVariablesToSum(listOflistOfInstantiations);
Double[] resultArray = maxOrMin ? initializeWithValue(query.getCardinality(), -1.0) : initializeWithValue(query.getCardinality(), 2.0);
for (ArrayList<Integer> instantiation : in(iter)) {
LinkedHashMap<Variable, Integer> mapOfInstantiations = new LinkedHashMap<>();
int k = 0;
for (Variable v : variablesInOrder) {
mapOfInstantiations.put(v, instantiation.get(k++));
}
ArrayList<Double> nonNormalizedProbabilitiesForThisInstantiation = new ArrayList<>(query.getCardinality());
for (int i = 0; i < query.getCardinality(); i++) {
mapOfInstantiations.put(query, i);
nonNormalizedProbabilitiesForThisInstantiation.add(factor.getEntryFor(mapOfInstantiations));
}
Double[] normalizedProba = nomalize(nonNormalizedProbabilitiesForThisInstantiation);
for (int i = 0; i < normalizedProba.length; i++) {
if (maxOrMin && normalizedProba[i] > resultArray[i]) {
resultArray[i] = normalizedProba[i];
}
if (!maxOrMin && normalizedProba[i] < resultArray[i]) {
resultArray[i] = normalizedProba[i];
}
}
}
ArrayList<Double> result = new ArrayList<>();
for (int i = 0; i < resultArray.length; i++) {
result.add(resultArray[i]);
}
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 AnytimeExactBPTest2 method isingModel.
public static Pair<TableVariable, TableFactorNetwork> isingModel(String queryName, int gridSize, double potential) {
Pair<TableVariable, TableFactorNetwork> pairQueryNet;
// TestCases.isingModelGridWithRandomWeigthsAndPotetial(gridSize, potential);
List<TableFactor> factors = null;
TableVariable query = null;
for (TableFactor f : factors) {
for (TableVariable v : f.getVariables()) {
if (v.getName().equals(queryName)) {
query = v;
// Util.println("Query not null");
}
}
// Util.println(f.getVariables());
}
TableFactorNetwork tfn = new TableFactorNetwork(factors);
pairQueryNet = new Pair<>(query, tfn);
return pairQueryNet;
}
Aggregations