Skip to content

Commit 2b35f53

Browse files
ewu63kanekosh
andauthored
Add unconstrained tests for all optimizers (#402)
* add tests for all optimizers * add timeout * fix stupid gradient bug * adjust tolerances * Update population size for NSGA2 test * adjust options * skip name check for NSGA2 * add missing metadata section * skip ParOpt test * need to cast to list * also skip NSGA2 in uncon test * added comment --------- Co-authored-by: Shugo Kaneko <[email protected]>
1 parent bf7d311 commit 2b35f53

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

.github/azure-pipelines.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ extends:
1818
IMAGE: auto
1919
ISORT: true
2020
COVERAGE: true
21+
TIMEOUT_BUILD: 30

pyoptsparse/pyNSGA2/pyNSGA2.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
# Standard Python modules
7+
import datetime
78
import os
89
import time
910

@@ -179,6 +180,12 @@ def objconfunc(nreal, nobj, ncon, x, f, g):
179180
# fmt: on
180181
optTime = time.time() - t0
181182

183+
if self.storeHistory:
184+
self.metadata["endTime"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
185+
self.metadata["optTime"] = optTime
186+
self.hist.writeData("metadata", self.metadata)
187+
self.hist.close()
188+
182189
# Broadcast a -1 to indcate NSGA2 has finished
183190
self.optProb.comm.bcast(-1, root=0)
184191

pyoptsparse/testing/pyOpt_testing.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ def check_hist_file(self, tol):
274274
hist = History(self.histFileName, flag="r")
275275
# Metadata checks
276276
metadata = hist.getMetadata()
277-
self.assertEqual(metadata["optimizer"], self.optName)
277+
# NSGA2's official name is NSGA-II
278+
if self.optName != "NSGA2":
279+
self.assertEqual(metadata["optimizer"], self.optName)
278280
metadata_def_keys = [
279281
"optName",
280282
"optOptions",

tests/test_sphere.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99

1010
# First party modules
1111
from pyoptsparse import Optimization
12+
from pyoptsparse.pyOpt_optimizer import Optimizers
1213
from pyoptsparse.testing import OptTest
1314

15+
ALL_OPTIMIZERS = sorted({e.name for e in Optimizers} - {"ParOpt", "NSGA2"})
16+
1417

1518
class TestSphere(OptTest):
1619
## Solve unconstrained Sphere problem.
@@ -38,7 +41,7 @@ class TestSphere(OptTest):
3841
xStar = {"xvars": np.zeros(N)}
3942

4043
# Tolerances
41-
tol = {"ALPSO": 1e-3}
44+
tol = {k: 5e-2 if k in ["CONMIN", "ALPSO", "NSGA2"] else 1e-6 for k in ALL_OPTIMIZERS}
4245

4346
optOptions = {
4447
"ALPSO": { # sphere
@@ -48,7 +51,15 @@ class TestSphere(OptTest):
4851
"c2": 1.25, # Social Parameter
4952
"stopCriteria": 0, # 0: maxOuterIter, 1: convergence
5053
"seed": 1235,
51-
}
54+
},
55+
"NSGA2": { # not tested due to NSGA2 testing issues but option is kept here for reference
56+
"PopSize": 32,
57+
"maxGen": 100,
58+
"seed": 123,
59+
},
60+
"SNOPT": {
61+
"Major iterations limit": 10,
62+
},
5263
}
5364

5465
def objfunc(self, xdict):
@@ -63,8 +74,7 @@ def objfunc(self, xdict):
6374
def sens(self, xdict, _funcs):
6475
self.ng += 1
6576
x = xdict["xvars"]
66-
67-
funcsSens = {"obj": {"xvars": 2 * np.ones(len(x))}}
77+
funcsSens = {"obj": {"xvars": 2 * x}}
6878

6979
fail = False
7080
return funcsSens, fail
@@ -83,7 +93,7 @@ def setup_optProb(self):
8393
# Objective
8494
self.optProb.addObj("obj")
8595

86-
@parameterized.expand(["ALPSO"])
96+
@parameterized.expand(ALL_OPTIMIZERS)
8797
def test_optimization(self, optName):
8898
self.optName = optName
8999
self.setup_optProb()

0 commit comments

Comments
 (0)