Skip to content

Commit 9a120d3

Browse files
committed
enforce popsize in increments of 4
1 parent 6278bca commit 9a120d3

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

pyoptsparse/pyNSGA2/pyNSGA2.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ def __init__(self, raiseError=True, options={}):
3434
if isinstance(nsga2, str) and raiseError:
3535
raise ImportError(nsga2)
3636

37+
if self.getOption("PopSize") % 4 != 0:
38+
raise ValueError("Option 'PopSize' must be a multiple of 4")
39+
3740
@staticmethod
3841
def _getInforms():
3942
informs = {}

pyoptsparse/pyNSGA2/source/tourselect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void selection (population *old_pop, population *new_pop, Global global, int *nr
3232
a2[rand] = a2[i];
3333
a2[i] = temp;
3434
}
35-
for (i=0; i<global.popsize-4; i+=4)
35+
for (i=0; i<global.popsize; i+=4)
3636
{
3737
parent1 = tournament (&old_pop->ind[a1[i]], &old_pop->ind[a1[i+1]], global);
3838
parent2 = tournament (&old_pop->ind[a1[i+2]], &old_pop->ind[a1[i+3]], global);

tests/test_nsga2_multi_objective.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ def test_opt(self, n_obj):
7373
assert_allclose(sol.fStar["obj1"], 12.0, atol=tol, rtol=tol)
7474
assert_allclose(sol.fStar["obj2"], 0.0, atol=tol, rtol=tol)
7575

76+
def test_options(self):
77+
with self.assertRaises(ValueError):
78+
# PopSize must be a multiple of 4
79+
self.setup_optProb(1)
80+
optOptions = {"PopSize": 5}
81+
sol = self.optimize(optOptions=optOptions)
82+
7683

7784
if __name__ == "__main__":
7885
unittest.main()

0 commit comments

Comments
 (0)