OpenGM  2.3.x
Discrete Graphical Model Library
daoopt.hxx
Go to the documentation of this file.
1 #ifndef DAOOPT_HXX_
2 #define DAOOPT_HXX_
3 
9 
10 
11 #include <Main.h>
12 #undef UNKNOWN
13 
14 namespace daoopt{
15 
16  template<class V, class I>
17  class OpengmVisitor : public daoopt::VisitorBase{
18  public:
19  OpengmVisitor(V& v, I& i) : visitor(v), inference(i) {};
20  V& visitor;
22  virtual bool visit(){
23  if(visitor(inference)==0) {return true;}
24  else {return false;}
25  };
26  };
27 
28 }
29 
30 namespace opengm {
31  namespace external {
32 
38  // DAOOPT
44  template<class GM>
45  class DAOOPT : public Inference<GM, opengm::Minimizer> {
46  public:
47  typedef GM GraphicalModelType;
53 
54 
55  template<class _GM>
56  struct RebindGm{
57  typedef DAOOPT<_GM> type;
58  };
59 
60  template<class _GM,class _ACC>
62  typedef DAOOPT<_GM> type;
63  };
64 
66  struct Parameter : public daoopt::ProgramOptions {
68  Parameter() : daoopt::ProgramOptions() {
69  // set default options, this is not done for all parameters by daoopt
70  subprobOrder = 0;
71  ibound = 10;
72  cbound = 1000;
73  cbound_worker = 1000;
74  rotateLimit = 1000;
75  order_iterations = 25;
76  order_timelimit = -1;
77  threads = -1;
78  sampleDepth = 10;
79  sampleRepeat = 1;
80  aobbLookahead = 5;
81  }
82  template<class P>
83  Parameter(const P & p) : daoopt::ProgramOptions() {
84  // set default options, this is not done for all parameters by daoopt
85  subprobOrder = p.subprobOrder;
86  ibound = p.ibound;
87  cbound = p.cbound;
88  cbound_worker = p.cbound_worker;
89  rotateLimit = p.rotateLimit;
90  order_iterations = p.order_iterations;
91  order_timelimit = p.order_timelimit;
92  threads = p.threads;
93  sampleDepth = p.sampleDepth;
94  sampleRepeat = p.sampleRepeat;
95  aobbLookahead = p.aobbLookahead;
96  }
97  };
98 
99  // construction
100  DAOOPT(const GraphicalModelType& gm, const Parameter& para = Parameter());
101  // destruction
102  ~DAOOPT();
103  // query
104  std::string name() const;
105  const GraphicalModelType& graphicalModel() const;
106  // inference
107  template<class VISITOR>
110  InferenceTermination arg(std::vector<LabelType>&, const size_t& = 1) const;
111  typename GM::ValueType bound() const;
112  typename GM::ValueType value() const;
113 
114  protected:
115  const GraphicalModelType& gm_;
117  daoopt::Main main_;
118  };
119 
120  template<class GM>
121  inline DAOOPT<GM>::DAOOPT(const typename DAOOPT<GM>::GraphicalModelType& gm, const Parameter& para)
122  : gm_(gm), parameter_(para) {
123 
124  if(!main_.start()) {
125  throw RuntimeError("Error starting DAOOPT main.");
126  }
127 
128  // check options
129  if (!parameter_.in_subproblemFile.empty() && parameter_.in_orderingFile.empty()) {
130  throw RuntimeError("Error: Specifying a subproblem requires reading a fixed ordering from file.");
131  }
132 
133  if (parameter_.subprobOrder < 0 || parameter_.subprobOrder > 3) {
134  throw RuntimeError("Error: subproblem ordering has to be 0(width-inc), 1(width-dec), 2(heur-inc) or 3(heur-dec)");
135  }
136 
137  if(parameter_.problemName.empty()) {
138  //Extract the problem name
139  if(parameter_.in_problemFile.empty()) {
140  // set problem name to openGM
141  parameter_.problemName = "openGM";
142  } else {
143  string& fname = parameter_.in_problemFile;
144  size_t len, start, pos1, pos2;
145  #if defined(WIN32)
146  pos1 = fname.find_last_of("\\");
147  #elif defined(LINUX)
148  pos1 = fname.find_last_of("/");
149  #endif
150  pos2 = fname.find_last_of(".");
151  if (pos1 == string::npos) { len = pos2; start = 0; }
152  else { len = (pos2-pos1-1); start = pos1+1; }
153  parameter_.problemName = fname.substr(start, len);
154  }
155  }
156 
157  // TODO set executable name (is this required by daoopt)???
158  /*if(parameter_.executableName.empty()) {
159  parameter_.executableName = ???
160  }*/
161 
162  main_.setOptions(new daoopt::ProgramOptions(static_cast<daoopt::ProgramOptions>(parameter_)));
163 
164  if(!main_.outputInfo()) {
165  throw RuntimeError("Error printing DAOOPT info.");
166  }
167 
168  if(parameter_.in_problemFile.empty()) {
169  daoopt::Problem* problem = new daoopt::Problem();
170  if(!problem->convertOPENGM(gm_)) {
171  throw RuntimeError("Error converting openGM to DAOOPT problem.");
172  }
173  main_.setProblem(problem);
174  } else {
175  if(!main_.loadProblem()) {
176  throw RuntimeError("Error loading DAOOPT problem.");
177  }
178  }
179  }
180 
181  template<class GM>
183 
184  }
185 
186  template<class GM>
187  inline std::string DAOOPT<GM>::name() const {
188  return "DAOOPT";
189  }
190 
191  template<class GM>
193  return gm_;
194  }
195 
196  template<class GM>
198  EmptyVisitorType visitor;
199  return this->infer(visitor);
200  }
201 
202  template<class GM>
203  template<class VISITOR>
204  inline InferenceTermination DAOOPT<GM>::infer(VISITOR & visitor) {
205 
206  visitor.begin(*this);
207  // TODO check for possible visitor injection method
208  visitor(*this);
209  if(!main_.runSLS()) {
210  throw RuntimeError("Error running DAOOPT SLS.");
211  }
212 visitor(*this);
213  if(!main_.findOrLoadOrdering()) {
214  throw RuntimeError("Error running DAOOPT find/load ordering.");
215  }
216  visitor(*this);
217  if(!main_.initDataStructs()) {
218  throw RuntimeError("Error initializing DAOOPT data structs.");
219  }
220  visitor(*this);
221  if(!main_.compileHeuristic()) {
222  throw RuntimeError("Error compiling DAOOPT heuristic.");
223  }
224  visitor(*this);
225  if(!main_.runLDS()) {
226  throw RuntimeError("Error running DAOOPT LDS.");
227  }
228  visitor(*this);
229  if(!main_.finishPreproc()) {
230  throw RuntimeError("Error finishing DAOOPT preprocessing.");
231  }
232 visitor(*this);
234  if(!main_.runSearch(v)) {
235  throw RuntimeError("Error running DAOOPT search.");
236  }
237  visitor(*this);
238  if(!main_.outputStats()) {
239  throw RuntimeError("Error output DAOOPT stats.");
240  }
241  visitor(*this);
242  visitor.end(*this);
243  return NORMAL;
244  }
245 
246  template<class GM>
247  inline InferenceTermination DAOOPT<GM>::arg(std::vector<LabelType>& arg, const size_t& n) const {
248  const daoopt::Problem& problem = main_.getProblem();
249 
250  const std::vector<daoopt::val_t>& assignment = problem.getSolutionAssg();
251  if(assignment.size() == gm_.numberOfVariables()){
252  arg.assign(assignment.begin(), assignment.end()-1);
253  }
254  else{
255  std::cout <<"Warning: DAOOPT return labeling of wrong size!"<<std::endl;
256  arg.resize(gm_.numberOfVariables(),0);
257  }
258  return NORMAL;
259  }
260 
261  template<class GM>
262  inline typename GM::ValueType DAOOPT<GM>::bound() const {
263  return AccumulationType::ineutral<ValueType>();
264  }
265 
266  template<class GM>
267  inline typename GM::ValueType DAOOPT<GM>::value() const {
268  //std::vector<LabelType> c;
269  //arg(c);
270  //return gm_.evaluate(c);
271 
272  const daoopt::Problem& problem = main_.getProblem();
273  const ValueType v = static_cast<ValueType>(-problem.getSolutionCost());
274  if(isnan(v))
275  return std::numeric_limits<ValueType>::infinity();
276  else
277  return v;
278  }
279  } // namespace external
280 } // namespace opengm
281 
282 #endif /* DAOOPT_HXX_ */
The OpenGM namespace.
Definition: config.hxx:43
const GraphicalModelType & gm_
Definition: daoopt.hxx:115
void infer(const typename INF::GraphicalModelType &gm, const typename INF::Parameter &param, std::vector< typename INF::LabelType > &conf)
Definition: inference.hxx:34
OpengmVisitor(V &v, I &i)
Definition: daoopt.hxx:19
opengm::Minimizer AccumulationType
Definition: daoopt.hxx:48
GM::ValueType bound() const
return a bound on the solution
Definition: daoopt.hxx:262
InferenceTermination arg(std::vector< LabelType > &, const size_t &=1) const
Definition: daoopt.hxx:247
visitors::VerboseVisitor< DAOOPT< GM > > VerboseVisitorType
Definition: daoopt.hxx:50
GM::ValueType value() const
return the solution (value)
Definition: daoopt.hxx:267
GraphicalModelType::ValueType ValueType
Definition: inference.hxx:50
Inference algorithm interface.
Definition: inference.hxx:43
std::string name() const
Definition: daoopt.hxx:187
daoopt::Main main_
Definition: daoopt.hxx:117
const GraphicalModelType & graphicalModel() const
Definition: daoopt.hxx:192
DAOOPT DAOOPT inference algorithm class.
Definition: daoopt.hxx:45
visitors::EmptyVisitor< DAOOPT< GM > > EmptyVisitorType
Definition: daoopt.hxx:51
Minimization as a unary accumulation.
Definition: minimizer.hxx:12
virtual bool visit()
Definition: daoopt.hxx:22
InferenceTermination infer()
Definition: daoopt.hxx:197
visitors::TimingVisitor< DAOOPT< GM > > TimingVisitorType
Definition: daoopt.hxx:52
OpenGM runtime error.
Definition: opengm.hxx:100
Parameter inherits from daoopt ProgramOptions.
Definition: daoopt.hxx:66
InferenceTermination
Definition: inference.hxx:24