Skip to content

Commit

Permalink
Support nlopt 2.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jslee02 committed Nov 11, 2024
1 parent 4b5b6a8 commit 036d044
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
26 changes: 26 additions & 0 deletions dart/optimizer/nlopt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@
dart_find_package(NLOPT)
dart_check_optional_package(NLOPT "dart-optimizer-nlopt" "nlopt" "2.4.1")

if(NOT NLOPT_VERSION)
# If version is not found, we just assume 2.9.0
set(NLOPT_VERSION "2.9.0")
endif()

# Attempt to parse version components
string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" NLOPT_MAJOR_VERSION "${NLOPT_VERSION}")
string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" NLOPT_MINOR_VERSION "${NLOPT_VERSION}")
string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" NLOPT_PATCH_VERSION "${NLOPT_VERSION}")

# Check if parsing succeeded
if(NOT (NLOPT_MAJOR_VERSION MATCHES "^[0-9]+$" AND
NLOPT_MINOR_VERSION MATCHES "^[0-9]+$" AND
NLOPT_PATCH_VERSION MATCHES "^[0-9]+$"))
message(WARNING "Failed to parse NLOPT_VERSION '${NLOPT_VERSION}'. Using default values (2, 9, 0) for version components.")
set(NLOPT_MAJOR_VERSION "2")
set(NLOPT_MINOR_VERSION "9")
set(NLOPT_PATCH_VERSION "0")
endif()

# Search all header and source files
file(GLOB hdrs "*.hpp")
file(GLOB srcs "*.cpp")
Expand All @@ -13,6 +33,12 @@ set(component_name optimizer-nlopt)
# Add target
dart_add_library(${target_name} ${hdrs} ${srcs})
target_link_libraries(${target_name} PUBLIC dart NLOPT::nlopt)
target_compile_definitions(${target_name}
PUBLIC
-DNLOPT_MAJOR_VERSION=${NLOPT_MAJOR_VERSION}
-DNLOPT_MINOR_VERSION=${NLOPT_MINOR_VERSION}
-DNLOPT_PATCH_VERSION=${NLOPT_PATCH_VERSION}
)

# Component
add_component(${PROJECT_NAME} ${component_name})
Expand Down
4 changes: 4 additions & 0 deletions dart/optimizer/nlopt/NloptSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ nlopt::algorithm NloptSolver::convertAlgorithm(NloptSolver::Algorithm algorithm)
NLOPTSOLVER_ALGORITHM_DART_TO_NLOPT(GN_ORIG_DIRECT_L)
NLOPTSOLVER_ALGORITHM_DART_TO_NLOPT(GD_STOGO)
NLOPTSOLVER_ALGORITHM_DART_TO_NLOPT(GD_STOGO_RAND)
#if !NLOPT_VERSION_GE(2, 9, 0)
NLOPTSOLVER_ALGORITHM_DART_TO_NLOPT(LD_LBFGS_NOCEDAL)
#endif
NLOPTSOLVER_ALGORITHM_DART_TO_NLOPT(LD_LBFGS)
NLOPTSOLVER_ALGORITHM_DART_TO_NLOPT(LN_PRAXIS)
NLOPTSOLVER_ALGORITHM_DART_TO_NLOPT(LD_VAR1)
Expand Down Expand Up @@ -311,7 +313,9 @@ NloptSolver::Algorithm NloptSolver::convertAlgorithm(nlopt::algorithm algorithm)
NLOPTSOLVER_ALGORITHM_NLOPT_TO_DART(GN_ORIG_DIRECT_L)
NLOPTSOLVER_ALGORITHM_NLOPT_TO_DART(GD_STOGO)
NLOPTSOLVER_ALGORITHM_NLOPT_TO_DART(GD_STOGO_RAND)
#if !NLOPT_VERSION_GE(2, 9, 0)
NLOPTSOLVER_ALGORITHM_NLOPT_TO_DART(LD_LBFGS_NOCEDAL)
#endif
NLOPTSOLVER_ALGORITHM_NLOPT_TO_DART(LD_LBFGS)
NLOPTSOLVER_ALGORITHM_NLOPT_TO_DART(LN_PRAXIS)
NLOPTSOLVER_ALGORITHM_NLOPT_TO_DART(LD_VAR1)
Expand Down
10 changes: 9 additions & 1 deletion dart/optimizer/nlopt/NloptSolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,18 @@

#include <nlopt.hpp>

#define NLOPT_VERSION_GE(x, y, z) \
((NLOPT_MAJOR_VERSION > (x)) \
|| (NLOPT_MAJOR_VERSION == (x) && NLOPT_MINOR_VERSION > (y)) \
|| (NLOPT_MAJOR_VERSION == (x) && NLOPT_MINOR_VERSION == (y) \
&& NLOPT_PATCH_VERSION >= (z)))

namespace dart {
namespace optimizer {

class Problem;

/// NloptSolver is a nonlinear programming solver that provides many unlerlying
/// NloptSolver is a nonlinear programming solver that provides many underlying
/// algorithms through nlopt (an third-party library:
/// https://nlopt.readthedocs.io/).
///
Expand Down Expand Up @@ -73,7 +79,9 @@ class NloptSolver : public Solver
GN_ORIG_DIRECT_L,
GD_STOGO,
GD_STOGO_RAND,
#if !NLOPT_VERSION_GE(2, 9, 0)
LD_LBFGS_NOCEDAL,
#endif
LD_LBFGS,
LN_PRAXIS,
LD_VAR1,
Expand Down
4 changes: 3 additions & 1 deletion pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ config = { cmd = """
-G Ninja \
-S . \
-B build \
-DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX \
-DCMAKE_PREFIX_PATH=$CONDA_PREFIX \
-DDART_VERBOSE=ON \
-DDART_USE_SYSTEM_IMGUI=ON
""" }
Expand Down Expand Up @@ -192,6 +193,7 @@ config = { cmd = """
-B build \
-G 'Visual Studio 17 2022' \
-DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX \
-DCMAKE_PREFIX_PATH=$CONDA_PREFIX \
-DDART_VERBOSE=ON \
-DDART_MSVC_DEFAULT_OPTIONS=ON \
-DBUILD_SHARED_LIBS=OFF \
Expand Down

0 comments on commit 036d044

Please sign in to comment.