Search in sources :

Example 6 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 makeFactorWithAPermutationOfTheEntriesOnPhiMaxAndPhiMin.

private static TableFactor makeFactorWithAPermutationOfTheEntriesOnPhiMaxAndPhiMin(TableFactor phiMin, TableFactor phiMax, ArrayList<Integer> binaryNumber) {
    ArrayList<Double> phiMinEntries = phiMin.getEntries();
    ArrayList<Double> phiMaxEntries = phiMax.getEntries();
    ArrayList<Double> newEntries = new ArrayList<>(phiMin.getEntries().size());
    for (int i = 0; i < binaryNumber.size(); i++) {
        newEntries.add(binaryNumber.get(i) == 0 ? phiMinEntries.get(i) : phiMaxEntries.get(i));
    }
    ArrayList<TableVariable> variables = (ArrayList<TableVariable>) phiMax.getVariables();
    TableFactor result = new TableFactor(variables, newEntries);
    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)

Example 7 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 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 8 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 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 9 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 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 10 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 AnytimeExactBPTest2 method importUAIFile.

public static Pair<TableVariable, TableFactorNetwork> importUAIFile(String fileName) {
    Pair<TableVariable, TableFactorNetwork> pairQueryNet = new Pair<>();
    try {
        FileReader modelFile = new FileReader(new File("").getAbsolutePath() + "/UAITests/" + fileName);
        UAIModel model = UAIModelReader.read(modelFile);
        // Converting the network
        pairQueryNet.second = new TableFactorNetwork(model);
        // get one variable and test over the network
        ArrayList<Variable> vars = new ArrayList<>(pairQueryNet.second.getBs());
        // pick any variable
        pairQueryNet.first = (TableVariable) vars.get(0);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return pairQueryNet;
}
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) UAIModel(com.sri.ai.praise.core.representation.classbased.table.core.uai.UAIModel) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) TableFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactorNetwork) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) Pair(com.sri.ai.util.base.Pair)

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