Exemple #1
0
Params
BeliefProp::getFactorJoint (
    FacNode* fn,
    const VarIds& jointVarIds)
{
  if (runned_ == false) {
    runSolver();
  }
  Factor res (fn->factor());
  const BpLinks& links = getLinks( fn);
  for (size_t i = 0; i < links.size(); i++) {
    Factor msg ({links[i]->varNode()->varId()},
                {links[i]->varNode()->range()},
                getVarToFactorMsg (links[i]));
    res.multiply (msg);
  }
  res.sumOutAllExcept (jointVarIds);
  res.reorderArguments (jointVarIds);
  res.normalize();
  Params jointDist = res.params();
  if (Globals::logDomain) {
    Util::exp (jointDist);
  }
  return jointDist;
}
Exemple #2
0
void
FactorTest::testSummingOut()
{
  VarIds vids   = {0, 1, 2, 3};
  Ranges ranges = {3, 2, 4, 3};
  Params params = {
    0.022757933283133, 0.0106825417145475, 0.0212115929862968, 
    0.0216271252738214, 0.0246935408909929, 0.00535101952882101, 
    0.00908008645423061, 0.0208088234425334, 0.00752400708452212, 
    0.0150052316136527, 0.0129311224551535, 0.0170340535302049, 
    0.00988081654256193, 0.0139063490493519, 0.025792784294836, 
    0.0248167234610076, 0.017219348482278, 0.0194292243637016, 
    0.00383554941557795, 0.0164407987747966, 0.00044152909395022, 
    0.00657900705816833, 0.00371715392294919, 0.0217825142487465, 
    0.00424392333677727, 0.0108602703755316, 0.00351559808401304, 
    0.00294727405145356, 0.0270575932871257, 0.005911864680038, 
    0.0138936584911577, 0.0227288019859002, 0.0165944064071987, 
    0.0080185268930961, 0.0172692026753632, 0.0142012227138332, 
    0.0133695464219171, 0.0263492891422071, 0.00792332157200822, 
    0.0208935535064392, 0.0142677961715013, 0.0208544440271617, 
    0.0108408824522857, 0.0241486127140633, 0.00767406849215521, 
    0.00954694217537661, 0.0218786116033257, 0.0248934169744332, 
    0.00188944195471982, 0.0257141610189036, 0.0142474911774847, 
    0.00233097104867004, 0.00520644350532678, 0.0179646451004339, 
    0.0241134853100298, 0.00945036684210405, 0.00173819089160705, 
    0.000542358809684406, 0.0123976408935576, 0.00170905959437435, 
    0.00645422348972241, 0.0262912993847153, 0.0244378615928878, 
    0.0230486298969212, 0.00722310170606624, 0.0146203396838926, 
    0.0101631280263959, 0.0205926481279833, 0.0138829042417413, 
    0.0180864495984042, 0.0143994770626774, 0.00106397584149748
  };

  Factor f (vids, ranges, params);
  double sum = std::accumulate (f.params().begin(), f.params().end(), 0.0);
  CPPUNIT_ASSERT (similiar (sum, 1.0));
  
  f.sumOut (0);
  f.sumOut (3);
  f.sumOut (2);
  
  sum = std::accumulate (f.params().begin(), f.params().end(), 0.0);
  CPPUNIT_ASSERT (similiar (sum, 1.0));
}
Exemple #3
0
void
BeliefProp::calcFactorToVarMsg (BpLink* link)
{
  FacNode* src = link->facNode();
  const VarNode* dst = link->varNode();
  const BpLinks& links = getLinks (src);
  // calculate the product of messages that were sent
  // to factor `src', except from var `dst'
  unsigned reps    = 1;
  unsigned msgSize = Util::sizeExpected (src->factor().ranges());
  Params msgProduct (msgSize, LogAware::multIdenty());
  if (Globals::logDomain) {
    for (size_t i = links.size(); i-- > 0; ) {
      if (links[i]->varNode() != dst) {
        if (Constants::showBpCalcs) {
          std::cout << "    message from " << links[i]->varNode()->label();
          std::cout << ": " ;
        }
        Util::apply_n_times (msgProduct, getVarToFactorMsg (links[i]),
            reps, std::plus<double>());
        if (Constants::showBpCalcs) {
          std::cout << std::endl;
        }
      }
      reps *= links[i]->varNode()->range();
    }
  } else {
    for (size_t i = links.size(); i-- > 0; ) {
      if (links[i]->varNode() != dst) {
        if (Constants::showBpCalcs) {
          std::cout << "    message from " << links[i]->varNode()->label();
          std::cout << ": " ;
        }
        Util::apply_n_times (msgProduct, getVarToFactorMsg (links[i]),
            reps, std::multiplies<double>());
        if (Constants::showBpCalcs) {
          std::cout << std::endl;
        }
      }
      reps *= links[i]->varNode()->range();
    }
  }
  Factor result (src->factor().arguments(),
      src->factor().ranges(), msgProduct);
  result.multiply (src->factor());
  if (Constants::showBpCalcs) {
    std::cout << "    message product:  " << msgProduct << std::endl;
    std::cout << "    original factor:  " << src->factor().params();
    std::cout << std::endl;
    std::cout << "    factor product:   " << result.params() << std::endl;
  }
  result.sumOutAllExcept (dst->varId());
  if (Constants::showBpCalcs) {
    std::cout << "    marginalized:     " << result.params() << std::endl;
  }
  link->nextMessage() = result.params();
  LogAware::normalize (link->nextMessage());
  if (Constants::showBpCalcs) {
    std::cout << "    curr msg:         " << link->message() << std::endl;
    std::cout << "    next msg:         " << link->nextMessage() << std::endl;
  }
}