use of com.sri.ai.expresso.core.DefaultExtensionalMultiSet 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;
}
use of com.sri.ai.expresso.core.DefaultExtensionalMultiSet 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;
}
use of com.sri.ai.expresso.core.DefaultExtensionalMultiSet in project aic-expresso by aic-sri-international.
the class IncrementalAnytimeBeliefPropagationWithSeparatorConditioning method variableMessage.
private 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 (!allNodesAreExplored && !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.summingBound(varToSumOut, model.getContext(), model.getTheory());
return bound;
// partitionInAVariableNode.node.setBound(bound);
}
use of com.sri.ai.expresso.core.DefaultExtensionalMultiSet in project aic-expresso by aic-sri-international.
the class Expressions method makeDefaultExtensionalMultiSetFromLabelAndSubTrees.
private static Expression makeDefaultExtensionalMultiSetFromLabelAndSubTrees(Object label, Object[] subTreeObjects) {
if (subTreeObjects.length == 1 && subTreeObjects[0] instanceof Collection) {
subTreeObjects = ((Collection) subTreeObjects[0]).toArray();
}
ArrayList<Expression> subTreeExpressions = Util.mapIntoArrayList(subTreeObjects, Expressions::makeFromObject);
if (subTreeExpressions.size() == 1) {
subTreeExpressions = new ArrayList<Expression>(Expressions.ensureListFromKleeneList(subTreeExpressions.get(0)));
}
Expression result = new DefaultExtensionalMultiSet(subTreeExpressions);
return result;
}
Aggregations