Search in sources :

Example 1 with FactorNode

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

the class IncrementalBeliefPropagationWithConditioningVersion2 method ExpandModel.

public PartitionTree ExpandModel(Iterator<FactorNode> it) {
    if (it.hasNext()) {
        // get factor to add
        FactorNode newfactor = it.next();
        // create partition tree of new Factor
        PartitionTree newPartition = new PartitionTree(newfactor);
        findPartitionGivenANode.put(newfactor, newPartition);
        Collection<VariableNode> exploredVariablesInModel = this.partitionTree.model.getExploredVariables();
        exploredVariablesInModel.add((VariableNode) partitionTree.node);
        // get variables in the tree linked to the factor. attach the factor to one of them
        Collection<VariableNode> variablesInTheTreeLinkedToFactor = this.partitionTree.model.getVariablesOfAFactor(newfactor);
        variablesInTheTreeLinkedToFactor.retainAll(exploredVariablesInModel);
        if (variablesInTheTreeLinkedToFactor.isEmpty()) {
            return null;
        }
        // get parent and link new factor to his parent
        VariableNode var = variablesInTheTreeLinkedToFactor.iterator().next();
        PartitionTree parentOfNewFactor = findPartitionGivenANode.get(var);
        parentOfNewFactor.children.add(newPartition);
        newPartition.parent = parentOfNewFactor;
        newPartition.model = parentOfNewFactor.model;
        // get variables to be put below new factor in the three
        // adding them to the tree
        Collection<VariableNode> variablesToBePutItTheTree = this.partitionTree.model.getVariablesOfAFactor(newfactor);
        variablesToBePutItTheTree.removeAll(exploredVariablesInModel);
        for (VariableNode v : variablesToBePutItTheTree) {
            PartitionTree p = new PartitionTree(v);
            newPartition.children.add(p);
            p.parent = newPartition;
            p.model = newPartition.model;
            findPartitionGivenANode.put(v, p);
        }
        // expand the model (add new factor to the explored part of the graph)
        this.partitionTree.model.ExpandModel(newfactor);
        return newPartition;
    }
    return null;
}
Also used : VariableNode(anytimeExactBeliefPropagation.Model.Node.VariableNode) FactorNode(anytimeExactBeliefPropagation.Model.Node.FactorNode)

Example 2 with FactorNode

use of anytimeExactBeliefPropagation.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);
    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 3 with FactorNode

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

the class Tests method testing3.

public static List<TupleOfData> testing3(String modelName, Model m, Integer... parameter) {
    List<TupleOfData> result = new ArrayList<TupleOfData>();
    int id = 0;
    m.clearExploredGraph();
    Iterator<FactorNode> bfsExpander = new BFS(m);
    IncrementalBeliefPropagationWithConditioning sbp = new IncrementalBeliefPropagationWithConditioning(m);
    while (bfsExpander.hasNext()) {
        TupleOfData t = new TupleOfData();
        long tStart = System.currentTimeMillis();
        Bound inferenceResult = sbp.expandAndComputeInference(bfsExpander);
        long tEnd = System.currentTimeMillis();
        long tDelta = tEnd - tStart;
        t.time = tDelta / 1000.0;
        t.typeOfComputationUsed = "S-BP";
        t.graphicalModelName = modelName;
        t.id = id++;
        t.numberOfExtremePoints = inferenceResult.getArguments().size();
        Pair<Double, Double> minAndMaxProbabilityofQueryequalsTrue = ModelGenerator.maxMinProbability(inferenceResult, m);
        t.minAndMaxProbabilityofQueryequalsTrue = minAndMaxProbabilityofQueryequalsTrue.first;
        t.maxAndMaxProbabilityofQueryequalsTrue = minAndMaxProbabilityofQueryequalsTrue.second;
        t.IntervalLength = t.maxAndMaxProbabilityofQueryequalsTrue - t.minAndMaxProbabilityofQueryequalsTrue;
        t.allExplored = m.AllExplored();
        for (int i = 0; i < parameter.length && i < t.parameter.length; i++) {
            t.parameter[i] = parameter[i];
        }
        result.add(t);
        println("....");
    }
    TupleOfData t = new TupleOfData();
    long tStart = System.currentTimeMillis();
    Expression inferenceLVE = ModelGenerator.lveCalculation(m);
    Bound EncapsulatingInference = Bounds.makeSingleElementBound(inferenceLVE, true);
    Pair<Double, Double> minAndMaxProbabilityofQueryequalsTrue = ModelGenerator.maxMinProbability(EncapsulatingInference, m);
    long tEnd = System.currentTimeMillis();
    long tDelta = tEnd - tStart;
    t.time = tDelta / 1000.0;
    t.minAndMaxProbabilityofQueryequalsTrue = minAndMaxProbabilityofQueryequalsTrue.first;
    t.maxAndMaxProbabilityofQueryequalsTrue = minAndMaxProbabilityofQueryequalsTrue.second;
    t.typeOfComputationUsed = "SGDPLL";
    t.graphicalModelName = modelName;
    t.id = id++;
    t.numberOfExtremePoints = 0;
    t.IntervalLength = 0;
    t.allExplored = true;
    for (int i = 0; i < parameter.length && i < t.parameter.length; i++) {
        t.parameter[i] = parameter[i];
    }
    result.add(t);
    println("------------------------------------------------------------");
    return result;
}
Also used : ArrayList(java.util.ArrayList) Bound(com.sri.ai.grinder.library.bounds.Bound) FactorNode(anytimeExactBeliefPropagation.Model.Node.FactorNode) BFS(anytimeExactBeliefPropagation.Model.BFS) Expression(com.sri.ai.expresso.api.Expression)

Example 4 with FactorNode

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

the class Tests method testing.

/**
 * This tests a model and, instead of printing information, stores its in a list of data structures
 * each element of the list corresponds to a iteration of the algorithm
 * @param modelName
 * @param m
 * @param parameter
 * @return
 */
public static List<TupleOfData> testing(String modelName, Model m, Integer... parameter) {
    List<TupleOfData> result = new ArrayList<TupleOfData>();
    int id = 0;
    m.clearExploredGraph();
    Iterator<FactorNode> bfsExpander = new BFS(m);
    IncrementalBeliefPropagationWithConditioning sbp = new IncrementalBeliefPropagationWithConditioning(m);
    double tTotalTime = 0;
    while (bfsExpander.hasNext()) {
        TupleOfData t = new TupleOfData();
        long tStart = System.currentTimeMillis();
        Bound inferenceResult = sbp.expandAndComputeInference(bfsExpander);
        long tEnd = System.currentTimeMillis();
        long tDelta = tEnd - tStart;
        t.time = tDelta / 1000.0;
        tTotalTime += tDelta / 1000.0;
        t.totalTime += tTotalTime;
        t.typeOfComputationUsed = "S-BP";
        t.graphicalModelName = modelName;
        t.id = id++;
        t.numberOfExtremePoints = inferenceResult.getArguments().size();
        Pair<Double, Double> minAndMaxProbabilityofQueryequalsTrue = ModelGenerator.maxMinProbability(inferenceResult, m);
        t.minAndMaxProbabilityofQueryequalsTrue = minAndMaxProbabilityofQueryequalsTrue.first;
        t.maxAndMaxProbabilityofQueryequalsTrue = minAndMaxProbabilityofQueryequalsTrue.second;
        t.IntervalLength = t.maxAndMaxProbabilityofQueryequalsTrue - t.minAndMaxProbabilityofQueryequalsTrue;
        t.allExplored = m.AllExplored();
        for (int i = 0; i < parameter.length && i < t.parameter.length; i++) {
            t.parameter[i] = parameter[i];
        }
        result.add(t);
        println("....");
    }
    TupleOfData t = new TupleOfData();
    long tStart = System.currentTimeMillis();
    Expression inferenceLVE = ModelGenerator.lveCalculation(m);
    Bound encapsulatingInference = Bounds.makeSingleElementBound(inferenceLVE, true);
    Pair<Double, Double> minAndMaxProbabilityofQueryequalsTrue = ModelGenerator.maxMinProbability(encapsulatingInference, m);
    long tEnd = System.currentTimeMillis();
    long tDelta = tEnd - tStart;
    t.time = tDelta / 1000.0;
    t.totalTime = t.time;
    t.minAndMaxProbabilityofQueryequalsTrue = minAndMaxProbabilityofQueryequalsTrue.first;
    t.maxAndMaxProbabilityofQueryequalsTrue = minAndMaxProbabilityofQueryequalsTrue.second;
    t.typeOfComputationUsed = "SGDPLL";
    t.graphicalModelName = modelName;
    t.id = id++;
    t.numberOfExtremePoints = 0;
    t.IntervalLength = 0;
    t.allExplored = true;
    for (int i = 0; i < parameter.length && i < t.parameter.length; i++) {
        t.parameter[i] = parameter[i];
    }
    result.add(t);
    println("------------------------- Done -----------------------------------");
    return result;
}
Also used : ArrayList(java.util.ArrayList) Bound(com.sri.ai.grinder.library.bounds.Bound) FactorNode(anytimeExactBeliefPropagation.Model.Node.FactorNode) BFS(anytimeExactBeliefPropagation.Model.BFS) Expression(com.sri.ai.expresso.api.Expression)

Example 5 with FactorNode

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

the class IncrementalBeliefPropagationWithConditioning 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.setOfFactorsInsidePartition) {
            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(anytimeExactBeliefPropagation.Model.Node.VariableNode) Set(java.util.Set) HashSet(java.util.HashSet) DefaultExtensionalMultiSet(com.sri.ai.expresso.core.DefaultExtensionalMultiSet) ArrayList(java.util.ArrayList) FactorNode(anytimeExactBeliefPropagation.Model.Node.FactorNode) HashSet(java.util.HashSet)

Aggregations

FactorNode (anytimeExactBeliefPropagation.Model.Node.FactorNode)11 VariableNode (anytimeExactBeliefPropagation.Model.Node.VariableNode)7 Expression (com.sri.ai.expresso.api.Expression)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 BFS (anytimeExactBeliefPropagation.Model.BFS)3 Bound (com.sri.ai.grinder.library.bounds.Bound)3 DefaultExtensionalMultiSet (com.sri.ai.expresso.core.DefaultExtensionalMultiSet)2 Node (anytimeExactBeliefPropagation.Model.Node.Node)1 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Set (java.util.Set)1