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 \ 34 inline void infer(
const typename INF::GraphicalModelType & gm,
const typename INF::Parameter & param, std::vector<typename INF::LabelType> & conf){
42 template <
class GM,
class ACC>
48 typedef typename GraphicalModelType::LabelType
LabelType;
49 typedef typename GraphicalModelType::IndexType
IndexType;
50 typedef typename GraphicalModelType::ValueType
ValueType;
52 typedef typename GraphicalModelType::FactorType
FactorType;
58 virtual std::string
name()
const = 0;
66 virtual void setStartingPoint(
typename std::vector<LabelType>::const_iterator);
71 virtual ValueType
bound()
const;
72 virtual ValueType
value()
const;
81 template<
class GM,
class ACC>
84 std::vector<LabelType>&
arg,
93 template<
class GM,
class ACC>
96 typename std::vector<LabelType>::const_iterator begin
100 template<
class GM,
class ACC>
103 std::vector<std::vector<LabelType> >& out
112 template<
class GM,
class ACC>
115 const size_t variableIndex,
125 template<
class GM,
class ACC>
128 const size_t factorIndex,
136 template<
class GM,
class ACC>
139 std::vector<IndexType>& variableIndices,
140 std::vector<LabelType>& givenLabels,
141 std::vector<LabelType>& conf
145 std::vector<IndexType> waitingVariables;
146 size_t variableId = 0;
147 size_t numberOfVariables = gm.numberOfVariables();
148 size_t numberOfFixedVariables = 0;
149 conf.assign(gm.numberOfVariables(),std::numeric_limits<LabelType>::max());
151 for(
size_t i=0; i<givenLabels.size() ;++i) {
153 OPENGM_ASSERT( givenLabels[i]<gm.numberOfLabels(variableIndices[i]));
154 conf[variableIndices[i]] = givenLabels[i];
155 waitingVariables.push_back(variableIndices[i]);
156 ++numberOfFixedVariables;
158 while(variableId<gm.numberOfVariables() && numberOfFixedVariables<numberOfVariables) {
159 while(waitingVariables.size()>0 && numberOfFixedVariables<numberOfVariables) {
160 size_t var = waitingVariables.back();
161 waitingVariables.pop_back();
164 for(
size_t i=0; i<gm.numberOfFactors(var); ++i) {
166 size_t afactorId = gm.factorOfVariable(var,i);
167 for(
size_t n=0; n<gm[afactorId].numberOfVariables();++n) {
168 if(conf[gm[afactorId].variableIndex(n)] == std::numeric_limits<LabelType>::max()) {
169 var2=gm[afactorId].variableIndex(n);
177 for(
size_t i=0; i<gm.numberOfFactors(var2); ++i) {
178 size_t factorId = gm.factorOfVariable(var2,i);
179 if(factorId != afactorId)
continue;
180 std::vector<IndexType> knownVariables;
181 std::vector<LabelType> knownStates;
182 std::vector<IndexType> unknownVariables;
188 for(
size_t n=0; n<gm[factorId].numberOfVariables();++n) {
189 if(gm[factorId].variableIndex(n)!=var2) {
190 if(conf[gm[factorId].variableIndex(n)] < std::numeric_limits<LabelType>::max()) {
191 knownVariables.push_back(gm[factorId].variableIndex(n));
192 knownStates.push_back(conf[gm[factorId].variableIndex(n)]);
194 unknownVariables.push_back(gm[factorId].variableIndex(n));
199 out.fixVariables(knownVariables.begin(), knownVariables.end(), knownStates.begin());
200 if(unknownVariables.size()>0)
201 out.template accumulate<AccumulationType>(unknownVariables.begin(),unknownVariables.end());
202 OperatorType::op(out,t);
205 std::vector<LabelType> state(t.numberOfVariables());
206 t.template accumulate<AccumulationType>(
value,state);
207 conf[var2] = state[0];
208 ++numberOfFixedVariables;
209 waitingVariables.push_back(var2);
213 if(conf[variableId]==std::numeric_limits<LabelType>::max()) {
221 std::vector<LabelType> state(out.numberOfVariables());
222 out.template accumulate<AccumulationType>(
value,state);
223 conf[variableId] = state[0];
224 waitingVariables.push_back(variableId);
328 template<
class GM,
class ACC>
331 std::vector<LabelType>& conf
336 size_t numberOfNodes = gm.numberOfVariables();
337 conf.resize(gm.numberOfVariables());
339 for(
size_t node=0; node<numberOfNodes; ++node) {
346 for(
size_t i=1; i<gm.numberOfLabels(node); ++i) {
347 if(ACC::bop(out(i), value)) {
357 template<
class GM,
class ACC>
360 std::vector<LabelType>& conf
364 std::vector<IndexType> knownVariables;
365 std::vector<LabelType> knownStates;
367 for(
size_t node=0; node<gm.numberOfVariables(); ++node) {
375 for(
size_t i=1; i<gm.numberOfLabels(node); ++i) {
381 if(fabs(out(i) - value)<0.00001) {
384 else if(ACC::bop(out(i), value)) {
391 knownVariables.push_back(node);
392 knownStates.push_back(state);
399 template<
class GM,
class ACC>
400 typename GM::ValueType
405 std::vector<LabelType> s;
408 return gm.evaluate(s);
411 return ACC::template neutral<ValueType>();
416 return std::numeric_limits<ValueType>::quiet_NaN();
421 template<
class GM,
class ACC>
422 typename GM::ValueType
426 return ACC::template ineutral<ValueType>();
430 return std::numeric_limits<ValueType>::quiet_NaN();
436 #endif // #ifndef OPENGM_INFERENCE_HXX
virtual std::string name() const =0
InferenceTermination constrainedOptimum(std::vector< IndexType > &, std::vector< LabelType > &, std::vector< LabelType > &) const
virtual ValueType value() const
return the solution (value)
virtual const GraphicalModelType & graphicalModel() const =0
virtual InferenceTermination factorMarginal(const size_t, IndependentFactorType &) const
output a solution for a marginal for all variables connected to a factor
void infer(const typename INF::GraphicalModelType &gm, const typename INF::Parameter ¶m, std::vector< typename INF::LabelType > &conf)
virtual ValueType bound() const
return a bound on the solution
virtual void setStartingPoint(typename std::vector< LabelType >::const_iterator)
set initial labeling
#define OPENGM_ASSERT(expression)
GraphicalModelType::OperatorType OperatorType
InferenceTermination modeFromFactorMarginal(std::vector< LabelType > &) const
InferenceTermination modeFromMarginal(std::vector< LabelType > &) const
virtual InferenceTermination infer()=0
GraphicalModelType::IndexType IndexType
GraphicalModelType::FactorType FactorType
virtual InferenceTermination args(std::vector< std::vector< LabelType > > &) const
GraphicalModelType::ValueType ValueType
Inference algorithm interface.
virtual InferenceTermination arg(std::vector< LabelType > &, const size_t=1) const
output a solution
GraphicalModelType::FunctionIdentifier FunctionIdentifier
virtual InferenceTermination marginal(const size_t, IndependentFactorType &) const
output a solution for a marginal for a specific variable
GraphicalModelType::LabelType LabelType
GraphicalModelType::IndependentFactorType IndependentFactorType