예제 #1
0
  public void testMdlToGraph() {
    List models = TestInference.createTestModels();
    for (Iterator mdlIt = models.iterator(); mdlIt.hasNext(); ) {
      UndirectedModel mdl = (UndirectedModel) mdlIt.next();
      UndirectedGraph g = Graphs.mdlToGraph(mdl);
      Set vertices = g.vertexSet();

      // check the number of vertices
      assertEquals(mdl.numVariables(), vertices.size());

      // check the number of edges
      int numEdgePtls = 0;
      for (Iterator factorIt = mdl.factors().iterator(); factorIt.hasNext(); ) {
        Factor factor = (Factor) factorIt.next();
        if (factor.varSet().size() == 2) numEdgePtls++;
      }
      assertEquals(numEdgePtls, g.edgeSet().size());

      // check that the neighbors of each vertex contain at least some of what they're supposed to
      Iterator it = vertices.iterator();
      while (it.hasNext()) {
        Variable var = (Variable) it.next();
        assertTrue(vertices.contains(var));
        Set neighborsInG = new HashSet(GraphHelper.neighborListOf(g, var));
        neighborsInG.add(var);

        Iterator factorIt = mdl.allFactorsContaining(var).iterator();
        while (factorIt.hasNext()) {
          Factor factor = (Factor) factorIt.next();
          assertTrue(neighborsInG.containsAll(factor.varSet()));
        }
      }
    }
  }
예제 #2
0
 // Verify that potentialOfVertex and potentialOfEdge (which use
 // caches) are consistent with the potentials set.
 public void testUndirectedCaches() {
   List models = TestInference.createTestModels();
   for (Iterator it = models.iterator(); it.hasNext(); ) {
     FactorGraph mdl = (FactorGraph) it.next();
     verifyCachesConsistent(mdl);
   }
 }
 /* Return true iff a clique in L strictly contains c. */
 private boolean findSuperClique(List l, VarSet c) {
   for (Iterator it = l.iterator(); it.hasNext(); ) {
     VarSet c2 = (VarSet) it.next();
     if (c2.containsAll(c)) {
       return true;
     }
   }
   return false;
 }
예제 #4
0
  // Verify that potentialOfVertex and potentialOfEdge (which use
  // caches) are consistent with the potentials set even if a vertex is removed.
  public void testUndirectedCachesAfterRemove() {
    List models = TestInference.createTestModels();
    for (Iterator mdlIt = models.iterator(); mdlIt.hasNext(); ) {
      FactorGraph mdl = (FactorGraph) mdlIt.next();
      mdl = (FactorGraph) mdl.duplicate();
      mdl.remove(mdl.get(0));

      // Verify that indexing correct
      for (Iterator it = mdl.variablesIterator(); it.hasNext(); ) {
        Variable var = (Variable) it.next();
        int idx = mdl.getIndex(var);
        assertTrue(idx >= 0);
        assertTrue(idx < mdl.numVariables());
      }

      // Verify that caches consistent
      verifyCachesConsistent(mdl);
    }
  }