Search in sources :

Example 1 with DefaultExtensionalMultiSet

use of com.sri.ai.expresso.core.DefaultExtensionalMultiSet in project aic-expresso by aic-sri-international.

the class IncrementalBeliefPropagationWithConditioning method variableMessage.

public 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 (!AllExplored && !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(anytimeExactBeliefPropagation.Model.Node.VariableNode) Expression(com.sri.ai.expresso.api.Expression) HashSet(java.util.HashSet)

Example 2 with DefaultExtensionalMultiSet

use of com.sri.ai.expresso.core.DefaultExtensionalMultiSet in project aic-expresso by aic-sri-international.

the class IncrementalBeliefPropagationWithConditioning method factorMessage.

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

Example 3 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);
    Expression evaluation = theory.evaluate(sumOnPhi, context);
    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(anytimeExactBeliefPropagation.Model.Node.FactorNode) HashSet(java.util.HashSet)

Example 4 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 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 5 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 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)

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