OpenGM  2.3.x
Discrete Graphical Model Library
multi_label_proposals.hxx
Go to the documentation of this file.
1 #ifndef OPENGM_MULTI_LABEL_PROPOSAL_HXX
2 #define OPENGM_MULTI_LABEL_PROPOSAL_HXX
3 
4 
5 
6 namespace opengm{
7  namespace proposals{
8 
9 
10  template<class GM, class ACC>
11  class LabelHistory{
12  public:
13  typedef ACC AccumulationType;
14  typedef GM GraphicalModelType;
16  typedef std::vector<LabelType > LabelVec;
17  typedef std::vector<ValueType > ValueVec;
18  typedef std::vector< LabelVec > LabelVecVec;
19 
20  LabelHistory(const GM & gm, size_t size)
21  : gm_(gm),
22  labels_(size,LabelVec(gm.numberOfVariables)),
23  valueVec_(size),
24  topIndex_(0),
25  entries_(0){
26  }
27 
28  LabelVec & topArg(){
29  return labels_[topIndex_];
30  }
31 
32  LabelVec & topValue(){
33  return valueVec_[topIndex_];
34  }
35 
36  void setTopValue(const ValueType & value){
37  valueVec_[topIndex_]=value;
38  }
39 
40  LabelVec & topArg(int histIndex){
41  int i = topIndex_ - histIndex;
42  if(i>=0)
43  return labels_[i];
44  else
45  return labels_[labels_.size()-1];
46  }
47 
48  LabelVec & topArg(int histIndex){
49  return valueVec_[topIndex_];
50  }
51 
52  void nextTop(){
53  entries_ = std::min(++entries_, int(labels_.size()));
54  topIndex_ = topIndex_+1 < labels_.size() ? topIndex_+1 : 0;
55  }
56 
57  size_t histSize()const{
58  return entries_;
59  }
60 
61  const GM & gm_;
62  LabelVecVec labels_;
63  ValueVec valueVec_;
64  int topIndex_;
65  int entries_;
66  };
67 
68 
69  template<class GM, class ACC>
71 
72  public:
73 
74  typedef ACC AccumulationType;
75  typedef GM GraphicalModelType;
77 
79  typedef std::vector<LabelType > LabelVec;
80  typedef std::vector<ValueType > ValueVec;
81  typedef std::vector< LabelVec > LabelVecVec;
82 
83  class Parameter{
84  public:
86  size_t r=0,
87  size_t alphaIncrement=1,
88  bool randomAlphaToLabel=false,
89  bool autoSeed=true,
90  int seed=-1
91  )
92  :
93  r_(r),
94  alphaIncrement_(alphaIncrement),
95  randomAlphaToLabel_(randomAlphaToLabel),
96  autoSeed_(autoSeed),
97  seed_(seed){
98  }
99  size_t r_;
102  bool autoSeed_;
103  unsigned int seed_;
104  };
105 
106  // interface
107  AlphaExpansion(const GM & gm, Parameter & param & Parameter())
108  : gm_(gm),
109  param_(param),
110  maxLabel_(gm.maxNumberOfLabels()){
111 
112  for(size_t i=0; i<maxLabel_; i)
113  alphaToLabel_[i] = i;
114  if(param.randomAlphaToLabel_){
115  if(!param_.seed_>=0 || param_.autoSeed_){
116  if(autoSeed_){
117  std::srand ( unsigned ( std::time(0) ) );
118  }
119  else{
120  std::srand ( unsigned ( param_.seed_ ) );
121  }
122  }
123  std::random_shuffle ( myvector.begin(), myvector.end() );
124  }
125  }
126 
127  size_t numProposals(){
128  return param_.r_*2 + 1;
129  }
130 
132  return static_cast<Int64Type>( (maxLabel_ / alphaIncrement_) + 1);
133  }
134 
135  size_t requiredHistorySize()const{
136  return 0;
137  }
138 
139  void reset(){
140  currentAlpha_ = 0;
141  }
142 
144  const LabelVec & current,
145  const History & bestHistory,
146  const bool improvementViaLastProposal,
147  LabelVecVec & proposals
148  ){
149  const Int64Type sl = -1 * param_.r_ + currentAlpha_;
150  const Int64Type el = 1 + param_.r_ + currentAlpha_;
151  for(size_t vi=0; vi<gm_.numberOfVariables(); ++vi){
152  for(Int64Type a=mr, pi=0 ; a<el; ++a,++pi){
153  if(a>0 && alphaToLabel_[a]<gm_.maxNumberOfLabels(vi))
154  proposals[pi][vi] = alphaToLabel_[a];
155  else
156  proposals[pi][vi] = current[vi];
157  }
158  }
159  currentAlpha_+=alphaIncrement_;
160  }
161 
162 
163 
164  private:
165  const GM & gm_;
166  Parameter param_;
167  std::vector<LabelType> alphaToLabel_;
168  size_t maxLabel_;
169  LabelType currentAlpha_;
170  };
171 
172  }
173 }
174 
175 
176 
177 #endif /*OPENGM_MULTI_LABEL_PROPOSAL_HXX*/
The OpenGM namespace.
Definition: config.hxx:43
detail_types::Int64Type Int64Type
int32
Definition: config.hxx:308
LabelHistory(const GM &gm, size_t size)
Parameter(size_t r=0, size_t alphaIncrement=1, bool randomAlphaToLabel=false, bool autoSeed=true, int seed=-1)
void getProposals(const LabelVec &current, const History &bestHistory, const bool improvementViaLastProposal, LabelVecVec &proposals)
void setTopValue(const ValueType &value)
AlphaExpansion(const GM &gm, Parameter &param &Parameter())