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