Search in sources :

Example 6 with FactorNode

use of anytimeExactBeliefPropagation.Model.Node.FactorNode in project aic-expresso by aic-sri-international.

the class Tests method testing3.

public static List<TupleOfData> testing3(String modelName, Model m, Integer... parameter) {
    List<TupleOfData> result = new ArrayList<TupleOfData>();
    int id = 0;
    m.clearExploredGraph();
    Iterator<FactorNode> bfsExpander = new BFS(m);
    IncrementalBeliefPropagationWithConditioning sbp = new IncrementalBeliefPropagationWithConditioning(m);
    while (bfsExpander.hasNext()) {
        TupleOfData t = new TupleOfData();
        long tStart = System.currentTimeMillis();
        Bound inferenceResult = sbp.expandAndComputeInference(bfsExpander);
        long tEnd = System.currentTimeMillis();
        long tDelta = tEnd - tStart;
        t.time = tDelta / 1000.0;
        t.typeOfComputationUsed = "S-BP";
        t.graphicalModelName = modelName;
        t.id = id++;
        t.numberOfExtremePoints = inferenceResult.getArguments().size();
        Pair<Double, Double> minAndMaxProbabilityofQueryequalsTrue = ModelGenerator.maxMinProbability(inferenceResult, m);
        t.minAndMaxProbabilityofQueryequalsTrue = minAndMaxProbabilityofQueryequalsTrue.first;
        t.maxAndMaxProbabilityofQueryequalsTrue = minAndMaxProbabilityofQueryequalsTrue.second;
        t.IntervalLength = t.maxAndMaxProbabilityofQueryequalsTrue - t.minAndMaxProbabilityofQueryequalsTrue;
        t.allExplored = m.AllExplored();
        for (int i = 0; i < parameter.length && i < t.parameter.length; i++) {
            t.parameter[i] = parameter[i];
        }
        result.add(t);
        println("....");
    }
    TupleOfData t = new TupleOfData();
    long tStart = System.currentTimeMillis();
    Expression inferenceLVE = ModelGenerator.lveCalculation(m);
    Bound EncapsulatingInference = Bounds.makeSingleElementBound(inferenceLVE, true);
    Pair<Double, Double> minAndMaxProbabilityofQueryequalsTrue = ModelGenerator.maxMinProbability(EncapsulatingInference, m);
    long tEnd = System.currentTimeMillis();
    long tDelta = tEnd - tStart;
    t.time = tDelta / 1000.0;
    t.minAndMaxProbabilityofQueryequalsTrue = minAndMaxProbabilityofQueryequalsTrue.first;
    t.maxAndMaxProbabilityofQueryequalsTrue = minAndMaxProbabilityofQueryequalsTrue.second;
    t.typeOfComputationUsed = "SGDPLL";
    t.graphicalModelName = modelName;
    t.id = id++;
    t.numberOfExtremePoints = 0;
    t.IntervalLength = 0;
    t.allExplored = true;
    for (int i = 0; i < parameter.length && i < t.parameter.length; i++) {
        t.parameter[i] = parameter[i];
    }
    result.add(t);
    println("------------------------------------------------------------");
    return result;
}
Also used : ArrayList(java.util.ArrayList) Bound(com.sri.ai.grinder.library.bounds.Bound) FactorNode(anytimeExactBeliefPropagation.Model.Node.FactorNode) BFS(anytimeExactBeliefPropagation.Model.BFS) Expression(com.sri.ai.expresso.api.Expression)

Example 7 with FactorNode

use of anytimeExactBeliefPropagation.Model.Node.FactorNode in project aic-expresso by aic-sri-international.

the class Tests method testing.

/**
 * 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 m
 * @param parameter
 * @return
 */
public static List<TupleOfData> testing(String modelName, Model m, Integer... parameter) {
    List<TupleOfData> result = new ArrayList<TupleOfData>();
    int id = 0;
    m.clearExploredGraph();
    Iterator<FactorNode> bfsExpander = new BFS(m);
    IncrementalBeliefPropagationWithConditioning sbp = new IncrementalBeliefPropagationWithConditioning(m);
    double tTotalTime = 0;
    while (bfsExpander.hasNext()) {
        TupleOfData t = new TupleOfData();
        long tStart = System.currentTimeMillis();
        Bound inferenceResult = sbp.expandAndComputeInference(bfsExpander);
        long tEnd = System.currentTimeMillis();
        long tDelta = tEnd - tStart;
        t.time = tDelta / 1000.0;
        tTotalTime += tDelta / 1000.0;
        t.totalTime += tTotalTime;
        t.typeOfComputationUsed = "S-BP";
        t.graphicalModelName = modelName;
        t.id = id++;
        t.numberOfExtremePoints = inferenceResult.getArguments().size();
        Pair<Double, Double> minAndMaxProbabilityofQueryequalsTrue = ModelGenerator.maxMinProbability(inferenceResult, m);
        t.minAndMaxProbabilityofQueryequalsTrue = minAndMaxProbabilityofQueryequalsTrue.first;
        t.maxAndMaxProbabilityofQueryequalsTrue = minAndMaxProbabilityofQueryequalsTrue.second;
        t.IntervalLength = t.maxAndMaxProbabilityofQueryequalsTrue - t.minAndMaxProbabilityofQueryequalsTrue;
        t.allExplored = m.AllExplored();
        for (int i = 0; i < parameter.length && i < t.parameter.length; i++) {
            t.parameter[i] = parameter[i];
        }
        result.add(t);
        println("....");
    }
    TupleOfData t = new TupleOfData();
    long tStart = System.currentTimeMillis();
    Expression inferenceLVE = ModelGenerator.lveCalculation(m);
    Bound encapsulatingInference = Bounds.makeSingleElementBound(inferenceLVE, true);
    Pair<Double, Double> minAndMaxProbabilityofQueryequalsTrue = ModelGenerator.maxMinProbability(encapsulatingInference, m);
    long tEnd = System.currentTimeMillis();
    long tDelta = tEnd - tStart;
    t.time = tDelta / 1000.0;
    t.totalTime = t.time;
    t.minAndMaxProbabilityofQueryequalsTrue = minAndMaxProbabilityofQueryequalsTrue.first;
    t.maxAndMaxProbabilityofQueryequalsTrue = minAndMaxProbabilityofQueryequalsTrue.second;
    t.typeOfComputationUsed = "SGDPLL";
    t.graphicalModelName = modelName;
    t.id = id++;
    t.numberOfExtremePoints = 0;
    t.IntervalLength = 0;
    t.allExplored = true;
    for (int i = 0; i < parameter.length && i < t.parameter.length; i++) {
        t.parameter[i] = parameter[i];
    }
    result.add(t);
    println("------------------------- Done -----------------------------------");
    return result;
}
Also used : ArrayList(java.util.ArrayList) Bound(com.sri.ai.grinder.library.bounds.Bound) FactorNode(anytimeExactBeliefPropagation.Model.Node.FactorNode) BFS(anytimeExactBeliefPropagation.Model.BFS) Expression(com.sri.ai.expresso.api.Expression)

Example 8 with FactorNode

use of anytimeExactBeliefPropagation.Model.Node.FactorNode in project aic-expresso by aic-sri-international.

the class Model method ExpandModel.

/**
 * This method receives as input an {@code Iterator<FactorNode>} object and expands the
 * {@code exploredGraphicalModel} by adding ONE FACTOR to it.
 *
 * There are various ways of doing such expansion, and it is then given to the User the
 * choice on which way to go.
 *
 * @param it
 */
public void ExpandModel(Iterator<FactorNode> it) {
    // BFS, DFS,...
    if (it.hasNext()) {
        FactorNode newFactorNode = it.next();
        for (Expression variable : Expressions.freeVariables(newFactorNode.getValue(), context)) {
            VariableNode v = new VariableNode(variable, isExtensional, theory, context);
            exploredGraphicalModel.add(v, newFactorNode);
        }
    }
}
Also used : VariableNode(anytimeExactBeliefPropagation.Model.Node.VariableNode) Expression(com.sri.ai.expresso.api.Expression) FactorNode(anytimeExactBeliefPropagation.Model.Node.FactorNode)

Example 9 with FactorNode

use of anytimeExactBeliefPropagation.Model.Node.FactorNode in project aic-expresso by aic-sri-international.

the class PartitionTree method CreatePartitionTreeWithBFS.

/*------------------------------------------------------------------------------------------------------------------------*/
/**
 * This class creates a partition based on a BFS process.
 *
 * There are various ways to create the partition tree.
 *
 * In fact any partition tree will provide an exact inference. But some of them will
 * generate so many separator variables (or cutset variables) that the inference time
 * can be close to the Naive computation.
 *
 * The study of the complexity of choosing the optimal tree (or a good heuristic)
 * still needs to be done in the project. (TODO)
 *
 * The choice of a BFS seems intuitively good because it distributes the nodes more or less
 * evenly among the partitions
 *
 * @param model
 */
public void CreatePartitionTreeWithBFS(Model model) {
    HashMap<Node, PartitionTree> hashTable = new HashMap<>();
    Set<Node> visited = new HashSet<>();
    Queue<Node> queue = new LinkedList<>();
    visited.add(node);
    queue.add(node);
    hashTable.put(this.node, this);
    while (!queue.isEmpty()) {
        Node n = queue.remove();
        PartitionTree parentPartition = hashTable.get(n);
        Set<Node> neighbors = new HashSet<>();
        if (n.isFactor()) {
            Collection<VariableNode> variableNeighbors = model.getExploredGraph().getAsOfB((FactorNode) n);
            neighbors.addAll(variableNeighbors);
        } else {
            Collection<FactorNode> factorNeighbors = model.getExploredGraph().getBsOfA((VariableNode) n);
            neighbors.addAll(factorNeighbors);
        }
        for (Node neighbor : neighbors) {
            if (!visited.contains(neighbor)) {
                queue.add(neighbor);
                visited.add(neighbor);
                PartitionTree pChild = new PartitionTree(neighbor);
                pChild.parent = parentPartition;
                hashTable.put(neighbor, pChild);
                parentPartition.children.add(pChild);
            }
        }
    }
}
Also used : VariableNode(anytimeExactBeliefPropagation.Model.Node.VariableNode) HashMap(java.util.HashMap) VariableNode(anytimeExactBeliefPropagation.Model.Node.VariableNode) Node(anytimeExactBeliefPropagation.Model.Node.Node) FactorNode(anytimeExactBeliefPropagation.Model.Node.FactorNode) LinkedList(java.util.LinkedList) FactorNode(anytimeExactBeliefPropagation.Model.Node.FactorNode) HashSet(java.util.HashSet)

Example 10 with FactorNode

use of anytimeExactBeliefPropagation.Model.Node.FactorNode in project aic-expresso by aic-sri-international.

the class PartitionTree method updateCutSet2.

/*------------------------------------------------------------------------------------------------------------------------*/
// Alternative iplementation of updating cutset:
@SuppressWarnings("unused")
private void updateCutSet2(Model m) {
    FactorNode factor = (FactorNode) this.node;
    // we take all variables of this factor, and remove those that haven't appeared in other parts of the graph
    Collection<VariableNode> newSeparatorVariables = m.getVariablesOfAFactor(factor);
    newSeparatorVariables.removeAll(this.children);
    newSeparatorVariables.remove(this.parent);
    // Update cutset of "Virgin Variables"
    // TODO
    // Update this cutset, and all above together
    addingToCutSet(newSeparatorVariables, null);
}
Also used : VariableNode(anytimeExactBeliefPropagation.Model.Node.VariableNode) FactorNode(anytimeExactBeliefPropagation.Model.Node.FactorNode)

Aggregations

FactorNode (anytimeExactBeliefPropagation.Model.Node.FactorNode)11 VariableNode (anytimeExactBeliefPropagation.Model.Node.VariableNode)7 Expression (com.sri.ai.expresso.api.Expression)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 BFS (anytimeExactBeliefPropagation.Model.BFS)3 Bound (com.sri.ai.grinder.library.bounds.Bound)3 DefaultExtensionalMultiSet (com.sri.ai.expresso.core.DefaultExtensionalMultiSet)2 Node (anytimeExactBeliefPropagation.Model.Node.Node)1 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Set (java.util.Set)1