Exemplo n.º 1
0
  /**
   * 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));
  }
Exemplo n.º 2
0
  /**
   * 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()));
  }