|
| 1 | +class Nlopt < Formula |
| 2 | + desc "Free/open-source library for nonlinear optimization" |
| 3 | + homepage "http://ab-initio.mit.edu/nlopt" |
| 4 | + url "http://ab-initio.mit.edu/nlopt/nlopt-2.4.2.tar.gz" |
| 5 | + sha256 "8099633de9d71cbc06cd435da993eb424bbcdbded8f803cdaa9fb8c6e09c8e89" |
| 6 | + revision 2 |
| 7 | + |
| 8 | + head do |
| 9 | + url "https://github.com/stevengj/nlopt.git" |
| 10 | + depends_on "cmake" => :build |
| 11 | + depends_on "swig" => :build |
| 12 | + end |
| 13 | + |
| 14 | + depends_on "numpy" |
| 15 | + |
| 16 | + def install |
| 17 | + ENV.deparallelize |
| 18 | + |
| 19 | + if build.head? |
| 20 | + system "cmake", ".", *std_cmake_args, |
| 21 | + "-DBUILD_MATLAB=OFF", |
| 22 | + "-DBUILD_OCTAVE=OFF", |
| 23 | + "-DWITH_CXX=ON" |
| 24 | + else |
| 25 | + system "./configure", "--prefix=#{prefix}", |
| 26 | + "--enable-shared", |
| 27 | + "--with-cxx", |
| 28 | + "--without-octave" |
| 29 | + system "make" |
| 30 | + end |
| 31 | + system "make", "install" |
| 32 | + |
| 33 | + # Create library links for C programs |
| 34 | + %w[0.dylib dylib a].each do |suffix| |
| 35 | + lib.install_symlink "#{lib}/libnlopt_cxx.#{suffix}" => "#{lib}/libnlopt.#{suffix}" |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + test do |
| 40 | + # Based on http://ab-initio.mit.edu/wiki/index.php/NLopt_Tutorial#Example_in_C.2FC.2B.2B |
| 41 | + (testpath/"test.c").write <<-EOS.undent |
| 42 | + #include <math.h> |
| 43 | + #include <nlopt.h> |
| 44 | + #include <stdio.h> |
| 45 | + double myfunc(unsigned n, const double *x, double *grad, void *my_func_data) { |
| 46 | + if (grad) { |
| 47 | + grad[0] = 0.0; |
| 48 | + grad[1] = 0.5 / sqrt(x[1]); |
| 49 | + } |
| 50 | + return sqrt(x[1]); |
| 51 | + } |
| 52 | + typedef struct { double a, b; } my_constraint_data; |
| 53 | + double myconstraint(unsigned n, const double *x, double *grad, void *data) { |
| 54 | + my_constraint_data *d = (my_constraint_data *) data; |
| 55 | + double a = d->a, b = d->b; |
| 56 | + if (grad) { |
| 57 | + grad[0] = 3 * a * (a*x[0] + b) * (a*x[0] + b); |
| 58 | + grad[1] = -1.0; |
| 59 | + } |
| 60 | + return ((a*x[0] + b) * (a*x[0] + b) * (a*x[0] + b) - x[1]); |
| 61 | + } |
| 62 | + int main() { |
| 63 | + double lb[2] = { -HUGE_VAL, 0 }; /* lower bounds */ |
| 64 | + nlopt_opt opt; |
| 65 | + opt = nlopt_create(NLOPT_LD_MMA, 2); /* algorithm and dimensionality */ |
| 66 | + nlopt_set_lower_bounds(opt, lb); |
| 67 | + nlopt_set_min_objective(opt, myfunc, NULL); |
| 68 | + my_constraint_data data[2] = { {2,0}, {-1,1} }; |
| 69 | + nlopt_add_inequality_constraint(opt, myconstraint, &data[0], 1e-8); |
| 70 | + nlopt_add_inequality_constraint(opt, myconstraint, &data[1], 1e-8); |
| 71 | + nlopt_set_xtol_rel(opt, 1e-4); |
| 72 | + double x[2] = { 1.234, 5.678 }; /* some initial guess */ |
| 73 | + double minf; /* the minimum objective value, upon return */ |
| 74 | +
|
| 75 | + if (nlopt_optimize(opt, x, &minf) < 0) |
| 76 | + return 1; |
| 77 | + else |
| 78 | + printf("found minimum at f(%g,%g) = %0.10g", x[0], x[1], minf); |
| 79 | + nlopt_destroy(opt); |
| 80 | + } |
| 81 | + EOS |
| 82 | + system ENV.cc, "test.c", "-o", "test", "-lnlopt", "-lm" |
| 83 | + assert_match "found minimum", shell_output("./test") |
| 84 | + end |
| 85 | +end |
0 commit comments