Search in sources :

Example 1 with BFS

use of IncrementalAnytimeExactBeliefPropagation.Model.BFS in project aic-expresso by aic-sri-international.

the class Tests method solveModelAndRecordResult.

/**
 * This tests a model and, instead of printing information,
 * stores its in a list of data structures.
 * Each element of the list corresponds to a iteration of the algorithm.
 * @param modelName
 * @param model
 * @param parameter
 * @return
 */
public static List<InferenceResult> solveModelAndRecordResult(String modelName, Model model, boolean incrementalversion, boolean runSGDPLL, double threshold, Integer... parameter) {
    List<InferenceResult> result = new ArrayList<InferenceResult>();
    model.clearExploredGraph();
    Iterator<PartitionTree> bfsExpander = new BFS(model);
    final IncrementalAnytimeBeliefPropagationWithSeparatorConditioning sbp = new IncrementalAnytimeBeliefPropagationWithSeparatorConditioning(model, bfsExpander);
    double totalTime = 0;
    double error = 1;
    if (threshold >= 1) {
        threshold = 0;
    }
    int id = 0;
    while (bfsExpander.hasNext() && error > threshold) {
        InferenceResult t = doInferenceAndStoreInformation(() -> incrementalversion ? sbp.expandAndComputeInference() : sbp.expandAndComputeInferenceByRebuildingPartitionTree(), sbp.getModel(), modelName, id++, totalTime, "S-BP", parameter);
        totalTime = t.totalTime;
        result.add(t);
        error = t.IntervalLength;
        println("...." + error);
    }
    if (runSGDPLL && !bfsExpander.hasNext()) {
        InferenceResult t = doInferenceAndStoreInformation(() -> makeSingleElementBound(LVECalculation(model), true), model, modelName, id++, totalTime, "SGDPLL", parameter);
        totalTime = t.totalTime;
        result.add(t);
        println("SGDPLL computed");
        // computing S-BP in the whole model (it is weirdly being faster than SGDPLL in almost any case!)
        model.clearExploredGraph();
        bfsExpander = new BFS(model);
        t = doInferenceAndStoreInformation(() -> sbp.inferenceOverEntireModel(), sbp.getModel(), modelName, id++, 0, "S-BP over Entire Model", parameter);
        result.add(t);
        println("S-BP over Entire Model computed");
    }
    println("------------------------- Done -----------------------------------");
    return result;
}
Also used : ArrayList(java.util.ArrayList) BFS(IncrementalAnytimeExactBeliefPropagation.Model.BFS)

Example 2 with BFS

use of IncrementalAnytimeExactBeliefPropagation.Model.BFS in project aic-expresso by aic-sri-international.

the class Tests method testingBFS.

private static void testingBFS() {
    Model m = new Model(isingModel(4, 3, context, parse("Boolean")), theory, true);
    Iterator<PartitionTree> bfsExpander = new BFS(m);
    while (bfsExpander.hasNext()) {
        PartitionTree p = bfsExpander.next();
        println("node : " + p.node);
        if (p.parent != null) {
            println("parent: " + p.parent.node);
        }
        println("children: ");
        for (PartitionTree pv : p.children) {
            println(pv.node.getValue());
        }
    }
}
Also used : ModelGenerator.nTreeModel(IncrementalAnytimeExactBeliefPropagation.ModelGenerator.nTreeModel) ModelGenerator.isingModel(IncrementalAnytimeExactBeliefPropagation.ModelGenerator.isingModel) Model(IncrementalAnytimeExactBeliefPropagation.Model.Model) ModelGenerator.lineModel(IncrementalAnytimeExactBeliefPropagation.ModelGenerator.lineModel) BFS(IncrementalAnytimeExactBeliefPropagation.Model.BFS)

Example 3 with BFS

use of IncrementalAnytimeExactBeliefPropagation.Model.BFS in project aic-expresso by aic-sri-international.

the class Tests method solvesModelBySBPAndBruteForce.

/**
 * Tests a given model and prints the results on the screen
 * @param modelName
 * @param model
 * @param printDetails - indicates whether to print all iterations or just the final result.
 */
private static void solvesModelBySBPAndBruteForce(String modelName, Model model, boolean printDetails) {
    Iterator<PartitionTree> bfsExpander = new BFS(model);
    IncrementalAnytimeBeliefPropagationWithSeparatorConditioning sbp = new IncrementalAnytimeBeliefPropagationWithSeparatorConditioning(model, bfsExpander);
    println("Exploring " + modelName);
    Bound inferenceResult = null;
    double totalTime = 0;
    while (!sbp.isAllExplored()) {
        long tStart = System.currentTimeMillis();
        inferenceResult = sbp.expandAndComputeInference();
        long tEnd = System.currentTimeMillis();
        long tDelta = tEnd - tStart;
        double time = tDelta / 1000.0;
        totalTime += time;
        // ModelGenerator.printModel(m, false);
        if (printDetails) {
            println("Number of extreme points: " + inferenceResult.getArguments().size());
            println("Extreme points: " + inferenceResult.getArguments());
            Pair<Double, Double> minAndMaxProbabilityofQueryequalsTrue = ModelGenerator.MaxMinProbability(inferenceResult, model);
            println("Minimal probability of query = true: " + minAndMaxProbabilityofQueryequalsTrue.first + "\nMaximal probability of query = true:" + minAndMaxProbabilityofQueryequalsTrue.second + "\nLength of interval (that is, (max - min)): " + (minAndMaxProbabilityofQueryequalsTrue.second - minAndMaxProbabilityofQueryequalsTrue.first) + "\nTime to compute this iteration: " + time + ". Total time: " + totalTime);
            println("----------------- Exploration is completed: " + sbp.isAllExplored() + "-----------------");
            println();
        }
    }
    if (!printDetails) {
        println(inferenceResult);
    }
    println("Computation by brute force with expresso: ");
    long start = System.currentTimeMillis();
    Expression bruteForceResult = ModelGenerator.LVECalculation(model);
    long end = System.currentTimeMillis();
    long timeInMilliseconds = end - start;
    double time = timeInMilliseconds / 1000.0;
    println(bruteForceResult + "\n" + "\nTime to compute:" + time);
}
Also used : Expression(com.sri.ai.expresso.api.Expression) Bound(com.sri.ai.grinder.library.bounds.Bound) Bounds.makeSingleElementBound(com.sri.ai.grinder.library.bounds.Bounds.makeSingleElementBound) BFS(IncrementalAnytimeExactBeliefPropagation.Model.BFS)

Aggregations

BFS (IncrementalAnytimeExactBeliefPropagation.Model.BFS)3 Model (IncrementalAnytimeExactBeliefPropagation.Model.Model)1 ModelGenerator.isingModel (IncrementalAnytimeExactBeliefPropagation.ModelGenerator.isingModel)1 ModelGenerator.lineModel (IncrementalAnytimeExactBeliefPropagation.ModelGenerator.lineModel)1 ModelGenerator.nTreeModel (IncrementalAnytimeExactBeliefPropagation.ModelGenerator.nTreeModel)1 Expression (com.sri.ai.expresso.api.Expression)1 Bound (com.sri.ai.grinder.library.bounds.Bound)1 Bounds.makeSingleElementBound (com.sri.ai.grinder.library.bounds.Bounds.makeSingleElementBound)1 ArrayList (java.util.ArrayList)1