diff --git a/CMakeLists.txt b/CMakeLists.txt index b6cb0b2b..fd608697 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.15) -set(libcmaes_VERSION 0.10.2) +set(libcmaes_VERSION 0.10.3) project (libcmaes VERSION ${libcmaes_VERSION} @@ -28,6 +28,7 @@ option (LIBCMAES_BUILD_TESTS "build tests" OFF) option (LIBCMAES_BUILD_EXAMPLES "build examples" ${LIBCMAES_TOP_LEVEL}) option (LIBCMAES_USE_OPENMP "Use OpenMP for multithreading" ON) option (LIBCMAES_ENABLE_SURROG "support for surrogates" ON) +option (LIBCMAES_EIGEN_5 "Use Eigen v5" OFF) # Offer the user the choice of overriding the installation directories set (INSTALL_LIB_DIR lib${LIB_SUFFIX} @@ -78,8 +79,44 @@ if(LIBCMAES_BUILD_PYTHON) endif () endif() +if (LIBCMAES_EIGEN_5) + set (LIBCMAES_EIGEN_MIN_VERSION 5.0.0) +else () + set (LIBCMAES_EIGEN_MIN_VERSION 3.4.0) +endif () + +find_package (Eigen3 REQUIRED) + +set (LIBCMAES_EIGEN_FOUND_VERSION ${Eigen3_VERSION}) +if (NOT LIBCMAES_EIGEN_FOUND_VERSION AND DEFINED EIGEN3_VERSION_STRING) + set (LIBCMAES_EIGEN_FOUND_VERSION ${EIGEN3_VERSION_STRING}) +endif () + +if (NOT LIBCMAES_EIGEN_FOUND_VERSION) + message (FATAL_ERROR "Could not determine the Eigen version exposed by package Eigen3.") +endif () -find_package (Eigen3 3.4.0 REQUIRED) +set (LIBCMAES_EIGEN_LOCATION "") +if (DEFINED Eigen3_DIR AND NOT "${Eigen3_DIR}" STREQUAL "") + set (LIBCMAES_EIGEN_LOCATION "package dir: ${Eigen3_DIR}") +elseif (DEFINED EIGEN3_ROOT_DIR AND NOT "${EIGEN3_ROOT_DIR}" STREQUAL "") + set (LIBCMAES_EIGEN_LOCATION "root: ${EIGEN3_ROOT_DIR}") +elseif (DEFINED EIGEN3_INCLUDE_DIRS AND NOT "${EIGEN3_INCLUDE_DIRS}" STREQUAL "") + set (LIBCMAES_EIGEN_LOCATION "includes: ${EIGEN3_INCLUDE_DIRS}") +endif () + +if (LIBCMAES_EIGEN_LOCATION) + message (STATUS "Using Eigen ${LIBCMAES_EIGEN_FOUND_VERSION} (${LIBCMAES_EIGEN_LOCATION})") +else () + message (STATUS "Using Eigen ${LIBCMAES_EIGEN_FOUND_VERSION}") +endif () + +if (LIBCMAES_EIGEN_FOUND_VERSION VERSION_LESS LIBCMAES_EIGEN_MIN_VERSION) + message ( + FATAL_ERROR + "LIBCMAES_EIGEN_5=${LIBCMAES_EIGEN_5} requires Eigen >= ${LIBCMAES_EIGEN_MIN_VERSION}, " + "but found ${LIBCMAES_EIGEN_FOUND_VERSION}. Set Eigen3_DIR or CMAKE_PREFIX_PATH to an appropriate Eigen installation.") +endif () if (LIBCMAES_USE_OPENMP) find_package (OpenMP QUIET) diff --git a/appveyor.yml b/appveyor.yml index 0fdd1cf8..f43c59be 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -2,14 +2,34 @@ clone_folder: C:\projects\libcmaes environment: matrix: - CMAKE_PLATFORM: "Visual Studio 14 2015" + EIGEN_VERSION: "3.3.4" + EIGEN_URL: "https://bitbucket.org/eigen/eigen/get/3.3.4.tar.gz" + EIGEN_ZIP: "3.3.4.tar.gz" + LIBCMAES_EIGEN_5: OFF + - CMAKE_PLATFORM: "Visual Studio 14 2015" + EIGEN_VERSION: "5.0.0" + EIGEN_URL: "https://gitlab.com/libeigen/eigen/-/archive/5.0.0/eigen-5.0.0.tar.gz" + EIGEN_ZIP: "eigen-5.0.0.tar.gz" + LIBCMAES_EIGEN_5: ON + - CMAKE_PLATFORM: "Visual Studio 14 2015 Win64" + EIGEN_VERSION: "3.3.4" + EIGEN_URL: "https://bitbucket.org/eigen/eigen/get/3.3.4.tar.gz" + EIGEN_ZIP: "3.3.4.tar.gz" + LIBCMAES_EIGEN_5: OFF - CMAKE_PLATFORM: "Visual Studio 14 2015 Win64" + EIGEN_VERSION: "5.0.0" + EIGEN_URL: "https://gitlab.com/libeigen/eigen/-/archive/5.0.0/eigen-5.0.0.tar.gz" + EIGEN_ZIP: "eigen-5.0.0.tar.gz" + LIBCMAES_EIGEN_5: ON install: - cinstall: python - - appveyor-retry appveyor DownloadFile https://bitbucket.org/eigen/eigen/get/3.3.4.tar.gz - - 7z x 3.3.4.tar.gz -oc:\projects > nul - - 7z x c:\projects\3.3.4.tar -oc:\projects > nul - - cd c:\projects\eigen-eigen-5a0156e40feb + - appveyor-retry appveyor DownloadFile "%EIGEN_URL%" -FileName eigen.tar.gz + - 7z x eigen.tar.gz -oc:\projects > nul + - 7z x c:\projects\eigen.tar -oc:\projects > nul + - cd c:\projects + - for /d %%i in (eigen-*) do set EIGEN_DIR=%%i + - cd %EIGEN_DIR% - mkdir build && cd build - cmake -G "%CMAKE_PLATFORM%" -DCMAKE_INSTALL_PREFIX=c:\Libraries\eigen -DCMAKE_BUILD_TYPE=Release .. - cmake --build . --config Release --target install @@ -17,6 +37,6 @@ install: build_script: - cd c:\projects\libcmaes - mkdir build && cd build - - cmake -G "%CMAKE_PLATFORM%" -DCMAKE_INSTALL_PREFIX=c:\Libraries\libcmaes -DEigen3_DIR=c:\Libraries\eigen\share\eigen3\cmake -DBOOST_ROOT="C:/Libraries/boost_1_63_0" .. + - cmake -G "%CMAKE_PLATFORM%" -DCMAKE_INSTALL_PREFIX=c:\Libraries\libcmaes -DLIBCMAES_EIGEN_5=%LIBCMAES_EIGEN_5% -DEigen3_DIR=c:\Libraries\eigen\share\eigen3\cmake -DBOOST_ROOT="C:/Libraries/boost_1_63_0" .. - cmake --build . --config Release --target install - ctest -C Release diff --git a/conanfile.py b/conanfile.py index 9d12ffac..f6d724b2 100644 --- a/conanfile.py +++ b/conanfile.py @@ -1,10 +1,9 @@ -import re, os, functools - -from conan import ConanFile, tools from conan import ConanFile from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout from conan.tools.files import load from conan.tools.env import Environment +import os +import re from conan.tools.scm import Git @@ -12,8 +11,6 @@ class CmaesConan(ConanFile): name = "libcmaes" - generators = "CMakeDeps" - # Optional metadata license = "MIT" author = " " @@ -23,11 +20,14 @@ class CmaesConan(ConanFile): # Binary configuration settings = "os", "compiler", "build_type", "arch" + + generators = "CMakeDeps" options = { "shared": [True, False], "openmp": [True, False], "surrog": [True, False], "enable_tests": [True, False], + "eigen_5": [True, False], } default_options = { "shared": True, @@ -35,6 +35,7 @@ class CmaesConan(ConanFile): "surrog": True, "enable_tests": False, "boost/*:without_python": False, + "eigen_5": False, } # Sources are located in the same place as this recipe, copy them to the recipe @@ -55,8 +56,10 @@ def build_requirements(self): self.test_requires("boost/1.85.0") def requirements(self): - self.requires("eigen/3.4.0", transitive_headers=True) - + if self.options.eigen_5: + self.requires("eigen/5.0.0") + else: + self.requires("eigen/3.4.0", transitive_headers=True) if self.options.openmp and self.settings.os != "Windows": self.requires("llvm-openmp/17.0.6", transitive_headers=True) @@ -94,6 +97,7 @@ def generate(self): tc.variables["LIBCMAES_ENABLE_SURROG"] = self.options.surrog tc.variables["LIBCMAES_BUILD_PYTHON"] = self.options.enable_tests tc.variables["LIBCMAES_BUILD_TESTS"] = self.options.enable_tests + tc.variables["LIBCMAES_EIGEN_5"] = self.options.eigen_5 tc.generate() def build(self): diff --git a/libcmaes-config.cmake.in b/libcmaes-config.cmake.in index 8fa61830..89b968da 100644 --- a/libcmaes-config.cmake.in +++ b/libcmaes-config.cmake.in @@ -3,7 +3,22 @@ include(CMakeFindDependencyMacro) list (APPEND CMAKE_MODULE_PATH @CMAKE_CURRENT_SOURCE_DIR@/lib/cmake/libcmaes ${CMAKE_CURRENT_LIST_DIR}) -find_dependency(Eigen3 @EIGEN3_VERSION@ REQUIRED) +find_dependency(Eigen3 REQUIRED) + +set (_libcmaes_eigen_version "${Eigen3_VERSION}") +if (NOT _libcmaes_eigen_version AND DEFINED EIGEN3_VERSION_STRING) + set (_libcmaes_eigen_version "${EIGEN3_VERSION_STRING}") +endif () + +if (NOT _libcmaes_eigen_version) + message (FATAL_ERROR "Could not determine the Eigen version exposed by package Eigen3.") +endif () + +if (_libcmaes_eigen_version VERSION_LESS "@LIBCMAES_EIGEN_MIN_VERSION@") + message ( + FATAL_ERROR + "libcmaes requires Eigen >= @LIBCMAES_EIGEN_MIN_VERSION@, but found ${_libcmaes_eigen_version}.") +endif () if(@LIBCMAES_USE_OPENMP@) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 397bb461..42720890 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -56,7 +56,9 @@ target_include_directories ( cmaes PUBLIC $ $ ) + target_link_libraries (cmaes PUBLIC Eigen3::Eigen) + if (LIBCMAES_USE_OPENMP) target_link_libraries (cmaes PUBLIC OpenMP::OpenMP_CXX) endif () diff --git a/src/ipopcmastrategy.cc b/src/ipopcmastrategy.cc index 45fbadcb..1f11b07e 100644 --- a/src/ipopcmastrategy.cc +++ b/src/ipopcmastrategy.cc @@ -74,8 +74,8 @@ namespace libcmaes reset_search_state(); // Update remaining budget - int fevals_global = CMAStrategy::_nevals; - int fevals_remaining = fevals_max - fevals_global; + int fevals_global = CMAStrategy::_nevals; + int fevals_remaining = fevals_max - fevals_global; // do not restart if max budget function calls is reached. if (has_max_fevals && fevals_remaining <= 0) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 87dafdab..4a80dcc8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,7 +1,18 @@ find_package(gflags REQUIRED) + +if (TARGET gflags::gflags) + set (LIBCMAES_GFLAGS_TARGET gflags::gflags) +elseif (TARGET gflags) + set (LIBCMAES_GFLAGS_TARGET gflags) +elseif (DEFINED GFLAGS_TARGET) + set (LIBCMAES_GFLAGS_TARGET ${GFLAGS_TARGET}) +else () + message (FATAL_ERROR "Could not determine a usable gflags target.") +endif () + macro (cmaes_add_test name) add_executable (${name} ${name}.cc) - target_link_libraries (${name} cmaes gflags) + target_link_libraries (${name} cmaes ${LIBCMAES_GFLAGS_TARGET}) if(MSVC) target_compile_definitions(${name} PUBLIC _USE_MATH_DEFINES) endif()