use of com.sri.ai.praise.core.representation.classbased.table.core.uai.UAIModel in project aic-praise by aic-sri-international.
the class AbstractUAI_to_Target_Translator method translate.
// END-Translator
//
@Override
protected void translate(String inputIdentifier, Reader[] inputModelReaders, PrintWriter[] translatedOutputs) throws Exception {
Reader uaiModelReader = inputModelReaders[0];
Reader uaiEvidenceReader = inputModelReaders[1];
//
// Instantiate the source UAI model
UAIModel uaiModel = UAIModelReader.read(uaiModelReader);
//
// Read the corresponding evidence and merge into the model
// This is required as the UAI solvers all take the evidence
// when they are searching for solutions, so other solvers
// need to have this information contained in their models
// as well.
UAIEvidenceReading.read(uaiEvidenceReader, uaiModel);
uaiModel.mergeEvidenceIntoModel();
translate(inputIdentifier, uaiModel, translatedOutputs);
}
use of com.sri.ai.praise.core.representation.classbased.table.core.uai.UAIModel 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.classbased.table.core.uai.UAIModel in project aic-praise by aic-sri-international.
the class UAIModelReader method read.
public static UAIModel read(Reader modelReader) throws IOException {
UAIModel result = null;
try (BufferedReader br = new BufferedReader(modelReader)) {
Preamble preamble = readPreamble(br);
Map<Integer, FunctionTable> tableIdxToTable = readFunctionTables(preamble, br);
result = new UAIModel(preamble.type, preamble.variableToCardinality, preamble.tableVariableIdxs, tableIdxToTable);
}
return result;
}
use of com.sri.ai.praise.core.representation.classbased.table.core.uai.UAIModel in project aic-praise by aic-sri-international.
the class UAIModelToExpressionFactorNetwork method main.
public static void main(String[] args) {
try {
// Importing the file and reading it
FileReader modelFile = new FileReader(new File("").getAbsolutePath() + "/UAITests/BN_0.uai");
UAIModel model = UAIModelReader.read(modelFile);
// Converting the network
ExpressionFactorNetwork network = convert(model, null);
// Printing the factors
for (IdentityWrapper<Factor> fwrapped : network.getAs()) {
ExpressionFactor f = (ExpressionFactor) fwrapped.getObject();
println(f);
}
/*// This seems to be OK! But when we analyze the connections between the factors:
IdentityWrapper<Factor> f = network.getAs().iterator().next();
println("Printing one of the factors of the network:\n\t "+f);
println("Printing this factor's connections:\n\t" + network.getBsOfA(f));
println("This shows that there is something wrong\n"
+ "In fact the connections in the graph are made based on the 'freeVariables' of a factor");
println("freeVariables of f: "+Expressions.freeVariables((ExpressionFactor)f.getObject(),((ExpressionFactor)f.getObject()).getContext()));
println("\nWe can check that those 'abnomalies' are indeed variables on the network:");
for(Variable v:network.getBs()) {
System.out.print(v + ", ");
}*/
} catch (IOException e) {
e.printStackTrace();
}
}
use of com.sri.ai.praise.core.representation.classbased.table.core.uai.UAIModel in project aic-praise by aic-sri-international.
the class UAIMARSolver method read.
private static UAIModel read(File uaiFile, File solutionDir) throws IOException {
UAIModel model = UAIModelReader.read(uaiFile);
UAIEvidenceReading.read(uaiFile, model);
// Result is specified in a separate file. This file has the same name as the original network
// file but with an added .MAR suffix. For instance, problem.uai will have a MAR result file problem.uai.MAR.
File marResultFile = new File(solutionDir, uaiFile.getName() + ".MAR");
Map<Integer, List<Double>> marResult = UAIResultReading.readMAR(marResultFile);
if (marResult.size() != model.numberVariables()) {
throw new IllegalArgumentException("Number of variables in result file, " + marResult.size() + ", does not match # in model, which is " + model.numberVariables());
}
for (Map.Entry<Integer, List<Double>> entry : marResult.entrySet()) {
model.addMARSolution(entry.getKey(), entry.getValue());
}
return model;
}
Aggregations