Skill Tracer
Skills › Scientific Agent Skills › pymoo › operators.md

operators.md

117 lines · skills/pymoo/references/operators.md at 5f2bc03

Open on GitHub
1# Genetic operators (pymoo 0.6.2)
2
3Use instantiated operators. Historical strings such as `"real_sbx"`,
4`"real_random"`, and `"real_pm"` are not accepted by current GA execution.
5

Genetic operators (pymoo 0.6.2)

Use instantiated operators. Historical strings such as "real_sbx", "real_random", and "real_pm" are not accepted by current GA execution.

Raw lines 1 to 5
6## Continuous variables
7
8```python
9from pymoo.algorithms.soo.nonconvex.ga import GA
10from pymoo.operators.sampling.lhs import LHS
11from pymoo.operators.crossover.sbx import SBX
12from pymoo.operators.mutation.pm import PM
14algorithm = GA(pop_size=40, sampling=LHS(),
15 crossover=SBX(prob=0.9, eta=15),
16 mutation=PM(prob=0.9, prob_var=None, eta=20),
17 eliminate_duplicates=True)
18```
20- Sampling: `FloatRandomSampling()` or `LHS()`. LHS stratifies marginal samples;
21 it does not guarantee superior optimizer performance.
22- SBX `prob` is the probability of applying crossover to a mating. Its separate
23 `prob_var` controls variable exchange (default 0.5). Larger `eta` tends to
24 produce offspring closer to their parents.
25- PM `prob` is the probability of mutating an **individual**, default 0.9.
26 `prob_var` is the per-variable mutation probability. `None` gives
27 `min(0.5, 1/problem.n_var)` in 0.6.2. `PM(prob=None)` is not the default-rate
28 recipe and can fail during mutation. Larger `eta` concentrates perturbations.
29- DE uses its own variation. Configure `DE(variant="DE/rand/1/bin", CR=0.7, F=0.5)`
30 from `pymoo.algorithms.soo.nonconvex.de`; do not substitute a string for GA's
31 crossover object.

Continuous variables

from pymoo.algorithms.soo.nonconvex.ga import GA
from pymoo.operators.sampling.lhs import LHS
from pymoo.operators.crossover.sbx import SBX
from pymoo.operators.mutation.pm import PM

algorithm = GA(pop_size=40, sampling=LHS(),
               crossover=SBX(prob=0.9, eta=15),
               mutation=PM(prob=0.9, prob_var=None, eta=20),
               eliminate_duplicates=True)
  • Sampling: FloatRandomSampling() or LHS(). LHS stratifies marginal samples; it does not guarantee superior optimizer performance.
  • SBX prob is the probability of applying crossover to a mating. Its separate prob_var controls variable exchange (default 0.5). Larger eta tends to produce offspring closer to their parents.
  • PM prob is the probability of mutating an individual, default 0.9. prob_var is the per-variable mutation probability. None gives min(0.5, 1/problem.n_var) in 0.6.2. PM(prob=None) is not the default-rate recipe and can fail during mutation. Larger eta concentrates perturbations.
  • DE uses its own variation. Configure DE(variant="DE/rand/1/bin", CR=0.7, F=0.5) from pymoo.algorithms.soo.nonconvex.de; do not substitute a string for GA's crossover object.
Raw lines 6 to 32
33## Binary variables
35```python
36from pymoo.operators.sampling.rnd import BinaryRandomSampling
37from pymoo.operators.crossover.pntx import TwoPointCrossover
38from pymoo.operators.mutation.bitflip import BitflipMutation
40binary_ga = GA(pop_size=40, sampling=BinaryRandomSampling(),
41 crossover=TwoPointCrossover(),
42 mutation=BitflipMutation(prob=1.0, prob_var=0.05))
43```
45`SinglePointCrossover`, `TwoPointCrossover`, `PointCrossover(n_points=...)`,
46`UniformCrossover`, and `HalfUniformCrossover` cover common binary recombination.
47`UniformCrossover(prob=0.9)` applies crossover to 90% of matings; the internal
48per-gene swap probability is fixed at 0.5. `BitflipMutation.prob_var`, not `prob`,
49is the per-bit probability. Bitflip expects Boolean decision arrays.

Binary variables

from pymoo.operators.sampling.rnd import BinaryRandomSampling
from pymoo.operators.crossover.pntx import TwoPointCrossover
from pymoo.operators.mutation.bitflip import BitflipMutation

binary_ga = GA(pop_size=40, sampling=BinaryRandomSampling(),
               crossover=TwoPointCrossover(),
               mutation=BitflipMutation(prob=1.0, prob_var=0.05))

SinglePointCrossover, TwoPointCrossover, PointCrossover(n_points=...), UniformCrossover, and HalfUniformCrossover cover common binary recombination. UniformCrossover(prob=0.9) applies crossover to 90% of matings; the internal per-gene swap probability is fixed at 0.5. BitflipMutation.prob_var, not prob, is the per-bit probability. Bitflip expects Boolean decision arrays.

Raw lines 33 to 50
51## Integers and mixed variables
53```python
54from pymoo.operators.sampling.rnd import IntegerRandomSampling
55from pymoo.operators.repair.rounding import RoundingRepair
57integer_ga = GA(pop_size=40, sampling=IntegerRandomSampling(),
58 crossover=SBX(prob=0.9, eta=15, vtype=float, repair=RoundingRepair()),
59 mutation=PM(eta=20, vtype=float, repair=RoundingRepair()))
60```
62Use integral bounds; rounding alone does not impose nonlinear constraints.
63For heterogeneous variables, define `vars` using `Real`, `Integer`, `Choice`,
64`Binary` and use `MixedVariableGA` or explicit mixed mating. Merely setting a
65problem's type does not replace incompatible continuous operators.

Integers and mixed variables

from pymoo.operators.sampling.rnd import IntegerRandomSampling
from pymoo.operators.repair.rounding import RoundingRepair

integer_ga = GA(pop_size=40, sampling=IntegerRandomSampling(),
    crossover=SBX(prob=0.9, eta=15, vtype=float, repair=RoundingRepair()),
    mutation=PM(eta=20, vtype=float, repair=RoundingRepair()))

Use integral bounds; rounding alone does not impose nonlinear constraints. For heterogeneous variables, define vars using Real, Integer, Choice, Binary and use MixedVariableGA or explicit mixed mating. Merely setting a problem's type does not replace incompatible continuous operators.

Raw lines 51 to 66
67## Permutations
69```python
70from pymoo.operators.sampling.rnd import PermutationRandomSampling
71from pymoo.operators.crossover.ox import OrderCrossover
72from pymoo.operators.mutation.inversion import InversionMutation
74permutation_ga = GA(pop_size=40, sampling=PermutationRandomSampling(),
75 crossover=OrderCrossover(), mutation=InversionMutation())
76```
78`EdgeRecombinationCrossover` is also available in
79`pymoo.operators.crossover.erx`. PMX and scramble mutation are not shipped
80operators in 0.6.2; implement and validate a custom operator if required. Preserve
81one occurrence of each item, and separately repair domain constraints such as
82route precedence.

Permutations

from pymoo.operators.sampling.rnd import PermutationRandomSampling
from pymoo.operators.crossover.ox import OrderCrossover
from pymoo.operators.mutation.inversion import InversionMutation

permutation_ga = GA(pop_size=40, sampling=PermutationRandomSampling(),
                    crossover=OrderCrossover(), mutation=InversionMutation())

EdgeRecombinationCrossover is also available in pymoo.operators.crossover.erx. PMX and scramble mutation are not shipped operators in 0.6.2; implement and validate a custom operator if required. Preserve one occurrence of each item, and separately repair domain constraints such as route precedence.

Raw lines 67 to 83
84## Selection and repair
86GA and NSGA-II supply algorithm-specific tournament comparators. A bare
87`TournamentSelection()` raises an error; a custom instance requires `func_comp`.
88For the standard single-objective comparator:
90```python
91from pymoo.algorithms.soo.nonconvex.ga import comp_by_cv_and_fitness
92from pymoo.operators.selection.tournament import TournamentSelection
93selection = TournamentSelection(func_comp=comp_by_cv_and_fitness, pressure=2)
94```
96Use a multi-objective comparator for multi-objective survival/ranking.
97`RandomSelection()` from `pymoo.operators.selection.rnd` selects uniformly.
99```python
100import numpy as np
101from pymoo.core.repair import Repair
103class BoxRepair(Repair):
104 def _do(self, problem, X, **kwargs):
105 return np.clip(X, problem.xl, problem.xu)
106```
108This repairs bounds only. A custom operator should accept `**kwargs`; randomized
109operators should use the supplied `random_state` (a NumPy Generator in 0.6.2),
110not an unseeded global RNG. Validate bounds, types, and problem-specific invariants
111after operator changes, then compare matched budgets over multiple seeds.
113Sources reviewed 2026-10-01: [selection](https://pymoo.org/operators/selection.html),
114[crossover](https://pymoo.org/operators/crossover.html),
115[mutation](https://pymoo.org/operators/mutation.html),
116[discrete variables](https://pymoo.org/customization/discrete.html), and released
1170.6.2 operator source. Code recipes were instantiated and exercised on toy inputs.

Selection and repair

GA and NSGA-II supply algorithm-specific tournament comparators. A bare TournamentSelection() raises an error; a custom instance requires func_comp. For the standard single-objective comparator:

from pymoo.algorithms.soo.nonconvex.ga import comp_by_cv_and_fitness
from pymoo.operators.selection.tournament import TournamentSelection
selection = TournamentSelection(func_comp=comp_by_cv_and_fitness, pressure=2)

Use a multi-objective comparator for multi-objective survival/ranking. RandomSelection() from pymoo.operators.selection.rnd selects uniformly.

import numpy as np
from pymoo.core.repair import Repair

class BoxRepair(Repair):
    def _do(self, problem, X, **kwargs):
        return np.clip(X, problem.xl, problem.xu)

This repairs bounds only. A custom operator should accept **kwargs; randomized operators should use the supplied random_state (a NumPy Generator in 0.6.2), not an unseeded global RNG. Validate bounds, types, and problem-specific invariants after operator changes, then compare matched budgets over multiple seeds.

Sources reviewed 2026-10-01: selection, crossover, mutation, discrete variables, and released 0.6.2 operator source. Code recipes were instantiated and exercised on toy inputs.

Raw lines 84 to 117