13 #include "FactorGraph.h"
14 #include "BruteForceExactInference.h"
15 #include "BeliefPropagation.h"
16 #include "DiffusionInference.h"
17 #include "SimulatedAnnealingInference.h"
66 tolerance_(1.0e-6), verbose_(false),
67 BPSchedule_(Grante::BeliefPropagation::Sequential), SASteps_(100),
68 SAT0_(10.0), SATfinal_(0.05) {
72 GRANTE(
const GraphicalModelType& gm,
const Parameter& para);
76 std::string
name()
const;
79 template<
class VISITOR>
83 typename GM::ValueType
bound()
const;
84 typename GM::ValueType
value()
const;
87 const GraphicalModelType&
gm_;
100 void groupFactors(std::vector<std::vector<IndexType> >& groupedFactors)
const;
101 void groupFactorTypes(
const std::vector<std::vector<IndexType> >& groupedFactors, std::vector<std::vector<IndexType> >& groupedFactorTypes)
const;
103 template<
class T,
class OBJECT>
106 (*object_)[
index_] =
static_cast<double>(v);
116 : gm_(gm), parameter_(para), granteModel_(new Grante::FactorGraphModel()), granteGraph_(NULL),
117 granteInferenceMethod_(NULL) {
120 std::vector<std::vector<IndexType> > groupedFactors;
124 std::vector<std::vector<IndexType> > groupedFactorTypes;
128 for(
size_t i = 0; i < groupedFactorTypes.size(); i++) {
130 std::stringstream ss;
132 std::string
name = ss.str();
135 IndexType currentFactor = groupedFactors[groupedFactorTypes[i][0]][0];
138 std::vector<unsigned int> cardinalities;
139 for(
IndexType j = 0; j <
gm_[currentFactor].numberOfVariables(); j++) {
140 cardinalities.push_back(static_cast<unsigned int>(
gm_.numberOfLabels(
gm_[currentFactor].variableIndex(j))));
144 granteModel_->AddFactorType(
new Grante::FactorType(name, cardinalities, std::vector<double>()));
148 std::vector<unsigned int> cardinalities;
149 for(
IndexType i = 0; i <
gm_.numberOfVariables(); i++) {
150 cardinalities.push_back(
gm_.numberOfLabels(i));
156 for(
size_t i = 0; i < groupedFactorTypes.size(); i++) {
158 std::stringstream ss;
160 std::string
name = ss.str();
162 Grante::FactorType* currentFactorType =
granteModel_->FindFactorType(name);
165 for(
size_t j = 0; j < groupedFactorTypes[i].size(); j++) {
166 OPENGM_ASSERT(groupedFactors[groupedFactorTypes[i][j]].size() > 0);
167 if(groupedFactors[groupedFactorTypes[i][j]].size() == 1) {
169 IndexType currentFactor = groupedFactors[groupedFactorTypes[i][j]][0];
171 std::vector<unsigned int> var_index;
172 for(
IndexType k = 0; k <
gm_[currentFactor].numberOfVariables(); k++) {
173 var_index.push_back(static_cast<unsigned int>(
gm_[currentFactor].variableIndex(k)));
176 std::vector<double> data(currentFactorType->ProdCardinalities());
181 function.forAllValuesInOrder(inserter);
184 Grante::Factor* factor =
new Grante::Factor(currentFactorType, var_index, data);
189 IndexType currentFactor = groupedFactors[groupedFactorTypes[i][j]][0];
191 std::vector<double> data(currentFactorType->ProdCardinalities());
196 function.forAllValuesInOrder(inserter);
197 Grante::FactorDataSource* currentDataSource =
new Grante::FactorDataSource(data);
200 for(
size_t k = 0; k < groupedFactors[groupedFactorTypes[i][j]].size(); k++) {
201 currentFactor = groupedFactors[groupedFactorTypes[i][j]][k];
203 std::vector<unsigned int> var_index;
204 for(
IndexType l = 0; l <
gm_[currentFactor].numberOfVariables(); l++) {
205 var_index.push_back(static_cast<unsigned int>(
gm_[currentFactor].variableIndex(l)));
208 Grante::Factor* factor =
new Grante::Factor(currentFactorType, var_index, currentDataSource);
251 if(granteInferenceMethod_) {
252 delete granteInferenceMethod_;
254 for(
size_t i = 0; i < granteDataSourceCollector_.size(); i++) {
255 delete granteDataSourceCollector_[i];
278 return this->infer(visitor);
282 template<
class VISITOR>
284 visitor.begin(*
this);
285 value_ = granteInferenceMethod_->MinimizeEnergy(granteState_);
292 arg.resize(gm_.numberOfVariables());
293 for(
IndexType i = 0; i < gm_.numberOfVariables(); i++) {
294 arg[i] =
static_cast<LabelType>(granteState_[i]);
313 if(granteState_.size() > 0) {
314 std::vector<LabelType> result;
319 AccumulationType::neutral(temp);
320 return value == temp;
327 groupedFactors.clear();
328 typedef std::map<std::pair<IndexType, std::vector<LabelType> >,
size_t> Map;
330 for(
IndexType i = 0; i < gm_.numberOfFactors(); i++) {
331 IndexType currentFunctionIndex = gm_[i].functionIndex();
332 std::vector<LabelType> currentCardinalities;
333 for(
IndexType j = 0; j < gm_[i].numberOfVariables(); j++) {
334 currentCardinalities.push_back(gm_.numberOfLabels(gm_[i].variableIndex(j)));
336 std::pair<IndexType, std::vector<LabelType> > currentKey(currentFunctionIndex, currentCardinalities);
337 typename Map::const_iterator iter = lookupTable.find(currentKey);
338 if(iter != lookupTable.end()) {
339 groupedFactors[iter->second].push_back(i);
341 std::vector<IndexType> newVec(1, i);
342 groupedFactors.push_back(newVec);
343 lookupTable[currentKey] = groupedFactors.size() - 1;
349 inline void GRANTE<GM>::groupFactorTypes(
const std::vector<std::vector<IndexType> >& groupedFactors, std::vector<std::vector<IndexType> >& groupedFactorTypes)
const {
350 groupedFactorTypes.clear();
351 typedef std::map<std::vector<LabelType>,
size_t > Map;
353 for(
IndexType i = 0; i < groupedFactors.size(); i++) {
354 IndexType currentNumberOfVariables = gm_[groupedFactors[i][0]].numberOfVariables();
355 std::vector<LabelType> currentCardinalities;
356 for(
IndexType j = 0; j < currentNumberOfVariables; j++) {
357 currentCardinalities.push_back(gm_.numberOfLabels(gm_[groupedFactors[i][0]].variableIndex(j)));
359 typename Map::const_iterator iter = lookupTable.find(currentCardinalities);
360 if(iter != lookupTable.end()) {
361 groupedFactorTypes[iter->second].push_back(i);
363 std::vector<IndexType> newVec(1, i);
364 groupedFactorTypes.push_back(newVec);
365 lookupTable[currentCardinalities] = groupedFactorTypes.size() - 1;
Grante::BeliefPropagation::MessageSchedule BPSchedule_
visitors::TimingVisitor< GRANTE< GM > > TimingVisitorType
Grante::InferenceMethod * granteInferenceMethod_
void groupFactors(std::vector< std::vector< IndexType > > &groupedFactors) const
GRANTE GRANTE inference algorithm class.
bool sanityCheck(ValueType value) const
visitors::VerboseVisitor< GRANTE< GM > > VerboseVisitorType
const GraphicalModelType & graphicalModel() const
#define OPENGM_ASSERT(expression)
std::vector< unsigned int > granteState_
InferenceTermination arg(std::vector< LabelType > &, const size_t &=1) const
Grante::FactorGraph * granteGraph_
GraphicalModelType::IndexType IndexType
reference to a Factor of a GraphicalModel
GM::ValueType value() const
return the solution (value)
void groupFactorTypes(const std::vector< std::vector< IndexType > > &groupedFactors, std::vector< std::vector< IndexType > > &groupedFactorTypes) const
GraphicalModelType::ValueType ValueType
static T ineutral()
inverse neutral element (with return)
Inference algorithm interface.
size_t numberOfIterations_
number of iterations for Belief Propagation method
Grante::FactorGraphModel * granteModel_
const GraphicalModelType & gm_
static T neutral()
neutral element (with return)
GRANTE(const GraphicalModelType &gm, const Parameter ¶)
GraphicalModelType::LabelType LabelType
Minimization as a unary accumulation.
void operator()(const T v)
InferenceTermination infer()
std::vector< Grante::FactorDataSource * > granteDataSourceCollector_
InferenceType inferenceType_
opengm::Minimizer AccumulationType
GM::ValueType bound() const
return a bound on the solution
visitors::EmptyVisitor< GRANTE< GM > > EmptyVisitorType