From: stevenj Date: Thu, 3 Jun 2010 02:58:24 +0000 (-0400) Subject: Guile wrapper seems mostly functional, sans exceptions (needed wrapper around opt... X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=34c83c497bad8afff41f3e82749638f5fb11ee48;p=nlopt.git Guile wrapper seems mostly functional, sans exceptions (needed wrapper around opt::optimize to return triplet) darcs-hash:20100603025824-c8de0-bfd2efa65d9993029b48081e35b7fb28fb331a22.gz --- diff --git a/swig/Makefile.am b/swig/Makefile.am index deeaf57..53036a1 100644 --- a/swig/Makefile.am +++ b/swig/Makefile.am @@ -8,6 +8,7 @@ HDR = $(top_builddir)/api/nlopt.hpp # Guile wrapper libnlopt@NLOPT_SUFFIX@_guile_la_SOURCES = nlopt-guile.cpp +libnlopt@NLOPT_SUFFIX@_guile_la_LIBADD = ../libnlopt@NLOPT_SUFFIX@.la libnlopt@NLOPT_SUFFIX@_guile_la_LDFLAGS = -version-info @SHARED_VERSION_INFO@ libnlopt@NLOPT_SUFFIX@_guile_la_CPPFLAGS = $(GUILE_CPPFLAGS) -I$(top_builddir)/api diff --git a/swig/nlopt-guile.i b/swig/nlopt-guile.i index faf23f8..4e6ea02 100644 --- a/swig/nlopt-guile.i +++ b/swig/nlopt-guile.i @@ -1,6 +1,7 @@ // -*- C++ -*- %{ +// vfunc wrapper around Guile function (val . grad) = f(x) static double vfunc_guile(const std::vector &x, std::vector &grad, void *f) { SCM xscm = scm_c_make_vector(x.size(), SCM_UNSPECIFIED); @@ -25,17 +26,37 @@ 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 } +%typecheck(SWIG_TYPECHECK_POINTER)(nlopt::vfunc vf, void *f_data) { + $1 = SCM_NFALSEP(scm_procedure_p($input)); +} // export constants as variables, rather than as functions returning the value %feature("constasvar", "1"); %scheme %{ -(dynamic-link "libnlopt@NLOPT_SUFFIX@.so") (load-extension "libnlopt@NLOPT_SUFFIX@_guile.so" "SWIG_init") %}