/** * Returns the factor in this graph, if any, whose domain is a given clique. * @return The factor defined over this clique. Returns null if * no such factor exists. Will not return * potential defined over subsets or supersets of this clique. * @see #addFactor(Factor) * @see #factorOf(Variable,Variable) * @see #factorOf(Variable) */ public Factor factorOf (VarSet varSet) { switch (varSet.size ()) { case 1: return factorOf (varSet.get (0)); case 2: return factorOf (varSet.get (0), varSet.get (1)); default: return factorOf ((Collection) varSet); } }
/** * Returns the factor in this graph, if any, whose domain is a given clique. * @return The factor defined over this clique. Returns null if * no such factor exists. Will not return * potential defined over subsets or supersets of this clique. * @see #addFactor(Factor) * @see #factorOf(Variable,Variable) * @see #factorOf(Variable) */ public Factor factorOf (VarSet varSet) { switch (varSet.size ()) { case 1: return factorOf (varSet.get (0)); case 2: return factorOf (varSet.get (0), varSet.get (1)); default: return factorOf ((Collection) varSet); } }
/** * Returns the factor in this graph, if any, whose domain is a given clique. * @return The factor defined over this clique. Returns null if * no such factor exists. Will not return * potential defined over subsets or supersets of this clique. * @see #addFactor(Factor) * @see #factorOf(Variable,Variable) * @see #factorOf(Variable) */ public Factor factorOf (VarSet varSet) { switch (varSet.size ()) { case 1: return factorOf (varSet.get (0)); case 2: return factorOf (varSet.get (0), varSet.get (1)); default: return factorOf ((Collection) varSet); } }
private Region createVarRegion (RegionGraph rg, FactorGraph mdl, Variable v1) { Factor ptl = mdl.factorOf (v1); if (ptl == null) { return rg.findRegion (v1, true); } else { return rg.findRegion (ptl, true); } }
private static Object objFromElt (FactorGraph fg, Element elt) { String type = elt.getName (); if (type.equals ("VAR")) { String vname = elt.getAttributeValue ("NAME"); return fg.findVariable (vname); } else if (type.equals("FACTOR")) { String varSetStr = elt.getAttributeValue ("VARS"); String[] vnames = varSetStr.split ("\\s+"); Variable[] vars = new Variable [vnames.length]; for (int i = 0; i < vnames.length; i++) { vars[i] = fg.findVariable (vnames[i]); } return fg.factorOf (new HashVarSet (vars)); } else { throw new RuntimeException ("Can't figure out element "+elt); } }
private static Object objFromElt (FactorGraph fg, Element elt) { String type = elt.getName (); if (type.equals ("VAR")) { String vname = elt.getAttributeValue ("NAME"); return fg.findVariable (vname); } else if (type.equals("FACTOR")) { String varSetStr = elt.getAttributeValue ("VARS"); String[] vnames = varSetStr.split ("\\s+"); Variable[] vars = new Variable [vnames.length]; for (int i = 0; i < vnames.length; i++) { vars[i] = fg.findVariable (vnames[i]); } return fg.factorOf (new HashVarSet (vars)); } else { throw new RuntimeException ("Can't figure out element "+elt); } }
private static Object objFromElt (FactorGraph fg, Element elt) { String type = elt.getName (); if (type.equals ("VAR")) { String vname = elt.getAttributeValue ("NAME"); return fg.findVariable (vname); } else if (type.equals("FACTOR")) { String varSetStr = elt.getAttributeValue ("VARS"); String[] vnames = varSetStr.split ("\\s+"); Variable[] vars = new Variable [vnames.length]; for (int i = 0; i < vnames.length; i++) { vars[i] = fg.findVariable (vnames[i]); } return fg.factorOf (new HashVarSet (vars)); } else { throw new RuntimeException ("Can't figure out element "+elt); } }
private Region createVarRegion (RegionGraph rg, FactorGraph mdl, Variable v1) { Factor ptl = mdl.factorOf (v1); if (ptl == null) { return rg.findRegion (v1, true); } else { return rg.findRegion (ptl, true); } }
private Region createVarRegion (RegionGraph rg, FactorGraph mdl, Variable v1) { Factor ptl = mdl.factorOf (v1); if (ptl == null) { return rg.findRegion (v1, true); } else { return rg.findRegion (ptl, true); } }
private void verifyCachesConsistent (FactorGraph mdl) { Factor pot, pot2, pot3; for (Iterator it = mdl.factors ().iterator (); it.hasNext ();) { pot = (Factor) it.next (); // System.out.println("Testing model "+i+" potential "+pot); Object[] vars = pot.varSet ().toArray (); switch (vars.length) { case 1: pot2 = mdl.factorOf ((Variable) vars[0]); assertTrue (pot == pot2); break; case 2: Variable var1 = (Variable) vars[0]; Variable var2 = (Variable) vars[1]; pot2 = mdl.factorOf (var1, var2); pot3 = mdl.factorOf (var2, var1); assertTrue (pot == pot2); assertTrue (pot2 == pot3); break; // Factors of size > 2 aren't now cached. default: break; } } }
public void testFactorOfSet () { Variable[] vars = new Variable [3]; for (int i = 0; i < vars.length; i++) { vars[i] = new Variable (2); } Factor factor = new TableFactor (vars, new double[] { 0, 1, 2, 3, 4, 5, 6, 7 }); FactorGraph fg = new FactorGraph (vars); fg.addFactor (factor); assertTrue (factor == fg.factorOf (factor.varSet ())); HashSet set = new HashSet (factor.varSet ()); assertTrue (factor == fg.factorOf (set)); set.remove (vars[0]); assertTrue (null == fg.factorOf (set)); }
public void testFactorOfSet () { Variable[] vars = new Variable [3]; for (int i = 0; i < vars.length; i++) { vars[i] = new Variable (2); } Factor factor = new TableFactor (vars, new double[] { 0, 1, 2, 3, 4, 5, 6, 7 }); FactorGraph fg = new FactorGraph (vars); fg.addFactor (factor); assertTrue (factor == fg.factorOf (factor.varSet ())); HashSet set = new HashSet (factor.varSet ()); assertTrue (factor == fg.factorOf (set)); set.remove (vars[0]); assertTrue (null == fg.factorOf (set)); }
public RegionGraph constructRegionGraph (FactorGraph mdl) { RegionGraph rg = new RegionGraph (); for (Iterator it = mdl.factorsIterator (); it.hasNext();) { Factor ptl = (Factor) it.next (); if (ptl.varSet ().size() == 1) continue; // Single-node potentials handled separately Region parent = new Region (ptl); // Now add appropriate edges to region graph for (Iterator childIt = ptl.varSet().iterator (); childIt.hasNext();) { Variable var = (Variable) childIt.next (); Factor childPtl = mdl.factorOf (var); Region child = rg.findRegion (childPtl, true); //add node potential to parent if necessary if (childPtl != null) { parent.addFactor (childPtl); child.addFactor (childPtl); } rg.add (parent, child); } } rg.computeInferenceCaches (); return rg; }
public RegionGraph constructRegionGraph (FactorGraph mdl) { RegionGraph rg = new RegionGraph (); for (Iterator it = mdl.factorsIterator (); it.hasNext();) { Factor ptl = (Factor) it.next (); if (ptl.varSet ().size() == 1) continue; // Single-node potentials handled separately Region parent = new Region (ptl); // Now add appropriate edges to region graph for (Iterator childIt = ptl.varSet().iterator (); childIt.hasNext();) { Variable var = (Variable) childIt.next (); Factor childPtl = mdl.factorOf (var); Region child = rg.findRegion (childPtl, true); //add node potential to parent if necessary if (childPtl != null) { parent.addFactor (childPtl); child.addFactor (childPtl); } rg.add (parent, child); } } rg.computeInferenceCaches (); return rg; }
public RegionGraph constructRegionGraph (FactorGraph mdl) { RegionGraph rg = new RegionGraph (); for (Iterator it = mdl.factorsIterator (); it.hasNext();) { Factor ptl = (Factor) it.next (); if (ptl.varSet ().size() == 1) continue; // Single-node potentials handled separately Region parent = new Region (ptl); // Now add appropriate edges to region graph for (Iterator childIt = ptl.varSet().iterator (); childIt.hasNext();) { Variable var = (Variable) childIt.next (); Factor childPtl = mdl.factorOf (var); Region child = rg.findRegion (childPtl, true); //add node potential to parent if necessary if (childPtl != null) { parent.addFactor (childPtl); child.addFactor (childPtl); } rg.add (parent, child); } } rg.computeInferenceCaches (); return rg; }
public void testThreeNodeModel () { Random r = new Random (23534709); FactorGraph mdl = new FactorGraph (); Variable root = new Variable (2); Variable childL = new Variable (2); Variable childR = new Variable (2); mdl.addFactor (root, childL, RandomGraphs.generateMixedPotentialValues (r, 1.5)); mdl.addFactor (root, childR, RandomGraphs.generateMixedPotentialValues (r, 1.5)); // assertTrue (mdl.isConnected (root, childL)); // assertTrue (mdl.isConnected (root, childR)); // assertTrue (mdl.isConnected (childL, childR)); assertTrue (mdl.isAdjacent (root, childR)); assertTrue (mdl.isAdjacent (root, childL)); assertTrue (!mdl.isAdjacent (childL, childR)); assertTrue (mdl.factorOf (root, childL) != null); assertTrue (mdl.factorOf (root, childR) != null); }
public void testThreeNodeModel () { Random r = new Random (23534709); FactorGraph mdl = new FactorGraph (); Variable root = new Variable (2); Variable childL = new Variable (2); Variable childR = new Variable (2); mdl.addFactor (root, childL, RandomGraphs.generateMixedPotentialValues (r, 1.5)); mdl.addFactor (root, childR, RandomGraphs.generateMixedPotentialValues (r, 1.5)); // assertTrue (mdl.isConnected (root, childL)); // assertTrue (mdl.isConnected (root, childR)); // assertTrue (mdl.isConnected (childL, childR)); assertTrue (mdl.isAdjacent (root, childR)); assertTrue (mdl.isAdjacent (root, childL)); assertTrue (!mdl.isAdjacent (childL, childR)); assertTrue (mdl.factorOf (root, childL) != null); assertTrue (mdl.factorOf (root, childR) != null); }
/** * Tests that models can be created that have multiple factors over the same edge, and that * potentialOfEdge returns the product in that case. */ public void testMultipleEdgePotentials () { Variable v1 = new Variable (2); Variable v2 = new Variable (2); Variable[] vars = new Variable[]{v1, v2}; FactorGraph mdl = new FactorGraph (vars); Factor ptl1 = new TableFactor (vars, new double[]{0.5, 0.5, 0.5, 0.5}); mdl.addFactor (ptl1); Factor ptl2 = new TableFactor (vars, new double[]{0.25, 0.25, 0.5, 0.5}); mdl.addFactor (ptl2); try { mdl.factorOf (v1, v2); fail (); } catch (RuntimeException e) {} Collection factors = mdl.allFactorsContaining (new HashVarSet (vars)); assertEquals (2, factors.size ()); assertTrue (factors.contains (ptl1)); assertTrue (factors.contains (ptl2)); double[] vals = {0.125, 0.125, 0.25, 0.25}; Factor total = TableFactor.multiplyAll (factors); Factor expected = new TableFactor (vars, vals); assertTrue ("Arrays not equal\n Expected " + ArrayUtils.toString (vals) + "\n Actual " + ArrayUtils.toString (((TableFactor) total).toValueArray ()), expected.almostEquals (total, 1e-10)); }
/** * Tests that models can be created that have multiple factors over the same variable, and that * potentialOfVertex returns the product in that case. */ public void testMultipleNodePotentials () { Variable var = new Variable (2); FactorGraph mdl = new FactorGraph (new Variable[]{var}); Factor ptl1 = new TableFactor (var, new double[]{0.5, 0.5}); mdl.addFactor (ptl1); Factor ptl2 = new TableFactor (var, new double[]{0.25, 0.25}); mdl.addFactor (ptl2); // verify that factorOf(var) doesn't work try { mdl.factorOf (var); fail (); } catch (RuntimeException e) {} // expected List factors = mdl.allFactorsOf (var); Factor total = TableFactor.multiplyAll (factors); double[] expected = {0.125, 0.125}; assertTrue ("Arrays not equal\n Expected " + ArrayUtils.toString (expected) + "\n Actual " + ArrayUtils.toString (((TableFactor) total).toValueArray ()), Arrays.equals (expected, ((TableFactor) total).toValueArray ())); }
/** * Tests that models can be created that have multiple factors over the same variable, and that * potentialOfVertex returns the product in that case. */ public void testMultipleNodePotentials () { Variable var = new Variable (2); FactorGraph mdl = new FactorGraph (new Variable[]{var}); Factor ptl1 = new TableFactor (var, new double[]{0.5, 0.5}); mdl.addFactor (ptl1); Factor ptl2 = new TableFactor (var, new double[]{0.25, 0.25}); mdl.addFactor (ptl2); // verify that factorOf(var) doesn't work try { mdl.factorOf (var); fail (); } catch (RuntimeException e) {} // expected List factors = mdl.allFactorsOf (var); Factor total = TableFactor.multiplyAll (factors); double[] expected = {0.125, 0.125}; assertTrue ("Arrays not equal\n Expected " + ArrayUtils.toString (expected) + "\n Actual " + ArrayUtils.toString (((TableFactor) total).toValueArray ()), Arrays.equals (expected, ((TableFactor) total).toValueArray ())); }