chiark / gitweb /
provide alternate optimize() method that does not areturn results via reference argum...
authorstevenj <stevenj@alum.mit.edu>
Thu, 3 Jun 2010 23:14:01 +0000 (19:14 -0400)
committerstevenj <stevenj@alum.mit.edu>
Thu, 3 Jun 2010 23:14:01 +0000 (19:14 -0400)
darcs-hash:20100603231401-c8de0-f74e227905ebfc20c7bb432282a5f52bdc9349ae.gz

api/nlopt-in.hpp
swig/nlopt-guile.i

index 2adad660332abedda1a717c9ffe98034fdc513fe..08f89f11179214b080875100e4bf16ea6cdb76f2 100644 (file)
@@ -70,6 +70,8 @@ namespace nlopt {
   class opt {
   private:
     nlopt_opt o;
+    result last_result;
+    double last_optf;
     
     void mythrow(nlopt_result ret) const {
       switch (ret) {
@@ -130,16 +132,20 @@ namespace nlopt {
 
   public:
     // Constructors etc.
-    opt() : o(NULL), xtmp(0), gradtmp(0), gradtmp0(0) {}
+    opt() : o(NULL), xtmp(0), gradtmp(0), gradtmp0(0), 
+           last_result(nlopt::FAILURE), last_optf(HUGE_VAL) {}
     ~opt() { nlopt_destroy(o); }
     opt(algorithm a, unsigned n) : 
       o(nlopt_create(nlopt_algorithm(a), n)), 
-      xtmp(0), gradtmp(0), gradtmp0(0) {
+      xtmp(0), gradtmp(0), gradtmp0(0),
+      last_result(nlopt::FAILURE), last_optf(HUGE_VAL) {
       if (!o) throw std::bad_alloc();
       nlopt_set_free_f_data(o, 1);
     }
-    opt(const opt& from) : o(nlopt_copy(from.o)) {
-      if (from.o && !o) throw std::bad_alloc();
+    opt(const opt& f) : o(nlopt_copy(f.o)), 
+                       xtmp(f.xtmp), gradtmp(f.gradtmp), gradtmp0(0),
+                       last_result(f.last_result), last_optf(f.last_optf) {
+      if (f.o && !o) throw std::bad_alloc();
       mythrow(nlopt_dup_f_data(o, sizeof(myfunc_data)));
     }
     opt& operator=(opt const& f) {
@@ -148,6 +154,8 @@ namespace nlopt {
       o = nlopt_copy(f.o);
       if (f.o && !o) throw std::bad_alloc();
       mythrow(nlopt_dup_f_data(o, sizeof(myfunc_data)));
+      xtmp = f.xtmp; gradtmp = f.gradtmp;
+      last_result = f.last_result; last_optf = f.last_optf;
       return *this;
     }
 
@@ -156,9 +164,20 @@ namespace nlopt {
       if (o && nlopt_get_dimension(o) != x.size())
         throw std::invalid_argument("dimension mismatch");
       nlopt_result ret = nlopt_optimize(o, x.empty() ? NULL : &x[0], &opt_f);
+      last_result = result(ret);
+      last_optf = opt_f;
       mythrow(ret);
-      return result(ret);
+      return last_result;
+    }
+
+    // variant mainly useful for SWIG wrappers:
+    std::vector<double> optimize(const std::vector<double> &x0) {
+      std::vector<double> x(x0);
+      last_result = optimize(x, last_optf);
+      return x;
     }
+    result last_optimize_result() const { return last_result; }
+    double last_optimum_value() const { return last_optf; }
 
     // accessors:
     algorithm get_algorithm() const {
index 4e6ea022a47b558b3226bcd0fff3075a56b22ae9..15c6fcffbb1c2253cc81f6fee086413601c1a915 100644 (file)
@@ -27,25 +27,8 @@ static double vfunc_guile(const std::vector<double> &x,
   throw std::invalid_argument("invalid result passed to nlopt");
 }
 
-extern SCM nlopt_do_optimize(nlopt::opt &opt, const std::vector<double> &x);
-
-// wrapper around opt::optimize that returns a triplet (result . (x . optf))
-SCM nlopt_do_optimize(nlopt::opt &opt, const std::vector<double> &x0)
-{
-  std::vector<double> x(x0);
-  double optf;
-  nlopt::result res = opt.optimize(x, optf);
-  SCM xscm = scm_c_make_vector(x.size(), SCM_UNSPECIFIED);
-  for (unsigned i = 0; i < x.size(); ++i)
-    scm_c_vector_set_x(xscm, i, scm_make_real(x[i]));
-  return scm_cons(scm_from_int(int(res)),
-                 scm_cons(xscm, scm_make_real(optf)));
-}
-
 %}
 
-extern SCM nlopt_do_optimize(nlopt::opt &opt, const std::vector<double> &x);
-
 %typemap(in)(nlopt::vfunc vf, void *f_data) {
   $1 = vfunc_guile;
   $2 = (void*) $input; // input is SCM pointer to Scheme function