class opt {
private:
nlopt_opt o;
+ result last_result;
+ double last_optf;
void mythrow(nlopt_result ret) const {
switch (ret) {
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) {
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;
}
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 {
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