chiark / gitweb /
Rewrite Python test from example
authorJulien Schueller <schueller@phimeca.com>
Wed, 13 Jul 2016 19:14:43 +0000 (21:14 +0200)
committerJulien Schueller <schueller@phimeca.com>
Thu, 14 Jul 2016 06:52:01 +0000 (08:52 +0200)
.travis.yml
test/CMakeLists.txt
test/t_python.py [new file with mode: 0644]
test/test_std.py [deleted file]

index a0ccf61dd7cfba4037f9ff51507df987e41c3649..d7cf71edcabd1f10a558206cf0df1ce6ed274603 100644 (file)
@@ -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 ..
index 353d98a57638916d1f813a6142ae05c3c0ea640d..2816357150f709399d9c3866906bb6aa759dc6c1 100644 (file)
@@ -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 (file)
index 0000000..8dee3db
--- /dev/null
@@ -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 (file)
index f7bfca6..0000000
+++ /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)