nlopt 2.6.1

This commit is contained in:
Jamie Snape 2019-05-21 09:26:44 -04:00 committed by Izaak Beekman
parent c754a6fd71
commit d8dd058ac8
No known key found for this signature in database
GPG key ID: A93CE70D8021BD0F

View file

@ -1,9 +1,9 @@
class Nlopt < Formula
desc "Free/open-source library for nonlinear optimization"
homepage "https://nlopt.readthedocs.io/"
url "https://github.com/stevengj/nlopt/releases/download/nlopt-2.4.2/nlopt-2.4.2.tar.gz"
sha256 "8099633de9d71cbc06cd435da993eb424bbcdbded8f803cdaa9fb8c6e09c8e89"
revision 2
url "https://github.com/stevengj/nlopt/archive/v2.6.1.tar.gz"
sha256 "66d63a505187fb6f98642703bd0ef006fedcae2f9a6d1efa4f362ea919a02650"
head "https://github.com/stevengj/nlopt.git"
bottle do
cellar :any
@ -13,81 +13,37 @@ class Nlopt < Formula
sha256 "e3ea4064beff9f39ab2fed6a9e5f643a18fd7abb2b2302713ce7f58f970ceb09" => :sierra
end
head do
url "https://github.com/stevengj/nlopt.git"
depends_on "cmake" => :build
depends_on "swig" => :build
end
depends_on "numpy"
depends_on "cmake" => [:build, :test]
def install
ENV.deparallelize
args = *std_cmake_args + %w[
-DNLOPT_GUILE=OFF
-DNLOPT_MATLAB=OFF
-DNLOPT_OCTAVE=OFF
-DNLOPT_PYTHON=OFF
-DNLOPT_SWIG=OFF
-DNLOPT_TESTS=OFF
]
if build.head?
system "cmake", ".", *std_cmake_args,
"-DBUILD_MATLAB=OFF",
"-DBUILD_OCTAVE=OFF",
"-DWITH_CXX=ON"
else
system "./configure", "--prefix=#{prefix}",
"--enable-shared",
"--with-cxx",
"--without-octave"
mkdir "build" do
system "cmake", *args, ".."
system "make"
system "make", "install"
end
system "make", "install"
# Create library links for C programs
%w[0.dylib dylib a].each do |suffix|
lib.install_symlink "#{lib}/libnlopt_cxx.#{suffix}" => "#{lib}/libnlopt.#{suffix}"
end
pkgshare.install "test/box.c"
end
test do
# Based on https://nlopt.readthedocs.io/en/latest/NLopt_Tutorial/#Example_in_C.2FC.2B.2B
(testpath/"test.c").write <<~EOS
#include <math.h>
#include <nlopt.h>
#include <stdio.h>
double myfunc(unsigned n, const double *x, double *grad, void *my_func_data) {
if (grad) {
grad[0] = 0.0;
grad[1] = 0.5 / sqrt(x[1]);
}
return sqrt(x[1]);
}
typedef struct { double a, b; } my_constraint_data;
double myconstraint(unsigned n, const double *x, double *grad, void *data) {
my_constraint_data *d = (my_constraint_data *) data;
double a = d->a, b = d->b;
if (grad) {
grad[0] = 3 * a * (a*x[0] + b) * (a*x[0] + b);
grad[1] = -1.0;
}
return ((a*x[0] + b) * (a*x[0] + b) * (a*x[0] + b) - x[1]);
}
int main() {
double lb[2] = { -HUGE_VAL, 0 }; /* lower bounds */
nlopt_opt opt;
opt = nlopt_create(NLOPT_LD_MMA, 2); /* algorithm and dimensionality */
nlopt_set_lower_bounds(opt, lb);
nlopt_set_min_objective(opt, myfunc, NULL);
my_constraint_data data[2] = { {2,0}, {-1,1} };
nlopt_add_inequality_constraint(opt, myconstraint, &data[0], 1e-8);
nlopt_add_inequality_constraint(opt, myconstraint, &data[1], 1e-8);
nlopt_set_xtol_rel(opt, 1e-4);
double x[2] = { 1.234, 5.678 }; /* some initial guess */
double minf; /* the minimum objective value, upon return */
if (nlopt_optimize(opt, x, &minf) < 0)
return 1;
else
printf("found minimum at f(%g,%g) = %0.10g", x[0], x[1], minf);
nlopt_destroy(opt);
}
(testpath/"CMakeLists.txt").write <<~EOS
cmake_minimum_required(VERSION 3.0)
project(box C)
find_package(NLopt REQUIRED)
add_executable(box "#{pkgshare}/box.c")
target_link_libraries(box NLopt::nlopt)
EOS
system ENV.cc, "test.c", "-o", "test", "-L#{opt_lib}", "-lnlopt", "-lm"
assert_match "found minimum", shell_output("./test")
system "cmake", "."
system "make"
assert_match "found", shell_output("./box")
end
end