DifferentialEvolutionDriver#
Note
DifferentialEvolutionDriver is based on SimpleGADriver and supports most of the same options and capabilities.
This differential evolution variant of a genetic algorithm optimizer supports only continuous variables. The DifferentialEvolutionDriver supports both constrained and unconstrained optimization.
The DifferentialEvolutionDriver has advantages and disadvantages when compared to the SimpleGADriver:
Pros
DifferentialEvolutionDriver is typically about 3 times faster than SimpleGADriver
DifferentialEvolutionDriver is usually more accurate than SimpleGADriver because it does not limit the number of bits available to represent inputs
DifferentialEvolutionDriver does not require the user to manually specify a number of representation bits
Cons
DifferentialEvolutionDriver only supports continuous input variables; SimpleGADriver also supports discrete
DifferentialEvolutionDriver does not support SimpleGADriver’s “compute_pareto” option for multi-objective optimization
Genetic algorithms do not use gradient information to find optimal solutions. This makes them ideal for problems that do not have gradients or problems with many local minima where gradient information is not helpful in finding the global minimum. A well known example of this is finding the global minimum of of the Rastrigin function:
The example below shows an OpenMDAO solution of a higher order Rastrigin function.
import openmdao.api as om
import numpy as np
ORDER = 6 # dimension of problem
span = 5 # upper and lower limits
class RastriginComp(om.ExplicitComponent):
def setup(self):
self.add_input('x', np.zeros(ORDER))
self.add_output('y', 0.0)
def compute(self, inputs, outputs):
x = inputs['x']
# nth dimensional Rastrigin function, array input and scalar output
# global minimum at f(0,0,0...) = 0
n = len(x)
s = 10 * n
for i in range(n):
if np.abs(x[i]) < 1e-200: # avoid underflow runtime warnings from squaring tiny numbers
x[i] = 0.0
s += x[i] * x[i] - 10 * np.cos(2 * np.pi * x[i])
outputs['y'] = s
prob = om.Problem()
prob.model.add_subsystem('rastrigin', RastriginComp(), promotes_inputs=['x'])
prob.model.add_design_var('x',
lower=-span * np.ones(ORDER),
upper=span * np.ones(ORDER))
prob.model.add_objective('rastrigin.y')
prob.driver = om.DifferentialEvolutionDriver()
prob.driver.options['max_gen'] = 400
prob.driver.options['Pc'] = 0.5
prob.driver.options['F'] = 0.5
prob.setup()
prob.run_driver()
print(prob['rastrigin.y'])
print(prob['x'])
/usr/share/miniconda/envs/test/lib/python3.11/site-packages/openmdao/core/group.py:1166: DerivativesWarning:Constraints or objectives [rastrigin.y] cannot be impacted by the design variables of the problem because no partials were defined for them in their parent component(s).
[0.]
[0. 0. 0. 0. 0. 0.]
DifferentialEvolutionDriver Options#
Option | Default | Acceptable Values | Acceptable Types | Description |
---|---|---|---|---|
F | 0.9 | N/A | N/A | Differential rate. |
Pc | 0.9 | N/A | N/A | Crossover probability. |
debug_print | [] | ['desvars', 'nl_cons', 'ln_cons', 'objs', 'totals'] | ['list'] | List of what type of Driver variables to print at each iteration. |
invalid_desvar_behavior | warn | ['warn', 'raise', 'ignore'] | N/A | Behavior of driver if the initial value of a design variable exceeds its bounds. The default value may beset using the `OPENMDAO_INVALID_DESVAR_BEHAVIOR` environment variable to one of the valid options. |
max_gen | 100 | N/A | N/A | Number of generations before termination. |
multi_obj_exponent | 1.0 | N/A | N/A | Multi-objective weighting exponent. |
multi_obj_weights | {} | N/A | ['dict'] | Weights of objectives for multi-objective optimization.Weights are specified as a dictionary with the absolute namesof the objectives. The same weights for all objectives are assumed, if not given. |
penalty_exponent | 1.0 | N/A | N/A | Penalty function exponent. |
penalty_parameter | 10.0 | N/A | N/A | Penalty function parameter. |
pop_size | 0 | N/A | N/A | Number of points in the GA. Set to 0 and it will be computed as 20 times the total number of inputs. |
procs_per_model | 1 | N/A | N/A | Number of processors to give each model under MPI. |
run_parallel | False | [True, False] | ['bool'] | Set to True to execute the points in a generation in parallel. |
DifferentialEvolutionDriver Constructor#
The call signature for the DifferentialEvolutionDriver constructor is:
- DifferentialEvolutionDriver.__init__(**kwargs)[source]
Initialize the DifferentialEvolutionDriver driver.
Using DifferentialEvolutionDriver#
You can change the number of generations to run the genetic algorithm by setting the “max_gen” option.
Branin
class definition
class Branin(om.ExplicitComponent):
"""
The Branin test problem. This version is the standard version and
contains two continuous parameters.
"""
def setup(self):
"""
Define the independent variables, output variables, and partials.
"""
# Inputs
self.add_input('x0', 0.0)
self.add_input('x1', 0.0)
# Outputs
self.add_output('f', val=0.0)
def setup_partials(self):
self.declare_partials(of='f', wrt=['x0', 'x1'])
def compute(self, inputs, outputs):
"""
Define the function f(xI, xC).
When Branin is used in a mixed integer problem, x0 is integer and x1 is continuous.
"""
x0 = inputs['x0']
x1 = inputs['x1']
a = 1.0
b = 5.1/(4.0*np.pi**2)
c = 5.0/np.pi
d = 6.0
e = 10.0
f = 1.0/(8.0*np.pi)
outputs['f'] = a*(x1 - b*x0**2 + c*x0 - d)**2 + e*(1-f)*np.cos(x0) + e
def compute_partials(self, inputs, partials):
"""
Provide the Jacobian.
"""
x0 = inputs['x0']
x1 = inputs['x1']
a = 1.0
b = 5.1/(4.0*np.pi**2)
c = 5.0/np.pi
d = 6.0
e = 10.0
f = 1.0/(8.0*np.pi)
partials['f', 'x1'] = 2.0*a*(x1 - b*x0**2 + c*x0 - d)
partials['f', 'x0'] = 2.0*a*(x1 - b*x0**2 + c*x0 - d)*(-2.*b*x0 + c) - e*(1.-f)*np.sin(x0)
import openmdao.api as om
from openmdao.test_suite.components.branin import Branin
prob = om.Problem()
model = prob.model
model.add_subsystem('comp', Branin(), promotes_inputs=[('x0', 'xI'), ('x1', 'xC')])
model.add_design_var('xI', lower=-5.0, upper=10.0)
model.add_design_var('xC', lower=0.0, upper=15.0)
model.add_objective('comp.f')
prob.driver = om.DifferentialEvolutionDriver()
prob.driver.options['max_gen'] = 5
prob.setup()
prob.run_driver()
You can change the population size by setting the “pop_size” option. The default value for pop_size is 0, which means that the driver automatically computes a population size that is 20 times the total number of input variables.
import openmdao.api as om
from openmdao.test_suite.components.branin import Branin
prob = om.Problem()
model = prob.model
model.add_subsystem('comp', Branin(), promotes_inputs=[('x0', 'xI'), ('x1', 'xC')])
model.add_design_var('xI', lower=-5.0, upper=10.0)
model.add_design_var('xC', lower=0.0, upper=15.0)
model.add_objective('comp.f')
prob.driver = om.DifferentialEvolutionDriver()
prob.driver.options['pop_size'] = 10
prob.setup()
prob.run_driver()