From: stevenj Date: Thu, 3 Jun 2010 23:14:01 +0000 (-0400) Subject: provide alternate optimize() method that does not areturn results via reference argum... X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=bf5119cafd7788b5ff6aeb214a0637ddef9e795d;p=nlopt.git provide alternate optimize() method that does not areturn results via reference arguments, to make it easier for SWIG to translate darcs-hash:20100603231401-c8de0-f74e227905ebfc20c7bb432282a5f52bdc9349ae.gz --- diff --git a/api/nlopt-in.hpp b/api/nlopt-in.hpp index 2adad66..08f89f1 100644 --- a/api/nlopt-in.hpp +++ b/api/nlopt-in.hpp @@ -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 optimize(const std::vector &x0) { + std::vector 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 { diff --git a/swig/nlopt-guile.i b/swig/nlopt-guile.i index 4e6ea02..15c6fcf 100644 --- a/swig/nlopt-guile.i +++ b/swig/nlopt-guile.i @@ -27,25 +27,8 @@ static double vfunc_guile(const std::vector &x, throw std::invalid_argument("invalid result passed to nlopt"); } -extern SCM nlopt_do_optimize(nlopt::opt &opt, const std::vector &x); - -// wrapper around opt::optimize that returns a triplet (result . (x . optf)) -SCM nlopt_do_optimize(nlopt::opt &opt, const std::vector &x0) -{ - std::vector 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 &x); - %typemap(in)(nlopt::vfunc vf, void *f_data) { $1 = vfunc_guile; $2 = (void*) $input; // input is SCM pointer to Scheme function