Search in sources :

Example 1 with UAIModel

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);
}
Also used : UAIModel(com.sri.ai.praise.core.representation.classbased.table.core.uai.UAIModel) UAIModelReader(com.sri.ai.praise.core.representation.classbased.table.core.uai.parsing.UAIModelReader) Reader(java.io.Reader)

Example 2 with UAIModel

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;
}
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 3 with UAIModel

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;
}
Also used : FunctionTable(com.sri.ai.praise.core.representation.classbased.table.core.data.FunctionTable) UAIModel(com.sri.ai.praise.core.representation.classbased.table.core.uai.UAIModel) BufferedReader(java.io.BufferedReader)

Example 4 with UAIModel

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();
    }
}
Also used : ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) UAIModel(com.sri.ai.praise.core.representation.classbased.table.core.uai.UAIModel) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File)

Example 5 with UAIModel

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;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UAIModel(com.sri.ai.praise.core.representation.classbased.table.core.uai.UAIModel) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

UAIModel (com.sri.ai.praise.core.representation.classbased.table.core.uai.UAIModel)7 File (java.io.File)5 IOException (java.io.IOException)4 FileReader (java.io.FileReader)3 ArrayList (java.util.ArrayList)3 FunctionTable (com.sri.ai.praise.core.representation.classbased.table.core.data.FunctionTable)2 UAIModelReader (com.sri.ai.praise.core.representation.classbased.table.core.uai.parsing.UAIModelReader)2 FileNotFoundException (java.io.FileNotFoundException)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Beta (com.google.common.annotations.Beta)1 AtomicDouble (com.google.common.util.concurrent.AtomicDouble)1 Expression (com.sri.ai.expresso.api.Expression)1 Expressions (com.sri.ai.expresso.helper.Expressions)1 MultiQuantifierEliminator (com.sri.ai.grinder.api.MultiQuantifierEliminator)1 Theory (com.sri.ai.grinder.api.Theory)1 Equality (com.sri.ai.grinder.library.Equality)1