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