OpenGM  2.3.x
Discrete Graphical Model Library
movemaker.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_MOVEMAKER_HXX
3 #define OPENGM_MOVEMAKER_HXX
4 
5 #include <algorithm>
6 #include <vector>
7 
19 
20 namespace opengm {
21 
23 template<class GM>
24 class Movemaker {
25 public:
26  typedef GM GraphicalModelType;
28  typedef typename std::vector<LabelType>::const_iterator LabelIterator;
30  typedef typename opengm::meta::TypeListGenerator<ViewFunction<GM>, ViewFixVariablesFunction<GM> >::type FunctionTypeList;
31  typedef opengm::VectorViewSpace<IndexType, LabelType> SubGmSpace;
34 
35  Movemaker(const GraphicalModelType&);
36  template<class StateIterator>
37  Movemaker(const GraphicalModelType&, StateIterator);
38  ValueType value() const;
39  template<class IndexIterator, class StateIterator>
40  ValueType valueAfterMove(IndexIterator, IndexIterator, StateIterator);
41  const LabelType& state(const size_t) const;
42  LabelIterator stateBegin() const;
43  LabelIterator stateEnd() const;
44  void reset();
45  template<class StateIterator>
46  void initialize(StateIterator);
47  template<class IndexIterator, class StateIterator>
48  ValueType move(IndexIterator, IndexIterator, StateIterator);
49  template<class ACCUMULATOR, class IndexIterator>
50  ValueType moveOptimally(IndexIterator, IndexIterator);
51  template<class ACCUMULATOR, class IndexIterator>
52  ValueType moveOptimallyWithAllLabelsChanging(IndexIterator, IndexIterator);
53  //template<class ACCUMULATOR, class IndexIterator>
54  //ValueType moveAstarOptimally(IndexIterator, IndexIterator);
55  template<class INFERENCE_TYPE, class INFERENCE_PARAMETER, class INDEX_ITERATOR, class STATE_ITERATOR>
56  void proposeMoveAccordingToInference(const INFERENCE_PARAMETER&, INDEX_ITERATOR, INDEX_ITERATOR, std::vector<LabelType>&)const;
57 
58 private:
59  typedef PositionAndLabel<IndexType, LabelType > PositionAndLabelType;
60  typedef opengm::BufferVector<PositionAndLabelType> PositionAndLabelVector;
61 
63  template<class INDEX_ITERATOR>
64  void addFactorsToSubGm(INDEX_ITERATOR, INDEX_ITERATOR, SubGmType&)const;
66  void addSingleSide(const IndexType, const IndexType, SubGmType &, std::set<IndexType>&)const;
67  void addHigherOrderBorderFactor(const IndexType, const opengm::BufferVector<IndexType>&, const PositionAndLabelVector &, SubGmType &, std::set<IndexType> &)const;
68  void addHigherOrderInsideFactor(const IndexType, const opengm::BufferVector<IndexType>&, SubGmType &, std::set<IndexType> &)const;
69  template<class FactorIndexIterator>
70  ValueType evaluateFactors(FactorIndexIterator, FactorIndexIterator, const std::vector<LabelType>&) const;
71 
72  const GraphicalModelType& gm_;
73  std::vector<std::set<size_t> > factorsOfVariable_;
74  std::vector<LabelType> state_;
75  std::vector<LabelType> stateBuffer_; // always equal to state_ (invariant)
76  ValueType energy_; // energy of state state_ (invariant)
77 };
78 
79 /*
80 template<class GM>
81 template<class ACCUMULATOR, class IndexIterator>
82 inline typename Movemaker<GM>::ValueType
83 Movemaker<GM>::moveAstarOptimally
84 (
85  IndexIterator variableIndicesBegin,
86  IndexIterator variableIndicesEnd
87 ) {
88  typedef opengm::AStar<SubGmType, ACCUMULATOR> SubGmInferenceType;
89  typedef typename SubGmInferenceType::Parameter SubGmInferenceParameterType;
90  SubGmInferenceParameterType para;
91  para.heuristic_ = para.STANDARDHEURISTIC;
92  std::vector<LabelType> states(std::distance(variableIndicesBegin, variableIndicesEnd));
93  this-> template proposeMoveAccordingToInference<
94  SubGmInferenceType, SubGmInferenceParameterType, IndexIterator, typename std::vector<LabelType>::iterator
95  > (para, variableIndicesBegin, variableIndicesEnd, states);
96  return this->move(variableIndicesBegin, variableIndicesEnd, states.begin());
97 }
98 */
99 template<class GM>
100 template<class INFERENCE_TYPE, class INFERENCE_PARAMETER, class INDEX_ITERATOR, class STATE_ITERATOR>
101 inline void
103 (
104  const INFERENCE_PARAMETER& inferenceParam,
105  INDEX_ITERATOR variablesBegin,
106  INDEX_ITERATOR variablesEnd,
107  std::vector<LabelType>& states
108 )const {
109  OPENGM_ASSERT(opengm::isSorted(variablesBegin, variablesEnd));
110  const size_t numberOfVariables = std::distance(variablesBegin, variablesEnd);
111  std::vector<LabelType> spaceVector(numberOfVariables);
112  for (size_t v = 0; v < numberOfVariables; ++v)
113  spaceVector[v] = gm_.numberOfLabels(variablesBegin[v]);
114  SubGmSpace subGmSpace(spaceVector);
115  SubGmType subGm(subGmSpace);
116  this->addFactorsToSubGm(variablesBegin, variablesEnd, subGm);
117  INFERENCE_TYPE subGmInference(subGm, inferenceParam);
118  subGmInference.infer();
119  subGmInference.arg(states);
120 }
121 
122 template<class GM>
124 (
125  const typename Movemaker<GM>::IndexType gmFactorIndex,
126  const typename Movemaker<GM>::IndexType subGmVarIndex,
127  typename Movemaker<GM>::SubGmType & subGm,
128  std::set<typename Movemaker<GM>::IndexType> & addedFactors
129 )const {
130  const size_t var1Index[] = {subGmVarIndex};
131  ViewFunction<GM> function = (gm_[gmFactorIndex]);
132  typename GM::FunctionIdentifier fid = subGm.addFunction(function);
133  subGm.addFactor(fid, var1Index, var1Index + 1);
134  addedFactors.insert(gmFactorIndex);
135 }
136 
137 template<class GM>
138 inline void Movemaker<GM>::addHigherOrderInsideFactor
139 (
140  const typename Movemaker<GM>::IndexType gmFactorIndex,
141  const opengm::BufferVector<typename Movemaker<GM>::IndexType> & subGmFactorVi,
142  typename Movemaker<GM>::SubGmType & subGm,
143  std::set<typename Movemaker<GM>::IndexType> & addedFactors
144 )const {
145  ViewFunction<GM> function(gm_[gmFactorIndex]);
146  typename GM::FunctionIdentifier fid = subGm.addFunction(function);
147  subGm.addFactor(fid, subGmFactorVi.begin(), subGmFactorVi.end());
148  addedFactors.insert(gmFactorIndex);
149 }
150 
151 template<class GM>
152 inline void Movemaker<GM>::addHigherOrderBorderFactor
153 (
154  const typename Movemaker<GM>::IndexType gmFactorIndex,
155  const opengm::BufferVector<typename Movemaker<GM>::IndexType> & subGmFactorVi,
156  const typename Movemaker<GM>::PositionAndLabelVector & factorFixVi,
157  typename Movemaker<GM>::SubGmType & subGm,
158  std::set<typename Movemaker<GM>::IndexType> & addedFactors
159 )const {
160  ViewFixVariablesFunction<GM> function(gm_[gmFactorIndex], factorFixVi);
161  typename GM::FunctionIdentifier fid = subGm.addFunction(function);
162  subGm.addFactor(fid, subGmFactorVi.begin(), subGmFactorVi.end());
163  addedFactors.insert(gmFactorIndex);
164 }
165 
166 template<class GM>
167 template<class INDEX_ITERATOR >
168 inline void Movemaker<GM>::addFactorsToSubGm
169 (
170  INDEX_ITERATOR variablesBegin,
171  INDEX_ITERATOR variablesEnd,
172  typename Movemaker<GM>::SubGmType & subGm
173 )const {
174  std::set<IndexType> addedFactors;
175  opengm::BufferVector<IndexType> subGmFactorVi;
176  opengm::BufferVector<opengm::PositionAndLabel<IndexType, LabelType > >factorFixVi;
177  subGm.reserveFactors(subGm.numberOfVariables()*7);
178  for (IndexType subGmVi = 0; subGmVi < subGm.numberOfVariables(); ++subGmVi) {
179  for (size_t f = 0; f < gm_.numberOfFactors(variablesBegin[subGmVi]); ++f) {
180  const size_t factorIndex = gm_.factorOfVariable(variablesBegin[subGmVi], f);
181  // if the factor has not been added
182  if (addedFactors.find(factorIndex) == addedFactors.end()) {
183  if (gm_[factorIndex].numberOfVariables() == 0) {
184  } else if (gm_[factorIndex].numberOfVariables() == 1)
185  this->addSingleSide(factorIndex, subGmVi, subGm, addedFactors);
186  else {
187  // find if all variables of the factor are in the subgraph or not:
188  subGmFactorVi.clear();
189  factorFixVi.clear();
190  for (IndexType vv = 0; vv < gm_[factorIndex].numberOfVariables(); ++vv) {
191  bool foundVarIndex = false;
192  IndexType varIndexSubGm = 0;
193  foundVarIndex = findInSortedSequence(variablesBegin, subGm.numberOfVariables(), gm_[factorIndex].variableIndex(vv), varIndexSubGm);
194  if (foundVarIndex == false) // variable is outside the subgraph
195  factorFixVi.push_back(opengm::PositionAndLabel<IndexType, LabelType > (vv, this->state(gm_[factorIndex].variableIndex(vv))));
196  else // variable is inside the subgraph
197  subGmFactorVi.push_back(varIndexSubGm);
198  }
199  if (factorFixVi.size() == 0) // all variables are in the subgraph
200  this->addHigherOrderInsideFactor(factorIndex, subGmFactorVi, subGm, addedFactors);
201  else // not all are in the subgraph
202  this->addHigherOrderBorderFactor(factorIndex, subGmFactorVi, factorFixVi, subGm, addedFactors);
203  }
204  }
205  }
206  }
207 }
208 
209 template<class GM>
211 (
212  const GraphicalModelType& gm
213 )
214 : gm_(gm),
215  factorsOfVariable_(gm.numberOfVariables()),
216  state_(gm.numberOfVariables()),
217  stateBuffer_(gm.numberOfVariables()),
218  energy_(gm.evaluate(state_.begin()))
219 {
220  for (size_t f = 0; f < gm.numberOfFactors(); ++f) {
221  for (size_t v = 0; v < gm[f].numberOfVariables(); ++v) {
222  factorsOfVariable_[gm[f].variableIndex(v)].insert(f);
223  }
224  }
225 }
226 
227 template<class GM>
228 template<class StateIterator>
230 (
231  const GraphicalModelType& gm,
232  StateIterator it
233 )
234 : gm_(gm),
235  factorsOfVariable_(gm.numberOfVariables()),
236  state_(gm.numberOfVariables()),
237  stateBuffer_(gm.numberOfVariables()),
238  energy_(gm.evaluate(it)) // fails if *it is out of bounds
239 {
240  for (size_t j = 0; j < gm.numberOfVariables(); ++j, ++it) {
241  state_[j] = *it;
242  stateBuffer_[j] = *it;
243  }
244  for (size_t f = 0; f < gm.numberOfFactors(); ++f) {
245  for (size_t v = 0; v < gm[f].numberOfVariables(); ++v) {
246  factorsOfVariable_[gm[f].variableIndex(v)].insert(f);
247  }
248  }
249 }
250 
251 template<class GM>
252 template<class StateIterator>
254 (
255  StateIterator it
256 ) {
257  energy_ = gm_.evaluate(it); // fails if *it is out of bounds
258  for (size_t j = 0; j < gm_.numberOfVariables(); ++j, ++it) {
259  state_[j] = *it;
260  stateBuffer_[j] = *it;
261  }
262 }
263 
264 template<class GM>
265 void
267  for (size_t j = 0; j < gm_.numberOfVariables(); ++j) {
268  state_[j] = 0;
269  stateBuffer_[j] = 0;
270  }
271  energy_ = gm_.evaluate(state_.begin());
272 }
273 
274 template<class GM>
275 inline typename Movemaker<GM>::ValueType
277  return energy_;
278 }
279 
280 template<class GM>
281 template<class IndexIterator, class StateIterator>
284 (
285  IndexIterator begin,
286  IndexIterator end,
287  StateIterator destinationState
288 ) {
289  ValueType destinationValue;
290  if(meta::Compare<OperatorType, opengm::Multiplier>::value){
291  //Partial update for multiplication is not numrical stabel! That why recalculate the objective
292 
293  // set stateBuffer_ to destinationState, and determine factors to recompute
294  for (IndexIterator it = begin; it != end; ++it, ++destinationState) {
295  stateBuffer_[*it] = *destinationState;
296  }
297  // evaluate destination state
298  destinationValue = gm_.evaluate(stateBuffer_);
299  // restore stateBuffer_
300  for (IndexIterator it = begin; it != end; ++it) {
301  stateBuffer_[*it] = state_[*it];
302  }
303  }else{
304  // do partial update
305 
306  // set stateBuffer_ to destinationState, and determine factors to recompute
307  std::set<size_t> factorsToRecompute;
308  for (IndexIterator it = begin; it != end; ++it, ++destinationState) {
309  OPENGM_ASSERT(*destinationState < gm_.numberOfLabels(*it));
310  if (state_[*it] != *destinationState) {
311  OPENGM_ASSERT(*destinationState < gm_.numberOfLabels(*it));
312  stateBuffer_[*it] = *destinationState;
313  std::set<size_t> tmpSet;
314  std::set_union(factorsToRecompute.begin(), factorsToRecompute.end(),
315  factorsOfVariable_[*it].begin(), factorsOfVariable_[*it].end(),
316  std::inserter(tmpSet, tmpSet.begin()));
317  factorsToRecompute.swap(tmpSet);
318  }
319  }
320  // \todo consider buffering the values of ALL factors at the current state!
321  destinationValue = energy_;
322  for (std::set<size_t>::const_iterator it = factorsToRecompute.begin(); it != factorsToRecompute.end(); ++it) {
323  OPENGM_ASSERT(*it < gm_.numberOfFactors());
324  // determine current and destination state of the current factor
325  std::vector<size_t> currentFactorState(gm_[*it].numberOfVariables());
326  std::vector<size_t> destinationFactorState(gm_[*it].numberOfVariables());
327  for (size_t j = 0; j < gm_[*it].numberOfVariables(); ++j) {
328  currentFactorState[j] = state_[gm_[*it].variableIndex(j)];
329  OPENGM_ASSERT(currentFactorState[j] < gm_[*it].numberOfLabels(j));
330  destinationFactorState[j] = stateBuffer_[gm_[*it].variableIndex(j)];
331  OPENGM_ASSERT(destinationFactorState[j] < gm_[*it].numberOfLabels(j));
332  }
333  OperatorType::op(destinationValue, gm_[*it](destinationFactorState.begin()), destinationValue);
334  OperatorType::iop(destinationValue, gm_[*it](currentFactorState.begin()), destinationValue);
335  }
336  // restore stateBuffer_
337  for (IndexIterator it = begin; it != end; ++it) {
338  stateBuffer_[*it] = state_[*it];
339  }
340  }
341  return destinationValue;
342 }
343 
344 template<class GM>
345 template<class IndexIterator, class StateIterator>
346 inline typename Movemaker<GM>::ValueType
348 (
349  IndexIterator begin,
350  IndexIterator end,
351  StateIterator sit
352 ) {
353  energy_ = valueAfterMove(begin, end, sit); // tests for assertions
354  while (begin != end) {
355  state_[*begin] = *sit;
356  stateBuffer_[*begin] = *sit;
357  ++begin;
358  ++sit;
359  }
360  return energy_;
361 }
362 
363 
368 template<class GM>
369 template<class ACCUMULATOR, class IndexIterator>
370 inline typename Movemaker<GM>::ValueType
372 (
373  IndexIterator variableIndices,
374  IndexIterator variableIndicesEnd
375 ) {
376  // determine factors to recompute
377  std::set<size_t> factorsToRecompute;
378  for (IndexIterator it = variableIndices; it != variableIndicesEnd; ++it) {
379  std::set<size_t> tmpSet;
380  std::set_union(factorsToRecompute.begin(), factorsToRecompute.end(),
381  factorsOfVariable_[*it].begin(), factorsOfVariable_[*it].end(),
382  std::inserter(tmpSet, tmpSet.begin()));
383  factorsToRecompute.swap(tmpSet);
384  }
385 
386  // find an optimal move and the corresponding energy of factors to recompute
387  size_t numberOfVariables = std::distance(variableIndices, variableIndicesEnd);
388  ValueType initialEnergy = evaluateFactors(
389  factorsToRecompute.begin(),
390  factorsToRecompute.end(),
391  state_);
392  ValueType bestEnergy = initialEnergy;
393  std::vector<size_t> bestState(numberOfVariables);
394  for (size_t j=0; j<numberOfVariables; ++j) {
395  const size_t vi = variableIndices[j];
396  stateBuffer_[vi] = 0;
397  }
398  for (;;) {
399  // compute energy
400  ValueType energy = evaluateFactors(
401  factorsToRecompute.begin(),
402  factorsToRecompute.end(),
403  stateBuffer_);
404  if(ACCUMULATOR::bop(energy, bestEnergy)) {
405  // update energy and state
406  bestEnergy = energy;
407  for (size_t j = 0; j < numberOfVariables; ++j) {
408  bestState[j] = stateBuffer_[variableIndices[j]];
409  }
410  }
411  // increment buffered state
412  for (size_t j = 0; j < numberOfVariables; ++j) {
413  const size_t vi = variableIndices[j];
414  if (stateBuffer_[vi] < gm_.numberOfLabels(vi) - 1) {
415  ++stateBuffer_[vi];
416  break;
417  } else {
418  if (j < numberOfVariables - 1) {
419  stateBuffer_[vi] = 0;
420  } else {
421  goto overflow;
422  }
423  }
424  }
425  }
426 overflow:
427  ;
428 
429  if (ACCUMULATOR::bop(bestEnergy, initialEnergy)) {
430  // update state_ and stateBuffer_
431  for (size_t j = 0; j < numberOfVariables; ++j) {
432  const size_t vi = variableIndices[j];
433  state_[vi] = bestState[j];
434  stateBuffer_[vi] = bestState[j];
435  }
436  // update energy
437  if(meta::And<
438  meta::Compare<ACCUMULATOR, opengm::Maximizer>::value,
439  meta::Compare<OperatorType, opengm::Multiplier>::value
440  >::value && energy_ == static_cast<ValueType> (0)) {
441  OPENGM_ASSERT(state_.size() == gm_.numberOfVariables());
442  energy_ = gm_.evaluate(state_.begin());
443  }
444  else {
445  OperatorType::iop(initialEnergy, energy_); // energy_ -= initialEnergy
446  OperatorType::op(bestEnergy, energy_); // energy_ += bestEnergy
447  }
448  } else {
449  // restore stateBuffer_
450  for (size_t j = 0; j < numberOfVariables; ++j) {
451  const size_t vi = variableIndices[j];
452  stateBuffer_[vi] = state_[vi];
453  }
454  }
455 
456  return energy_;
457 }
458 
459 
461 template<class GM>
462 template<class ACCUMULATOR, class IndexIterator>
463 inline typename Movemaker<GM>::ValueType
465 (
466  IndexIterator variableIndices,
467  IndexIterator variableIndicesEnd
468 ) {
469  // determine factors to recompute
470  std::set<size_t> factorsToRecompute;
471  for (IndexIterator it = variableIndices; it != variableIndicesEnd; ++it) {
472  std::set<size_t> tmpSet;
473  std::set_union(factorsToRecompute.begin(), factorsToRecompute.end(),
474  factorsOfVariable_[*it].begin(), factorsOfVariable_[*it].end(),
475  std::inserter(tmpSet, tmpSet.begin()));
476  factorsToRecompute.swap(tmpSet);
477  }
478 
479  // find an optimal move and the corresponding energy of factors to recompute
480  size_t numberOfVariables = std::distance(variableIndices, variableIndicesEnd);
481  ValueType initialEnergy = evaluateFactors(
482  factorsToRecompute.begin(),
483  factorsToRecompute.end(),
484  state_);
485  ValueType bestEnergy = initialEnergy;
486  std::vector<size_t> bestState(numberOfVariables);
487  // set initial labeling
488  for(size_t j=0; j<numberOfVariables; ++j) {
489  if(gm_.space().numberOfLabels(variableIndices[j]) == 1) {
490  // restore stateBuffer_
491  for(size_t k=0; k<j; ++k) {
492  stateBuffer_[k] = state_[k];
493  }
494  return energy_;
495  }
496  else {
497  const size_t vi = variableIndices[j];
498  if(state_[vi] == 0) {
499  stateBuffer_[vi] = 1;
500  }
501  else {
502  stateBuffer_[vi] = 0;
503  }
504  }
505  }
506  for (;;) {
507 # ifndef NDEBUG
508  for(size_t j=0; j<numberOfVariables; ++j) {
509  const size_t vi = variableIndices[j];
510  OPENGM_ASSERT(stateBuffer_[vi] != state_[vi]);
511  }
512 # endif
513  // compute energy
514  ValueType energy = evaluateFactors(
515  factorsToRecompute.begin(),
516  factorsToRecompute.end(),
517  stateBuffer_);
518  if(ACCUMULATOR::bop(energy, bestEnergy)) {
519  // update energy and state
520  bestEnergy = energy;
521  for (size_t j = 0; j < numberOfVariables; ++j) {
522  bestState[j] = stateBuffer_[variableIndices[j]];
523  }
524  }
525  // increment buffered state
526  for (size_t j=0; j<numberOfVariables; ++j) {
527  const size_t vi = variableIndices[j];
528  if(stateBuffer_[vi] < gm_.numberOfLabels(vi) - 1) {
529  if(stateBuffer_[vi] + 1 != state_[vi]) {
530  ++stateBuffer_[vi];
531  break;
532  }
533  else if(stateBuffer_[vi] + 1 < gm_.numberOfLabels(vi) - 1) {
534  stateBuffer_[vi] += 2; // skip current label
535  break;
536  }
537  else {
538  if (j < numberOfVariables - 1) {
539  if(state_[vi] == 0) {
540  stateBuffer_[vi] = 1;
541  }
542  else {
543  stateBuffer_[vi] = 0;
544  }
545  } else {
546  goto overflow2;
547  }
548  }
549  } else {
550  if (j < numberOfVariables - 1) {
551  if(state_[vi] == 0) {
552  stateBuffer_[vi] = 1;
553  }
554  else {
555  stateBuffer_[vi] = 0;
556  }
557  } else {
558  goto overflow2;
559  }
560  }
561  }
562  }
563 overflow2:
564  ;
565 
566  if (ACCUMULATOR::bop(bestEnergy, initialEnergy)) {
567  // update state_ and stateBuffer_
568  for (size_t j = 0; j < numberOfVariables; ++j) {
569  const size_t vi = variableIndices[j];
570  state_[vi] = bestState[j];
571  stateBuffer_[vi] = bestState[j];
572  }
573  // update energy
574  if(meta::And<
575  meta::Compare<ACCUMULATOR, opengm::Maximizer>::value,
576  meta::Compare<OperatorType, opengm::Multiplier>::value
577  >::value && energy_ == static_cast<ValueType> (0)) {
578  energy_ = gm_.evaluate(state_.begin());
579  }
580  else {
581  OperatorType::iop(initialEnergy, energy_); // energy_ -= initialEnergy
582  OperatorType::op(bestEnergy, energy_); // energy_ += bestEnergy
583  }
584  } else {
585  // restore stateBuffer_
586  for (size_t j = 0; j < numberOfVariables; ++j) {
587  const size_t vi = variableIndices[j];
588  stateBuffer_[vi] = state_[vi];
589  }
590  }
591 
592  return energy_;
593 }
594 
595 template<class GM>
596 inline const typename Movemaker<GM>::LabelType&
598 (
599  const size_t variableIndex
600 ) const {
601  OPENGM_ASSERT(variableIndex < state_.size());
602  return state_[variableIndex];
603 }
604 
605 template<class GM>
606 inline typename Movemaker<GM>::LabelIterator
608  return state_.begin();
609 }
610 
611 template<class GM>
612 inline typename Movemaker<GM>::LabelIterator
614  return state_.end();
615 }
616 
617 template<class GM>
618 template<class FactorIndexIterator>
619 inline typename Movemaker<GM>::ValueType
621 (
622  FactorIndexIterator begin,
623  FactorIndexIterator end,
624  const std::vector<LabelType>& state
625 ) const {
626  ValueType value = OperatorType::template neutral<ValueType>();
627  for(; begin != end; ++begin) {
628  std::vector<size_t> currentFactorState(gm_[*begin].numberOfVariables());
629  for (size_t j=0; j<gm_[*begin].numberOfVariables(); ++j) {
630  currentFactorState[j] = state[gm_[*begin].variableIndex(j)];
631  }
632  OperatorType::op(value, gm_[*begin](currentFactorState.begin()), value);
633  }
634  return value;
635 }
636 
637 } // namespace opengm
638 
639 #endif // #ifndef OPENGM_MOVEMAKER_HXX
ValueType value() const
Definition: movemaker.hxx:276
The OpenGM namespace.
Definition: config.hxx:43
const LabelType & state(const size_t) const
Definition: movemaker.hxx:598
void proposeMoveAccordingToInference(const INFERENCE_PARAMETER &, INDEX_ITERATOR, INDEX_ITERATOR, std::vector< LabelType > &) const
Definition: movemaker.hxx:103
ValueType valueAfterMove(IndexIterator, IndexIterator, StateIterator)
Definition: movemaker.hxx:284
std::vector< LabelType >::const_iterator LabelIterator
Definition: movemaker.hxx:28
Movemaker(const GraphicalModelType &)
Definition: movemaker.hxx:211
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
LabelIterator stateEnd() const
Definition: movemaker.hxx:613
ValueType moveOptimallyWithAllLabelsChanging(IndexIterator, IndexIterator)
Definition: movemaker.hxx:465
A fremework for move making algorithms.
Definition: bruteforce.hxx:10
ValueType moveOptimally(IndexIterator, IndexIterator)
for a subset of variables, move to a labeling that is optimal w.r.t. ACCUMULATOR
Definition: movemaker.hxx:372
void initialize(StateIterator)
Definition: movemaker.hxx:254
LabelIterator stateBegin() const
Definition: movemaker.hxx:607
Funcion that refers to a factor of another GraphicalModel in which some variables are fixed...
ValueType move(IndexIterator, IndexIterator, StateIterator)
Definition: movemaker.hxx:348