- brew tap homebrew/python
- brew tap homebrew/science
- brew update
- - brew install swig numpy octave
+ - brew install swig numpy octave guile
script:
- mkdir build && pushd build
- cmake -DCMAKE_INSTALL_PREFIX=~/.local -DWITH_CXX=ON ..
endif ()
+find_program(GUILE_EXECUTABLE
+ NAMES guile
+ )
+
find_program(GUILE_CONFIG_EXECUTABLE
NAMES guile-config
)
execute_process (COMMAND ${GUILE_CONFIG_EXECUTABLE} info prefix
OUTPUT_VARIABLE GUILE_ROOT_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE)
-
+
execute_process (COMMAND ${GUILE_CONFIG_EXECUTABLE} info sitedir
OUTPUT_VARIABLE GUILE_SITE_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE)
configure_file (${CMAKE_CURRENT_SOURCE_DIR}/nlopt.scm.in ${CMAKE_CURRENT_BINARY_DIR}/nlopt.scm @ONLY)
endif ()
- set (SWIG_MODULE_nlopt_guile_REAL_NAME nlopt_guile)
+ set (SWIG_MODULE_nlopt_guile_REAL_NAME nlopt${NLOPT_SUFFIX}_guile)
add_library (${SWIG_MODULE_nlopt_guile_REAL_NAME} MODULE ${guile_cpp_source})
target_link_libraries(${SWIG_MODULE_nlopt_guile_REAL_NAME} ${nlopt_lib})
target_link_libraries(${SWIG_MODULE_nlopt_guile_REAL_NAME} ${GUILE_LIBRARIES})
if (OCTAVE_FOUND)
add_test (NAME test_octave COMMAND ${OCTAVE_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/t_octave.m ${CMAKE_SOURCE_DIR}/octave ${CMAKE_BINARY_DIR}/octave)
+endif ()
+
+if (GUILE_FOUND AND ((SWIG_FOUND AND SWIG_VERSION VERSION_GREATER 2.0.9) OR (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/nlopt-guile.cpp)))
+ set (GUILECHECK_ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/swig"
+ "GUILE_LOAD_PATH=${CMAKE_BINARY_DIR}/swig"
+ )
+ add_test (NAME test_guile COMMAND ${GUILE_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/t_guile.scm)
+ set_tests_properties (test_guile PROPERTIES ENVIRONMENT "${GUILECHECK_ENVIRONMENT}")
endif ()
\ No newline at end of file
--- /dev/null
+(use-modules (nlopt))
+
+(define (myfunc x grad)
+ (if grad
+ (begin
+ (vector-set! grad 0 0.0)
+ (vector-set! grad 1 (/ 0.5 (sqrt (vector-ref x 1))))))
+ (sqrt (vector-ref x 1)))
+
+(define (myconstraint x grad a b)
+ (let ((x0 (vector-ref x 0)) (x1 (vector-ref x 1)))
+ (if grad
+ (begin
+ (vector-set! grad 0 (* 3 a (expt (+ (* a x0) b) 2)))
+ (vector-set! grad 1 -1.0)))
+ (- (expt (+ (* a x0) b) 3) x1)))
+
+(define opt (new-nlopt-opt NLOPT-LD-MMA 2))
+(nlopt-opt-set-lower-bounds opt (vector (- (inf)) 0))
+(nlopt-opt-set-min-objective opt myfunc)
+(nlopt-opt-add-inequality-constraint opt (lambda (x grad)
+ (myconstraint x grad 2 0))
+ 1e-8)
+(nlopt-opt-add-inequality-constraint opt (lambda (x grad)
+ (myconstraint x grad -1 1))
+ 1e-8)
+(nlopt-opt-set-xtol-rel opt 1e-4)
+(define x (nlopt-opt-optimize opt (vector 1.234 5.678)))
+(define minf (nlopt-opt-last-optimum-value opt))
+(define result (nlopt-opt-last-optimize-result opt))
+(display "x=")
+(display x)
+(newline)
+(display "minf=")
+(display minf)
+(newline)