2 #ifndef OPENGM_GIBBS_HXX
3 #define OPENGM_GIBBS_HXX
27 namespace detail_gibbs {
29 template<
class OPERATOR,
class ACCUMULATOR,
class PROBABILITY>
30 struct ValuePairToProbability;
32 template<
class PROBABILITY>
33 struct ValuePairToProbability<Multiplier, Maximizer, PROBABILITY>
35 typedef PROBABILITY ProbabilityType;
37 static ProbabilityType convert(
const T newValue,
const T oldValue)
38 {
return static_cast<ProbabilityType
>(newValue) / static_cast<ProbabilityType>(oldValue); }
41 template<
class PROBABILITY>
42 struct ValuePairToProbability<Adder, Minimizer, PROBABILITY>
44 typedef PROBABILITY ProbabilityType;
46 static ProbabilityType convert(
const T newValue,
const T oldValue)
47 {
return static_cast<ProbabilityType
>(std::exp(oldValue - newValue)); }
66 void assign(
const GraphicalModelType&);
69 template<
class VariableIndexIterator>
70 size_t addMarginal(VariableIndexIterator, VariableIndexIterator);
72 void operator()(
const GibbsType&,
const ValueType,
const ValueType,
const size_t,
const bool,
const bool);
75 void begin(
const GibbsType&,
const ValueType,
const ValueType)
const {}
76 void end(
const GibbsType&,
const ValueType,
const ValueType)
const {}
81 const IndependentFactorType&
marginal(
const size_t)
const;
84 const GraphicalModelType* gm_;
85 size_t numberOfSamples_;
86 size_t numberOfAcceptedSamples_;
87 size_t numberOfRejectedSamples_;
88 std::vector<IndependentFactorType> marginals_;
89 std::vector<size_t> stateCache_;
94 template<
class GM,
class ACC>
112 const size_t maxNumberOfSamplingSteps = 1e5,
113 const size_t numberOfBurnInSteps = 1e5,
114 const bool useTemp=
false,
119 const std::vector<size_t>& startPoint = std::vector<size_t>()
145 std::string
name()
const;
149 template<
class VISITOR>
161 return static_cast<ValueType>(((std::cos(arg/periode)+1.0)/2.0)*(max-min))+min;
165 ValueType getTemperature(
const size_t step)
const{
167 static_cast<ValueType>(step),
173 Parameter parameter_;
174 const GraphicalModelType& gm_;
175 MovemakerType movemaker_;
176 std::vector<size_t> currentBestState_;
181 template<
class GM,
class ACC>
185 const GraphicalModelType& gm,
188 : parameter_(parameter),
191 currentBestState_(gm.numberOfVariables()),
195 ACC::ineutral(currentBestValue_);
197 if(parameter.
startPoint_.size() == gm.numberOfVariables()) {
198 movemaker_.initialize(parameter.
startPoint_.begin());
200 currentBestValue_ = movemaker_.value();
203 throw RuntimeError(
"parameter.startPoint_.size() is neither zero nor equal to the number of variables.");
208 template<
class GM,
class ACC>
211 if(parameter_.startPoint_.size() != 0) {
212 if(parameter_.startPoint_.size() == gm_.numberOfVariables()) {
213 movemaker_.initialize(parameter_.startPoint_.begin());
214 currentBestState_ = parameter_.startPoint_;
215 currentBestValue_ = movemaker_.value();
218 throw RuntimeError(
"parameter.startPoint_.size() is neither zero nor equal to the number of variables.");
223 std::fill(currentBestState_.begin(), currentBestState_.end(), 0);
227 template<
class GM,
class ACC>
234 movemaker_.initialize(begin);
236 for(
IndexType vi=0;vi<static_cast<IndexType>(gm_.numberOfVariables());++vi ){
237 currentBestState_[vi]=movemaker_.state(vi);
239 currentBestValue_ = movemaker_.value();
247 template<
class GM,
class ACC>
254 template<
class GM,
class ACC>
261 template<
class GM,
class ACC>
265 EmptyVisitorType visitor;
266 return infer(visitor);
269 template<
class GM,
class ACC>
270 template<
class VISITOR>
275 visitor.begin(*
this);
276 opengm::RandomUniform<size_t> randomVariable(0, gm_.numberOfVariables());
277 opengm::RandomUniform<ProbabilityType> randomProb(0, 1);
279 if(parameter_.useTemp_==
false){
280 for(
size_t iteration = 0; iteration < parameter_.maxNumberOfSamplingSteps_ + parameter_.numberOfBurnInSteps_; ++iteration) {
282 size_t variableIndex = 0;
284 variableIndex = randomVariable();
287 variableIndex < gm_.numberOfVariables() - 1 ? ++variableIndex : variableIndex = 0;
291 opengm::RandomUniform<size_t> randomLabel(0, gm_.numberOfLabels(variableIndex));
292 const size_t label = randomLabel();
295 const bool burningIn = (iteration < parameter_.numberOfBurnInSteps_);
296 if(label != movemaker_.state(variableIndex)) {
297 const ValueType oldValue = movemaker_.value();
298 const ValueType newValue = movemaker_.valueAfterMove(&variableIndex, &variableIndex + 1, &label);
299 if(AccumulationType::bop(newValue, oldValue)) {
300 movemaker_.move(&variableIndex, &variableIndex + 1, &label);
301 if(AccumulationType::bop(newValue, currentBestValue_) && newValue != currentBestValue_) {
302 currentBestValue_ = newValue;
303 for(
size_t k = 0; k < currentBestState_.size(); ++k) {
304 currentBestState_[k] = movemaker_.state(k);
311 const ProbabilityType pFlip =
312 detail_gibbs::ValuePairToProbability<
314 >::convert(newValue, oldValue);
315 if(randomProb() < pFlip) {
316 movemaker_.move(&variableIndex, &variableIndex + 1, &label);
330 for(
size_t iteration = 0; iteration < parameter_.maxNumberOfSamplingSteps_ + parameter_.numberOfBurnInSteps_; ++iteration) {
332 size_t variableIndex = 0;
334 variableIndex = randomVariable();
337 variableIndex < gm_.numberOfVariables() - 1 ? ++variableIndex : variableIndex = 0;
341 opengm::RandomUniform<size_t> randomLabel(0, gm_.numberOfLabels(variableIndex));
342 const size_t label = randomLabel();
345 const bool burningIn = (iteration < parameter_.numberOfBurnInSteps_);
346 if(label != movemaker_.state(variableIndex)) {
347 const ValueType oldValue = movemaker_.value();
348 const ValueType newValue = movemaker_.valueAfterMove(&variableIndex, &variableIndex + 1, &label);
349 if(AccumulationType::bop(newValue, oldValue)) {
350 movemaker_.move(&variableIndex, &variableIndex + 1, &label);
351 if(AccumulationType::bop(newValue, currentBestValue_) && newValue != currentBestValue_) {
352 currentBestValue_ = newValue;
353 for(
size_t k = 0; k < currentBestState_.size(); ++k) {
354 currentBestState_[k] = movemaker_.state(k);
361 const ProbabilityType pFlip =
362 detail_gibbs::ValuePairToProbability<
364 >::convert(newValue, oldValue);
365 if(randomProb() < pFlip*this->getTemperature(iteration)){
367 movemaker_.move(&variableIndex, &variableIndex + 1, &label);
387 template<
class GM,
class ACC>
391 std::vector<LabelType>& x,
395 x.resize(gm_.numberOfVariables());
396 for(
size_t j = 0; j < x.size(); ++j) {
398 x[j] = currentBestState_[j];
400 x[j] = movemaker_.state(j);
410 template<
class GM,
class ACC>
418 return movemaker_.state(j);
421 template<
class GM,
class ACC>
425 return movemaker_.value();
428 template<
class GM,
class ACC>
436 return currentBestState_[j];
439 template<
class GM,
class ACC>
443 return currentBestValue_;
446 template<
class GIBBS>
451 numberOfAcceptedSamples_(0),
452 numberOfRejectedSamples_(0),
457 template<
class GIBBS>
464 numberOfAcceptedSamples_(0),
465 numberOfRejectedSamples_(0),
470 template<
class GIBBS>
479 template<
class GIBBS>
485 const size_t iteration,
492 ++numberOfAcceptedSamples_;
495 ++numberOfRejectedSamples_;
497 for(
size_t j = 0; j < marginals_.size(); ++j) {
498 for(
size_t k = 0; k < marginals_[j].numberOfVariables(); ++k) {
499 stateCache_[k] = gibbs.markovState(marginals_[j].variableIndex(k));
501 ++marginals_[j](stateCache_.begin());
506 template<
class GIBBS>
507 template<
class VariableIndexIterator>
510 VariableIndexIterator begin,
511 VariableIndexIterator end
514 if(marginals_.back().numberOfVariables() > stateCache_.size()) {
515 stateCache_.resize(marginals_.back().numberOfVariables());
517 return marginals_.size() - 1;
520 template<
class GIBBS>
523 const size_t variableIndex
525 size_t variableIndices[] = {variableIndex};
526 return addMarginal(variableIndices, variableIndices + 1);
529 template<
class GIBBS>
532 return numberOfSamples_;
535 template<
class GIBBS>
538 return numberOfAcceptedSamples_;
541 template<
class GIBBS>
544 return numberOfRejectedSamples_;
547 template<
class GIBBS>
550 return marginals_.size();
553 template<
class GIBBS>
556 const size_t setIndex
558 return marginals_[setIndex];
563 #endif // #ifndef OPENGM_GIBBS_HXX
VariableProposal variableProposal_
size_t numberOfAcceptedSamples() const
size_t numberOfMarginals() const
void operator()(const GibbsType &, const ValueType, const ValueType, const size_t, const bool, const bool)
visitors::EmptyVisitor< Gibbs< GM, ACC > > EmptyVisitorType
GraphicalModelType::IndependentFactorType IndependentFactorType
size_t addMarginal(VariableIndexIterator, VariableIndexIterator)
GibbsType::ValueType ValueType
virtual InferenceTermination arg(std::vector< LabelType > &, const size_t=1) const
output a solution
GibbsType::GraphicalModelType GraphicalModelType
InferenceTermination infer()
#define OPENGM_ASSERT(expression)
GraphicalModelType::OperatorType OperatorType
size_t maxNumberOfSamplingSteps_
LabelType markovState(const size_t) const
GraphicalModelType::IndexType IndexType
Movemaker< GraphicalModelType > MovemakerType
const GraphicalModelType & graphicalModel() const
ValueType markovValue() const
size_t numberOfSamples() const
GraphicalModelType::ValueType ValueType
visitors::VerboseVisitor< Gibbs< GM, ACC > > VerboseVisitorType
Inference algorithm interface.
std::vector< size_t > startPoint_
ValueType currentBestValue() const
LabelType currentBestState(const size_t) const
size_t numberOfRejectedSamples() const
Gibbs(const GraphicalModelType &, const Parameter ¶m=Parameter())
visitors::TimingVisitor< Gibbs< GM, ACC > > TimingVisitorType
const IndependentFactorType & marginal(const size_t) const
size_t numberOfBurnInSteps_
void end(const GibbsType &, const ValueType, const ValueType) const
GraphicalModelType::LabelType LabelType
Parameter(const size_t maxNumberOfSamplingSteps=1e5, const size_t numberOfBurnInSteps=1e5, const bool useTemp=false, const ValueType tmin=0.0001, const ValueType tmax=1, const IndexType periods=10, const VariableProposal variableProposal=RANDOM, const std::vector< size_t > &startPoint=std::vector< size_t >())
void begin(const GibbsType &, const ValueType, const ValueType) const
void assign(const GraphicalModelType &)
void setStartingPoint(typename std::vector< LabelType >::const_iterator)
set initial labeling
Visitor for the Gibbs sampler to compute arbitrary marginal probabilities.