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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations