Skip to content

perf(nsga): 2.64x faster Pareto front sorting (multi-objective GA run) - #387

Open
dexhunter wants to merge 2 commits into
ahmedfgad:masterfrom
dexhunter:perf/nsga-pareto-sorting
Open

dexhunter wants to merge 2 commits into
ahmedfgad:masterfrom
dexhunter:perf/nsga-pareto-sorting

Conversation

@dexhunter

@dexhunter dexhunter commented Nov 1, 2026 •

Copy link
Copy Markdown

TL;DR

  • NSGA.get_non_dominated_set in pygad/utils/nsga.py now changes each fitness vector to a Python list once. One plain loop then compares the lists.
  • Benchmark wall time for a multi-objective GA workload, median of 9 timed pairs: 56.364 ms -> 21.488 ms, 2.64x faster.
  • Results are identical for numeric 1D fitness vectors. The 596 tests of the NSGA, adaptive mutation, save solutions and gene space files pass.
  • Malformed direct calls, which repository callers cannot make, changed. Two empty fitness vectors raised IndexError and are now non-dominated. A scalar fitness now raises TypeError.

What & Why

Before, get_non_dominated_set checked every pair of solutions. For each pair it built a new numpy array from zip(). Then it called .all() and .any() on two more arrays. The fitness vectors are short, so this numpy work cost much more than the comparison itself. non_dominated_sorting also called this loop once for each Pareto front.

Now the code changes each fitness vector to a Python list once, with numpy.asarray(v).tolist(). Then one loop compares the lists with plain Python. Solution j dominates solution i when all values of j are >= and at least one value is >. This is the same rule as before. non_dominated_sorting calls this function once per front, so it gets faster too. The same pair objects go into the same lists in the same order. All inputs use this one loop: numpy arrays, lists and tuples.

Results

Times are per call, one CPU core. Each value is the median of 3 runs, and each run takes the best of 7 repeats.

Situation Before After Change
non_dominated_sorting, 10 solutions x 2 objectives 0.296 ms 0.027 ms 10.9x faster
non_dominated_sorting, 50 solutions x 2 objectives 9.04 ms 0.44 ms 20.4x faster
non_dominated_sorting, 200 solutions x 3 objectives 127.97 ms 4.88 ms 26.2x faster
get_non_dominated_set, 50 solutions, fitness vectors are Python lists 1.382 ms 0.081 ms 17.1x faster
get_non_dominated_set, 1 solution (no pair to compare) 0.26 us 0.65 us 0.39 us slower (one list conversion)

Why this is safe and easy to maintain

The change is small and stays inside get_non_dominated_set. The function is now shorter than before: one list conversion and one double loop with one clear domination test. It returns the same pair objects in the same order. The NSGA-III tests, the adaptive mutation tests and the save solutions tests cover this code.

Repository callers always pass rows of one 2D numeric array, so they cannot make the malformed calls. For a direct call with two or more empty fitness vectors, the old code raised IndexError. The new code returns them all as non-dominated. For a direct call with one solution that has a scalar fitness, the old code returned it. The new code raises TypeError.

Validation

  • Workload: 10 multi-objective pygad.GA runs that copy test_adaptive_mutation_mutation_probability_multi_objective, plus 13 direct non_dominated_sorting calls with ties, duplicates, int fitness and one objective. Before 56.364 ms, after 21.488 ms. The output was checked to be identical to the unmodified code.
  • Tests: python -m pytest -q tests/test_adaptive_mutation.py tests/test_save_solutions.py tests/test_gene_space.py tests/test_gene_space_allow_duplicate_genes.py tests/test_nsga3.py tests/test_nsga3_dtlz2.py tests/test_nsga3_pipeline.py tests/test_nsga3_population_growth.py -> 596 passed (same as on master).
  • Quick check:
import timeit, numpy, pygad
from pygad.utils.nsga import NSGA
s = NSGA(); s.supported_int_float_types = pygad.GA.supported_int_float_types
f = numpy.random.RandomState(0).rand(50, 2)
print(min(timeit.repeat(lambda: s.non_dominated_sorting(f), number=1, repeat=7)))

AI assistance

This PR was prepared with AI assistance from Aiden. Candidates were tracked with Weco Observe.

Weco share link: https://dashboard.weco.ai/share/PYAiMz5MRfbwe0rWTcdOuZYmr0ssct2v

get_non_dominated_set built a new numpy array from zip() for every pair
of solutions and called .all()/.any() on it. The fitness vectors are
tiny, so this cost far more than the comparison itself.

When all fitness vectors are 1D numeric numpy arrays of the same dtype
and shape, convert them to lists once and compare them with plain
Python. non_dominated_sorting calls this function once per front,
so it gets faster too. Output (pair objects and order) is unchanged;
other inputs keep the old loop.

Multi-objective GA workload: 58.90 ms -> 21.99 ms (62.7% faster).

Co-Authored-By: Aiden <aiden@weco.ai>
@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

📊 Change summary

1 files changed · +12 / −13 lines

pie showData
    title Lines changed per file (25 total)
    "pygad/utils/nsga.py" : 25
Loading
Per-file breakdown
File + − total
pygad/utils/nsga.py 12 13 25
Total (1 files) 12 13 25

The function had a type-checking helper, a fast loop and the old numpy
loop as a fallback. Keep one loop for all inputs: convert each fitness
vector to a list once, then test "all >= and any >" with plain Python.
This is the same domination rule as the old numpy loop, so the result
and its order do not change for numeric fitness vectors.

The helper, its dtype and shape checks and the second loop are gone.
The function is now shorter than it was before this branch.

Co-Authored-By: Aiden <aiden@weco.ai>
@dexhunter dexhunter changed the title perf(nsga): 2.6x faster Pareto front sorting (multi-objective GA run) perf(nsga): 2.68x faster Pareto front sorting (multi-objective GA run) Nov 27, 2026
@dexhunter dexhunter changed the title perf(nsga): 2.68x faster Pareto front sorting (multi-objective GA run) perf(nsga): 2.64x faster Pareto front sorting (multi-objective GA run) Nov 27, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant