Search in sources :

Example 6 with DefaultExtensionalMultiSet

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;
}
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 7 with DefaultExtensionalMultiSet

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

Example 8 with DefaultExtensionalMultiSet

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);
}
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 9 with DefaultExtensionalMultiSet

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;
}
Also used : DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) Collection(java.util.Collection) DefaultExtensionalMultiSet(com.sri.ai.expresso.core.DefaultExtensionalMultiSet) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)9 DefaultExtensionalMultiSet (com.sri.ai.expresso.core.DefaultExtensionalMultiSet)9 ArrayList (java.util.ArrayList)8 HashSet (java.util.HashSet)8 Bound (com.sri.ai.grinder.library.bounds.Bound)6 VariableNode (IncrementalAnytimeExactBeliefPropagation.Model.Node.VariableNode)4 Set (java.util.Set)4 VariableNode (anytimeExactBeliefPropagation.Model.Node.VariableNode)2 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)2 FactorNode (IncrementalAnytimeExactBeliefPropagation.Model.Node.FactorNode)1 FactorNode (anytimeExactBeliefPropagation.Model.Node.FactorNode)1 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)1 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)1 IndexExpressions (com.sri.ai.grinder.library.indexexpression.IndexExpressions)1 Collection (java.util.Collection)1