Skip to content

Commit

Permalink
Bug fixes in lbp.h
Browse files Browse the repository at this point in the history
  • Loading branch information
radum2275 committed Jun 17, 2016
1 parent c24da20 commit ea9ed96
Show file tree
Hide file tree
Showing 12 changed files with 683 additions and 178 deletions.
8 changes: 4 additions & 4 deletions example/demo.cpp
Expand Up @@ -22,7 +22,7 @@ void demo_debug() {
// Init parameters
unsigned int ibound = 2;
unsigned int iterations = 10;
const char* inputFile = "/home/radu/git/merlin/example/simple5.uai";
const char* inputFile = "/home/radu/git/merlin/test/mrf.wcnf.uai";
const char* evidenceFile = "/home/radu/git/merlin/example/simple5.evid";
const char* queryFile = "/home/radu/git/merlin/example/simple5.map";
const char* outputFile = "/home/radu/git/merlin/example/simple5.out";
Expand All @@ -40,9 +40,9 @@ void demo_debug() {
eng.set_param_iterations(iterations);
eng.read_model(inputFile);
eng.read_evidence(evidenceFile);
//eng.read_query(queryFile);
eng.set_task(MERLIN_TASK_MAP);
eng.set_algorithm(MERLIN_ALGO_JGLP);
// eng.read_query(queryFile);
eng.set_task(MERLIN_TASK_MAR);
eng.set_algorithm(MERLIN_ALGO_LBP);
eng.run();
}

Expand Down
2 changes: 1 addition & 1 deletion include/base.h
Expand Up @@ -25,7 +25,7 @@
///

// Software version
#define VERSIONINFO "libmerlin 1.0.0"
#define VERSIONINFO "libmerlin 1.3.0"
#define COPYRIGHT "(c) Copyright IBM Corp. 2015, 2016\nAll Rights Reserved"

#ifndef IBM_MERLIN_BASE_H_
Expand Down
2 changes: 1 addition & 1 deletion include/factor.h
Expand Up @@ -32,8 +32,8 @@

#include "enum.h"
#include "util.h"
#include "varset.h"
#include "index.h"
#include "variable_set.h"

namespace merlin {

Expand Down
59 changes: 39 additions & 20 deletions include/factor_graph.h
Expand Up @@ -37,9 +37,9 @@ namespace merlin {
/// A graphical model represented as a bipartite graph between *variable nodes*,
/// corresponding to the variables, and *factor nodes*, corresponding to the factors.
/// Internally, factors and variables are mainly referenced by integer indices, with
/// 0 <= f < nFactors() and 0 <= v < nvar(). However, many interface functions are called
/// using a variable object, Var(label,dim). To convert, var(v) gives the vth Var object
/// and the internal function _vindex(V) gives the index corresponding to variable object V.
/// 0 <= f < nFactors() and 0 <= v < nvar(). However, many interface functions are called
/// using a variable object, var(label,dim). To convert, var(v) gives the vth var object
/// and the internal function _vindex(V) gives the index corresponding to variable object V.
///
class factor_graph: public graphical_model {
public:
Expand All @@ -56,7 +56,7 @@ class factor_graph: public graphical_model {
/// \brief Default constructor.
///
factor_graph() :
graphical_model(), m_vindex() {
graphical_model(), m_vindex() {
};

///
Expand All @@ -71,10 +71,9 @@ class factor_graph: public graphical_model {
/// \brief Constructor.
/// \param fs The list of factors
///
factor_graph(std::vector<merlin::factor> fs) :
graphical_model(fs), m_vindex() {
factor_graph(std::vector<factor> fs) : graphical_model(fs), m_vindex() {
m_vindex.resize(nvar());
check_factors();
create_factor_graph();
}

///
Expand All @@ -86,7 +85,7 @@ class factor_graph: public graphical_model {
factor_graph(InputIterator first, InputIterator last) :
graphical_model(first, last), m_vindex() {
m_vindex.resize(nvar());
check_factors();
create_factor_graph();
}

///
Expand All @@ -103,7 +102,7 @@ class factor_graph: public graphical_model {
/// \brief Add a new factor to the graphical model.
/// \param F The factor to be added
/// \return the index of the newly added factor.
findex add_factor(const merlin::factor& F) {
findex add_factor(const factor& F) {
findex use = graphical_model::add_factor(F); // add the factor to the underlying collection
m_vindex.resize(nvar(), vindex(-1)); // then update variable nodes and edges (!!!)
if (F.nvar() == 1 && m_vindex[*F.vars().begin()] == vindex(-1))
Expand All @@ -112,7 +111,7 @@ class factor_graph: public graphical_model {
for (variable_set::const_iterator v = F.vars().begin();
v != F.vars().end(); ++v) {
if (m_vindex[*v] == vindex(-1))
m_vindex[*v] = graphical_model::add_factor(merlin::factor(*v, 1.0));
m_vindex[*v] = graphical_model::add_factor(factor(*v, 1.0));
add_edge(use, local_factor(*v)); // add edge to variable node
}
return use;
Expand Down Expand Up @@ -215,30 +214,50 @@ class factor_graph: public graphical_model {


///
/// \brief Check the factors for consistency (internal use only).
/// \brief Create the factor graph.
///
void check_factors() {
if (m_vindex.size() < nvar())
/// Creates the bi-partite graph by adding nodes corresponding to local
/// variable factors (ie, unary factors definded for each variable)
/// and connecting them to the nodes corresponding to the factors. Let M be
/// the number of factors. Then the nodes corresponding to the factors
/// are indexed [0 .. M-1], while the nodes corresponding to the variables
/// are indexed [M .. M+N-1] where N is the number of variables.
///
void create_factor_graph() {

// init var to node index map
if (m_vindex.size() < nvar()) {
m_vindex.resize(nvar());
}

// identify unary factors (if any)
std::vector<bool> found(nvar(), false);
for (size_t i = 0; i < m_factors.size(); ++i) {
if (m_factors[i].nvar() == 1) {
size_t v = _vindex(*m_factors[i].vars().begin());
if (!found[v])
m_vindex[v] = i;
if (!found[v]) m_vindex[v] = i;
found[v] = true;
}
}
for (size_t v = 0; v < found.size(); ++v)

// create local factors for each of the variables
for (size_t v = 0; v < found.size(); ++v) {
if (!found[v]) {
m_vindex[v]=
add_factor(merlin::factor(var(v), 1.0));
m_vindex[v] = graphical_model::add_factor(factor(var(v), 1.0));
}
}

// create the bi-partite graph (edges from factor node to its variable nodes)
if (num_edges() != 0) {
throw std::runtime_error("Initial factor graph must be empty.");
}
for (size_t i = 0; i < m_factors.size(); ++i) {
if (!is_var_node(i))
if (!is_var_node(i)) { // factor node
for (variable_set::const_iterator v = m_factors[i].vars().begin();
v != m_factors[i].vars().end(); ++v)
v != m_factors[i].vars().end(); ++v) {
add_edge(i, local_factor(*v));
}
}
}
}

Expand Down

0 comments on commit ea9ed96

Please sign in to comment.