From 368270f108b2ae4a7988d9a39043768af668c9d3 Mon Sep 17 00:00:00 2001 From: SpaceIm <30052553+SpaceIm@users.noreply.github.com> Date: Mon, 24 Feb 2020 00:25:15 +0100 Subject: [PATCH] add nlopt/2.6.1 --- recipes/nlopt/all/CMakeLists.txt | 7 + recipes/nlopt/all/conandata.yml | 4 + recipes/nlopt/all/conanfile.py | 104 +++++++++++++ recipes/nlopt/all/test_package/CMakeLists.txt | 9 ++ recipes/nlopt/all/test_package/conanfile.py | 17 +++ recipes/nlopt/all/test_package/test_package.c | 142 ++++++++++++++++++ recipes/nlopt/config.yml | 3 + 7 files changed, 286 insertions(+) create mode 100644 recipes/nlopt/all/CMakeLists.txt create mode 100644 recipes/nlopt/all/conandata.yml create mode 100644 recipes/nlopt/all/conanfile.py create mode 100644 recipes/nlopt/all/test_package/CMakeLists.txt create mode 100644 recipes/nlopt/all/test_package/conanfile.py create mode 100644 recipes/nlopt/all/test_package/test_package.c create mode 100644 recipes/nlopt/config.yml diff --git a/recipes/nlopt/all/CMakeLists.txt b/recipes/nlopt/all/CMakeLists.txt new file mode 100644 index 0000000000000..217b9530b904d --- /dev/null +++ b/recipes/nlopt/all/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 2.8.11) +project(cmake_wrapper) + +include(conanbuildinfo.cmake) +conan_basic_setup() + +add_subdirectory("source_subfolder") diff --git a/recipes/nlopt/all/conandata.yml b/recipes/nlopt/all/conandata.yml new file mode 100644 index 0000000000000..8601ec6264aa7 --- /dev/null +++ b/recipes/nlopt/all/conandata.yml @@ -0,0 +1,4 @@ +sources: + "2.6.1": + url: "https://github.com/stevengj/nlopt/archive/v2.6.1.tar.gz" + sha256: "66d63a505187fb6f98642703bd0ef006fedcae2f9a6d1efa4f362ea919a02650" diff --git a/recipes/nlopt/all/conanfile.py b/recipes/nlopt/all/conanfile.py new file mode 100644 index 0000000000000..0d0a98539def9 --- /dev/null +++ b/recipes/nlopt/all/conanfile.py @@ -0,0 +1,104 @@ +import glob +import os + +from conans import ConanFile, CMake, tools + +class NloptConan(ConanFile): + name = "nlopt" + description = "Library for nonlinear optimization, wrapping many " \ + "algorithms for global and local, constrained or " \ + "unconstrained, optimization." + license = ["LGPL-2.1-or-later", "MIT"] + topics = ("conan", "nlopt", "optimization", "nonlinear") + homepage = "https://github.com/stevengj/nlopt" + url = "https://github.com/conan-io/conan-center-index" + exports_sources = "CMakeLists.txt" + generators = "cmake" + settings = "os", "arch", "compiler", "build_type" + options = { + "shared": [True, False], + "fPIC": [True, False], + "enable_cxx_routines": [True, False] + } + default_options = { + "shared": False, + "fPIC": True, + "enable_cxx_routines": True + } + + _cmake = None + + @property + def _source_subfolder(self): + return "source_subfolder" + + @property + def _build_subfolder(self): + return "build_subfolder" + + def config_options(self): + if self.settings.os == "Windows": + del self.options.fPIC + + def configure(self): + if not self.options.enable_cxx_routines: + del self.settings.compiler.libcxx + del self.settings.compiler.cppstd + + def source(self): + tools.get(**self.conan_data["sources"][self.version]) + os.rename(self.name + "-" + self.version, self._source_subfolder) + + def build(self): + cmake = self._configure_cmake() + cmake.build() + + def _configure_cmake(self): + if self._cmake: + return self._cmake + self._cmake = CMake(self) + self._cmake.definitions["NLOPT_CXX"] = self.options.enable_cxx_routines + self._cmake.definitions["NLOPT_FORTRAN"] = False + self._cmake.definitions["NLOPT_PYTHON"] = False + self._cmake.definitions["NLOPT_OCTAVE"] = False + self._cmake.definitions["NLOPT_MATLAB"] = False + self._cmake.definitions["NLOPT_GUILE"] = False + self._cmake.definitions["NLOPT_SWIG"] = False + self._cmake.definitions["NLOPT_TESTS"] = False + self._cmake.definitions["WITH_THREADLOCAL"] = True + self._cmake.configure(build_folder=self._build_subfolder) + return self._cmake + + def package(self): + self.copy("COPYING", dst="licenses", src=self._source_subfolder) + algs_licenses = [ + {"subdir": "ags" , "license_name": "COPYRIGHT"}, + {"subdir": "bobyqa", "license_name": "COPYRIGHT"}, + {"subdir": "cobyla", "license_name": "COPYRIGHT"}, + {"subdir": "direct", "license_name": "COPYING" }, + {"subdir": "esch" , "license_name": "COPYRIGHT"}, + {"subdir": "luskan", "license_name": "COPYRIGHT"}, + {"subdir": "newuoa", "license_name": "COPYRIGHT"}, + {"subdir": "slsqp" , "license_name": "COPYRIGHT"}, + {"subdir": "stogo" , "license_name": "COPYRIGHT"}, + {"subdir": "newuoa", "license_name": "COPYRIGHT"}, + {"subdir": "newuoa", "license_name": "COPYRIGHT"}, + ] + for alg_license in algs_licenses: + self.copy(alg_license["license_name"], + dst=os.path.join("licenses", alg_license["subdir"]), + src= os.path.join(self._source_subfolder, "src", "algs", alg_license["subdir"])) + cmake = self._configure_cmake() + cmake.install() + tools.rmdir(os.path.join(self.package_folder, "lib", "cmake")) + tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig")) + tools.rmdir(os.path.join(self.package_folder, "share")) + for pdb_file in glob.glob(os.path.join(self.package_folder, "bin", "*.pdb")): + os.remove(pdb_file) + + def package_info(self): + self.cpp_info.names["cmake_find_package"] = "NLopt" + self.cpp_info.names["cmake_find_package_multi"] = "NLopt" + self.cpp_info.libs = tools.collect_libs(self) + if self.settings.os == "Linux": + self.cpp_info.system_libs.append("m") diff --git a/recipes/nlopt/all/test_package/CMakeLists.txt b/recipes/nlopt/all/test_package/CMakeLists.txt new file mode 100644 index 0000000000000..843770bf38dbb --- /dev/null +++ b/recipes/nlopt/all/test_package/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 2.8.11) +project(test_package) + +include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) +conan_basic_setup() + +add_executable(${PROJECT_NAME} test_package.c) +target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS}) +set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE CXX) diff --git a/recipes/nlopt/all/test_package/conanfile.py b/recipes/nlopt/all/test_package/conanfile.py new file mode 100644 index 0000000000000..ea57a464900be --- /dev/null +++ b/recipes/nlopt/all/test_package/conanfile.py @@ -0,0 +1,17 @@ +import os + +from conans import ConanFile, CMake, tools + +class TestPackageConan(ConanFile): + settings = "os", "compiler", "build_type", "arch" + generators = "cmake" + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def test(self): + if not tools.cross_building(self.settings): + bin_path = os.path.join("bin", "test_package") + self.run(bin_path, run_environment=True) diff --git a/recipes/nlopt/all/test_package/test_package.c b/recipes/nlopt/all/test_package/test_package.c new file mode 100644 index 0000000000000..3f92320779b87 --- /dev/null +++ b/recipes/nlopt/all/test_package/test_package.c @@ -0,0 +1,142 @@ +#include + +#include +#include + +/****************************************************************************/ +/* test function from M. J. Box, "A new method of constrained optimization + and a comparison with other methods," Computer J. 8 (1), 42-52 (1965) */ + +int testfuncs_verbose = 1; +int testfuncs_counter = 0; + +static double testfuncs_status(int n, const double *x, double f) +{ + ++testfuncs_counter; + if (testfuncs_verbose) { + int i; + printf("f_%d (%g", testfuncs_counter, x[0]); + for (i = 1; i < n; ++i) + printf(", %g", x[i]); + printf(") = %g\n", f); + } + return f; +} + +#define RETURN(f) return testfuncs_status(n, x, f); + +static const double k1 = -145421.402, k2 = 2931.1506, k3 = -40.427932, k4 = 5106.192, + k5 = 15711.36, k6 = -161622.577, k7 = 4176.15328, k8 = 2.8260078, + k9 = 9200.476, k10 = 13160.295, k11 = -21686.9194, k12 = 123.56928, + k13 = -21.1188894, k14 = 706.834, k15 = 2898.573, k16 = 28298.388, + k17 = 60.81096, k18 = 31.242116, k19 = 329.574, k20 = -2882.082, + k21 = 74095.3845, k22 = -306.262544, k23 = 16.243649, k24 = -3094.252, + k25 = -5566.2628, k26 = -26237, k27 = 99, k28 = -0.42, k29 = 1300, k30 = 2100, k31 = 925548.252, k32 = -61968.8432, k33 = 23.3088196, k34 = -27096.648, k35 = -50843.766; + +static double box(int n, const double *x, double *grad, void *data) +{ + double x1 = x[0], x2 = x[1], x3 = x[2], x4 = x[3], x5 = x[4]; + double b, x6, y1, y2, y3, y4, u; + const double a0 = 9, a1 = 15, a2 = 50, a3 = 9.583, a4 = 20, a5 = 15, a6 = 6, a7 = 0.75; + b = x2 + 0.01 * x3; + x6 = (k1 + k2 * x2 + k3 * x3 + k4 * x4 + k5 * x5) * x1; + y1 = k6 + k7 * x2 + k8 * x3 + k9 * x4 + k10 * x5; + y2 = k11 + k12 * x2 + k13 * x3 + k14 * x4 + k15 * x5; + y3 = k16 + k17 * x2 + k18 * x3 + k19 * x4 + k20 * x5; + y4 = k21 + k22 * x2 + k23 * x3 + k24 * x4 + k25 * x5; + u = a2 * y1 + a3 * y2 + a4 * y3 + a5 * y4 + 7840 * a6 - 100000 * a0 - 50800 * b * a7 + k31 + k32 * x2 + k33 * x3 + k34 * x4 + k35 * x5; + if (grad) { + int i; + grad[0] = u + a1 * (k1 + k2 * x2 + k3 * x3 + k4 * x4 + k5 * x5); + grad[1] = x1 * (a2 * k7 + a3 * k12 + a4 * k17 + a5 * k22 - 50800 * a7 + k32) + a1 * (k2 * x1); + grad[2] = x1 * (a2 * k8 + a3 * k13 + a4 * k18 + a5 * k23 - 50800 * a7 * 0.01) + a1 * x1 * k3; + grad[3] = x1 * (a2 * k9 + a3 * k14 + a4 * k19 + a5 * k24) + a1 * x1 * k4; + grad[4] = x1 * (a2 * k10 + a3 * k15 + a4 * k20 + a5 * k25) + a1 * x1 * k5; + for (i = 0; i < 5; ++i) + grad[i] = -grad[i]; + } + RETURN(-(u * x1 - 24345 + a1 * x6)); +} + +static double box_constraint(int n, const double *x, double *grad, void *data) +{ + int which_constraint = *((int *) data); + double x1 = x[0], x2 = x[1], x3 = x[2], x4 = x[3], x5 = x[4]; + double x6, y1, y2, y3, x7, x8; + int i; + + x6 = (k1 + k2 * x2 + k3 * x3 + k4 * x4 + k5 * x5) * x1; + y1 = k6 + k7 * x2 + k8 * x3 + k9 * x4 + k10 * x5; + y2 = k11 + k12 * x2 + k13 * x3 + k14 * x4 + k15 * x5; + y3 = k16 + k17 * x2 + k18 * x3 + k19 * x4 + k20 * x5; + x7 = (y1 + y2 + y3) * x1; + x8 = (k26 + k27 * x2 + k28 * x3 + k29 * x4 + k30 * x5) * x1 + x6 + x7; + + if (grad) { + grad[0] = grad[1] = grad[2] = grad[3] = grad[4] = 0; + + if (which_constraint != 2 && which_constraint != -2) { + /* grad x6 */ + grad[0] += (k1 + k2 * x2 + k3 * x3 + k4 * x4 + k5 * x5); + grad[1] += x1 * k2; + grad[2] += x1 * k3; + grad[3] += x1 * k4; + grad[4] += x1 * k5; + } + if (which_constraint != 1 && which_constraint != -1) { + /* grad x7 */ + grad[0] += (y1 + y2 + y3); + grad[1] += x1 * (k7 + k12 + k17); + grad[2] += x1 * (k8 + k13 + k18); + grad[3] += x1 * (k9 + k14 + k19); + grad[4] += x1 * (k10 + k15 + k20); + } + if (which_constraint == 3 || which_constraint == -3) { + /* grad (x8 - x6 - x7) */ + grad[0] += k26 + k27 * x2 + k28 * x3 + k29 * x4 + k30 * x5; + grad[1] += x1 * k27; + grad[2] += x1 * k28; + grad[3] += x1 * k29; + grad[4] += x1 * k30; + } + } + + if (which_constraint == -1) { + if (grad) + for (i = 0; i < 5; ++i) + grad[i] = -grad[i]; + return -x6; + } else if (which_constraint == 1) { + return x6 - 294000; + } else if (which_constraint == -2) { + if (grad) + for (i = 0; i < 5; ++i) + grad[i] = -grad[i]; + return -x7; + } else if (which_constraint == 2) { + return x7 - 294000; + } else if (which_constraint == -3) { + if (grad) + for (i = 0; i < 5; ++i) + grad[i] = -grad[i]; + return -x8; + } else if (which_constraint == 3) { + return x8 - 277200; + } + return 0; +} + +int main(void) +{ + const double box_lb[5] = { 0, 1.2, 20, 9, 6.5 }; + const double box_ub[5] = { HUGE_VAL, 2.4, 60, 9.3, 7.0 }; + const double box_xmin[5] = { 4.53743, 2.4, 60, 9.3, 7.0 }; + + int cdata[6] = { -1, 1, -2, 2, -3, 3 }; + double x[5] = { 2.52, 2, 37.5, 9.25, 6.8 }; + double minf; + + nlopt_minimize_constrained(NLOPT_LN_COBYLA, 5, box, NULL, 6, box_constraint, cdata, sizeof(int), box_lb, box_ub, x, &minf, -HUGE_VAL, 1e-10, 0, 0, NULL, 0, 0); + printf("found f(%g,%g,%g,%g,%g) = %0.8f after %d iters\n", x[0], x[1], x[2], x[3], x[4], minf, testfuncs_counter); + return 0; +} diff --git a/recipes/nlopt/config.yml b/recipes/nlopt/config.yml new file mode 100644 index 0000000000000..36b28f1209ae7 --- /dev/null +++ b/recipes/nlopt/config.yml @@ -0,0 +1,3 @@ +versions: + "2.6.1": + folder: all