Pymoo nsga2 pareto export

From XuShuwenn/RedAct · View interactive version

Pymoo nsga2 pareto export is an AI agent skill from the GitHub repository XuShuwenn/RedAct. Use pymoo’s NSGA-II to compute a Pareto front for simple multi-objective problems and export sorted, formatted points.

How to use this skill

Skill file

NSGA-II Pareto Front with pymoo

This skill describes a robust workflow to set up and run a short NSGA-II optimization using the pymoo library for simple multi-objective problems, extract a non-dominated set, and export results in a predictable, sorted format.

When to Use

Activate this skill when the user requests:

Core Workflow

Follow these steps to ensure a correct and reproducible export of Pareto solutions.

  1. Define the problem

Example using ElementwiseProblem:

```python

import numpy as np

from pymoo.core.problem import ElementwiseProblem

class TwoObj1D(ElementwiseProblem):

def init(self, xl, xu, f1, f2):

super().init(n_var=1, n_obj=2, n_constr=0, xl=xl, xu=xu)

self.f1 = f1

self.f2 = f2

def _evaluate(self, x, out, args, *kwargs):

xi = float(x[0])

out["F"] = np.array([self.f1(xi), self.f2(xi)], dtype=float)

```

  1. Configure NSGA-II and termination

```python

from pymoo.algorithms.moo.nsga2 import NSGA2

from pymoo.termination import get_termination

from pymoo.optimize import minimize

algorithm = NSGA2(pop_size=POPULATION)

termination = get_termination("n_gen", N_GENERATIONS)

res = minimize(

problem, algorithm, termination=termination, seed=SEED if SEED is not None else None,

save_history=False, verbose=False

)

```

  1. Extract candidate solutions

```python

import numpy as np

X = np.array(res.X).reshape(-1)

F = np.array(res.F)

```

  1. Filter to the non-dominated set

```python

def is_nondominated(F):

# Minimization: i is dominated if exists j with F[j] <= F[i] for all objectives

# and strictly less for at least one objective.

n = len(F)

keep = np.ones(n, dtype=bool)

for i in range(n):

if not keep[i]:

continue

for j in range(n):

if i == j:

continue

if np.all(F[j] <= F[i]) and np.any(F[j] < F[i]):

keep[i] = False

break

return keep

mask = is_nondominated(F)

X_nd, F_nd = X[mask], F[mask]

```

  1. Sort by the decision variable and format output

```python

order = np.argsort(X_nd)

X_sorted = X_nd[order]

F_sorted = F_nd[order]

lines = []

for xi, (f1, f2) in zip(X_sorted, F_sorted):

lines.append(f"{xi:.3f}={f1},{f2}")

# Write lines to the requested output path

```

Verification

Before finalizing, perform these checks:

```python

assert np.all(np.diff(X_sorted) >= 0)

```

Common Pitfalls

Optional Script Usage

Use the provided helper scripts to streamline verification and export:

```

python scripts/nsga2_1d_two_obj.py \

--f1 '(x-2)2' --f2 '(x+2)2' \

--xl -10 --xu 10 --pop 50 --gen 3 \

--out output.txt --round-x 3

```

Success Criteria:

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.