forked from mdolab/pyoptsparse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_nsga2_multi_objective.py
More file actions
53 lines (37 loc) · 1.32 KB
/
Copy pathtest_nsga2_multi_objective.py
File metadata and controls
53 lines (37 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
""" Test NSGA2."""
# Standard Python modules
import unittest
# External modules
from numpy.testing import assert_allclose
# First party modules
from pyoptsparse import Optimization
# Local modules
from testing_utils import OptTest
class TestNSGA2(OptTest):
name = "quadratic"
optName = "NSGA2"
def objfunc(self, xdict):
x = xdict["x"]
y = xdict["y"]
funcs = {}
funcs["obj1"] = (x - 0.0) ** 2 + (y - 0.0) ** 2
funcs["obj2"] = (x - 1.0) ** 2 + (y - 1.0) ** 2
fail = False
return funcs, fail
def setup_optProb(self):
# Instantiate Optimization Problem
self.optProb = Optimization("quadratic", self.objfunc)
self.optProb.addVar("x", value=0, lower=-600, upper=600)
self.optProb.addVar("y", value=0, lower=-600, upper=600)
self.optProb.addObj("obj1")
self.optProb.addObj("obj2")
def test_opt(self):
self.setup_optProb()
# 300 generations will find x=(0,0), 200 or less will find x=(1,1)
optOptions = {"maxGen": 200}
sol = self.optimize(optOptions=optOptions)
tol = 1e-2
assert_allclose(sol.variables["x"][0].value, 1.0, atol=tol, rtol=tol)
assert_allclose(sol.variables["y"][0].value, 1.0, atol=tol, rtol=tol)
if __name__ == "__main__":
unittest.main()