Usage
Introduction
An optimization by ropt using the plugin works mostly as any other optimization run. However, there are a few things to consider:
- Gradient Information: The plugin does not use gradient information, as
pymooalgorithms typically don't support user-defined gradients. Any gradient calculation settings inroptwill be ignored. - Ignored
roptParameters: Some standardroptoptimization parameters likemax_iterationsandtoleranceare not used by this plugin and will have no effect. - Initial Values:
pymoogenerally ignores initial variable values. However, you still need to provide an initial value vector inroptsimply to define the number of variables; a zero vector is sufficient for this purpose. - Constraint Support: Both linear and non-linear constraints are handled.
Linear constraints are automatically converted to non-linear constraints
internally before being passed to
pymoo. - Algorithm Specification: You must specify the
pymooalgorithm using its fully qualified object name as found in thepymoo.algorithmsmodule (e.g.,soo.nonconvex.ga.GA), not just a short name. - Algorithm Configuration via
options: The chosenpymooalgorithm and its specific parameters are configured entirely through theoptionsfield within theroptconfiguration object. The structure of thisoptionsdictionary directly mirrors the way options are set inpymoo, as detailed in the configuration section below.
Configuration
The algorithm to specify is set by the method field in optimization section
of a ropt configuration. Futher configuration of pymoo algorithms is
performed via the options field. The following example demonstrates the
configuration process for a Genetic
Algorithm, derived from the pymoo
manual, incorporating a penalty constraint. Here is how this is done in pymoo:
from pymoo.algorithms.soo.nonconvex.ga import GA
from pymoo.operators.crossover.sbx import SBX
from pymoo.operators.mutation.pm import PM
from pymoo.operators.repair.rounding import RoundingRepair
from pymoo.operators.sampling.rnd import IntegerRandomSampling
from pymoo.optimize import minimize
from pymoo.constraints.as_penalty import ConstraintsAsPenalty
from pymoo.problems import get_problem
problem = get_problem("g1")
method = GA(
pop_size=20,
sampling=IntegerRandomSampling(),
crossover=SBX(prob=1.0, eta=3.0, vtype=float, repair=RoundingRepair()),
mutation=PM(prob=1.0, eta=3.0, vtype=float, repair=RoundingRepair()),
eliminate_duplicates=True,
)
res = minimize(
ConstraintsAsPenalty(problem, penalty=100.0),
method,
termination=("n_gen", 40),
seed=1234,
)
To configure the equivalent optimization in ropt, first set the method field
within the optimization section of the ropt configuration to the fully
qualified name of the algorithm object in the pymoo.algorithms module (e.g.,
"soo.nonconvex.ga.GA").
Next, define the specific algorithm parameters, termination criteria,
constraints handling object, and random seed within the options field. This
field accepts a nested dictionary structure. The ropt-pymoo plugin parses this
dictionary to instantiate and configure the necessary pymoo objects based on
the provided names and parameters.
The general structure within the options dictionary is as follows:
- Algorithm Parameters: Arguments passed directly to the main algorithm's
constructor (like
pop_sizeforGA) are typically nested under a top-levelparameterskey. - Object Parameters: When a parameter's value is itself a
pymooobject (e.g.,sampling,crossover,mutation), specify it using a nested dictionary containing:- An
objectkey: The fully qualified name of thepymooclass (e.g.,"operators.sampling.rnd.IntegerRandomSampling"). - An optional
parameterskey: A dictionary of arguments to pass to that object's constructor. This can be nested further if those arguments are also objects.
- An
- Termination, Constraints, Seed: These are typically defined using their
own top-level keys (
termination,constraints,seed) within theoptionsdictionary, often following the sameobject/parameterspattern if they require configuration.
For clarity, the configuration corresponding to the preceding Python GA
example is shown below in YAML format, demonstrating how simple values and
nested objects are specified.
Within the options dictionary, the parameters key holds the arguments for
the GA object. The keys inside this parameters dictionary correspond to the
keyword arguments accepted by the GA constructor:
parameters:
pop_size: 20
sampling: # Specify objects using their fully qualified names:
object: operators.sampling.rnd.IntegerRandomSampling
crossover:
object: operators.crossover.sbx.SBX
parameters: # Specify parameters to an object with a `parameters` field:
prob: 1.0
eta: 3.0
vtype: float
repair:
object: operators.repair.rounding.RoundingRepair
mutation:
object: operators.mutation.pm.PM
parameters:
prob: 1.0
eta: 3.0
vtype: float
repair:
object: operators.repair.rounding.RoundingRepair
eliminate_duplicates: True
The termination criterion is configured using the termination field, either by
using a list of parameters that is passed on to the
pymoo.termination.get_termination()
function:
Alternatively, the termination can be configured using a specific termination
object. Specify this object using its fully qualified name from the
pymoo.termination module:
Constraints defined in the ropt configuration are automatically passed to
pymoo for handling. You can
customize how pymoo manages these constraints by specifying a particular
constraint handling class, such as
ConstraintsAsPenalty used in
the example. To configure this, add a constraints field to the options
dictionary, specifying the fully qualified name of the desired class from the
pymoo.constraints module and its parameters:
Finally, since GA requires random number generation, we specify a seed: