Skip to content

Commit 43b3867

Browse files
committed
Support nlopt 2.9.0
1 parent 4b5b6a8 commit 43b3867

File tree

5 files changed

+321
-3
lines changed

5 files changed

+321
-3
lines changed

dart/optimizer/nlopt/CMakeLists.txt

+26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@
22
dart_find_package(NLOPT)
33
dart_check_optional_package(NLOPT "dart-optimizer-nlopt" "nlopt" "2.4.1")
44

5+
if(NOT NLOPT_VERSION)
6+
# If version is not found, we just assume 2.9.0
7+
set(NLOPT_VERSION "2.9.0")
8+
endif()
9+
10+
# Attempt to parse version components
11+
string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" NLOPT_MAJOR_VERSION "${NLOPT_VERSION}")
12+
string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" NLOPT_MINOR_VERSION "${NLOPT_VERSION}")
13+
string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" NLOPT_PATCH_VERSION "${NLOPT_VERSION}")
14+
15+
# Check if parsing succeeded
16+
if(NOT (NLOPT_MAJOR_VERSION MATCHES "^[0-9]+$" AND
17+
NLOPT_MINOR_VERSION MATCHES "^[0-9]+$" AND
18+
NLOPT_PATCH_VERSION MATCHES "^[0-9]+$"))
19+
message(WARNING "Failed to parse NLOPT_VERSION '${NLOPT_VERSION}'. Using default values (2, 9, 0) for version components.")
20+
set(NLOPT_MAJOR_VERSION "2")
21+
set(NLOPT_MINOR_VERSION "9")
22+
set(NLOPT_PATCH_VERSION "0")
23+
endif()
24+
525
# Search all header and source files
626
file(GLOB hdrs "*.hpp")
727
file(GLOB srcs "*.cpp")
@@ -13,6 +33,12 @@ set(component_name optimizer-nlopt)
1333
# Add target
1434
dart_add_library(${target_name} ${hdrs} ${srcs})
1535
target_link_libraries(${target_name} PUBLIC dart NLOPT::nlopt)
36+
target_compile_definitions(${target_name}
37+
PUBLIC
38+
-DNLOPT_MAJOR_VERSION=${NLOPT_MAJOR_VERSION}
39+
-DNLOPT_MINOR_VERSION=${NLOPT_MINOR_VERSION}
40+
-DNLOPT_PATCH_VERSION=${NLOPT_PATCH_VERSION}
41+
)
1642

1743
# Component
1844
add_component(${PROJECT_NAME} ${component_name})

dart/optimizer/nlopt/NloptSolver.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ nlopt::algorithm NloptSolver::convertAlgorithm(NloptSolver::Algorithm algorithm)
257257
NLOPTSOLVER_ALGORITHM_DART_TO_NLOPT(GN_ORIG_DIRECT_L)
258258
NLOPTSOLVER_ALGORITHM_DART_TO_NLOPT(GD_STOGO)
259259
NLOPTSOLVER_ALGORITHM_DART_TO_NLOPT(GD_STOGO_RAND)
260+
#if !NLOPT_VERSION_GE(2, 9, 0)
260261
NLOPTSOLVER_ALGORITHM_DART_TO_NLOPT(LD_LBFGS_NOCEDAL)
262+
#endif
261263
NLOPTSOLVER_ALGORITHM_DART_TO_NLOPT(LD_LBFGS)
262264
NLOPTSOLVER_ALGORITHM_DART_TO_NLOPT(LN_PRAXIS)
263265
NLOPTSOLVER_ALGORITHM_DART_TO_NLOPT(LD_VAR1)
@@ -311,7 +313,9 @@ NloptSolver::Algorithm NloptSolver::convertAlgorithm(nlopt::algorithm algorithm)
311313
NLOPTSOLVER_ALGORITHM_NLOPT_TO_DART(GN_ORIG_DIRECT_L)
312314
NLOPTSOLVER_ALGORITHM_NLOPT_TO_DART(GD_STOGO)
313315
NLOPTSOLVER_ALGORITHM_NLOPT_TO_DART(GD_STOGO_RAND)
316+
#if !NLOPT_VERSION_GE(2, 9, 0)
314317
NLOPTSOLVER_ALGORITHM_NLOPT_TO_DART(LD_LBFGS_NOCEDAL)
318+
#endif
315319
NLOPTSOLVER_ALGORITHM_NLOPT_TO_DART(LD_LBFGS)
316320
NLOPTSOLVER_ALGORITHM_NLOPT_TO_DART(LN_PRAXIS)
317321
NLOPTSOLVER_ALGORITHM_NLOPT_TO_DART(LD_VAR1)

dart/optimizer/nlopt/NloptSolver.hpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,18 @@
3939

4040
#include <nlopt.hpp>
4141

42+
#define NLOPT_VERSION_GE(x, y, z) \
43+
((NLOPT_MAJOR_VERSION > (x)) \
44+
|| (NLOPT_MAJOR_VERSION == (x) && NLOPT_MINOR_VERSION > (y)) \
45+
|| (NLOPT_MAJOR_VERSION == (x) && NLOPT_MINOR_VERSION == (y) \
46+
&& NLOPT_PATCH_VERSION >= (z)))
47+
4248
namespace dart {
4349
namespace optimizer {
4450

4551
class Problem;
4652

47-
/// NloptSolver is a nonlinear programming solver that provides many unlerlying
53+
/// NloptSolver is a nonlinear programming solver that provides many underlying
4854
/// algorithms through nlopt (an third-party library:
4955
/// https://nlopt.readthedocs.io/).
5056
///
@@ -73,7 +79,9 @@ class NloptSolver : public Solver
7379
GN_ORIG_DIRECT_L,
7480
GD_STOGO,
7581
GD_STOGO_RAND,
82+
#if !NLOPT_VERSION_GE(2, 9, 0)
7683
LD_LBFGS_NOCEDAL,
84+
#endif
7785
LD_LBFGS,
7886
LN_PRAXIS,
7987
LD_VAR1,

0 commit comments

Comments
 (0)