This file is indexed.

/usr/include/opengm/inference/gibbs.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
#pragma once
#ifndef OPENGM_GIBBS_HXX
#define OPENGM_GIBBS_HXX

#include <vector>
#include <string>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <typeinfo>

#include "opengm/opengm.hxx"
#include "opengm/utilities/random.hxx"
#include "opengm/inference/inference.hxx"
#include "opengm/inference/movemaker.hxx"
#include "opengm/operations/minimizer.hxx"
#include "opengm/operations/maximizer.hxx"
#include "opengm/operations/adder.hxx"
#include "opengm/operations/multiplier.hxx"
#include "opengm/operations/integrator.hxx"
#include "opengm/inference/visitors/visitors.hxx"

namespace opengm {

/// \cond HIDDEN_SYMBOLS
namespace detail_gibbs {

   template<class OPERATOR, class ACCUMULATOR, class PROBABILITY>
   struct ValuePairToProbability;

   template<class PROBABILITY>
   struct ValuePairToProbability<Multiplier, Maximizer, PROBABILITY>
   {
      typedef PROBABILITY ProbabilityType;
      template<class T>
         static ProbabilityType convert(const T newValue, const T oldValue)
            { return static_cast<ProbabilityType>(newValue) / static_cast<ProbabilityType>(oldValue); }
   };

   template<class PROBABILITY>
   struct ValuePairToProbability<Adder, Minimizer, PROBABILITY>
   {
      typedef PROBABILITY ProbabilityType;
      template<class T>
         static ProbabilityType convert(const T newValue, const T oldValue)
            { return static_cast<ProbabilityType>(std::exp(oldValue - newValue)); }
   };
}
/// \endcond

/// \brief Visitor for the Gibbs sampler to compute arbitrary marginal probabilities
///
/// \ingroup inference
template<class GIBBS>
class GibbsMarginalVisitor {
public:
   typedef GIBBS GibbsType;
   typedef typename GibbsType::ValueType ValueType;
   typedef typename GibbsType::GraphicalModelType GraphicalModelType;
   typedef typename GraphicalModelType::IndependentFactorType IndependentFactorType;

   // construction
   GibbsMarginalVisitor();
   GibbsMarginalVisitor(const GraphicalModelType&);
   void assign(const GraphicalModelType&);

   // manipulation
   template<class VariableIndexIterator>
      size_t addMarginal(VariableIndexIterator, VariableIndexIterator);
   size_t addMarginal(const size_t);
   void operator()(const GibbsType&, const ValueType, const ValueType, const size_t, const bool, const bool);

   // query
   void begin(const GibbsType&, const ValueType, const ValueType) const {}
   void end(const GibbsType&, const ValueType, const ValueType) const {}
   size_t numberOfSamples() const;
   size_t numberOfAcceptedSamples() const;
   size_t numberOfRejectedSamples() const;
   size_t numberOfMarginals() const;
   const IndependentFactorType& marginal(const size_t) const;

private:
   const GraphicalModelType* gm_;
   size_t numberOfSamples_;
   size_t numberOfAcceptedSamples_;
   size_t numberOfRejectedSamples_;
   std::vector<IndependentFactorType> marginals_;
   std::vector<size_t> stateCache_;

};

/// \brief Gibbs sampling
template<class GM, class ACC>
class Gibbs 
: public Inference<GM, ACC> {
public:
   typedef ACC AccumulationType;
   typedef GM GraphicalModelType;
   OPENGM_GM_TYPE_TYPEDEFS;
   typedef Movemaker<GraphicalModelType> MovemakerType;
   typedef visitors::VerboseVisitor<Gibbs<GM, ACC> > VerboseVisitorType;
   typedef visitors::EmptyVisitor<Gibbs<GM, ACC> > EmptyVisitorType;
   typedef visitors::TimingVisitor<Gibbs<GM, ACC> > TimingVisitorType;
   typedef double ProbabilityType;

   class Parameter {
   public:
      enum VariableProposal {RANDOM, CYCLIC};

      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>()
      )
      :  maxNumberOfSamplingSteps_(maxNumberOfSamplingSteps), 
         numberOfBurnInSteps_(numberOfBurnInSteps), 
         variableProposal_(variableProposal),
         startPoint_(startPoint),
         useTemp_(useTemp),
         tempMin_(tmin),
         tempMax_(tmax),
         periods_(periods){
         p_=static_cast<ValueType>(maxNumberOfSamplingSteps_/periods_);
      }
      bool useTemp_;
      ValueType tempMin_;
      ValueType tempMax_;
      size_t periods_;
      ValueType p_;
      size_t maxNumberOfSamplingSteps_;
      size_t numberOfBurnInSteps_;
      VariableProposal variableProposal_;
      std::vector<size_t> startPoint_;
      
      
   };

   Gibbs(const GraphicalModelType&, const Parameter& param = Parameter());
   std::string name() const;
   const GraphicalModelType& graphicalModel() const;
   void reset();
   InferenceTermination infer();
   template<class VISITOR>
      InferenceTermination infer(VISITOR&);
   void setStartingPoint(typename std::vector<LabelType>::const_iterator);
   virtual InferenceTermination arg(std::vector<LabelType>&, const size_t = 1) const;

   LabelType markovState(const size_t) const;
   ValueType markovValue() const;
   LabelType currentBestState(const size_t) const;
   ValueType currentBestValue() const;

private:
   ValueType cosTemp(const ValueType arg,const ValueType periode,const ValueType min,const ValueType max)const{
      return static_cast<ValueType>(((std::cos(arg/periode)+1.0)/2.0)*(max-min))+min;
      //if(v<
   }

   ValueType getTemperature(const size_t step)const{
      return cosTemp( 
         static_cast<ValueType>(step),
         parameter_.p_,
         parameter_.tempMin_,
         parameter_.tempMax_
      );
   }
   Parameter parameter_;
   const GraphicalModelType& gm_;
   MovemakerType movemaker_;
   std::vector<size_t> currentBestState_;
   ValueType currentBestValue_;
   bool inInference_;
};

template<class GM, class ACC>
inline
Gibbs<GM, ACC>::Gibbs
(
   const GraphicalModelType& gm, 
   const Parameter& parameter
)
:  parameter_(parameter), 
   gm_(gm), 
   movemaker_(gm), 
   currentBestState_(gm.numberOfVariables()),
   currentBestValue_()
{
   inInference_=false;
   ACC::ineutral(currentBestValue_);
   if(parameter.startPoint_.size() != 0) {
      if(parameter.startPoint_.size() == gm.numberOfVariables()) {
         movemaker_.initialize(parameter.startPoint_.begin());
         currentBestState_ = parameter.startPoint_;
         currentBestValue_ = movemaker_.value();
      }
      else {
         throw RuntimeError("parameter.startPoint_.size() is neither zero nor equal to the number of variables.");
      }
   }
}

template<class GM, class ACC>
inline void
Gibbs<GM, ACC>::reset() {
   if(parameter_.startPoint_.size() != 0) {
      if(parameter_.startPoint_.size() == gm_.numberOfVariables()) {
         movemaker_.initialize(parameter_.startPoint_.begin());
         currentBestState_ = parameter_.startPoint_;
         currentBestValue_ = movemaker_.value();
      }
      else {
         throw RuntimeError("parameter.startPoint_.size() is neither zero nor equal to the number of variables.");
      }
   }
   else {
     movemaker_.reset();
     std::fill(currentBestState_.begin(), currentBestState_.end(), 0);
   }
}

template<class GM, class ACC>
inline void
Gibbs<GM, ACC>::setStartingPoint
(
   typename std::vector<typename Gibbs<GM, ACC>::LabelType>::const_iterator begin
) {
   try{
      movemaker_.initialize(begin);

      for(IndexType vi=0;vi<static_cast<IndexType>(gm_.numberOfVariables());++vi ){
         currentBestState_[vi]=movemaker_.state(vi);
      }
      currentBestValue_ = movemaker_.value();

   }
   catch(...) {
      throw RuntimeError("unsuitable starting point");
   }
}

template<class GM, class ACC>
inline std::string
Gibbs<GM, ACC>::name() const
{
   return "Gibbs";
}

template<class GM, class ACC>
inline const typename Gibbs<GM, ACC>::GraphicalModelType&
Gibbs<GM, ACC>::graphicalModel() const
{
   return gm_;
}

template<class GM, class ACC>
inline InferenceTermination
Gibbs<GM, ACC>::infer()
{
   EmptyVisitorType visitor;
   return infer(visitor);
}

template<class GM, class ACC>
template<class VISITOR>
InferenceTermination Gibbs<GM, ACC>::infer(
   VISITOR& visitor
) {
   inInference_=true;
   visitor.begin(*this);
   opengm::RandomUniform<size_t> randomVariable(0, gm_.numberOfVariables());
   opengm::RandomUniform<ProbabilityType> randomProb(0, 1);
   
   if(parameter_.useTemp_==false){
      for(size_t iteration = 0; iteration < parameter_.maxNumberOfSamplingSteps_ + parameter_.numberOfBurnInSteps_; ++iteration) {
         // select variable
         size_t variableIndex = 0;
         if(this->parameter_.variableProposal_ == Parameter::RANDOM) {
            variableIndex = randomVariable();
         }
         else if(this->parameter_.variableProposal_ == Parameter::CYCLIC) {
            variableIndex < gm_.numberOfVariables() - 1 ? ++variableIndex : variableIndex = 0;
         }

         // draw label
         opengm::RandomUniform<size_t> randomLabel(0, gm_.numberOfLabels(variableIndex));
         const size_t label = randomLabel();

         // move
         const bool burningIn = (iteration < parameter_.numberOfBurnInSteps_);
         if(label != movemaker_.state(variableIndex)) {
            const ValueType oldValue = movemaker_.value();
            const ValueType newValue = movemaker_.valueAfterMove(&variableIndex, &variableIndex + 1, &label);
            if(AccumulationType::bop(newValue, oldValue)) {
               movemaker_.move(&variableIndex, &variableIndex + 1, &label);
               if(AccumulationType::bop(newValue, currentBestValue_) && newValue != currentBestValue_) {
                  currentBestValue_ = newValue;
                  for(size_t k = 0; k < currentBestState_.size(); ++k) {
                     currentBestState_[k] = movemaker_.state(k);
                  }
               }
               visitor(*this);
               //visitor(*this, newValue, currentBestValue_, iteration, true, burningIn);
            }
            else {
               const ProbabilityType pFlip =
                  detail_gibbs::ValuePairToProbability<
                     OperatorType, AccumulationType, ProbabilityType
                  >::convert(newValue, oldValue);
               if(randomProb() < pFlip) {
                  movemaker_.move(&variableIndex, &variableIndex + 1, &label); 
                  visitor(*this);
                  //visitor(*this, newValue, currentBestValue_, iteration, true, burningIn);
               }
               else {
                  visitor(*this);
                 // visitor(*this, newValue, currentBestValue_, iteration, false, burningIn);
               }
            }
            ++iteration;
         }
      }
   }
   else {
      for(size_t iteration = 0; iteration < parameter_.maxNumberOfSamplingSteps_ + parameter_.numberOfBurnInSteps_; ++iteration) {
         // select variable
         size_t variableIndex = 0;
         if(this->parameter_.variableProposal_ == Parameter::RANDOM) {
            variableIndex = randomVariable();
         }
         else if(this->parameter_.variableProposal_ == Parameter::CYCLIC) {
            variableIndex < gm_.numberOfVariables() - 1 ? ++variableIndex : variableIndex = 0;
         }

         // draw label
         opengm::RandomUniform<size_t> randomLabel(0, gm_.numberOfLabels(variableIndex));
         const size_t label = randomLabel();

         // move
         const bool burningIn = (iteration < parameter_.numberOfBurnInSteps_);
         if(label != movemaker_.state(variableIndex)) {
            const ValueType oldValue = movemaker_.value();
            const ValueType newValue = movemaker_.valueAfterMove(&variableIndex, &variableIndex + 1, &label);
            if(AccumulationType::bop(newValue, oldValue)) {
               movemaker_.move(&variableIndex, &variableIndex + 1, &label);
               if(AccumulationType::bop(newValue, currentBestValue_) && newValue != currentBestValue_) {
                  currentBestValue_ = newValue;
                  for(size_t k = 0; k < currentBestState_.size(); ++k) {
                     currentBestState_[k] = movemaker_.state(k);
                  }
               }
               visitor(*this);
               //visitor(*this, newValue, currentBestValue_, iteration, true, burningIn);
            }
            else {
               const ProbabilityType pFlip =
                  detail_gibbs::ValuePairToProbability<
                     OperatorType, AccumulationType, ProbabilityType
                  >::convert(newValue, oldValue);
               if(randomProb() < pFlip*this->getTemperature(iteration)){
                  //std::cout<<"temp="<<this->getTemperature(iteration)<<"\n";
                  movemaker_.move(&variableIndex, &variableIndex + 1, &label); 
                  visitor(*this);
                  //visitor(*this, newValue, currentBestValue_, iteration, true, burningIn);
               }
               else {
                  //std::cout<<"temp="<<this->getTemperature(iteration)<<"\n";
                  visitor(*this);
                  //visitor(*this, newValue, currentBestValue_, iteration, false, burningIn);
               }
            }
            ++iteration;
         }
      }
   }
   //visitor.end(*this, currentBestValue_, currentBestValue_);
   visitor.end(*this);
   inInference_=false;
   return NORMAL;
}

template<class GM, class ACC>
inline InferenceTermination
Gibbs<GM, ACC>::arg
(
   std::vector<LabelType>& x, 
   const size_t N
) const {
   if(N == 1) {
      x.resize(gm_.numberOfVariables());
      for(size_t j = 0; j < x.size(); ++j) {
         if(!inInference_)
            x[j] = currentBestState_[j];
         else{
            x[j] = movemaker_.state(j);
         }
      }
      return NORMAL;
   }
   else {
      return UNKNOWN;
   }
}

template<class GM, class ACC>
inline typename Gibbs<GM, ACC>::LabelType
Gibbs<GM, ACC>::markovState
(
   const size_t j
) const
{
   OPENGM_ASSERT(j < gm_.numberOfVariables());
   return movemaker_.state(j);
}

template<class GM, class ACC>
inline typename Gibbs<GM, ACC>::ValueType
Gibbs<GM, ACC>::markovValue() const
{
   return movemaker_.value();
}

template<class GM, class ACC>
inline typename Gibbs<GM, ACC>::LabelType
Gibbs<GM, ACC>::currentBestState
(
   const size_t j
) const
{
   OPENGM_ASSERT(j < gm_.numberOfVariables());
   return currentBestState_[j];
}

template<class GM, class ACC>
inline typename Gibbs<GM, ACC>::ValueType
Gibbs<GM, ACC>::currentBestValue() const
{
   return currentBestValue_;
}

template<class GIBBS>
inline
GibbsMarginalVisitor<GIBBS>::GibbsMarginalVisitor()
:  gm_(NULL), 
   numberOfSamples_(0), 
   numberOfAcceptedSamples_(0), 
   numberOfRejectedSamples_(0), 
   marginals_(), 
   stateCache_()
{}

template<class GIBBS>
inline
GibbsMarginalVisitor<GIBBS>::GibbsMarginalVisitor(
   const typename GibbsMarginalVisitor<GIBBS>::GraphicalModelType& gm
)
:  gm_(&gm), 
   numberOfSamples_(0), 
   numberOfAcceptedSamples_(0), 
   numberOfRejectedSamples_(0), 
   marginals_(), 
   stateCache_()
{}

template<class GIBBS>
inline void
GibbsMarginalVisitor<GIBBS>::assign(
   const typename GibbsMarginalVisitor<GIBBS>::GraphicalModelType& gm
)
{
    gm_ = &gm;
}

template<class GIBBS>
inline void
GibbsMarginalVisitor<GIBBS>::operator()(
   const typename GibbsMarginalVisitor<GIBBS>::GibbsType& gibbs, 
   const typename GibbsMarginalVisitor<GIBBS>::ValueType currentValue, 
   const typename GibbsMarginalVisitor<GIBBS>::ValueType bestValue, 
   const size_t iteration, 
   const bool accepted, 
   const bool burningIn
) {
   if(!burningIn) {
      ++numberOfSamples_;
      if(accepted) {
         ++numberOfAcceptedSamples_;
      }
      else {
         ++numberOfRejectedSamples_;
      }
      for(size_t j = 0; j < marginals_.size(); ++j) {
         for(size_t k = 0; k < marginals_[j].numberOfVariables(); ++k) {
            stateCache_[k] = gibbs.markovState(marginals_[j].variableIndex(k));
         }
         ++marginals_[j](stateCache_.begin());
      }
   }
}

template<class GIBBS>
template<class VariableIndexIterator>
inline size_t
GibbsMarginalVisitor<GIBBS>::addMarginal(
   VariableIndexIterator begin, 
   VariableIndexIterator end
) {
   marginals_.push_back(IndependentFactorType(*gm_, begin, end));
   if(marginals_.back().numberOfVariables() > stateCache_.size()) {
      stateCache_.resize(marginals_.back().numberOfVariables());
   }
   return marginals_.size() - 1;
}

template<class GIBBS>
inline size_t
GibbsMarginalVisitor<GIBBS>::addMarginal(
   const size_t variableIndex
) {
   size_t variableIndices[] = {variableIndex};
   return addMarginal(variableIndices, variableIndices + 1);
}

template<class GIBBS>
inline size_t
GibbsMarginalVisitor<GIBBS>::numberOfSamples() const {
   return numberOfSamples_;
}

template<class GIBBS>
inline size_t
GibbsMarginalVisitor<GIBBS>::numberOfAcceptedSamples() const {
   return numberOfAcceptedSamples_;
}

template<class GIBBS>
inline size_t
GibbsMarginalVisitor<GIBBS>::numberOfRejectedSamples() const {
   return numberOfRejectedSamples_;
}

template<class GIBBS>
inline size_t
GibbsMarginalVisitor<GIBBS>::numberOfMarginals() const {
   return marginals_.size();
}

template<class GIBBS>
inline const typename GibbsMarginalVisitor<GIBBS>::IndependentFactorType&
GibbsMarginalVisitor<GIBBS>::marginal(
   const size_t setIndex
) const {
   return marginals_[setIndex];
}

} // namespace opengm

#endif // #ifndef OPENGM_GIBBS_HXX