git: 1b6bef5d436a - main - math/py-nlopt: New port: Nonlinear optimization library

From: Yuri Victorovich <yuri_at_FreeBSD.org>
Date: Sun, 19 Jun 2022 21:38:14 UTC
The branch main has been updated by yuri:

URL: https://cgit.FreeBSD.org/ports/commit/?id=1b6bef5d436ab3213b9996a06880e155ffaadd06

commit 1b6bef5d436ab3213b9996a06880e155ffaadd06
Author:     Yuri Victorovich <yuri@FreeBSD.org>
AuthorDate: 2022-06-19 21:37:32 +0000
Commit:     Yuri Victorovich <yuri@FreeBSD.org>
CommitDate: 2022-06-19 21:38:11 +0000

    math/py-nlopt: New port: Nonlinear optimization library
---
 math/Makefile                           |  1 +
 math/py-nlopt/Makefile                  | 33 +++++++++++++++++++++++++++++++++
 math/py-nlopt/distinfo                  |  5 +++++
 math/py-nlopt/files/example.py          | 24 ++++++++++++++++++++++++
 math/py-nlopt/files/patch-extensions.py | 11 +++++++++++
 math/py-nlopt/pkg-descr                 |  4 ++++
 math/py-nlopt/pkg-plist                 |  7 +++++++
 7 files changed, 85 insertions(+)

diff --git a/math/Makefile b/math/Makefile
index 10558e48f46b..ba5ac17f60e8 100644
--- a/math/Makefile
+++ b/math/Makefile
@@ -897,6 +897,7 @@
     SUBDIR += py-ndindex
     SUBDIR += py-networkx
     SUBDIR += py-nevergrad
+    SUBDIR += py-nlopt
     SUBDIR += py-numexpr
     SUBDIR += py-numpoly
     SUBDIR += py-numpy
diff --git a/math/py-nlopt/Makefile b/math/py-nlopt/Makefile
new file mode 100644
index 000000000000..b47a8898a4ee
--- /dev/null
+++ b/math/py-nlopt/Makefile
@@ -0,0 +1,33 @@
+PORTNAME=	nlopt
+DISTVERSION=	2.7.1
+CATEGORIES=	math
+#MASTER_SITES=	CHEESESHOP # https://github.com/DanielBok/nlopt-python/issues/15
+PKGNAMEPREFIX=	${PYTHON_PKGNAMEPREFIX}
+
+MAINTAINER=	yuri@FreeBSD.org
+COMMENT=	Nonlinear optimization library
+
+LICENSE=	LGPL21 MIT
+LICENSE_COMB=	multi
+LICENSE_FILE=	${WRKSRC}/LICENSE
+
+BUILD_DEPENDS=	cmake:devel/cmake \
+		swig:devel/swig \
+		${PYNUMPY}
+RUN_DEPENDS=	${PYNUMPY}
+
+USES=		python
+USE_PYTHON=	distutils # autoplist is broken, see https://github.com/DanielBok/nlopt-python/issues/17
+
+USE_GITHUB=	yes
+GH_ACCOUNT=	DanielBok
+GH_PROJECT=	nlopt-python
+GH_TUPLE=	stevengj:nlopt:4a0e93c:nlopt/extern/nlopt
+
+post-install:
+	@${STRIP_CMD} ${STAGEDIR}${PYTHON_SITELIBDIR}/nlopt/_nlopt.so
+
+do-test:
+	@${SETENV} ${TEST_ENV} PYTHONPATH=${STAGEDIR}${PYTHONPREFIX_SITELIBDIR} ${PYTHON_CMD} ${FILESDIR}/example.py
+
+.include <bsd.port.mk>
diff --git a/math/py-nlopt/distinfo b/math/py-nlopt/distinfo
new file mode 100644
index 000000000000..d896243b4cf7
--- /dev/null
+++ b/math/py-nlopt/distinfo
@@ -0,0 +1,5 @@
+TIMESTAMP = 1655669851
+SHA256 (DanielBok-nlopt-python-2.7.1_GH0.tar.gz) = 838a46f67309b1e1f63227a27726e8a7be19f9b05f0972d40b98a52ccfb799fb
+SIZE (DanielBok-nlopt-python-2.7.1_GH0.tar.gz) = 6749
+SHA256 (stevengj-nlopt-4a0e93c_GH0.tar.gz) = 3d8036633821df60f387431fde51cd11c125743a24ccbda40221518ad21663a4
+SIZE (stevengj-nlopt-4a0e93c_GH0.tar.gz) = 2044242
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:
diff --git a/math/py-nlopt/pkg-descr b/math/py-nlopt/pkg-descr
new file mode 100644
index 000000000000..9d5064c5cdc6
--- /dev/null
+++ b/math/py-nlopt/pkg-descr
@@ -0,0 +1,4 @@
+Python bindings for NLOpt that contains various routines for
+non-linear optimization.
+
+WWW: https://github.com/DanielBok/nlopt-python
diff --git a/math/py-nlopt/pkg-plist b/math/py-nlopt/pkg-plist
new file mode 100644
index 000000000000..325f0732f7c5
--- /dev/null
+++ b/math/py-nlopt/pkg-plist
@@ -0,0 +1,7 @@
+%%PYTHON_SITELIBDIR%%/nlopt/__init__.py
+%%PYTHON_SITELIBDIR%%/nlopt/__pycache__/__init__%%PYTHON_EXT_SUFFIX%%.opt-1.pyc
+%%PYTHON_SITELIBDIR%%/nlopt/__pycache__/__init__%%PYTHON_EXT_SUFFIX%%.pyc
+%%PYTHON_SITELIBDIR%%/nlopt/__pycache__/nlopt%%PYTHON_EXT_SUFFIX%%.opt-1.pyc
+%%PYTHON_SITELIBDIR%%/nlopt/__pycache__/nlopt%%PYTHON_EXT_SUFFIX%%.pyc
+%%PYTHON_SITELIBDIR%%/nlopt/_nlopt.so
+%%PYTHON_SITELIBDIR%%/nlopt/nlopt.py