From 2e4dfab1ff17d9cbc197dbff5bbf13ccd6483218 Mon Sep 17 00:00:00 2001 From: Ella Wu <602725+ewu63@users.noreply.github.com> Date: Sat, 12 Apr 2025 18:00:22 -0700 Subject: [PATCH 1/6] make swig mandatory and therefore nsga2 a default optimizer --- pyoptsparse/pyNSGA2/meson.build | 66 ++++++++++++++++----------------- pyproject.toml | 2 +- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/pyoptsparse/pyNSGA2/meson.build b/pyoptsparse/pyNSGA2/meson.build index e9cfdcca4..48faac1ae 100644 --- a/pyoptsparse/pyNSGA2/meson.build +++ b/pyoptsparse/pyNSGA2/meson.build @@ -1,40 +1,36 @@ -swig = find_program('swig', required: false) +swig = find_program('swig', required: true) -if swig.found() - nsga2_source = custom_target('nsga2_wrap.c', - input : ['source/swig/nsga2.i'], - output : ['nsga2_wrap.c'], - command: ['swig', '-o', 'pyoptsparse/pyNSGA2/nsga2_wrap.c', '-python', '-interface', 'nsga2', '@INPUT@'] - ) +nsga2_source = custom_target('nsga2_wrap.c', + input : ['source/swig/nsga2.i'], + output : ['nsga2_wrap.c'], + command: ['swig', '-o', 'pyoptsparse/pyNSGA2/nsga2_wrap.c', '-python', '-interface', 'nsga2', '@INPUT@'] + ) - py3_target.extension_module('nsga2', - 'source/allocate.c', - 'source/auxiliary.c', - 'source/crossover.c', - 'source/crowddist.c', - 'source/decode.c', - 'source/dominance.c', - 'source/eval.c', - 'source/fillnds.c', - 'source/initialize.c', - 'source/list.c', - 'source/merge.c', - 'source/mutation.c', - 'source/nsga2.c', - 'source/rand.c', - 'source/rank.c', - 'source/report.c', - 'source/sort.c', - 'source/tourselect.c', - nsga2_source, - include_directories: 'source', - dependencies : py3_dep, - subdir: 'pyoptsparse/pyNSGA2', - link_language: 'c', - install : false) -else - message('SWIG was not found, therefore NSGA2 will not be built.') -endif +py3_target.extension_module('nsga2', + 'source/allocate.c', + 'source/auxiliary.c', + 'source/crossover.c', + 'source/crowddist.c', + 'source/decode.c', + 'source/dominance.c', + 'source/eval.c', + 'source/fillnds.c', + 'source/initialize.c', + 'source/list.c', + 'source/merge.c', + 'source/mutation.c', + 'source/nsga2.c', + 'source/rand.c', + 'source/rank.c', + 'source/report.c', + 'source/sort.c', + 'source/tourselect.c', + nsga2_source, + include_directories: 'source', + dependencies : py3_dep, + subdir: 'pyoptsparse/pyNSGA2', + link_language: 'c', + install : false) #python_sources = [ # '__init__.py', diff --git a/pyproject.toml b/pyproject.toml index e70531965..b978ca481 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,3 @@ [build-system] -requires = ["setuptools>=42", "meson>=0.60.0", "ninja", "numpy>=2.0"] +requires = ["setuptools>=42", "meson>=0.60.0", "ninja", "numpy>=2.0", "swig] build-backend = "setuptools.build_meta" From 72629c44edcd47ab087af8bd1de7a6c831756614 Mon Sep 17 00:00:00 2001 From: Ella Wu <602725+ewu63@users.noreply.github.com> Date: Sat, 12 Apr 2025 18:02:59 -0700 Subject: [PATCH 2/6] make wrapper raise error --- README.md | 2 +- doc/install.rst | 1 - pyoptsparse/pyNSGA2/pyNSGA2.py | 2 +- tests/testing_utils.py | 2 +- 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6085827aa..9d0ad2826 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ ALPSO, CONMIN, IPOPT, NLPQLP, NSGA2, PSQP, SLSQP, ParOpt and SNOPT are currently We do not provide the source code for SNOPT and NLPQLP, due to their restrictive license requirements. Please contact the authors of the respective optimizers if you wish to obtain them. -Furthermore, ParOpt and IPOPT are available as a open source package but must be installed separately. +Furthermore, ParOpt and IPOPT are available as open source packages but must be installed separately. Please see the documentation page of each optimizer for purchase and installation instructions. ## Integration into other frameworks diff --git a/doc/install.rst b/doc/install.rst index fe1b5424c..fef89344c 100644 --- a/doc/install.rst +++ b/doc/install.rst @@ -34,7 +34,6 @@ pyOptSparse has the following dependencies: We recommend ``gcc`` and ``gfortran`` which can be installed via the package manager for your operating system. Please make sure these are installed and available for use. -In order to use NSGA2, SWIG (v1.3+) is also required, which can be installed via the package manager. Python dependencies are automatically handled by ``pip``, so they do not need to be installed separately. .. note:: diff --git a/pyoptsparse/pyNSGA2/pyNSGA2.py b/pyoptsparse/pyNSGA2/pyNSGA2.py index 6b0cc3027..c6bd2e5c4 100644 --- a/pyoptsparse/pyNSGA2/pyNSGA2.py +++ b/pyoptsparse/pyNSGA2/pyNSGA2.py @@ -17,7 +17,7 @@ # import the compiled module THIS_DIR = os.path.dirname(os.path.abspath(__file__)) -nsga2 = try_import_compiled_module_from_path("nsga2", THIS_DIR) +nsga2 = try_import_compiled_module_from_path("nsga2", THIS_DIR, raise_warning=True) class NSGA2(Optimizer): diff --git a/tests/testing_utils.py b/tests/testing_utils.py index b599a7eb8..7fb041b50 100644 --- a/tests/testing_utils.py +++ b/tests/testing_utils.py @@ -59,7 +59,7 @@ def get_dict_distance(d, d2): } # these are optimizers which are installed by default -DEFAULT_OPTIMIZERS = {"SLSQP", "PSQP", "CONMIN", "ALPSO"} +DEFAULT_OPTIMIZERS = {"SLSQP", "PSQP", "CONMIN", "ALPSO", "NSGA2"} # Define gradient-based optimizers GRAD_BASED_OPTIMIZERS = {"CONMIN", "IPOPT", "NLPQLP", "ParOpt", "PSQP", "SLSQP", "SNOPT"} From 4d77402d47eb545a654c1a779c7e2a6677e002ea Mon Sep 17 00:00:00 2001 From: Ella Wu <602725+ewu63@users.noreply.github.com> Date: Sat, 12 Apr 2025 18:06:40 -0700 Subject: [PATCH 3/6] missing quote --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index b978ca481..ce02acea6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,3 @@ [build-system] -requires = ["setuptools>=42", "meson>=0.60.0", "ninja", "numpy>=2.0", "swig] +requires = ["setuptools>=42", "meson>=0.60.0", "ninja", "numpy>=2.0", "swig"] build-backend = "setuptools.build_meta" From a287ff8ea2f6b00b487cf90efa9ac52950ccef38 Mon Sep 17 00:00:00 2001 From: Ella Wu <602725+ewu63@users.noreply.github.com> Date: Sat, 12 Apr 2025 18:06:46 -0700 Subject: [PATCH 4/6] do not skip nsga2 tests --- tests/test_nsga2_multi_objective.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/test_nsga2_multi_objective.py b/tests/test_nsga2_multi_objective.py index 5d9f2b7eb..6d28ed665 100644 --- a/tests/test_nsga2_multi_objective.py +++ b/tests/test_nsga2_multi_objective.py @@ -9,6 +9,7 @@ # First party modules from pyoptsparse import Optimization +from pyoptsparse.pyNSGA2 import nsga2 # Local modules from testing_utils import OptTest @@ -19,13 +20,6 @@ class TestNSGA2(OptTest): optName = "NSGA2" histFileName = None - def setUp(self): - try: - # First party modules - from pyoptsparse.pyNSGA2 import nsga2 # noqa: F401 - except ImportError: - raise unittest.SkipTest("Optimizer not available: NSGA2") - def objfunc(self, xdict): x = xdict["x"] y = xdict["y"] From fe98a20e131fbb808cd6a2f050a7e98bb6eef1ae Mon Sep 17 00:00:00 2001 From: Ella Wu <602725+ewu63@users.noreply.github.com> Date: Sat, 12 Apr 2025 18:07:32 -0700 Subject: [PATCH 5/6] version bump --- pyoptsparse/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyoptsparse/__init__.py b/pyoptsparse/__init__.py index cda0d1003..8c42fef00 100644 --- a/pyoptsparse/__init__.py +++ b/pyoptsparse/__init__.py @@ -1,4 +1,4 @@ -__version__ = "2.13.1" +__version__ = "2.13.2" from .pyOpt_history import History from .pyOpt_variable import Variable From a9133a0f8e4cfa9c69faca16446afdcc964b704e Mon Sep 17 00:00:00 2001 From: Ella Wu <602725+ewu63@users.noreply.github.com> Date: Sun, 13 Apr 2025 00:00:24 -0700 Subject: [PATCH 6/6] flake8 --- tests/test_nsga2_multi_objective.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_nsga2_multi_objective.py b/tests/test_nsga2_multi_objective.py index 6d28ed665..2ee012ca5 100644 --- a/tests/test_nsga2_multi_objective.py +++ b/tests/test_nsga2_multi_objective.py @@ -1,4 +1,4 @@ -""" Test NSGA2.""" +"""Test NSGA2.""" # Standard Python modules import unittest @@ -9,7 +9,6 @@ # First party modules from pyoptsparse import Optimization -from pyoptsparse.pyNSGA2 import nsga2 # Local modules from testing_utils import OptTest