This file is indexed.

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

#include <typeinfo>
#include <limits>
#include "opengm/inference/inference.hxx"
#include "opengm/inference/visitors/visitors.hxx"

namespace opengm {

  /// DynamicProgramming
  ///\ingroup inference
  /// \ingroup messagepassing_inference
  template<class GM, class ACC>
  class DynamicProgramming : public Inference<GM, ACC> {
  public:
    typedef ACC AccumulationType;
    typedef ACC AccumulatorType;
    typedef GM GraphicalModelType;
    OPENGM_GM_TYPE_TYPEDEFS;
    typedef LabelType  MyStateType;
    typedef ValueType  MyValueType;
    typedef visitors::VerboseVisitor<DynamicProgramming<GM, ACC> > VerboseVisitorType;
    typedef visitors::EmptyVisitor<DynamicProgramming<GM, ACC> >   EmptyVisitorType;
    typedef visitors::TimingVisitor<DynamicProgramming<GM, ACC> >  TimingVisitorType;


    template<class _GM>
    struct RebindGm{
        typedef DynamicProgramming<_GM, ACC> type;
    };

    template<class _GM,class _ACC>
    struct RebindGmAndAcc{
        typedef DynamicProgramming<_GM, _ACC > type;
    };

    struct Parameter {
        Parameter(){

        }
        template<class P>
        Parameter(const P &p)
        : roots_(p.roots_){
        }
        
      std::vector<IndexType> roots_;
    };

    DynamicProgramming(const GraphicalModelType&, const Parameter& = Parameter());
    ~DynamicProgramming();

    std::string name() const;
    const GraphicalModelType& graphicalModel() const;
    InferenceTermination infer();
    template< class VISITOR>
    InferenceTermination infer(VISITOR &);
    InferenceTermination arg(std::vector<LabelType>&, const size_t = 1) const;
    
    
    void getNodeInfo(const IndexType Inode, std::vector<ValueType>& values, std::vector<std::vector<LabelType> >& substates, std::vector<IndexType>& nodes) const;
    

  private:
    const GraphicalModelType& gm_;
    Parameter para_;
    MyValueType* valueBuffer_;
    MyStateType* stateBuffer_;
    std::vector<MyValueType*> valueBuffers_;
    std::vector<MyStateType*> stateBuffers_;
    std::vector<size_t> nodeOrder_; 
    std::vector<size_t> orderedNodes_;
    bool inferenceStarted_;
  };

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

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

  template<class GM, class ACC>
  DynamicProgramming<GM, ACC>::~DynamicProgramming()
  {
    free(valueBuffer_);
    free(stateBuffer_);
  }
  
  template<class GM, class ACC>
  inline DynamicProgramming<GM, ACC>::DynamicProgramming
  (
  const GraphicalModelType& gm, 
  const Parameter& para
  ) 
  :  gm_(gm), inferenceStarted_(false)
  {
    OPENGM_ASSERT(gm_.isAcyclic());
    para_ = para;
    
    // Set nodeOrder 
    std::vector<size_t> numChildren(gm_.numberOfVariables(),0);
    std::vector<size_t> nodeList;
    size_t orderCount = 0;
    size_t varCount   = 0;
    nodeOrder_.resize(gm_.numberOfVariables(),std::numeric_limits<std::size_t>::max());
    size_t rootCounter=0;
    while(varCount < gm_.numberOfVariables() && orderCount < gm_.numberOfVariables()){
      if(rootCounter<para_.roots_.size()){
        nodeOrder_[para_.roots_[rootCounter]] = orderCount++;
        nodeList.push_back(para_.roots_[rootCounter]);
        ++rootCounter;
      }
      else if(nodeOrder_[varCount]==std::numeric_limits<std::size_t>::max()){
        nodeOrder_[varCount] = orderCount++;
        nodeList.push_back(varCount);
      }
      ++varCount;
      while(nodeList.size()>0){
        size_t node = nodeList.back();
        nodeList.pop_back();
        for(typename GM::ConstFactorIterator it=gm_.factorsOfVariableBegin(node); it !=gm_.factorsOfVariableEnd(node); ++it){
          const typename GM::FactorType& factor = gm_[(*it)];
          if( factor.numberOfVariables() == 2 ){
            if( factor.variableIndex(1) == node && nodeOrder_[factor.variableIndex(0)]==std::numeric_limits<std::size_t>::max() ){
              nodeOrder_[factor.variableIndex(0)] = orderCount++;
              nodeList.push_back(factor.variableIndex(0));
              ++numChildren[node];
            }
            if( factor.variableIndex(0) == node && nodeOrder_[factor.variableIndex(1)]==std::numeric_limits<std::size_t>::max() ){
              nodeOrder_[factor.variableIndex(1)] = orderCount++;
              nodeList.push_back(factor.variableIndex(1));
              ++numChildren[node];                       
            }
          }
        }
      }
    }

    // Allocate memmory
    size_t memSizeValue = 0;
    size_t memSizeState = 0;
    for(size_t i=0; i<gm_.numberOfVariables();++i){
      memSizeValue += gm_.numberOfLabels(i);
      memSizeState += gm.numberOfLabels(i) * numChildren[i];
    }
    valueBuffer_ = (MyValueType*) malloc(memSizeValue*sizeof(MyValueType));
    stateBuffer_ = (MyStateType*) malloc(memSizeState*sizeof(MyStateType));
    valueBuffers_.resize(gm_.numberOfVariables());
    stateBuffers_.resize(gm_.numberOfVariables()); 
    
    MyValueType* valuePointer =  valueBuffer_;
    MyStateType* statePointer =  stateBuffer_;
    for(size_t i=0; i<gm_.numberOfVariables();++i){
      valueBuffers_[i] = valuePointer;
      valuePointer += gm.numberOfLabels(i);
      stateBuffers_[i] = statePointer;
      statePointer +=  gm.numberOfLabels(i) * numChildren[i];
    }
    
    orderedNodes_.resize(gm_.numberOfVariables(),std::numeric_limits<std::size_t>::max());
    for(size_t i=0; i<gm_.numberOfVariables(); ++i)
      orderedNodes_[nodeOrder_[i]] = i;
    
  }
  
  template<class GM, class ACC>
  inline InferenceTermination 
  DynamicProgramming<GM, ACC>::infer(){
    EmptyVisitorType v;
    return infer(v);
  }
  
  template<class GM, class ACC>
  template<class VISITOR>
  inline InferenceTermination 
  DynamicProgramming<GM, ACC>::infer
  (
  VISITOR & visitor
  ){
     visitor.begin(*this);
     inferenceStarted_ = true;
    for(size_t i=1; i<=gm_.numberOfVariables();++i){
      const size_t node = orderedNodes_[gm_.numberOfVariables()-i];
      // set buffer neutral
      for(size_t n=0; n<gm_.numberOfLabels(node); ++n){
        OperatorType::neutral(valueBuffers_[node][n]);
      }
      // accumulate messages
      size_t childrenCounter = 0;
      for(typename GM::ConstFactorIterator it=gm_.factorsOfVariableBegin(node); it !=gm_.factorsOfVariableEnd(node); ++it){
        const typename GM::FactorType& factor = gm_[(*it)];

        // unary
        if(factor.numberOfVariables()==1 ){
          for(size_t n=0; n<gm_.numberOfLabels(node); ++n){
            const ValueType fac = factor(&n);
            OperatorType::op(fac, valueBuffers_[node][n]); 
          } 
        }
        
        //pairwise
        if( factor.numberOfVariables()==2 ){
          size_t vec[] = {0,0};
          if(factor.variableIndex(0) == node && nodeOrder_[factor.variableIndex(1)]>nodeOrder_[node] ){
            const size_t node2 = factor.variableIndex(1);
            MyStateType s;
            MyValueType v,v2;
            for(vec[0]=0; vec[0]<gm_.numberOfLabels(node); ++vec[0]){
              ACC::neutral(v);
              for(vec[1]=0; vec[1]<gm_.numberOfLabels(node2); ++vec[1]){ 
                const ValueType fac = factor(vec);
                OperatorType::op(fac,valueBuffers_[node2][vec[1]],v2) ;
                if(ACC::bop(v2,v)){
                  v=v2;
                  s=vec[1];
                }
              }
              stateBuffers_[node][childrenCounter*gm_.numberOfLabels(node)+vec[0]] = s;
              OperatorType::op(v,valueBuffers_[node][vec[0]]);
            }  
            ++childrenCounter;
            
          }
          if(factor.variableIndex(1) == node && nodeOrder_[factor.variableIndex(0)]>nodeOrder_[node]){ 
            const size_t node2 = factor.variableIndex(0);
            MyStateType s;
            MyValueType v,v2;
            for(vec[1]=0; vec[1]<gm_.numberOfLabels(node); ++vec[1]){
              ACC::neutral(v);
              for(vec[0]=0; vec[0]<gm_.numberOfLabels(node2); ++vec[0]){
                const ValueType fac = factor(vec);
                OperatorType::op(fac,valueBuffers_[node2][vec[0]],v2); 
                if(ACC::bop(v2,v)){
                  v=v2;
                  s=vec[0];
                }
              }  
              stateBuffers_[node][childrenCounter*gm_.numberOfLabels(node)+vec[1]] = s;
              OperatorType::op(v,valueBuffers_[node][vec[1]]); 
            }
            ++childrenCounter;                      
          }
        }
        // higher order
        if( factor.numberOfVariables()>2 ){
           throw std::runtime_error("This implementation of Dynamic Programming does only support second order models so far, but could be extended.");
        }

      } 
    }
    visitor.end(*this);
    return NORMAL;
  }

  template<class GM, class ACC>
  inline InferenceTermination DynamicProgramming<GM, ACC>::arg
  (
  std::vector<LabelType>& arg, 
  const size_t n
  ) const {
    if(n > 1) {
       arg.assign(gm_.numberOfVariables(), 0);
      return UNKNOWN;
    } 
    else {
       if(inferenceStarted_) {
         std::vector<size_t> nodeList;
         arg.assign(gm_.numberOfVariables(), std::numeric_limits<LabelType>::max() );
         size_t var = 0;
         while(var < gm_.numberOfVariables()){
           if(arg[var]==std::numeric_limits<LabelType>::max()){
             MyValueType v; ACC::neutral(v);
             for(size_t i=0; i<gm_.numberOfLabels(var); ++i){
               if(ACC::bop(valueBuffers_[var][i], v)){
                 v = valueBuffers_[var][i];
                 arg[var]=i;
               }
             }
             nodeList.push_back(var);
           }
           ++var;
           while(nodeList.size()>0){
             size_t node = nodeList.back();
             size_t childrenCounter = 0;
             nodeList.pop_back();
             for(typename GM::ConstFactorIterator it=gm_.factorsOfVariableBegin(node); it !=gm_.factorsOfVariableEnd(node); ++it){
               const typename GM::FactorType& factor = gm_[(*it)];
               if(factor.numberOfVariables()==2 ){
                 if(factor.variableIndex(1)==node && nodeOrder_[factor.variableIndex(0)] > nodeOrder_[node] ){
                   arg[factor.variableIndex(0)] = stateBuffers_[node][childrenCounter*gm_.numberOfLabels(node)+arg[node]];
                   nodeList.push_back(factor.variableIndex(0));
                   ++childrenCounter;
                 }
                 if(factor.variableIndex(0)==node && nodeOrder_[factor.variableIndex(1)] > nodeOrder_[node] ){
                   arg[factor.variableIndex(1)] = stateBuffers_[node][childrenCounter*gm_.numberOfLabels(node)+arg[node]];
                   nodeList.push_back(factor.variableIndex(1));
                   ++childrenCounter;
                 }
               }
             }
           }
         }
         return NORMAL;
       } else {
          arg.assign(gm_.numberOfVariables(), 0);
          return UNKNOWN;
       }
    }
  }

  template<class GM, class ACC>
  inline void DynamicProgramming<GM, ACC>::getNodeInfo(const IndexType Inode, std::vector<ValueType>& values, std::vector<std::vector<LabelType> >& substates, std::vector<IndexType>& nodes) const{
    values.clear();
    substates.clear();
    nodes.clear();
    values.resize(gm_.numberOfLabels(Inode));
    substates.resize(gm_.numberOfLabels(Inode));
    std::vector<LabelType> arg;
    bool firstround = true;
    std::vector<size_t> nodeList;
    for(IndexType i=0;i<gm_.numberOfLabels(Inode); ++i){
      arg.assign(gm_.numberOfVariables(), std::numeric_limits<LabelType>::max() );
      arg[Inode]=i;
      values[i]=valueBuffers_[Inode][i];
      nodeList.push_back(Inode);
      if(i!=0){
        firstround=false;
      }
      
      while(nodeList.size()>0){
        size_t node = nodeList.back();
        size_t childrenCounter = 0;
        nodeList.pop_back();
        for(typename GM::ConstFactorIterator it=gm_.factorsOfVariableBegin(node); it !=gm_.factorsOfVariableEnd(node); ++it){
          const typename GM::FactorType& factor = gm_[(*it)];
          if(factor.numberOfVariables()==2 ){
            if(factor.variableIndex(1)==node && nodeOrder_[factor.variableIndex(0)] > nodeOrder_[node] ){
              arg[factor.variableIndex(0)] = stateBuffers_[node][childrenCounter*gm_.numberOfLabels(node)+arg[node]];
              substates[i].push_back(stateBuffers_[node][childrenCounter*gm_.numberOfLabels(node)+arg[node]]);
              if(firstround==true){              
                nodes.push_back(factor.variableIndex(0));
              }
              nodeList.push_back(factor.variableIndex(0));
              ++childrenCounter;             
            }
            if(factor.variableIndex(0)==node && nodeOrder_[factor.variableIndex(1)] > nodeOrder_[node] ){
              arg[factor.variableIndex(1)] = stateBuffers_[node][childrenCounter*gm_.numberOfLabels(node)+arg[node]];
              substates[i].push_back(stateBuffers_[node][childrenCounter*gm_.numberOfLabels(node)+arg[node]]);
              if(firstround==true){
                nodes.push_back(factor.variableIndex(1));
              }
              nodeList.push_back(factor.variableIndex(1)); 
              ++childrenCounter;                                 
            }
          }
        }
      }
    }
  }
  
  
} // namespace opengm

#endif // #ifndef OPENGM_DYNAMICPROGRAMMING_HXX