Repository navigation
Conversation
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>
📊 Change summary1 files changed · +12 / −13 lines pie showData
title Lines changed per file (25 total)
"pygad/utils/nsga.py" : 25
Per-file breakdown
|
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>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
NSGA.get_non_dominated_setinpygad/utils/nsga.pynow changes each fitness vector to a Python list once. One plain loop then compares the lists.IndexErrorand are now non-dominated. A scalar fitness now raisesTypeError.What & Why
Before,
get_non_dominated_setchecked every pair of solutions. For each pair it built a new numpy array fromzip(). 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_sortingalso 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. Solutionjdominates solutioniwhen all values ofjare>=and at least one value is>. This is the same rule as before.non_dominated_sortingcalls 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.
non_dominated_sorting, 10 solutions x 2 objectivesnon_dominated_sorting, 50 solutions x 2 objectivesnon_dominated_sorting, 200 solutions x 3 objectivesget_non_dominated_set, 50 solutions, fitness vectors are Python listsget_non_dominated_set, 1 solution (no pair to compare)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 raisesTypeError.Validation
pygad.GAruns that copytest_adaptive_mutation_mutation_probability_multi_objective, plus 13 directnon_dominated_sortingcalls 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.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 onmaster).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