From: Julien Schueller Date: Wed, 13 Jul 2016 19:14:43 +0000 (+0200) Subject: Rewrite Python test from example X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=0e036625b3e188009cc7a003629aa96b961c46a2;p=nlopt.git Rewrite Python test from example --- diff --git a/.travis.yml b/.travis.yml index a0ccf61..d7cf71e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,7 +35,7 @@ matrix: - brew tap homebrew/python - brew tap homebrew/science - brew update - - brew install swig python octave + - brew install swig numpy octave script: - mkdir build && pushd build - cmake -DCMAKE_INSTALL_PREFIX=~/.local -DWITH_CXX=ON .. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 353d98a..2816357 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -28,8 +28,8 @@ if (NUMPY_FOUND AND PYTHONLIBS_FOUND AND (SWIG_FOUND OR (EXISTS ${CMAKE_SOURCE_D set (PYINSTALLCHECK_ENVIRONMENT "LD_LIBRARY_PATH=${INSTALL_DESTDIR}${INSTALL_LIB_DIR}:$ENV{LD_LIBRARY_PATH}" "PYTHONPATH=${INSTALL_DESTDIR}${CMAKE_INSTALL_PREFIX}/${PYTHON_MODULE_PATH}${PATH_SEP}$ENV{PYTHONPATH}" ) - add_test (NAME test_std_python COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_std.py) - set_tests_properties (test_std_python PROPERTIES ENVIRONMENT "${PYINSTALLCHECK_ENVIRONMENT}") + add_test (NAME test_python COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/t_python.py) + set_tests_properties (test_python PROPERTIES ENVIRONMENT "${PYINSTALLCHECK_ENVIRONMENT}") endif () if (OCTAVE_FOUND) diff --git a/test/t_python.py b/test/t_python.py new file mode 100644 index 0000000..8dee3db --- /dev/null +++ b/test/t_python.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +from __future__ import print_function +import nlopt +import numpy as np + + +def myfunc(x, grad): + if grad.size > 0: + grad[0] = 0.0 + grad[1] = 0.5 / np.sqrt(x[1]) + return np.sqrt(x[1]) + +def myconstraint(x, grad, a, b): + if grad.size > 0: + grad[0] = 3 * a * (a*x[0] + b)**2 + grad[1] = -1.0 + return (a*x[0] + b)**3 - x[1] + +opt = nlopt.opt(nlopt.LD_MMA, 2) +opt.set_lower_bounds([-float('inf'), 0]) +opt.set_min_objective(myfunc) +opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,2,0), 1e-8) +opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,-1,1), 1e-8) +opt.set_xtol_rel(1e-4) +x = opt.optimize([1.234, 5.678]) +minf = opt.last_optimum_value() +print('optimum at ', x[0],x[1]) +print('minimum value = ', minf) +print('result code = ', opt.last_optimize_result()) diff --git a/test/test_std.py b/test/test_std.py deleted file mode 100644 index f7bfca6..0000000 --- a/test/test_std.py +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env python - -from __future__ import print_function -import nlopt -import numpy as np - -print ('nlopt version=', nlopt.__version__) - -def f(x, grad): - F, L, E, I = x - D=F*L**3/(3.*E*I) - return D - -n = 4 -opt = nlopt.opt(nlopt.LN_COBYLA, n) -opt.set_min_objective(f) -lb = np.array([40., 50., 30e3, 1.]) -ub = np.array([60., 60., 40e3, 10.]) -x = (lb+ub)/2. -opt.set_lower_bounds(lb) -opt.set_upper_bounds(ub) -opt.set_xtol_rel(1e-3) -opt.set_ftol_rel(1e-3) -xopt = opt.optimize(x) - -opt_val = opt.last_optimum_value() -result = opt.last_optimize_result() -print ('opt_result=', result) -print ('optimizer=', xopt) -print ('opt_val=', opt_val)