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