Skip to content

Commit

Permalink
Merge d45b1fb into 524ff1d
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-de-vries committed Nov 12, 2019
2 parents 524ff1d + d45b1fb commit 9a302e0
Show file tree
Hide file tree
Showing 2 changed files with 25 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
22 changes: 22 additions & 0 deletions openmdao/drivers/tests/test_genetic_algorithm_driver.py
Expand Up @@ -353,6 +353,28 @@ 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')
prob.model.add_constraint('g_x', upper=np.zeros(dim))

prob.setup()
prob.run_driver()

# Check that the constraint is satisfied (x <= 1)
for i in range(dim):
self.assertLessEqual(prob["x"][i], 1.0)


class TestDriverOptionsSimpleGA(unittest.TestCase):

Expand Down

0 comments on commit 9a302e0

Please sign in to comment.