Search in sources :

Example 6 with Variable

use of com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable 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 7 with Variable

use of com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable 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)

Example 8 with Variable

use of com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable in project aic-praise by aic-sri-international.

the class AnytimeExactBPTest2 method getListOfListOfInstantiations.

public static List<List<Integer>> getListOfListOfInstantiations(List<Variable> ListOfVariables) {
    List<List<Integer>> listOflistOfInstantiations = new ArrayList<>();
    for (Variable v : ListOfVariables) {
        TableVariable tv = (TableVariable) v;
        ArrayList<Integer> toAdd = new ArrayList<>(tv.getCardinality());
        for (int i = 0; i < tv.getCardinality(); i++) {
            toAdd.add(i);
        }
        listOflistOfInstantiations.add(toAdd);
    }
    return listOflistOfInstantiations;
}
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) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList) 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 9 with Variable

use of com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable in project aic-praise by aic-sri-international.

the class AnytimeExactBPTest method solveWithExactBP.

private ExpressionFactor solveWithExactBP(Expression query, ExpressionFactorNetwork factorNetwork) {
    ExactBPNode<Variable, Factor> exactBP = new ExpressionExactBP(query, factorNetwork);
    ExpressionFactor result = (ExpressionFactor) exactBP.apply();
    return result;
}
Also used : ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) Variable(com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) ExpressionExactBP(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionExactBP)

Example 10 with Variable

use of com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable in project aic-praise by aic-sri-international.

the class ExpressionBasedProblemToInterfaceBasedProblemConversion method translate.

public static Problem translate(ExpressionBasedProblem expressionBasedProblem) {
    ExpressionVariable queryVariable = new DefaultExpressionVariable(expressionBasedProblem.getQuerySymbol());
    ExpressionFactorNetwork factorNetwork = makeFactorNetwork(expressionBasedProblem);
    Predicate<Expression> isExpressionParameterPredicate = expressionBasedProblem.getIsParameterPredicate();
    Predicate<Variable> isParameterPredicate = makeIsParameterPredicate(isExpressionParameterPredicate);
    Problem problem = new DefaultVariableMarginalQuery(queryVariable, factorNetwork, isParameterPredicate);
    return problem;
}
Also used : ExpressionFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable) Variable(com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) Expression(com.sri.ai.expresso.api.Expression) DefaultVariableMarginalQuery(com.sri.ai.praise.core.representation.interfacebased.factor.core.DefaultVariableMarginalQuery) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable) ExpressionBasedProblem(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedProblem) Problem(com.sri.ai.praise.core.representation.interfacebased.factor.api.Problem)

Aggregations

Variable (com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable)23 Factor (com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor)11 ArrayList (java.util.ArrayList)10 TableFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor)9 TableVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)9 ExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor)4 AtomicPolytope (com.sri.ai.praise.core.representation.interfacebased.polytope.api.AtomicPolytope)4 Polytope (com.sri.ai.praise.core.representation.interfacebased.polytope.api.Polytope)4 Util.mapIntoArrayList (com.sri.ai.util.Util.mapIntoArrayList)4 List (java.util.List)4 Expression (com.sri.ai.expresso.api.Expression)3 TableFactorNetwork (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactorNetwork)3 TableBoxVariable (com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.box.TableBoxVariable)3 IntensionalConvexHullOfFactors (com.sri.ai.praise.core.representation.interfacebased.polytope.core.byexpressiveness.convexhull.IntensionalConvexHullOfFactors)3 Predicate (com.google.common.base.Predicate)2 AnytimeExactBP (com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.anytime.rodrigo.AnytimeExactBP)2 ExactBP (com.sri.ai.praise.core.inference.byinputrepresentation.interfacebased.core.exactbp.fulltime.core.ExactBP)2 ExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable)2 ExpressionExactBP (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionExactBP)2 RandomTableFactorMaker.makeRandomTableFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.helper.RandomTableFactorMaker.makeRandomTableFactor)2