public void testOutputToDot() throws IOException { FactorGraph mdl = TestInference.createRandomGrid(3, 4, 2, new Random(4234)); PrintWriter out = new PrintWriter(new FileWriter(new File("grmm-model.dot"))); mdl.printAsDot(out); out.close(); System.out.println("Now you can open up grmm-model.dot in Graphviz."); }
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())); } } } }
// 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); } }
// 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); } }