Comprehensive reference for genetic operators in pymoo.
Sampling operators initialize populations at the start of optimization.
Purpose: Generate random initial solutions
Types:
- FloatRandomSampling: Continuous variables
- BinaryRandomSampling: Binary variables
- IntegerRandomSampling: Integer variables
- PermutationRandomSampling: Permutation-based problems
Usage:
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 Hypercube
Usage:
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 function
Usage:
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)
- Higher eta → offspring closer to parents
- Lower eta → more exploration
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/bin
- DE/best/1/bin
- DE/current-to-best/1/bin
Parameters:
- CR: Crossover rate
- F: Scaling factor
Purpose: 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 points
Purpose: 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 probability
- eta: Distribution index (default: 20)
- Higher eta → smaller perturbations
- Lower eta → larger perturbations
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 recommendation
- Higher for more exploration
- Lower for more exploitation
Purpose: Flip bits with specified probability
Parameters:
- prob: Per-bit flip probability
Usage:
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: - High (0.8-0.95): Standard for most problems - Lower: More emphasis on mutation
Mutation probability:
- 1/n_var: Standard recommendation
- Higher: More exploration, slower convergence
- Lower: Faster convergence, risk of premature convergence
Distribution indices (eta): - Crossover eta (15-30): Higher for local search - Mutation eta (20-50): Higher for exploitation
Continuous problems: - Crossover: SBX - Mutation: Polynomial Mutation - Selection: Tournament
Binary problems: - Crossover: Two-point or Uniform - Mutation: Bitflip - Selection: Tournament
Permutation problems: - Crossover: Order Crossover (OX) - Mutation: Inversion or Scramble - Selection: Tournament
Mixed-variable problems: - Use appropriate operators per variable type - Ensure operator compatibility
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:
- Sampling: "real_random", "real_lhs", "bin_random", "perm_random"
- Crossover: "real_sbx", "real_de", "int_sbx", "bin_ux", "bin_hux"
- Mutation: "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()