scientific-skills/pymoo/references/operators.md
Comprehensive reference for genetic operators in pymoo.
Sampling operators initialize populations at the start of optimization.
Purpose: Generate random initial solutions Types:
FloatRandomSampling: Continuous variablesBinaryRandomSampling: Binary variablesIntegerRandomSampling: Integer variablesPermutationRandomSampling: Permutation-based problemsUsage:
from pymoo.operators.sampling.rnd import FloatRandomSampling
sampling = FloatRandomSampling()
Purpose: Space-filling initial population Benefit: Better coverage of search space than random Types:
LHS: Standard Latin HypercubeUsage:
from pymoo.operators.sampling.lhs import LHS
sampling = LHS()
Provide initial population through Population object or NumPy array
Selection operators choose parents for reproduction.
Purpose: Select parents through tournament competition Mechanism: Randomly select k individuals, choose best Parameters:
pressure: Tournament size (default: 2)func_comp: Comparison functionUsage:
from pymoo.operators.selection.tournament import TournamentSelection
selection = TournamentSelection(pressure=2)
Purpose: Uniform random parent selection Use case: Baseline or exploration-focused algorithms
Usage:
from pymoo.operators.selection.rnd import RandomSelection
selection = RandomSelection()
Crossover operators recombine parent solutions to create offspring.
Purpose: Primary crossover for continuous optimization Mechanism: Simulates single-point crossover of binary-encoded variables Parameters:
prob: Crossover probability (default: 0.9)eta: Distribution index (default: 15)
Usage:
from pymoo.operators.crossover.sbx import SBX
crossover = SBX(prob=0.9, eta=15)
String shorthand: "real_sbx"
Purpose: DE-specific recombination Variants:
DE/rand/1/binDE/best/1/binDE/current-to-best/1/binParameters:
CR: Crossover rateF: Scaling factorPurpose: Cut and swap at one point Usage:
from pymoo.operators.crossover.pntx import SinglePointCrossover
crossover = SinglePointCrossover()
Purpose: Cut and swap between two points Usage:
from pymoo.operators.crossover.pntx import TwoPointCrossover
crossover = TwoPointCrossover()
Purpose: Multiple cut points Parameters:
n_points: Number of crossover pointsPurpose: Each gene independently from either parent Parameters:
prob: Per-gene swap probability (default: 0.5)Usage:
from pymoo.operators.crossover.ux import UniformCrossover
crossover = UniformCrossover(prob=0.5)
Purpose: Exchange exactly half of differing genes Benefit: Maintains genetic diversity
Purpose: Preserve relative order from parents Use case: Traveling salesman, scheduling problems
Usage:
from pymoo.operators.crossover.ox import OrderCrossover
crossover = OrderCrossover()
Purpose: Preserve edge information from parents Use case: Routing problems where edge connectivity matters
Purpose: Exchange segments while maintaining permutation validity
Mutation operators introduce variation to maintain diversity.
Purpose: Primary mutation for continuous optimization Mechanism: Polynomial probability distribution Parameters:
prob: Per-variable mutation probabilityeta: Distribution index (default: 20)
Usage:
from pymoo.operators.mutation.pm import PM
mutation = PM(prob=None, eta=20) # prob=None means 1/n_var
String shorthand: "real_pm"
Probability guidelines:
None or 1/n_var: Standard recommendationPurpose: Flip bits with specified probability Parameters:
prob: Per-bit flip probabilityUsage:
from pymoo.operators.mutation.bitflip import BitflipMutation
mutation = BitflipMutation(prob=0.05)
Purpose: PM adapted for integers Ensures: Valid integer values after mutation
Purpose: Reverse a segment of the permutation Use case: Maintains some order structure
Usage:
from pymoo.operators.mutation.inversion import InversionMutation
mutation = InversionMutation()
Purpose: Randomly shuffle a segment
Define custom mutation by extending Mutation class
Repair operators fix constraint violations or ensure solution feasibility.
Purpose: Round to nearest valid value Use case: Integer/discrete variables with bound constraints
Purpose: Reflect out-of-bounds values back into feasible region Use case: Box-constrained continuous problems
Purpose: Project infeasible solutions onto feasible region Use case: Linear constraints
Purpose: Domain-specific constraint handling
Implementation: Extend Repair class
Example:
from pymoo.core.repair import Repair
class MyRepair(Repair):
def _do(self, problem, X, **kwargs):
# Modify X to satisfy constraints
# Return repaired X
return X
Crossover probability:
Mutation probability:
1/n_var: Standard recommendationDistribution indices (eta):
Continuous problems:
Binary problems:
Permutation problems:
Mixed-variable problems:
Pymoo supports convenient string-based operator specification:
from pymoo.algorithms.soo.nonconvex.ga import GA
algorithm = GA(
pop_size=100,
sampling="real_random",
crossover="real_sbx",
mutation="real_pm"
)
Available strings:
"real_random", "real_lhs", "bin_random", "perm_random""real_sbx", "real_de", "int_sbx", "bin_ux", "bin_hux""real_pm", "int_pm", "bin_bitflip", "perm_inv"from pymoo.operators.sampling.rnd import FloatRandomSampling
from pymoo.operators.crossover.sbx import SBX
from pymoo.operators.mutation.pm import PM
from pymoo.operators.selection.tournament import TournamentSelection
sampling = FloatRandomSampling()
crossover = SBX(prob=0.9, eta=15)
mutation = PM(eta=20)
selection = TournamentSelection()
from pymoo.operators.sampling.rnd import BinaryRandomSampling
from pymoo.operators.crossover.pntx import TwoPointCrossover
from pymoo.operators.mutation.bitflip import BitflipMutation
sampling = BinaryRandomSampling()
crossover = TwoPointCrossover()
mutation = BitflipMutation(prob=0.05)
from pymoo.operators.sampling.rnd import PermutationRandomSampling
from pymoo.operators.crossover.ox import OrderCrossover
from pymoo.operators.mutation.inversion import InversionMutation
sampling = PermutationRandomSampling()
crossover = OrderCrossover()
mutation = InversionMutation()