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