Search in sources :

Example 1 with VariableNode

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

the class IncrementalBeliefPropagationWithConditioningVersion2 method ExpandModel.

public PartitionTree ExpandModel(Iterator<FactorNode> it) {
    if (it.hasNext()) {
        // get factor to add
        FactorNode newfactor = it.next();
        // create partition tree of new Factor
        PartitionTree newPartition = new PartitionTree(newfactor);
        findPartitionGivenANode.put(newfactor, newPartition);
        Collection<VariableNode> exploredVariablesInModel = this.partitionTree.model.getExploredVariables();
        exploredVariablesInModel.add((VariableNode) partitionTree.node);
        // get variables in the tree linked to the factor. attach the factor to one of them
        Collection<VariableNode> variablesInTheTreeLinkedToFactor = this.partitionTree.model.getVariablesOfAFactor(newfactor);
        variablesInTheTreeLinkedToFactor.retainAll(exploredVariablesInModel);
        if (variablesInTheTreeLinkedToFactor.isEmpty()) {
            return null;
        }
        // get parent and link new factor to his parent
        VariableNode var = variablesInTheTreeLinkedToFactor.iterator().next();
        PartitionTree parentOfNewFactor = findPartitionGivenANode.get(var);
        parentOfNewFactor.children.add(newPartition);
        newPartition.parent = parentOfNewFactor;
        newPartition.model = parentOfNewFactor.model;
        // get variables to be put below new factor in the three
        // adding them to the tree
        Collection<VariableNode> variablesToBePutItTheTree = this.partitionTree.model.getVariablesOfAFactor(newfactor);
        variablesToBePutItTheTree.removeAll(exploredVariablesInModel);
        for (VariableNode v : variablesToBePutItTheTree) {
            PartitionTree p = new PartitionTree(v);
            newPartition.children.add(p);
            p.parent = newPartition;
            p.model = newPartition.model;
            findPartitionGivenANode.put(v, p);
        }
        // expand the model (add new factor to the explored part of the graph)
        this.partitionTree.model.ExpandModel(newfactor);
        return newPartition;
    }
    return null;
}
Also used : VariableNode(anytimeExactBeliefPropagation.Model.Node.VariableNode) FactorNode(anytimeExactBeliefPropagation.Model.Node.FactorNode)

Example 2 with VariableNode

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

the class IncrementalBeliefPropagationWithConditioning method ComputeSeparator.

/**
 * Given the partition, compute the separator. TODO more efficient implementation
 * @param p
 * @return
 */
private Set<VariableNode> ComputeSeparator(PartitionTree pTree) {
    // Create sets with the variables in each partition
    List<Set<VariableNode>> VariablePartition = new ArrayList<Set<VariableNode>>();
    for (PartitionTree p : pTree.children) {
        Set<VariableNode> variablesOfP = new HashSet<>();
        for (FactorNode phi : p.setOfFactorsInsidePartition) {
            Collection<VariableNode> VarsOfPhi = model.getExploredGraph().getAsOfB(phi);
            variablesOfP.addAll(VarsOfPhi);
        }
        VariablePartition.add(variablesOfP);
    }
    // take the variables that compose the intersection of those sets
    Set<VariableNode> separatorVariables = new HashSet<>();
    for (int i = 0; i < VariablePartition.size(); i++) {
        for (int j = i + 1; j < VariablePartition.size(); j++) {
            Set<VariableNode> intersectionAti = new HashSet<>();
            intersectionAti.addAll(VariablePartition.get(i));
            intersectionAti.retainAll(VariablePartition.get(j));
            separatorVariables.addAll(intersectionAti);
        }
    }
    return separatorVariables;
}
Also used : VariableNode(anytimeExactBeliefPropagation.Model.Node.VariableNode) Set(java.util.Set) HashSet(java.util.HashSet) DefaultExtensionalMultiSet(com.sri.ai.expresso.core.DefaultExtensionalMultiSet) ArrayList(java.util.ArrayList) FactorNode(anytimeExactBeliefPropagation.Model.Node.FactorNode) HashSet(java.util.HashSet)

Example 3 with VariableNode

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

the class IncrementalBeliefPropagationWithConditioning method variableMessage.

public Bound variableMessage(PartitionTree partitionInAVariableNode, Set<VariableNode> SeparatorVariablesOnLevelAbove) {
    // or notToSumVariables
    if (!partitionInAVariableNode.node.isVariable()) {
        println("error in S-BP!!!");
        return null;
    }
    PairOf<Set<VariableNode>> sep = ComputeSeparatorOnThisLevelAndSeparatorOnLevelsBelow(partitionInAVariableNode, SeparatorVariablesOnLevelAbove);
    Set<VariableNode> SeparatorOnThisLevel = sep.first;
    Set<VariableNode> SeparatorForNextLevels = sep.second;
    // calling children partitions. for each partition, call message passing,
    // store bound
    Bound[] boundsOfChildrenMessages = new Bound[partitionInAVariableNode.children.size()];
    Set<Expression> variablesToSumOut = new HashSet<>();
    for (VariableNode v : SeparatorOnThisLevel) {
        variablesToSumOut.add(v.getValue());
    }
    // obs. it can be equivalently thought as attaching a "simplex factor" to non exhausted nodes.
    if (!AllExplored && !model.isExhausted((VariableNode) partitionInAVariableNode.node)) {
        Expression var = partitionInAVariableNode.node.getValue();
        Bound bound = Bounds.simplex(arrayList(var), model.getTheory(), model.getContext(), model.isExtensional());
        // partitionInAVariableNode.node.setBound(bound);
        return bound;
    }
    int i = 0;
    for (PartitionTree p : partitionInAVariableNode.children) {
        Bound boundInP = factorMessage(p, SeparatorForNextLevels);
        // Bound boundInP = p.node.getBound();
        boundsOfChildrenMessages[i] = boundInP;
        i++;
    }
    Bound bound = Bounds.boundProduct(model.getTheory(), model.getContext(), model.isExtensional(), boundsOfChildrenMessages);
    ArrayList<Expression> varToSumOutList = new ArrayList<>();
    varToSumOutList.addAll(variablesToSumOut);
    Expression varToSumOut = new DefaultExtensionalMultiSet(varToSumOutList);
    bound = bound.sumOut(varToSumOut, model.getContext(), model.getTheory());
    return bound;
// partitionInAVariableNode.node.setBound(bound);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) DefaultExtensionalMultiSet(com.sri.ai.expresso.core.DefaultExtensionalMultiSet) Bound(com.sri.ai.grinder.library.bounds.Bound) ArrayList(java.util.ArrayList) DefaultExtensionalMultiSet(com.sri.ai.expresso.core.DefaultExtensionalMultiSet) VariableNode(anytimeExactBeliefPropagation.Model.Node.VariableNode) Expression(com.sri.ai.expresso.api.Expression) HashSet(java.util.HashSet)

Example 4 with VariableNode

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

the class IncrementalBeliefPropagationWithConditioning method factorMessage.

public Bound factorMessage(PartitionTree partitionInAFactorNode, Set<VariableNode> SeparatorVariablesOnLevelAbove) {
    if (!partitionInAFactorNode.node.isFactor()) {
        println("error in S-BP!!!");
        return null;
    }
    PairOf<Set<VariableNode>> sep = ComputeSeparatorOnThisLevelAndSeparatorOnLevelsBelow(partitionInAFactorNode, SeparatorVariablesOnLevelAbove);
    Set<VariableNode> SeparatorOnThisLevel = sep.first;
    Set<VariableNode> SeparatorForNextLevels = sep.second;
    // calling children partitions. for each partition, call message passing,
    // store VariableNode (we are going to sum them all out) and
    // store bound
    Bound[] boundsOfChildrenMessages = new Bound[partitionInAFactorNode.children.size()];
    Set<Expression> variablesToSumOut = new HashSet<>();
    for (VariableNode v : SeparatorOnThisLevel) {
        variablesToSumOut.add(v.getValue());
    }
    int i = 0;
    for (PartitionTree p : partitionInAFactorNode.children) {
        Bound boundInP = variableMessage(p, SeparatorForNextLevels);
        // Bound boundInP = p.node.getBound();
        boundsOfChildrenMessages[i] = boundInP;
        variablesToSumOut.add(p.node.getValue());
        i++;
    }
    for (VariableNode v : SeparatorVariablesOnLevelAbove) {
        variablesToSumOut.remove(v.getValue());
    }
    Bound bound = Bounds.boundProduct(model.getTheory(), model.getContext(), model.isExtensional(), boundsOfChildrenMessages);
    ArrayList<Expression> varToSumOutList = new ArrayList<>();
    varToSumOutList.addAll(variablesToSumOut);
    Expression varToSumOut = new DefaultExtensionalMultiSet(varToSumOutList);
    bound = bound.sumOutProductByFactor(varToSumOut, partitionInAFactorNode.node.getValue(), model.getContext(), model.getTheory());
    return bound;
// partitionInAFactorNode.node.setBound(bound);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) DefaultExtensionalMultiSet(com.sri.ai.expresso.core.DefaultExtensionalMultiSet) Bound(com.sri.ai.grinder.library.bounds.Bound) ArrayList(java.util.ArrayList) DefaultExtensionalMultiSet(com.sri.ai.expresso.core.DefaultExtensionalMultiSet) VariableNode(anytimeExactBeliefPropagation.Model.Node.VariableNode) Expression(com.sri.ai.expresso.api.Expression) HashSet(java.util.HashSet)

Example 5 with VariableNode

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

the class BFS method next.

@Override
public FactorNode next() {
    if (!hasNext())
        throw new NoSuchElementException();
    // removes from front of queue
    FactorNode next = queue.remove();
    for (VariableNode neighorVariable : graph.getAsOfB(next)) {
        for (FactorNode neighborFactor : graph.getBsOfA(neighorVariable)) {
            if (!this.visited.contains(neighborFactor)) {
                this.queue.add(neighborFactor);
                this.visited.add(neighborFactor);
            }
        }
    }
    return next;
}
Also used : VariableNode(anytimeExactBeliefPropagation.Model.Node.VariableNode) FactorNode(anytimeExactBeliefPropagation.Model.Node.FactorNode)

Aggregations

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