Pymoo constrained opt

From XuShuwenn/RedAct · View interactive version

Pymoo constrained opt is an AI agent skill from the GitHub repository XuShuwenn/RedAct. Solve constrained optimization problems using pymoo with NSGA-II to minimize objective functions subject to constraints.

How to use this skill

Skill file

Constrained Optimization with pymoo

When to Use

Problem Example

Using pymoo

from pymoo.optimize import minimize
from pymoo.algorithms.nsga2 import NSGA2


- Prefer one compact self-contained script: define the pymoo problem class, run `minimize(...)`, extract the final result, and write `/root/output.txt` in the same script.
- Encode the objective, bounds, and inequality constraints inside the problem class using pymoo's feasibility convention `G <= 0`; for example, `x1 + x2 >= 1` becomes `out["G"] = [1.0 - x[0] - x[1]]`.
- Use `ElementwiseProblem` for simple per-point evaluations unless the task clearly needs a different pymoo problem type.


# Define problem with constraints
# Run NSGA-II optimizer

Output Format

To /root/output.txt:

Optimal x1: X.XX
Optimal x2: X.XX
Objective: X.XX

Round to 2 decimal places.

After writing /root/output.txt, read it back once and confirm it contains exactly the required three labeled lines in order (Optimal x1, Optimal x2, Objective) with values rounded to 2 decimal places.

pymoo Components

Tips

import numpy as np

X = np.atleast_2d(res.X)
F = np.atleast_1d(res.F).reshape(-1)
G_raw = getattr(res, "G", None)
G = None if G_raw is None else np.atleast_2d(G_raw)

if G is not None and G.shape[0] == X.shape[0]:
    feasible = np.all(G <= 1e-6, axis=1)
    if not np.any(feasible):
        raise ValueError("No feasible solution found")
    X = X[feasible]
    F = F[feasible]

best = int(np.argmin(F))
x = X[best]
x1, x2 = float(x[0]), float(x[1])
obj = float(F[best])
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.