NLopt.hxx
1 // Copyright (c) 2007-2010 Massachusetts Institute of Technology
2 // Modifications by Marc Fragu, copyright (C) 2011 INRIA
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21 
22 
23 #ifndef SELDON_COMPUTATION_OPTIMIZATION_NLOPT_HXX
24 #define SELDON_COMPUTATION_OPTIMIZATION_NLOPT_HXX
25 
26 
27 #include <nlopt.hpp>
28 
29 
30 namespace nlopt
31 {
32 
33 
34  typedef double (*svfunc)(const Seldon::Vector<double> &x,
35  Seldon::Vector<double> &grad, void *data);
36 
37  class SeldonOpt
38  {
39 
40  private:
41 
42  nlopt_opt o;
43 
44  void mythrow(nlopt_result ret) const
45  {
46  switch (ret)
47  {
48  case NLOPT_FAILURE:
49  throw Seldon::Error("SeldonOpt", "Nlopt failure.");
50  case NLOPT_OUT_OF_MEMORY:
51  throw Seldon::NoMemory("SeldonOpt",
52  "Nlopt failed to allocate the"
53  " requested storage space.");
54  case NLOPT_INVALID_ARGS:
55  throw Seldon::WrongArgument("SeldonOpt", "Nlopt invalid argument.");
56  case NLOPT_ROUNDOFF_LIMITED:
57  throw Seldon::Error("SeldonOpt", "Nlopt roundoff-limited.");
58  case NLOPT_FORCED_STOP:
59  throw Seldon::Error("SeldonOpt", "Nlopt forced stop.");
60  default:
61  break;
62  }
63  }
64 
65 
66  typedef struct
67  {
68  SeldonOpt* o;
69  mfunc mf;
70  func f;
71  void* f_data;
72  svfunc vf;
73  nlopt_munge munge_destroy, munge_copy;
74  } myfunc_data;
75 
76 
77  static void *free_myfunc_data(void *p)
78  {
79  myfunc_data *d = (myfunc_data *) p;
80  if (d)
81  {
82  if (d->f_data && d->munge_destroy)
83  d->munge_destroy(d->f_data);
84  delete d;
85  }
86  return NULL;
87  }
88 
89 
90  static void *dup_myfunc_data(void *p)
91  {
92  myfunc_data *d = (myfunc_data *) p;
93  if (d)
94  {
95  void *f_data;
96  if (d->f_data && d->munge_copy) {
97  f_data = d->munge_copy(d->f_data);
98  if (!f_data)
99  return NULL;
100  }
101  else
102  f_data = d->f_data;
103  myfunc_data *dnew = new myfunc_data;
104  if (dnew)
105  {
106  *dnew = *d;
107  dnew->f_data = f_data;
108  }
109  return (void*) dnew;
110  }
111  else
112  return NULL;
113  }
114 
115 
116  static double myfunc(unsigned n, const double *x, double *grad,
117  void *d_)
118  {
119  myfunc_data *d = reinterpret_cast<myfunc_data*>(d_);
120  return d->f(n, x, grad, d->f_data);
121  }
122 
123 
124  static void mymfunc(unsigned m, double *result,
125  unsigned n, const double *x,
126  double *grad, void *d_)
127  {
128  myfunc_data *d = reinterpret_cast<myfunc_data*>(d_);
129  d->mf(m, result, n, x, grad, d->f_data);
130  return;
131  }
132 
133 
134  Seldon::Vector<double> xtmp, gradtmp, gradtmp0;
135 
136 
137  static double myvfunc(unsigned n, const double *x, double *grad,
138  void *d_)
139  {
140  myfunc_data *d = reinterpret_cast<myfunc_data*>(d_);
141  Seldon::Vector<double> &xv = d->o->xtmp;
142  if (n)
143  memcpy(xv.GetData(), x, n * sizeof(double));
144 
145  double val=d->vf(xv, grad ? d->o->gradtmp : d->o->gradtmp0,
146  d->f_data);
147  if (grad && n)
148  {
149  Seldon::Vector<double> &gradv = d->o->gradtmp;
150  memcpy(grad, gradv.GetData(), n * sizeof(double));
151  }
152  return val;
153  }
154 
155 
156  void alloc_tmp()
157  {
158  if (xtmp.GetSize() != int(nlopt_get_dimension(o)))
159  {
160  xtmp = Seldon::Vector<double>(nlopt_get_dimension(o));
161  gradtmp = Seldon::Vector<double>(nlopt_get_dimension(o));
162  }
163  }
164 
165 
166  result last_result;
167  double last_optf;
168  nlopt_result forced_stop_reason;
169 
170 
171  public:
172 
173  SeldonOpt() : o(NULL), xtmp(0), gradtmp(0), gradtmp0(0),
174  last_result(nlopt::FAILURE), last_optf(HUGE_VAL),
175  forced_stop_reason(NLOPT_FORCED_STOP)
176  {
177  }
178 
179 
180  ~SeldonOpt()
181  {
182  nlopt_destroy(o);
183  }
184 
185 
186  SeldonOpt(algorithm a, unsigned n) :
187  o(nlopt_create(nlopt_algorithm(a), n)),
188  xtmp(0), gradtmp(0), gradtmp0(0),
189  last_result(nlopt::FAILURE), last_optf(HUGE_VAL),
190  forced_stop_reason(NLOPT_FORCED_STOP)
191  {
192  if (!o)
193  throw Seldon::NoMemory("SeldonOpt(algorithm a, unsigned n)",
194  "Nlopt failed to allocate the"
195  " requested storage space.");
196  nlopt_set_munge(o, free_myfunc_data, dup_myfunc_data);
197  }
198 
199 
200  SeldonOpt(const SeldonOpt& f) :
201  o(nlopt_copy(f.o)),
202  xtmp(f.xtmp), gradtmp(f.gradtmp),
203  gradtmp0(0),
204  last_result(f.last_result),
205  last_optf(f.last_optf),
206  forced_stop_reason(f.forced_stop_reason)
207  {
208  if (f.o && !o)
209  throw Seldon::NoMemory("SeldonOpt(const SeldonOpt& f)",
210  "Nlopt failed to allocate the"
211  " requested storage space.");
212  }
213 
214 
215  SeldonOpt& operator=(SeldonOpt const& f)
216  {
217  if (this == &f)
218  return *this;
219  nlopt_destroy(o);
220  o = nlopt_copy(f.o);
221  if (f.o && !o)
222  throw Seldon::NoMemory("SeldonOpt::operator=(const SeldonOpt& f)",
223  "Nlopt failed to allocate the"
224  " requested storage space.");
225  xtmp = f.xtmp;
226  gradtmp = f.gradtmp;
227  last_result = f.last_result;
228  last_optf = f.last_optf;
229  forced_stop_reason = f.forced_stop_reason;
230  return *this;
231  }
232 
233 
234  result optimize(Seldon::Vector<double>& x, double& opt_f)
235  {
236  if (o && int(nlopt_get_dimension(o)) != x.GetSize())
237  throw Seldon::WrongArgument("SeldonOpt::optimize("
238  "Seldon::Vector<double>& x,"
239  " double& opt_f)", "Dimension mismatch.");
240  forced_stop_reason = NLOPT_FORCED_STOP;
241  nlopt_result ret =
242  nlopt_optimize(o, x.GetSize() == 0 ? NULL : x.GetData(),
243  &opt_f);
244  last_result = result(ret);
245  last_optf = opt_f;
246  if (ret == NLOPT_FORCED_STOP)
247  mythrow(forced_stop_reason);
248  mythrow(ret);
249  return last_result;
250  }
251 
252 
254  {
256  last_result = optimize(x, last_optf);
257  return x;
258  }
259 
260 
261  result last_optimize_result() const
262  {
263  return last_result;
264  }
265 
266 
267  double last_optimum_value() const
268  {
269  return last_optf;
270  }
271 
272 
273  algorithm get_algorithm() const
274  {
275  if (!o)
276  throw Seldon::Error("SeldonOpt::get_algorithm() const",
277  "Uninitialized nlopt::SeldonOpt.");
278  return algorithm(nlopt_get_algorithm(o));
279  }
280 
281 
282  const char *get_algorithm_name() const
283  {
284  if (!o)
285  Seldon::Error("SeldonOpt::get_algorithm() const",
286  "Uninitialized nlopt::SeldonOpt.");
287  return nlopt_algorithm_name(nlopt_get_algorithm(o));
288  }
289 
290 
291  unsigned get_dimension() const
292  {
293  if (!o)
294  Seldon::Error("SeldonOpt::get_algorithm() const",
295  "Uninitialized nlopt::SeldonOpt.");
296  return nlopt_get_dimension(o);
297  }
298 
299 
300  void set_min_objective(func f, void *f_data)
301  {
302  myfunc_data *d = new myfunc_data;
303  if (!d)
304  throw Seldon::NoMemory("SeldonOpt::set_min_objective(func f, "
305  "void *f_data)", "Nlopt failed to allocate the"
306  " requested storage space.");
307  d->o = this;
308  d->f = f;
309  d->f_data = f_data;
310  d->mf = NULL;
311  d->vf = NULL;
312  d->munge_destroy = d->munge_copy = NULL;
313  mythrow(nlopt_set_min_objective(o, myfunc, d));
314  }
315 
316 
317  void set_min_objective(svfunc vf, void *f_data) {
318  myfunc_data *d = new myfunc_data;
319  if (!d)
320  throw Seldon::NoMemory("SeldonOpt::set_min_objective(func f, "
321  "void *f_data)", "Nlopt failed to allocate the"
322  " requested storage space.");
323  d->o = this;
324  d->f = NULL;
325  d->f_data = f_data;
326  d->mf = NULL;
327  d->vf = vf;
328  d->munge_destroy = d->munge_copy = NULL;
329  mythrow(nlopt_set_min_objective(o, myvfunc, d));
330  alloc_tmp();
331  }
332 
333 
334  void set_max_objective(func f, void *f_data) {
335  myfunc_data *d = new myfunc_data;
336  if (!d)
337  throw Seldon::NoMemory("SeldonOpt::set_min_objective(func f, "
338  "void *f_data)", "Nlopt failed to allocate the"
339  " requested storage space.");
340  d->o = this;
341  d->f = f;
342  d->f_data = f_data;
343  d->mf = NULL;
344  d->vf = NULL;
345  d->munge_destroy = d->munge_copy = NULL;
346  mythrow(nlopt_set_max_objective(o, myfunc, d));
347  }
348 
349 
350  void set_max_objective(svfunc vf, void *f_data)
351  {
352  myfunc_data *d = new myfunc_data;
353  if (!d)
354  throw Seldon::NoMemory("SeldonOpt::set_max_objective(func f, "
355  "void *f_data)", "Nlopt failed to allocate the"
356  " requested storage space.");
357  d->o = this;
358  d->f = NULL;
359  d->f_data = f_data;
360  d->mf = NULL;
361  d->vf = vf;
362  d->munge_destroy = d->munge_copy = NULL;
363  mythrow(nlopt_set_max_objective(o, myvfunc, d));
364  alloc_tmp();
365  }
366 
367 
368  void set_min_objective(func f, void *f_data,
369  nlopt_munge md, nlopt_munge mc)
370  {
371  myfunc_data *d = new myfunc_data;
372  if (!d)
373  throw Seldon::NoMemory("SeldonOpt::set_min_objective(func f, "
374  "void *f_data)", "Nlopt failed to allocate the"
375  " requested storage space.");
376  d->o = this;
377  d->f = f;
378  d->f_data = f_data;
379  d->mf = NULL;
380  d->vf = NULL;
381  d->munge_destroy = md;
382  d->munge_copy = mc;
383  mythrow(nlopt_set_min_objective(o, myfunc, d));
384  }
385 
386 
387  void set_max_objective(func f, void *f_data,
388  nlopt_munge md, nlopt_munge mc)
389  {
390  myfunc_data *d = new myfunc_data;
391  if (!d)
392  throw Seldon::NoMemory("SeldonOpt::set_max_objective(func f, "
393  "void *f_data)", "Nlopt failed to allocate the"
394  " requested storage space.");
395  d->o = this;
396  d->f = f;
397  d->f_data = f_data;
398  d->mf = NULL;
399  d->vf = NULL;
400  d->munge_destroy = md;
401  d->munge_copy = mc;
402  mythrow(nlopt_set_max_objective(o, myfunc, d));
403  }
404 
405 
406  void remove_inequality_constraints()
407  {
408  nlopt_result ret = nlopt_remove_inequality_constraints(o);
409  mythrow(ret);
410  }
411 
412 
413  void add_inequality_constraint(func f, void *f_data, double tol = 0)
414  {
415  myfunc_data *d = new myfunc_data;
416  if (!d)
417  throw Seldon::NoMemory("SeldonOpt::add_inequality_constraint(func f, "
418  "void *f_data, double tol = 0)",
419  "Nlopt failed to allocate the"
420  " requested storage space.");
421  d->o = this;
422  d->f = f;
423  d->f_data = f_data;
424  d->mf = NULL;
425  d->vf = NULL;
426  d->munge_destroy = d->munge_copy = NULL;
427  mythrow(nlopt_add_inequality_constraint(o, myfunc, d, tol));
428  }
429 
430 
431  void add_inequality_constraint(svfunc vf, void *f_data,
432  double tol = 0)
433  {
434  myfunc_data *d = new myfunc_data;
435  if (!d)
436  throw Seldon::NoMemory("SeldonOpt::add_inequality_constraint(func f, "
437  "void *f_data, double tol = 0)",
438  "Nlopt failed to allocate the"
439  " requested storage space.");
440  d->o = this;
441  d->f = NULL;
442  d->f_data = f_data;
443  d->mf = NULL;
444  d->vf = vf;
445  d->munge_destroy = d->munge_copy = NULL;
446  mythrow(nlopt_add_inequality_constraint(o, myvfunc, d, tol));
447  alloc_tmp();
448  }
449 
450 
451  void add_inequality_mconstraint(mfunc mf, void *f_data,
452  const Seldon::Vector<double> &tol)
453  {
454  myfunc_data *d = new myfunc_data;
455  if (!d)
456  throw Seldon::NoMemory("SeldonOpt::add_inequality_mconstraint(func f,"
457  " void *f_data, double tol = 0)",
458  "Nlopt failed to allocate the"
459  " requested storage space.");
460  d->o = this;
461  d->mf = mf;
462  d->f_data = f_data;
463  d->f = NULL;
464  d->vf = NULL;
465  d->munge_destroy = d->munge_copy = NULL;
466  mythrow(nlopt_add_inequality_mconstraint(o, tol.GetSize(),
467  mymfunc, d,
468  tol.GetSize() == 0
469  ? NULL : tol.GetData()));
470  }
471 
472 
473  void remove_equality_constraints()
474  {
475  nlopt_result ret = nlopt_remove_equality_constraints(o);
476  mythrow(ret);
477  }
478 
479 
480  void add_equality_constraint(func f, void *f_data, double tol = 0)
481  {
482  myfunc_data *d = new myfunc_data;
483  if (!d)
484  throw Seldon::NoMemory("SeldonOpt::add_equality_constraint(func f, "
485  "void *f_data, double tol = 0)",
486  "Nlopt failed to allocate the"
487  " requested storage space.");
488  d->o = this;
489  d->f = f;
490  d->f_data = f_data;
491  d->mf = NULL;
492  d->vf = NULL;
493  d->munge_destroy = d->munge_copy = NULL;
494  nlopt_add_equality_constraint(o, myfunc, d, tol);
495  mythrow(nlopt_add_equality_constraint(o, myfunc, d, tol));
496  }
497 
498 
499  void add_equality_constraint(svfunc vf, void *f_data, double tol = 0)
500  {
501  myfunc_data *d = new myfunc_data;
502  if (!d)
503  throw Seldon::NoMemory("SeldonOpt::add_equality_constraint(func f, "
504  "void *f_data, double tol = 0)",
505  "Nlopt failed to allocate the"
506  " requested storage space.");
507  d->o = this;
508  d->f = NULL;
509  d->f_data = f_data;
510  d->mf = NULL;
511  d->vf = vf;
512  d->munge_destroy = d->munge_copy = NULL;
513  mythrow(nlopt_add_equality_constraint(o, myvfunc, d, tol));
514  alloc_tmp();
515  }
516 
517 
518  void add_equality_mconstraint(mfunc mf, void *f_data,
519  const Seldon::Vector<double> &tol)
520  {
521  myfunc_data *d = new myfunc_data;
522  if (!d)
523  throw Seldon::NoMemory("SeldonOpt::add_equality_mconstraint(func f, "
524  "void *f_data, double tol = 0)",
525  "Nlopt failed to allocate the"
526  " requested storage space.");
527  d->o = this;
528  d->mf = mf;
529  d->f_data = f_data;
530  d->f = NULL;
531  d->vf = NULL;
532  d->munge_destroy = d->munge_copy = NULL;
533  mythrow(nlopt_add_equality_mconstraint(o, tol.GetSize(),
534  mymfunc, d,
535  tol.GetSize() == 0 ?
536  NULL : tol.GetData()));
537  }
538 
539 
540  // For internal use in SWIG wrappers (see also above)
541  void add_inequality_constraint(func f, void *f_data,
542  nlopt_munge md, nlopt_munge mc,
543  double tol=0)
544  {
545  myfunc_data *d = new myfunc_data;
546  if (!d)
547  throw Seldon::NoMemory("SeldonOpt::add_inequality_constraint(func f, "
548  "void *f_data, double tol = 0)",
549  "Nlopt failed to allocate the"
550  " requested storage space.");
551  d->o = this;
552  d->f = f;
553  d->f_data = f_data;
554  d->mf = NULL;
555  d->vf = NULL;
556  d->munge_destroy = md;
557  d->munge_copy = mc;
558  mythrow(nlopt_add_inequality_constraint(o, myfunc, d, tol));
559  }
560 
561 
562  void add_equality_constraint(func f, void *f_data,
563  nlopt_munge md, nlopt_munge mc,
564  double tol=0)
565  {
566  myfunc_data *d = new myfunc_data;
567  if (!d)
568  throw Seldon::NoMemory("SeldonOpt::add_inequality_constraint(func f, "
569  "void *f_data, double tol = 0)",
570  "Nlopt failed to allocate the"
571  " requested storage space.");
572  d->o = this;
573  d->f = f;
574  d->f_data = f_data;
575  d->mf = NULL;
576  d->vf = NULL;
577  d->munge_destroy = md;
578  d->munge_copy = mc;
579  mythrow(nlopt_add_equality_constraint(o, myfunc, d, tol));
580  }
581 
582 
583  void add_inequality_mconstraint(mfunc mf, void *f_data,
584  nlopt_munge md, nlopt_munge mc,
585  const Seldon::Vector<double> &tol)
586  {
587  myfunc_data *d = new myfunc_data;
588  if (!d)
589  throw Seldon::NoMemory("SeldonOpt::add_inequality_mconstraint(func f,"
590  " void *f_data, double tol = 0)",
591  "Nlopt failed to allocate the"
592  " requested storage space.");
593  d->o = this;
594  d->mf = mf;
595  d->f_data = f_data;
596  d->f = NULL;
597  d->vf = NULL;
598  d->munge_destroy = md; d->munge_copy = mc;
599  mythrow(nlopt_add_inequality_mconstraint(o, tol.GetSize(),
600  mymfunc, d,
601  tol.GetSize() == 0
602  ? NULL : tol.GetData()));
603  }
604 
605 
606  void add_equality_mconstraint(mfunc mf, void *f_data,
607  nlopt_munge md, nlopt_munge mc,
608  const Seldon::Vector<double> &tol)
609  {
610  myfunc_data *d = new myfunc_data;
611  if (!d)
612  throw Seldon::NoMemory("SeldonOpt::add_equality_mconstraint(mfunc mf,"
613  " void *f_data,"
614  "nlopt_munge md, nlopt_munge mc,"
615  "const Seldon::Vector<double> &tol)",
616  "Nlopt failed to allocate the"
617  " requested storage space.");
618  d->o = this;
619  d->mf = mf;
620  d->f_data = f_data;
621  d->f = NULL;
622  d->vf = NULL;
623  d->munge_destroy = md;
624  d->munge_copy = mc;
625  mythrow(nlopt_add_equality_mconstraint(o, tol.GetSize(), mymfunc,
626  d, tol.GetSize() == 0
627  ? NULL :
628  tol.GetData()));
629  }
630 
631 
632 #define SELDON_NLOPT_GETSET_VEC(name) \
633  void set_##name(double val) { \
634  mythrow(nlopt_set_##name##1(o, val)); \
635  } \
636  void get_##name(Seldon::Vector<double> &v) const { \
637  if (o && int(nlopt_get_dimension(o)) != v.GetSize()) \
638  throw Seldon::WrongArgument("SeldonOpt::get_" #name "(Vector&)" \
639  " const", \
640  "Nlopt invalid argument."); \
641  mythrow(nlopt_get_##name(o, v.GetSize() == 0 ? NULL : \
642  v.GetData())); \
643  } \
644  Seldon::Vector<double> get_##name() const { \
645  if (!o) throw \
646  Seldon::Error("SeldonOpt::get_" #name "() const", \
647  "Uninitialized nlopt::SeldonOpt."); \
648  Seldon::Vector<double> v(nlopt_get_dimension(o)); \
649  get_##name(v); \
650  return v; \
651  } \
652  void set_##name(const Seldon::Vector<double> &v) { \
653  if (o && int(nlopt_get_dimension(o)) != v.GetSize()) \
654  throw Seldon::WrongArgument("SeldonOpt::get_" #name "(Vector&)" \
655  " const", \
656  "Nlopt invalid argument."); \
657  mythrow(nlopt_set_##name(o, v.GetSize() == 0 ? \
658  NULL : v.GetData())); \
659  }
660 
661  SELDON_NLOPT_GETSET_VEC(lower_bounds)
662  SELDON_NLOPT_GETSET_VEC(upper_bounds)
663 
664 
665 #define SELDON_NLOPT_GETSET(T, name) \
666  T get_##name() const { \
667  if (!o) throw \
668  Seldon::Error("SeldonOpt::get_" #name "() const", \
669  "Uninitialized nlopt::SeldonOpt."); \
670  return nlopt_get_##name(o); \
671  } \
672  void set_##name(T name) { \
673  mythrow(nlopt_set_##name(o, name)); \
674  }
675 
676 
677  SELDON_NLOPT_GETSET(double, stopval)
678  SELDON_NLOPT_GETSET(double, ftol_rel)
679  SELDON_NLOPT_GETSET(double, ftol_abs)
680  SELDON_NLOPT_GETSET(double, xtol_rel)
681  SELDON_NLOPT_GETSET_VEC(xtol_abs)
682  SELDON_NLOPT_GETSET(int, maxeval)
683  SELDON_NLOPT_GETSET(double, maxtime)
684 
685  SELDON_NLOPT_GETSET(int, force_stop)
686 
687 
688  void force_stop()
689  {
690  set_force_stop(1);
691  }
692 
693 
694  void set_local_optimizer(const SeldonOpt &lo)
695  {
696  nlopt_result ret = nlopt_set_local_optimizer(o, lo.o);
697  mythrow(ret);
698  }
699 
700 
701  SELDON_NLOPT_GETSET(unsigned, population)
702  SELDON_NLOPT_GETSET_VEC(initial_step)
703 
704 
705  void set_default_initial_step(const Seldon::Vector<double>& x)
706  {
707  nlopt_result ret
708  = nlopt_set_default_initial_step(o, x.GetSize() == 0 ?
709  NULL : x.GetData());
710  mythrow(ret);
711  }
712 
713 
714  void get_initial_step(const Seldon::Vector<double> &x,
715  Seldon::Vector<double> &dx) const
716  {
717  if (o && (int(nlopt_get_dimension(o)) != x.GetSize()
718  || int(nlopt_get_dimension(o)) != dx.GetSize()))
719  throw Seldon::WrongArgument("SeldonOpt::get_initial_step("
720  "Vector<double>& x, double& dx)",
721  "Dimension mismatch.");
722  nlopt_result ret = nlopt_get_initial_step(o, x.GetSize() == 0 ?
723  NULL : x.GetData(),
724  dx.GetSize() == 0 ?
725  NULL : dx.GetData());
726  mythrow(ret);
727  }
728 
729 
730  Seldon::Vector<double> get_initial_step_(const Seldon::Vector<double>& x)
731  const
732  {
733  if (!o)
734  throw Seldon::Error("SeldonOpt::get_initial_step_"
735  "(const Seldon::Vector<double>& x)",
736  "Uninitialized nlopt::SeldonOpt.");
737  Seldon::Vector<double> v(nlopt_get_dimension(o));
738  get_initial_step(x, v);
739  return v;
740  }
741  };
742 
743 
744 #undef SELDON_NLOPT_GETSET
745 #undef SELDON_NLOPT_GETSET_VEC
746 
747 
748 }
749 
750 #endif
Seldon::Vector< double >
nlopt::SeldonOpt
Definition: NLopt.hxx:37
Seldon::Error
Definition: Errors.hxx:38
Seldon::WrongArgument
Definition: Errors.hxx:76
Seldon::NoMemory
Definition: Errors.hxx:90