Pymoo Elite
All-in-one skill covering multi, objective, optimization, framework. Includes structured workflows, validation checks, and reusable patterns for scientific.
Pymoo Elite
Solve single-objective and multi-objective optimization problems using Pymoo, a comprehensive Python optimization framework. This skill covers algorithm selection, constraint handling, custom operators, performance metrics, and real-world optimization workflows for engineering and scientific applications.
When to Use This Skill
Choose Pymoo Elite when you need to:
- Solve multi-objective optimization problems with Pareto-optimal solutions
- Apply evolutionary algorithms (NSGA-II, NSGA-III, MOEA/D) to complex search spaces
- Optimize designs with multiple conflicting objectives and constraints
- Benchmark optimization algorithms on standard test problems
Consider alternatives when:
- You need gradient-based optimization for differentiable objectives (use scipy.optimize or Optuna)
- You need Bayesian optimization for expensive black-box functions (use BoTorch or Ax)
- You need reinforcement learning-based optimization (use stable-baselines3)
Quick Start
pip install pymoo
from pymoo.algorithms.moo.nsga2 import NSGA2 from pymoo.problems import get_problem from pymoo.optimize import minimize from pymoo.visualization.scatter import Scatter # Define a multi-objective test problem problem = get_problem("zdt1") # Configure NSGA-II algorithm algorithm = NSGA2(pop_size=100) # Run optimization res = minimize( problem, algorithm, ("n_gen", 200), seed=42, verbose=True ) print(f"Solutions found: {len(res.F)}") print(f"Best objectives: {res.F[:3]}") # Plot Pareto front Scatter().add(res.F).show()
Core Concepts
Available Algorithms
| Algorithm | Type | Best For |
|---|---|---|
NSGA2 | Multi-objective | 2-3 objectives, general purpose |
NSGA3 | Many-objective | 4+ objectives |
MOEAD | Multi-objective | Uniform Pareto front distribution |
GA | Single-objective | Discrete/combinatorial problems |
DE | Single-objective | Continuous optimization |
PSO | Single-objective | Fast convergence, simple landscapes |
CMAES | Single-objective | Small-dimensional, precise optima |
Custom Optimization Problem
from pymoo.core.problem import Problem import numpy as np class EngineeringDesign(Problem): """Optimize a beam design with weight vs deflection trade-off.""" def __init__(self): super().__init__( n_var=3, # width, height, length n_obj=2, # weight, deflection n_constr=2, # stress, buckling constraints xl=np.array([1.0, 1.0, 10.0]), # lower bounds xu=np.array([10.0, 10.0, 100.0]) # upper bounds ) def _evaluate(self, X, out, *args, **kwargs): width = X[:, 0] height = X[:, 1] length = X[:, 2] # Objective 1: Weight (minimize) weight = 7850 * width * height * length * 1e-6 # kg # Objective 2: Deflection (minimize) I = (width * height**3) / 12 # moment of inertia E = 210e3 # Young's modulus (MPa) F = 1000 # applied force (N) deflection = (F * length**3) / (48 * E * I) out["F"] = np.column_stack([weight, deflection]) # Constraint 1: Max stress < 250 MPa stress = (F * length) / (4 * width * height**2 / 6) - 250 # Constraint 2: Min height/width ratio > 0.5 ratio = 0.5 - height / width out["G"] = np.column_stack([stress, ratio]) # Solve from pymoo.algorithms.moo.nsga2 import NSGA2 from pymoo.optimize import minimize problem = EngineeringDesign() algorithm = NSGA2(pop_size=200) res = minimize(problem, algorithm, ("n_gen", 300), seed=42) print(f"Pareto solutions: {len(res.F)}") for i in range(min(5, len(res.X))): print(f" Design: w={res.X[i,0]:.1f}, h={res.X[i,1]:.1f}, " f"L={res.X[i,2]:.1f} → Weight={res.F[i,0]:.2f}kg, " f"Deflection={res.F[i,1]:.4f}mm")
Configuration
| Parameter | Description | Default |
|---|---|---|
pop_size | Population size per generation | 100 |
n_gen | Maximum number of generations | 200 |
seed | Random seed for reproducibility | None |
crossover | Crossover operator | SBX(prob=0.9) |
mutation | Mutation operator | PM(prob=1/n_var) |
sampling | Initial population sampling | FloatRandomSampling() |
eliminate_duplicates | Remove duplicate solutions | true |
Best Practices
-
Normalize objectives to similar scales — If one objective ranges 0-1 and another 0-10000, the algorithm biases toward the larger-scale objective. Normalize all objectives to [0, 1] range or use reference point-based algorithms (NSGA-III) which handle scaling more robustly.
-
Run multiple seeds and compare — Evolutionary algorithms are stochastic. Run the same problem with 5-10 different seeds and compare the Pareto fronts. Use hypervolume indicator to quantitatively assess solution quality across runs.
-
Start with large population, reduce later — Begin with
pop_size=200-500to explore the search space broadly, then refine with smaller populations focused on promising regions. Small populations risk premature convergence to suboptimal Pareto fronts. -
Set constraints as inequalities ≤ 0 — Pymoo expects constraints in the form g(x) ≤ 0. A constraint like "stress must be less than 250 MPa" becomes
g = stress - 250. Positive values indicate violation; negative or zero values indicate satisfaction. -
Visualize Pareto fronts during optimization — Use
pymoo.visualization.Scatterto plot intermediate Pareto fronts every 50 generations. This reveals whether the algorithm is still improving or has converged, helping you decide when to stop.
Common Issues
All solutions violate constraints — The initial random population may not contain any feasible solutions. Increase pop_size to explore more of the search space, or provide feasible starting solutions via custom sampling. Also check that constraint bounds are not contradictory.
Pareto front is incomplete or sparse — Insufficient generations or population size causes gaps in the Pareto front. Increase n_gen and pop_size, or switch to MOEA/D which explicitly maintains evenly distributed reference directions for uniform Pareto coverage.
Optimization runs too slowly — The bottleneck is usually the _evaluate function. Vectorize computations using NumPy (the X input is already a 2D array with one row per solution). For expensive simulations, consider surrogate-assisted optimization or parallel evaluation with pymoo.core.problem.starmap_parallelized_eval.
Reviews
No reviews yet. Be the first to review this template!
Similar Templates
Full-Stack Code Reviewer
Comprehensive code review skill that checks for security vulnerabilities, performance issues, accessibility, and best practices across frontend and backend code.
Test Suite Generator
Generates comprehensive test suites with unit tests, integration tests, and edge cases. Supports Jest, Vitest, Pytest, and Go testing.
Pro Architecture Workspace
Battle-tested skill for architectural, decision, making, framework. Includes structured workflows, validation checks, and reusable patterns for development.