/* 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; }
@Test public void testGetNumConfigs() { Var v0 = getVar(0, 2); Var v1 = getVar(1, 3); Var v2 = getVar(2, 5); VarSet vars = new VarSet(); vars.add(v0); vars.add(v1); vars.add(v2); assertEquals(2 * 3 * 5, vars.calcNumConfigs()); }
protected void afterFactorAdd(Factor factor) { super.afterFactorAdd(factor); CPT cpt = (CPT) factor; Variable child = cpt.getChild(); VarSet parents = cpt.getParents(); allCpts.put(child, cpt); graph.addVertex(child); graph.addAllVertices(parents); for (Iterator it = parents.iterator(); it.hasNext(); ) { Variable rent = (Variable) it.next(); graph.addEdge(rent, child); } }
private void initSizes() { sizes = new int[vertsList.size()]; for (int i = 0; i < sizes.length; i++) { Variable var = vertsList.get(i); if (var.isContinuous()) { throw new UnsupportedOperationException( "Attempt to create AssignmentIterator over " + vertsList + ", but " + var + " is continuous."); } sizes[i] = var.getNumOutcomes(); } max = vertsList.weight(); }
/** * Run initialization analysis on this expression. The init parameter provides access to the * initialization analysis phase (in particular, to the associated error handler), and the * initialized parameter reflects the set of variables that are known to have been initialized * before this expression is evaluated. The return result is the set of variables that are known * to be initialized after the expression has been evaluated. Because there are no side-effecting * operations in the mini language described here, the return result is actually the same as the * input value for initialized in all cases. But, of course, this could change in future if new * constructs were added to the language ... */ public VarSet analyze(InitAnalysis init, VarSet initialized) { if (!VarSet.includes(v, initialized)) { init.report( new Failure( pos, "The variable \"" + this + "\" may be used before it has been initialized")); } return initialized; }
protected Assignment constructAssignment() { int current = indexOfCurrentAssn(); if (sizes == null) initSizes(); // Lazily build sizes array int[] outcomes = new int[sizes.length]; Matrixn.singleToIndices(current, outcomes, sizes); Variable[] vars = (Variable[]) vertsList.toArray(new Variable[0]); return new Assignment(vars, outcomes); }
@Test public void testGetConfigArray1() { Var v0 = getVar(0, 2); Var v1 = getVar(1, 3); Var v2 = getVar(2, 5); VarSet vars1 = new VarSet(); vars1.add(v0); vars1.add(v1); vars1.add(v2); VarSet vars2 = new VarSet(); vars2.add(v1); vars2.add(v2); int[] configs = vars2.getConfigArr(vars1); System.out.println(Arrays.toString(configs)); assertEquals(2 * 3 * 5, configs.length); Assert.assertArrayEquals( new int[] { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14 }, configs); }
private void checkForNoCycle(VarSet parents, Variable child, CPT cpt) { ConnectivityInspector inspector = new ConnectivityInspector(graph); for (Iterator it = parents.iterator(); it.hasNext(); ) { Variable rent = (Variable) it.next(); if (inspector.pathExists(child, rent)) { throw new IllegalArgumentException( "Error adding CPT: Would create directed cycle" + "From: " + rent + " To:" + child + "\nCPT: " + cpt); } } }
@Test public void testGetConfigArray2Swapped() { Var v0 = getVar(0, 2); Var v1 = getVar(1, 3); Var v2 = getVar(2, 5); VarSet vars1 = new VarSet(); vars1.add(v0); vars1.add(v1); vars1.add(v2); VarSet vars2 = new VarSet(); vars2.add(v0); vars2.add(v2); System.out.println(new DenseFactor(vars1)); // TODO: we can't loop over a particular configuration of vars1, only the config in which each // (non-vars2) variable has state 0. int[] configs = vars1.getConfigArr(vars2); System.out.println(Arrays.toString(configs)); assertEquals(2 * 5, configs.length); Assert.assertArrayEquals(new int[] {0, 1, 6, 7, 12, 13, 18, 19, 24, 25}, configs); }
/** * Add an entry for the variable (i.e., environment entry) that is associated with this identifier * to the given set of variables if it is not already included. */ VarSet addTo(VarSet vars) { return VarSet.includes(v, vars) ? vars : new VarSet(v, vars); }
/** Adds edges to graph until it is triangulated. */ private void triangulate(final UndirectedGraph mdl) { UndirectedGraph mdl2 = dupGraph(mdl); ArrayList<Variable> vars = new ArrayList<Variable>(mdl.vertexSet()); Alphabet<Variable> varMap = makeVertexMap(vars); cliques = new ArrayList(); // debug if (logger.isLoggable(Level.FINER)) { logger.finer("Triangulating model: " + mdl); String ret = ""; for (int i = 0; i < vars.size(); i++) { Variable next = (Variable) vars.get(i); ret += next.toString() + "\n"; // " (" + mdl.getIndex(next) + ")\n "; } logger.finer(ret); } while (!vars.isEmpty()) { Variable v = (Variable) pickVertexToRemove(mdl2, vars); logger.finer("Triangulating vertex " + v); VarSet varSet = new BitVarSet(v.getUniverse(), GraphHelper.neighborListOf(mdl2, v)); varSet.add(v); if (!findSuperClique(cliques, varSet)) { cliques.add(varSet); if (logger.isLoggable(Level.FINER)) { logger.finer( " Elim clique " + varSet + " size " + varSet.size() + " weight " + varSet.weight()); } } // must remove V from graph first, because adding the edges // will change the rating of other vertices connectNeighbors(mdl2, v); vars.remove(v); mdl2.removeVertex(v); } if (logger.isLoggable(Level.FINE)) { logger.fine("Triangulation done. Cliques are: "); int totSize = 0, totWeight = 0, maxSize = 0, maxWeight = 0; for (Iterator it = cliques.iterator(); it.hasNext(); ) { VarSet c = (VarSet) it.next(); logger.finer(c.toString()); totSize += c.size(); maxSize = Math.max(c.size(), maxSize); totWeight += c.weight(); maxWeight = Math.max(c.weight(), maxWeight); } double sz = cliques.size(); logger.fine( "Jt created " + sz + " cliques. Size: avg " + (totSize / sz) + " max " + (maxSize) + " Weight: avg " + (totWeight / sz) + " max " + (maxWeight)); } }