Search in sources :

Example 1 with FactorNode

use of IncrementalAnytimeExactBeliefPropagation.Model.Node.FactorNode 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 2 with FactorNode

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

the class IncrementalAnytimeBeliefPropagationWithSeparatorConditioning method expandAndComputeInference.

public Bound expandAndComputeInference() {
    if (partitionTreeIterator.hasNext()) {
        PartitionTree nextFactorPartitionTree = partitionTreeIterator.next();
        FactorNode nextFactor = (FactorNode) nextFactorPartitionTree.node;
        model.ExpandModel(nextFactor);
        updatePartitionTree(nextFactorPartitionTree);
        Bound result = partitionTree.node.getBound();
        result = result.normalize(model.getTheory(), model.getContext());
        return result;
    }
    return null;
}
Also used : Bound(com.sri.ai.grinder.library.bounds.Bound) FactorNode(IncrementalAnytimeExactBeliefPropagation.Model.Node.FactorNode)

Example 3 with FactorNode

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

the class IncrementalAnytimeBeliefPropagationWithSeparatorConditioning method expandAndComputeInferenceByRebuildingPartitionTree.

public Bound expandAndComputeInferenceByRebuildingPartitionTree() {
    if (partitionTreeIterator.hasNext()) {
        PartitionTree nextFactorPartitionTree = partitionTreeIterator.next();
        FactorNode nextFactor = (FactorNode) nextFactorPartitionTree.node;
        model.ExpandModel(nextFactor);
        Bound result = inference();
        result = result.normalize(model.getTheory(), model.getContext());
        return result;
    }
    return null;
}
Also used : Bound(com.sri.ai.grinder.library.bounds.Bound) FactorNode(IncrementalAnytimeExactBeliefPropagation.Model.Node.FactorNode)

Example 4 with FactorNode

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

the class BFS method next.

@Override
public PartitionTree next() {
    if (first) {
        first = false;
        return this.partitionQuery;
    }
    if (!hasNext())
        throw new NoSuchElementException();
    // removes from front of queue
    FactorNode nextFactor = queue.remove();
    PartitionTree nextFactorPartition = fromNodeToPartition.get(nextFactor);
    for (VariableNode neighborVariable : graph.getAsOfB(nextFactor)) {
        PartitionTree neighborVariablePartition = fromNodeToPartition.get(neighborVariable);
        if (neighborVariablePartition == null) {
            neighborVariablePartition = new PartitionTree(neighborVariable, nextFactorPartition);
            fromNodeToPartition.put(neighborVariable, neighborVariablePartition);
        }
        for (FactorNode neighborFactor : graph.getBsOfA(neighborVariable)) {
            if (!visited.contains(neighborFactor)) {
                PartitionTree neighborFactorPartition = new PartitionTree(neighborFactor, neighborVariablePartition);
                fromNodeToPartition.put(neighborFactor, neighborFactorPartition);
                queue.add(neighborFactor);
                visited.add(neighborFactor);
            }
        }
    }
    return nextFactorPartition;
}
Also used : PartitionTree(IncrementalAnytimeExactBeliefPropagation.PartitionTree) VariableNode(IncrementalAnytimeExactBeliefPropagation.Model.Node.VariableNode) NoSuchElementException(java.util.NoSuchElementException) FactorNode(IncrementalAnytimeExactBeliefPropagation.Model.Node.FactorNode)

Example 5 with FactorNode

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

the class ModelGenerator method LVECalculation.

public static Expression LVECalculation(Collection<FactorNode> factorNodes, Expression query, Context context, Theory theory) {
    Set<Expression> factorExpressions = new HashSet<>();
    for (FactorNode f : factorNodes) {
        factorExpressions.add(f.getValue());
    }
    Expression product = apply(TIMES, factorExpressions);
    Set<Expression> freevariables = Expressions.freeVariables(product, context);
    freevariables.remove(query);
    ArrayList<Expression> varToSumOut = new ArrayList<>();
    varToSumOut.addAll(freevariables);
    Expression variablesToBeSummedOut = new DefaultExtensionalMultiSet(varToSumOut);
    IndexExpressionsSet indices = getIndexExpressionsOfFreeVariablesIn(variablesToBeSummedOut, context);
    Expression setOfFactorInstantiations = IntensionalSet.makeMultiSet(indices, // head
    product, // No Condition
    makeSymbol(true));
    Expression sumOnPhi = apply(SUM, setOfFactorInstantiations);
    println("Evaluating " + sumOnPhi);
    Expression evaluation = theory.evaluate(sumOnPhi, context);
    println("Finished evaluating " + sumOnPhi);
    Expression result = Bounds.normalizeSingleExpression(evaluation, theory, context);
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) DefaultExtensionalMultiSet(com.sri.ai.expresso.core.DefaultExtensionalMultiSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) FactorNode(IncrementalAnytimeExactBeliefPropagation.Model.Node.FactorNode) HashSet(java.util.HashSet)

Aggregations

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