summaryrefslogtreecommitdiff
path: root/math/py-nlopt/files
diff options
context:
space:
mode:
authorYuri Victorovich <yuri@FreeBSD.org>2022-06-19 14:37:32 -0700
committerYuri Victorovich <yuri@FreeBSD.org>2022-06-19 14:38:11 -0700
commit1b6bef5d436ab3213b9996a06880e155ffaadd06 (patch)
treed2140cccdf884c2a62f42e4c237ccbadd08d76de /math/py-nlopt/files
parentdevel/kerl: update to version 2.5.1 (diff)
math/py-nlopt: New port: Nonlinear optimization library
Diffstat (limited to 'math/py-nlopt/files')
-rw-r--r--math/py-nlopt/files/example.py24
-rw-r--r--math/py-nlopt/files/patch-extensions.py11
2 files changed, 35 insertions, 0 deletions
diff --git a/math/py-nlopt/files/example.py b/math/py-nlopt/files/example.py
new file mode 100644
index 000000000000..bcde61ba78f8
--- /dev/null
+++ b/math/py-nlopt/files/example.py
@@ -0,0 +1,24 @@
+import nlopt
+from numpy import *
+def myfunc(x, grad):
+ if grad.size > 0:
+ grad[0] = 0.0
+ grad[1] = 0.5 / sqrt(x[1])
+ return 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/math/py-nlopt/files/patch-extensions.py b/math/py-nlopt/files/patch-extensions.py
new file mode 100644
index 000000000000..1c8cfb1104cd
--- /dev/null
+++ b/math/py-nlopt/files/patch-extensions.py
@@ -0,0 +1,11 @@
+--- extensions.py.orig 2022-06-19 20:42:31 UTC
++++ extensions.py
+@@ -25,7 +25,7 @@ class NLOptBuild(build_ext):
+ except OSError:
+ raise RuntimeError("CMake must be installed")
+
+- if platform.system() not in ("Windows", "Linux", "Darwin"):
++ if platform.system() not in ("Windows", "Linux", "Darwin", "FreeBSD"):
+ raise RuntimeError(f"Unsupported os: {platform.system()}")
+
+ for ext in self.extensions: