// Nonlinear constraints:
- void remove_inequality_constraints(void) {
+ void remove_inequality_constraints() {
nlopt_result ret = nlopt_remove_inequality_constraints(o);
mythrow(ret);
}
alloc_tmp();
}
- void remove_equality_constraints(void) {
+ void remove_equality_constraints() {
nlopt_result ret = nlopt_remove_equality_constraints(o);
mythrow(ret);
}
#define NLOPT_GETSET_VEC(name) \
void set_##name(double val) { \
- nlopt_result ret = nlopt_set_##name##1(o, val); \
- mythrow(ret); \
+ mythrow(nlopt_set_##name##1(o, val)); \
} \
void get_##name(std::vector<double> &v) const { \
if (o && nlopt_get_dimension(o) != v.size()) \
throw std::invalid_argument("dimension mismatch"); \
- nlopt_result ret = nlopt_get_##name(o, v.empty() ? NULL : &v[0]); \
- mythrow(ret); \
+ mythrow(nlopt_get_##name(o, v.empty() ? NULL : &v[0])); \
} \
- std::vector<double> get_##name(void) const { \
+ std::vector<double> get_##name() const { \
if (!o) throw std::runtime_error("uninitialized nlopt::opt"); \
std::vector<double> v(nlopt_get_dimension(o)); \
get_##name(v); \
void set_##name(const std::vector<double> &v) { \
if (o && nlopt_get_dimension(o) != v.size()) \
throw std::invalid_argument("dimension mismatch"); \
- nlopt_result ret = nlopt_set_##name(o, v.empty() ? NULL : &v[0]); \
- mythrow(ret); \
+ mythrow(nlopt_set_##name(o, v.empty() ? NULL : &v[0])); \
}
NLOPT_GETSET_VEC(lower_bounds)
return nlopt_get_##name(o); \
} \
void set_##name(T name) { \
- nlopt_result ret = nlopt_set_##name(o, name); \
- mythrow(ret); \
+ mythrow(nlopt_set_##name(o, name)); \
}
NLOPT_GETSET(double, stopval)
NLOPT_GETSET(double, ftol_rel)
//////////////////////////////////////////////////////////////////////
- inline void srand(unsigned long seed) { nlopt_srand(seed); }
- inline void srand_time(void) { nlopt_srand_time(); }
- inline void version(int &major, int &minor, int &bugfix) {
+ void srand(unsigned long seed) { nlopt_srand(seed); }
+ void srand_time() { nlopt_srand_time(); }
+ void version(int &major, int &minor, int &bugfix) {
nlopt_version(&major, &minor, &bugfix);
}
- inline const char *algorithm_name(algorithm a) {
+ int version_major() {
+ int major, minor, bugfix;
+ nlopt_version(&major, &minor, &bugfix);
+ return major;
+ }
+ int version_minor() {
+ int major, minor, bugfix;
+ nlopt_version(&major, &minor, &bugfix);
+ return minor;
+ }
+ int version_bugfix() {
+ int major, minor, bugfix;
+ nlopt_version(&major, &minor, &bugfix);
+ return bugfix;
+ }
+ const char *algorithm_name(algorithm a) {
return nlopt_algorithm_name(nlopt_algorithm(a));
}
nlopt_result nlopt_remove_inequality_constraints(nlopt_opt opt)
{
+ if (!opt) return NLOPT_INVALID_ARGS;
free(opt->fc);
opt->fc = NULL;
opt->m = opt->m_alloc = 0;
nlopt_result nlopt_remove_equality_constraints(nlopt_opt opt)
{
+ if (!opt) return NLOPT_INVALID_ARGS;
free(opt->h);
opt->h = NULL;
opt->p = opt->p_alloc = 0;
-EXTRA_DIST = nlopt.i nlopt-guile.i nlopt-enum-renames.i nlopt.scm.in
+SWIG_SRC = nlopt.i nlopt-exceptions.i nlopt-enum-renames.i
+EXTRA_DIST = $(SWIG_SRC) nlopt-guile.i nlopt.scm.in
BUILT_SOURCES = nlopt-guile.cpp nlopt-enum-renames.i nlopt.scm.in
if MAINTAINER_MODE
-nlopt-guile.cpp: nlopt.i nlopt-enum-renames.i nlopt-guile.i $(HDR)
+nlopt-guile.cpp: $(SWIG_SRC) nlopt-guile.i $(HDR)
swig -I$(top_builddir)/api -outdir $(builddir) -c++ -guile -scmstub -o $@ nlopt.i
rm -f nlopt.scm.in
mv nlopt.scm nlopt.scm.in
--- /dev/null
+// since exception specifications in C++ are evil, we instead provide
+// %catches specifications here so that SWIG can generate language-specific
+// exceptions (at least for exceptions explicitly thrown by NLopt)
+//
+// manually doing this stuff is annoying
+
+%catches(std::bad_alloc) nlopt::opt::opt();
+%catches(std::bad_alloc) nlopt::opt::opt(algorithm a, unsigned n);
+%catches(std::bad_alloc) nlopt::opt::opt(const opt& f);
+%catches(std::bad_alloc) nlopt::opt::operator=(opt const& f);
+
+%catches(nlopt::roundoff_limited,nlopt::forced_stop,std::runtime_error,std::bad_alloc,std::invalid_argument) nlopt::opt::optimize(std::vector<double> &x, double &opt_f);
+%catches(nlopt::roundoff_limited,nlopt::forced_stop,std::runtime_error,std::bad_alloc,std::invalid_argument) nlopt::opt::optimize(const std::vector<double> &x0);
+
+%catches(std::runtime_error) nlopt::opt::get_algorithm();
+%catches(std::runtime_error) nlopt::opt::get_algorithm_name();
+%catches(std::runtime_error) nlopt::opt::get_dimension();
+
+%catches(std::bad_alloc,std::invalid_argument) nlopt::opt::set_min_objective(func f, void *f_data);
+%catches(std::bad_alloc,std::invalid_argument) nlopt::opt::set_min_objective(vfunc vf, void *f_data);
+%catches(std::bad_alloc,std::invalid_argument) nlopt::opt::set_max_objective(func f, void *f_data);
+%catches(std::bad_alloc,std::invalid_argument) nlopt::opt::set_max_objective(vfunc vf, void *f_data);
+
+%catches(std::invalid_argument) nlopt::opt::remove_inequality_constraints();
+%catches(std::bad_alloc,std::invalid_argument) nlopt::opt::add_inequality_constraint(func f, void *f_data, double tol=0);
+%catches(std::bad_alloc,std::invalid_argument) nlopt::opt::add_inequality_constraint(vfunc vf, void *f_data, double tol=0);
+%catches(std::invalid_argument) nlopt::opt::remove_equality_constraints();
+%catches(std::bad_alloc,std::invalid_argument) nlopt::opt::add_equality_constraint(func f, void *f_data, double tol=0);
+%catches(std::bad_alloc,std::invalid_argument) nlopt::opt::add_equality_constraint(vfunc vf, void *f_data, double tol=0);
+
+#define SET_EXCEPT(name, T) %catches(std::invalid_argument) nlopt::opt::set_##name(T val);
+#define GET_EXCEPT(name) %catches(std::invalid_argument) nlopt::opt::get_##name();
+#define SETVEC_EXCEPT(name) %catches(std::invalid_argument) nlopt::opt::set_##name(const std::vector<double> &v);
+#define GETVEC_EXCEPT(name) %catches(std::bad_alloc,std::invalid_argument) nlopt::opt::get_##name(std::vector<double> &v);
+#define GETSET_EXCEPT(name, T) GET_EXCEPT(name) SET_EXCEPT(name, T)
+#define GETSETVEC_EXCEPT(name) GET_EXCEPT(name) SET_EXCEPT(name, double) GETVEC_EXCEPT(name) SETVEC_EXCEPT(name)
+
+GETSETVEC_EXCEPT(lower_bounds)
+GETSETVEC_EXCEPT(upper_bounds)
+
+GETSET_EXCEPT(stopval, double)
+GETSET_EXCEPT(ftol_rel, double)
+GETSET_EXCEPT(ftol_abs, double)
+GETSET_EXCEPT(xtol_rel, double)
+GETSETVEC_EXCEPT(xtol_abs)
+GETSET_EXCEPT(maxeval, int)
+GETSET_EXCEPT(maxtime, double)
+GETSET_EXCEPT(force_stop, int)
+
+%catches(std::invalid_argument) nlopt::opt::force_stop();
+
+%catches(std::bad_alloc,std::invalid_argument) nlopt::opt::set_local_optimizer(const opt &lo);
+
+GETSET_EXCEPT(local_population, unsigned)
+GETSETVEC_EXCEPT(initial_step)
+
+%catches(std::bad_alloc,std::invalid_argument) nlopt::opt::set_default_initial_step(const std::vector<double> &x);
+%catches(std::invalid_argument) nlopt::opt::get_initial_step(const std::vector<double> &x, std::vector<double> &dx);
+%catches(std::bad_alloc,std::invalid_argument) nlopt::opt::get_initial_step(const std::vector<double> &x);
%rename(nlopt_algorithm_name) nlopt::algorithm_name;
%include "nlopt-enum-renames.i"
+%include "nlopt-exceptions.i"
+
#ifdef SWIGGUILE
%include "nlopt-guile.i"
#endif