Skip to content

Commit

Permalink
Merge 51c1ea4 into 524ff1d
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-de-vries committed Nov 11, 2019
2 parents 524ff1d + 51c1ea4 commit 09da83e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions openmdao/drivers/genetic_algorithm_driver.py
Expand Up @@ -441,13 +441,13 @@ def objective_callback(self, x, icase):
for name, val in iteritems(self.get_constraint_values()):
con = self._cons[name]
# The not used fields will either None or a very large number
if (con['lower'] is not None) and (con['lower'] > -almost_inf):
if (con['lower'] is not None) and np.any(con['lower'] > -almost_inf):
diff = val - con['lower']
violation = np.array([0. if d >= 0 else abs(d) for d in diff])
elif (con['upper'] is not None) and (con['upper'] < almost_inf):
elif (con['upper'] is not None) and np.any(con['upper'] < almost_inf):
diff = val - con['upper']
violation = np.array([0. if d <= 0 else abs(d) for d in diff])
elif (con['equals'] is not None) and (abs(con['equals']) < almost_inf):
elif (con['equals'] is not None) and np.any(np.abs(con['equals']) < almost_inf):
diff = val - con['equals']
violation = np.absolute(diff)
constraint_violations = np.hstack((constraint_violations, violation))
Expand Down
20 changes: 20 additions & 0 deletions openmdao/drivers/tests/test_genetic_algorithm_driver.py
Expand Up @@ -353,6 +353,26 @@ def test_SimpleGADriver_missing_objective(self):

self.assertEqual(exception.args[0], msg)

def test_vectorized_constraints(self):
prob = om.Problem()
model = prob.model

dim = 2
model.add_subsystem('x', om.IndepVarComp('x', np.ones(dim)), promotes=['*'])
model.add_subsystem('f_x', om.ExecComp('f_x = sum(x * x)', x=np.ones(dim), f_x=1.0), promotes=['*'])
model.add_subsystem('g_x', om.ExecComp('g_x = 1 - x', x=np.ones(dim), g_x=np.zeros(dim)), promotes=['*'])

prob.driver = om.SimpleGADriver()

prob.model.add_design_var('x', lower=-10, upper=10)
prob.model.add_objective('f_x')

# Without bugfix to genetic_algorithm_driver, using vectorized constraint limits causes an exception
prob.model.add_constraint('g_x', upper=np.zeros(dim))

prob.setup()
prob.run_driver()


class TestDriverOptionsSimpleGA(unittest.TestCase):

Expand Down

0 comments on commit 09da83e

Please sign in to comment.