Search in sources :

Example 1 with VariableNode

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

the class IncrementalAnytimeBeliefPropagationWithSeparatorConditioning method factorMessage.

private 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.summingPhiTimesBound(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(IncrementalAnytimeExactBeliefPropagation.Model.Node.VariableNode) Expression(com.sri.ai.expresso.api.Expression) HashSet(java.util.HashSet)

Example 2 with VariableNode

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

the class IncrementalAnytimeBeliefPropagationWithSeparatorConditioning method inference.

public Bound inference() {
    VariableNode query = model.getQuery();
    this.partitionTree = new PartitionTree(query, model);
    allNodesAreExplored = model.allNodesAreExplored();
    Bound result = variableMessage(partitionTree, new HashSet<VariableNode>());
    return result;
}
Also used : VariableNode(IncrementalAnytimeExactBeliefPropagation.Model.Node.VariableNode) Bound(com.sri.ai.grinder.library.bounds.Bound)

Example 3 with VariableNode

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

the class IncrementalAnytimeBeliefPropagationWithSeparatorConditioning 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.setOfFactors) {
            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(IncrementalAnytimeExactBeliefPropagation.Model.Node.VariableNode) Set(java.util.Set) HashSet(java.util.HashSet) DefaultExtensionalMultiSet(com.sri.ai.expresso.core.DefaultExtensionalMultiSet) ArrayList(java.util.ArrayList) FactorNode(IncrementalAnytimeExactBeliefPropagation.Model.Node.FactorNode) HashSet(java.util.HashSet)

Example 4 with VariableNode

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

the class IncrementalAnytimeBeliefPropagationWithSeparatorConditioning method variableMessage.

private Bound variableMessage(PartitionTree currentNode) {
    Set<Expression> variablesToSumOut = new HashSet<>();
    for (VariableNode v : currentNode.separator) {
        variablesToSumOut.add(v.getValue());
    }
    Bound[] boundsOfChildrenMessages = new Bound[currentNode.children.size()];
    int i = 0;
    for (PartitionTree p : currentNode.children) {
        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.summingBound(varToSumOut, model.getContext(), model.getTheory());
    return bound;
}
Also used : VariableNode(IncrementalAnytimeExactBeliefPropagation.Model.Node.VariableNode) Expression(com.sri.ai.expresso.api.Expression) Bound(com.sri.ai.grinder.library.bounds.Bound) ArrayList(java.util.ArrayList) DefaultExtensionalMultiSet(com.sri.ai.expresso.core.DefaultExtensionalMultiSet) HashSet(java.util.HashSet)

Example 5 with VariableNode

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

the class IncrementalAnytimeBeliefPropagationWithSeparatorConditioning method factorMessage.

private Bound factorMessage(PartitionTree currentNode) {
    Set<Expression> variablesToSumOut = new HashSet<>();
    for (VariableNode v : currentNode.separator) {
        variablesToSumOut.add(v.getValue());
    }
    Bound[] boundsOfChildrenMessages = new Bound[currentNode.children.size()];
    int i = 0;
    for (PartitionTree p : currentNode.children) {
        Bound boundInP = p.node.getBound();
        boundsOfChildrenMessages[i] = boundInP;
        variablesToSumOut.add(p.node.getValue());
        i++;
    }
    for (VariableNode v : currentNode.cutsetOfAllLevelsAbove) {
        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.summingPhiTimesBound(varToSumOut, currentNode.node.getValue(), model.getContext(), model.getTheory());
    return bound;
}
Also used : VariableNode(IncrementalAnytimeExactBeliefPropagation.Model.Node.VariableNode) Expression(com.sri.ai.expresso.api.Expression) Bound(com.sri.ai.grinder.library.bounds.Bound) ArrayList(java.util.ArrayList) DefaultExtensionalMultiSet(com.sri.ai.expresso.core.DefaultExtensionalMultiSet) HashSet(java.util.HashSet)

Aggregations

VariableNode (IncrementalAnytimeExactBeliefPropagation.Model.Node.VariableNode)11 Expression (com.sri.ai.expresso.api.Expression)6 HashSet (java.util.HashSet)6 FactorNode (IncrementalAnytimeExactBeliefPropagation.Model.Node.FactorNode)5 DefaultExtensionalMultiSet (com.sri.ai.expresso.core.DefaultExtensionalMultiSet)5 Bound (com.sri.ai.grinder.library.bounds.Bound)5 ArrayList (java.util.ArrayList)5 Set (java.util.Set)3 Node (IncrementalAnytimeExactBeliefPropagation.Model.Node.Node)1 PartitionTree (IncrementalAnytimeExactBeliefPropagation.PartitionTree)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 NoSuchElementException (java.util.NoSuchElementException)1