Search in sources :

Example 11 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 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;
}
Also used : ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) Expression(com.sri.ai.expresso.api.Expression) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor)

Example 12 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 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");
    }
}
Also used : TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) File(java.io.File) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)

Example 13 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 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);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)

Example 14 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 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;
}
Also used : 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) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)

Example 15 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 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;
}
Also used : TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) TableFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactorNetwork) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)

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