2 #ifndef OPENGM_INFERENCE_HXX
3 #define OPENGM_INFERENCE_HXX
13 #define OPENGM_GM_TYPE_TYPEDEFS \
14 typedef typename GraphicalModelType::LabelType LabelType; \
15 typedef typename GraphicalModelType::IndexType IndexType; \
16 typedef typename GraphicalModelType::ValueType ValueType; \
17 typedef typename GraphicalModelType::OperatorType OperatorType; \
18 typedef typename GraphicalModelType::FactorType FactorType; \
19 typedef typename GraphicalModelType::IndependentFactorType IndependentFactorType; \
20 typedef typename GraphicalModelType::FunctionIdentifier FunctionIdentifier \
33 template <
class GM,
class ACC>
39 typedef typename GraphicalModelType::LabelType
LabelType;
40 typedef typename GraphicalModelType::IndexType
IndexType;
41 typedef typename GraphicalModelType::ValueType
ValueType;
43 typedef typename GraphicalModelType::FactorType
FactorType;
49 virtual std::string
name()
const = 0;
57 virtual void setStartingPoint(
typename std::vector<LabelType>::const_iterator);
62 virtual ValueType
bound()
const;
63 virtual ValueType
value()
const;
72 template<
class GM,
class ACC>
75 std::vector<LabelType>& arg,
84 template<
class GM,
class ACC>
87 typename std::vector<LabelType>::const_iterator begin
91 template<
class GM,
class ACC>
94 std::vector<std::vector<LabelType> >& out
103 template<
class GM,
class ACC>
106 const size_t variableIndex,
116 template<
class GM,
class ACC>
119 const size_t factorIndex,
127 template<
class GM,
class ACC>
130 std::vector<IndexType>& variableIndices,
131 std::vector<LabelType>& givenLabels,
132 std::vector<LabelType>& conf
135 const GM& gm = graphicalModel();
136 std::vector<IndexType> waitingVariables;
137 size_t variableId = 0;
138 size_t numberOfVariables = gm.numberOfVariables();
139 size_t numberOfFixedVariables = 0;
140 conf.assign(gm.numberOfVariables(),std::numeric_limits<LabelType>::max());
142 for(
size_t i=0; i<givenLabels.size() ;++i) {
144 OPENGM_ASSERT( givenLabels[i]<gm.numberOfLabels(variableIndices[i]));
145 conf[variableIndices[i]] = givenLabels[i];
146 waitingVariables.push_back(variableIndices[i]);
147 ++numberOfFixedVariables;
149 while(variableId<gm.numberOfVariables() && numberOfFixedVariables<numberOfVariables) {
150 while(waitingVariables.size()>0 && numberOfFixedVariables<numberOfVariables) {
151 size_t var = waitingVariables.back();
152 waitingVariables.pop_back();
155 for(
size_t i=0; i<gm.numberOfFactors(var); ++i) {
157 size_t afactorId = gm.factorOfVariable(var,i);
158 for(
size_t n=0; n<gm[afactorId].numberOfVariables();++n) {
159 if(conf[gm[afactorId].variableIndex(n)] == std::numeric_limits<LabelType>::max()) {
160 var2=gm[afactorId].variableIndex(n);
168 for(
size_t i=0; i<gm.numberOfFactors(var2); ++i) {
169 size_t factorId = gm.factorOfVariable(var2,i);
170 if(factorId != afactorId)
continue;
171 std::vector<IndexType> knownVariables;
172 std::vector<LabelType> knownStates;
173 std::vector<IndexType> unknownVariables;
179 for(
size_t n=0; n<gm[factorId].numberOfVariables();++n) {
180 if(gm[factorId].variableIndex(n)!=var2) {
181 if(conf[gm[factorId].variableIndex(n)] < std::numeric_limits<LabelType>::max()) {
182 knownVariables.push_back(gm[factorId].variableIndex(n));
183 knownStates.push_back(conf[gm[factorId].variableIndex(n)]);
185 unknownVariables.push_back(gm[factorId].variableIndex(n));
190 out.fixVariables(knownVariables.begin(), knownVariables.end(), knownStates.begin());
191 if(unknownVariables.size()>0)
192 out.template accumulate<AccumulationType>(unknownVariables.begin(),unknownVariables.end());
193 OperatorType::op(out,t);
196 std::vector<LabelType> state(t.numberOfVariables());
197 t.template accumulate<AccumulationType>(value,state);
198 conf[var2] = state[0];
199 ++numberOfFixedVariables;
200 waitingVariables.push_back(var2);
204 if(conf[variableId]==std::numeric_limits<LabelType>::max()) {
212 std::vector<LabelType> state(out.numberOfVariables());
213 out.template accumulate<AccumulationType>(value,state);
214 conf[variableId] = state[0];
215 waitingVariables.push_back(variableId);
319 template<
class GM,
class ACC>
322 std::vector<LabelType>& conf
325 const GM& gm = graphicalModel();
327 size_t numberOfNodes = gm.numberOfVariables();
328 conf.resize(gm.numberOfVariables());
330 for(
size_t node=0; node<numberOfNodes; ++node) {
337 for(
size_t i=1; i<gm.numberOfLabels(node); ++i) {
338 if(ACC::bop(out(i), value)) {
348 template<
class GM,
class ACC>
351 std::vector<LabelType>& conf
354 const GM& gm = graphicalModel();
355 std::vector<IndexType> knownVariables;
356 std::vector<LabelType> knownStates;
358 for(
size_t node=0; node<gm.numberOfVariables(); ++node) {
366 for(
size_t i=1; i<gm.numberOfLabels(node); ++i) {
372 if(fabs(out(i) - value)<0.00001) {
375 else if(ACC::bop(out(i), value)) {
382 knownVariables.push_back(node);
383 knownStates.push_back(state);
386 return constrainedOptimum( knownVariables, knownStates, conf);
390 template<
class GM,
class ACC>
391 typename GM::ValueType
396 std::vector<LabelType> s;
397 const GM& gm = graphicalModel();
399 return gm.evaluate(s);
402 return ACC::template neutral<ValueType>();
407 return std::numeric_limits<ValueType>::quiet_NaN();
412 template<
class GM,
class ACC>
413 typename GM::ValueType
417 return ACC::template ineutral<ValueType>();
421 return std::numeric_limits<ValueType>::quiet_NaN();
427 #endif // #ifndef OPENGM_INFERENCE_HXX
virtual std::string name() const =0
virtual InferenceTermination arg(std::vector< LabelType > &, const size_t=1) const
output a solution
virtual const GraphicalModelType & graphicalModel() const =0
virtual void setStartingPoint(typename std::vector< LabelType >::const_iterator)
set initial labeling
#define OPENGM_ASSERT(expression)
GraphicalModelType::OperatorType OperatorType
virtual InferenceTermination args(std::vector< std::vector< LabelType > > &) const
virtual InferenceTermination infer()=0
GraphicalModelType::IndexType IndexType
GraphicalModelType::FactorType FactorType
virtual ValueType bound() const
return a bound on the solution
GraphicalModelType::ValueType ValueType
virtual InferenceTermination marginal(const size_t, IndependentFactorType &) const
output a solution for a marginal for a specific variable
Inference algorithm interface.
GraphicalModelType::FunctionIdentifier FunctionIdentifier
virtual ValueType value() const
return the solution (value)
InferenceTermination constrainedOptimum(std::vector< IndexType > &, std::vector< LabelType > &, std::vector< LabelType > &) const
virtual InferenceTermination factorMarginal(const size_t, IndependentFactorType &) const
output a solution for a marginal for all variables connected to a factor
GraphicalModelType::LabelType LabelType
InferenceTermination modeFromMarginal(std::vector< LabelType > &) const
InferenceTermination modeFromFactorMarginal(std::vector< LabelType > &) const
GraphicalModelType::IndependentFactorType IndependentFactorType