From a2de450c7cda9946ad9026af64d7ef20f2491257 Mon Sep 17 00:00:00 2001 From: Ella Wu <602725+ewu63@users.noreply.github.com> Date: Wed, 29 May 2024 16:27:29 -0700 Subject: [PATCH 01/12] add tests for all optimizers --- tests/test_sphere.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tests/test_sphere.py b/tests/test_sphere.py index 34c8f0e2..5c9c8c27 100644 --- a/tests/test_sphere.py +++ b/tests/test_sphere.py @@ -9,8 +9,11 @@ # First party modules from pyoptsparse import Optimization +from pyoptsparse.pyOpt_optimizer import Optimizers from pyoptsparse.testing import OptTest +ALL_OPTIMIZERS = [e.name for e in Optimizers] + class TestSphere(OptTest): ## Solve unconstrained Sphere problem. @@ -38,7 +41,7 @@ class TestSphere(OptTest): xStar = {"xvars": np.zeros(N)} # Tolerances - tol = {"ALPSO": 1e-3} + tol = {k: 1e-3 for k in ALL_OPTIMIZERS} optOptions = { "ALPSO": { # sphere @@ -48,7 +51,14 @@ class TestSphere(OptTest): "c2": 1.25, # Social Parameter "stopCriteria": 0, # 0: maxOuterIter, 1: convergence "seed": 1235, - } + }, + "NSGA2": { + "PopSize": 10, + "maxGen": 10, + }, + "SNOPT": { + "Major iterations limit": 10, + }, } def objfunc(self, xdict): @@ -83,7 +93,7 @@ def setup_optProb(self): # Objective self.optProb.addObj("obj") - @parameterized.expand(["ALPSO"]) + @parameterized.expand(ALL_OPTIMIZERS) def test_optimization(self, optName): self.optName = optName self.setup_optProb() From e07538f1d0620e3c68103d8277bf693b7ee50052 Mon Sep 17 00:00:00 2001 From: Ella Wu <602725+ewu63@users.noreply.github.com> Date: Wed, 12 Mar 2025 23:03:55 -0700 Subject: [PATCH 02/12] add timeout --- .github/azure-pipelines.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/azure-pipelines.yaml b/.github/azure-pipelines.yaml index bcce5c0d..955eabd5 100644 --- a/.github/azure-pipelines.yaml +++ b/.github/azure-pipelines.yaml @@ -18,3 +18,4 @@ extends: IMAGE: auto ISORT: true COVERAGE: true + TIMEOUT_BUILD: 30 From dc58a85c232684df8a04f6be355c567c2660add2 Mon Sep 17 00:00:00 2001 From: Ella Wu <602725+ewu63@users.noreply.github.com> Date: Sun, 20 Apr 2025 00:06:10 -0700 Subject: [PATCH 03/12] fix stupid gradient bug --- tests/test_sphere.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_sphere.py b/tests/test_sphere.py index 5c9c8c27..ea7311d3 100644 --- a/tests/test_sphere.py +++ b/tests/test_sphere.py @@ -73,8 +73,7 @@ def objfunc(self, xdict): def sens(self, xdict, _funcs): self.ng += 1 x = xdict["xvars"] - - funcsSens = {"obj": {"xvars": 2 * np.ones(len(x))}} + funcsSens = {"obj": {"xvars": 2 * x}} fail = False return funcsSens, fail From c411629312f6d8c9c02b27aa6042d544cca078b0 Mon Sep 17 00:00:00 2001 From: Ella Wu <602725+ewu63@users.noreply.github.com> Date: Sun, 20 Apr 2025 00:06:19 -0700 Subject: [PATCH 04/12] adjust tolerances --- tests/test_sphere.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_sphere.py b/tests/test_sphere.py index ea7311d3..b113023a 100644 --- a/tests/test_sphere.py +++ b/tests/test_sphere.py @@ -41,7 +41,7 @@ class TestSphere(OptTest): xStar = {"xvars": np.zeros(N)} # Tolerances - tol = {k: 1e-3 for k in ALL_OPTIMIZERS} + tol = {k: 5e-2 if k in ["CONMIN", "ALPSO"] else 1e-6 for k in ALL_OPTIMIZERS} optOptions = { "ALPSO": { # sphere From e582363781e53406e834bba7b73b7a2482aac90a Mon Sep 17 00:00:00 2001 From: Shugo Kaneko <49300827+kanekosh@users.noreply.github.com> Date: Fri, 25 Apr 2025 15:07:21 -0400 Subject: [PATCH 05/12] Update population size for NSGA2 test --- tests/test_sphere.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_sphere.py b/tests/test_sphere.py index b113023a..b393668d 100644 --- a/tests/test_sphere.py +++ b/tests/test_sphere.py @@ -53,7 +53,7 @@ class TestSphere(OptTest): "seed": 1235, }, "NSGA2": { - "PopSize": 10, + "PopSize": 12, "maxGen": 10, }, "SNOPT": { From 8831ba692c9e23e109fd1877b13f3f89c6bee54a Mon Sep 17 00:00:00 2001 From: Ella Wu <602725+ewu63@users.noreply.github.com> Date: Fri, 25 Apr 2025 22:35:24 -0700 Subject: [PATCH 06/12] adjust options --- tests/test_sphere.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_sphere.py b/tests/test_sphere.py index b393668d..e18aa92e 100644 --- a/tests/test_sphere.py +++ b/tests/test_sphere.py @@ -41,7 +41,7 @@ class TestSphere(OptTest): xStar = {"xvars": np.zeros(N)} # Tolerances - tol = {k: 5e-2 if k in ["CONMIN", "ALPSO"] else 1e-6 for k in ALL_OPTIMIZERS} + tol = {k: 5e-2 if k in ["CONMIN", "ALPSO", "NSGA2"] else 1e-6 for k in ALL_OPTIMIZERS} optOptions = { "ALPSO": { # sphere @@ -53,8 +53,8 @@ class TestSphere(OptTest): "seed": 1235, }, "NSGA2": { - "PopSize": 12, - "maxGen": 10, + "PopSize": 24, + "maxGen": 150, }, "SNOPT": { "Major iterations limit": 10, From 7a51682099dd478a3da8a5fbcfd1855634e29706 Mon Sep 17 00:00:00 2001 From: Ella Wu <602725+ewu63@users.noreply.github.com> Date: Fri, 25 Apr 2025 22:35:31 -0700 Subject: [PATCH 07/12] skip name check for NSGA2 --- pyoptsparse/testing/pyOpt_testing.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pyoptsparse/testing/pyOpt_testing.py b/pyoptsparse/testing/pyOpt_testing.py index c9535e39..e6dafabf 100644 --- a/pyoptsparse/testing/pyOpt_testing.py +++ b/pyoptsparse/testing/pyOpt_testing.py @@ -274,7 +274,9 @@ def check_hist_file(self, tol): hist = History(self.histFileName, flag="r") # Metadata checks metadata = hist.getMetadata() - self.assertEqual(metadata["optimizer"], self.optName) + # NSGA2's official name is NSGA-II + if self.optName != "NSGA2": + self.assertEqual(metadata["optimizer"], self.optName) metadata_def_keys = [ "optName", "optOptions", From e6752650552b0f37c301dfd5ff7debb6aa263ea5 Mon Sep 17 00:00:00 2001 From: Ella Wu <602725+ewu63@users.noreply.github.com> Date: Fri, 25 Apr 2025 22:50:43 -0700 Subject: [PATCH 08/12] add missing metadata section --- pyoptsparse/pyNSGA2/pyNSGA2.py | 7 +++++++ tests/test_sphere.py | 5 +++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pyoptsparse/pyNSGA2/pyNSGA2.py b/pyoptsparse/pyNSGA2/pyNSGA2.py index c83faa18..b7710828 100644 --- a/pyoptsparse/pyNSGA2/pyNSGA2.py +++ b/pyoptsparse/pyNSGA2/pyNSGA2.py @@ -4,6 +4,7 @@ """ # Standard Python modules +import datetime import os import time @@ -179,6 +180,12 @@ def objconfunc(nreal, nobj, ncon, x, f, g): # fmt: on optTime = time.time() - t0 + if self.storeHistory: + self.metadata["endTime"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + self.metadata["optTime"] = optTime + self.hist.writeData("metadata", self.metadata) + self.hist.close() + # Broadcast a -1 to indcate NSGA2 has finished self.optProb.comm.bcast(-1, root=0) diff --git a/tests/test_sphere.py b/tests/test_sphere.py index e18aa92e..b6fa617a 100644 --- a/tests/test_sphere.py +++ b/tests/test_sphere.py @@ -53,8 +53,9 @@ class TestSphere(OptTest): "seed": 1235, }, "NSGA2": { - "PopSize": 24, - "maxGen": 150, + "PopSize": 32, + "maxGen": 100, + "seed": 123, }, "SNOPT": { "Major iterations limit": 10, From 4275285278f4a1925734eb98cba51126e274cf11 Mon Sep 17 00:00:00 2001 From: Ella Wu <602725+ewu63@users.noreply.github.com> Date: Thu, 31 Jul 2025 18:22:54 -0700 Subject: [PATCH 09/12] skip ParOpt test --- tests/test_sphere.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_sphere.py b/tests/test_sphere.py index b6fa617a..c7bc417e 100644 --- a/tests/test_sphere.py +++ b/tests/test_sphere.py @@ -12,7 +12,7 @@ from pyoptsparse.pyOpt_optimizer import Optimizers from pyoptsparse.testing import OptTest -ALL_OPTIMIZERS = [e.name for e in Optimizers] +ALL_OPTIMIZERS = {e.name for e in Optimizers} - {"ParOpt"} class TestSphere(OptTest): From 862f65f6aeb5976feaf551fc1ff82bbc25a83112 Mon Sep 17 00:00:00 2001 From: Ella Wu <602725+ewu63@users.noreply.github.com> Date: Thu, 31 Jul 2025 18:27:58 -0700 Subject: [PATCH 10/12] need to cast to list --- tests/test_sphere.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_sphere.py b/tests/test_sphere.py index c7bc417e..5dfd31d2 100644 --- a/tests/test_sphere.py +++ b/tests/test_sphere.py @@ -12,7 +12,7 @@ from pyoptsparse.pyOpt_optimizer import Optimizers from pyoptsparse.testing import OptTest -ALL_OPTIMIZERS = {e.name for e in Optimizers} - {"ParOpt"} +ALL_OPTIMIZERS = sorted({e.name for e in Optimizers} - {"ParOpt"}) class TestSphere(OptTest): From 49d50e7f09d318488bac1e1fa0fc66fce2273645 Mon Sep 17 00:00:00 2001 From: Ella Wu <602725+ewu63@users.noreply.github.com> Date: Thu, 31 Jul 2025 18:37:26 -0700 Subject: [PATCH 11/12] also skip NSGA2 in uncon test --- tests/test_sphere.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_sphere.py b/tests/test_sphere.py index 5dfd31d2..a253e158 100644 --- a/tests/test_sphere.py +++ b/tests/test_sphere.py @@ -12,7 +12,7 @@ from pyoptsparse.pyOpt_optimizer import Optimizers from pyoptsparse.testing import OptTest -ALL_OPTIMIZERS = sorted({e.name for e in Optimizers} - {"ParOpt"}) +ALL_OPTIMIZERS = sorted({e.name for e in Optimizers} - {"ParOpt", "NSGA2"}) class TestSphere(OptTest): From 86a0dc9a4e9273e1799f95d6c2edce62e3dd053d Mon Sep 17 00:00:00 2001 From: Ella Wu <602725+ewu63@users.noreply.github.com> Date: Mon, 11 Aug 2025 15:42:00 -0700 Subject: [PATCH 12/12] added comment --- tests/test_sphere.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_sphere.py b/tests/test_sphere.py index a253e158..b1b5e292 100644 --- a/tests/test_sphere.py +++ b/tests/test_sphere.py @@ -52,7 +52,7 @@ class TestSphere(OptTest): "stopCriteria": 0, # 0: maxOuterIter, 1: convergence "seed": 1235, }, - "NSGA2": { + "NSGA2": { # not tested due to NSGA2 testing issues but option is kept here for reference "PopSize": 32, "maxGen": 100, "seed": 123,