-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loose coupling nsgaii elite population selection #4821
Merged
toshihikoyanase
merged 12 commits into
optuna:master
from
Alnusjaponica:loose-coupling-NSGAII-elite-population-selection
Aug 2, 2023
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
db514bb
Add NSGAIIElitePopulationSelectionStrategy
Alnusjaponica 264b3e1
Separate _select_elite_population() from `NSGAIISampler`
Alnusjaponica 0c5256b
Remove unnecessary check
Alnusjaponica 5c17342
Fix imports result from _elite_population_selection
Alnusjaponica 4fccbcf
Add test for exception
Alnusjaponica 130fcf3
Fix duplicates variable
Alnusjaponica d511a75
Add test for result of elite_population_selection_strategy
Alnusjaponica ba4c384
Merge branch 'optuna:master' into loose-coupling-NSGAII-elite-populat…
Alnusjaponica caaf9f7
Add docstrings to newly added arguments
Alnusjaponica a6378bd
Remove word "process" from docstring
Alnusjaponica 2193feb
Add experimental note
Alnusjaponica ec296bc
Split notes
Alnusjaponica File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
150 changes: 150 additions & 0 deletions
150
optuna/samplers/nsgaii/_elite_population_selection_strategy.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
from __future__ import annotations | ||
|
||
from collections import defaultdict | ||
from collections.abc import Callable | ||
from collections.abc import Sequence | ||
import itertools | ||
|
||
import optuna | ||
from optuna.samplers.nsgaii._dominates_function import _constrained_dominates | ||
from optuna.samplers.nsgaii._dominates_function import _validate_constraints | ||
from optuna.study import Study | ||
from optuna.study._multi_objective import _dominates | ||
from optuna.trial import FrozenTrial | ||
|
||
|
||
class NSGAIIElitePopulationSelectionStrategy: | ||
def __init__( | ||
self, | ||
*, | ||
population_size: int, | ||
constraints_func: Callable[[FrozenTrial], Sequence[float]] | None = None, | ||
) -> None: | ||
if population_size < 2: | ||
raise ValueError("`population_size` must be greater than or equal to 2.") | ||
|
||
self._population_size = population_size | ||
self._constraints_func = constraints_func | ||
|
||
def __call__(self, study: Study, population: list[FrozenTrial]) -> list[FrozenTrial]: | ||
"""Select elite population from the given trials by NSGA-II algorithm. | ||
|
||
Args: | ||
study: | ||
Target study object. | ||
population: | ||
Trials in the study. | ||
|
||
Returns: | ||
A list of trials that are selected as elite population. | ||
""" | ||
_validate_constraints(population, self._constraints_func) | ||
dominates = _dominates if self._constraints_func is None else _constrained_dominates | ||
population_per_rank = _fast_non_dominated_sort(population, study.directions, dominates) | ||
|
||
elite_population: list[FrozenTrial] = [] | ||
for individuals in population_per_rank: | ||
if len(elite_population) + len(individuals) < self._population_size: | ||
elite_population.extend(individuals) | ||
else: | ||
n = self._population_size - len(elite_population) | ||
_crowding_distance_sort(individuals) | ||
elite_population.extend(individuals[:n]) | ||
break | ||
|
||
return elite_population | ||
|
||
|
||
def _calc_crowding_distance(population: list[FrozenTrial]) -> defaultdict[int, float]: | ||
"""Calculates the crowding distance of population. | ||
|
||
We define the crowding distance as the summation of the crowding distance of each dimension | ||
of value calculated as follows: | ||
|
||
* If all values in that dimension are the same, i.e., [1, 1, 1] or [inf, inf], | ||
the crowding distances of all trials in that dimension are zero. | ||
* Otherwise, the crowding distances of that dimension is the difference between | ||
two nearest values besides that value, one above and one below, divided by the difference | ||
between the maximal and minimal finite value of that dimension. Please note that: | ||
* the nearest value below the minimum is considered to be -inf and the | ||
nearest value above the maximum is considered to be inf, and | ||
* inf - inf and (-inf) - (-inf) is considered to be zero. | ||
""" | ||
|
||
manhattan_distances: defaultdict[int, float] = defaultdict(float) | ||
if len(population) == 0: | ||
return manhattan_distances | ||
|
||
for i in range(len(population[0].values)): | ||
population.sort(key=lambda x: x.values[i]) | ||
|
||
# If all trials in population have the same value in the i-th dimension, ignore the | ||
# objective dimension since it does not make difference. | ||
if population[0].values[i] == population[-1].values[i]: | ||
continue | ||
|
||
vs = [-float("inf")] + [trial.values[i] for trial in population] + [float("inf")] | ||
|
||
# Smallest finite value. | ||
v_min = next(x for x in vs if x != -float("inf")) | ||
|
||
# Largest finite value. | ||
v_max = next(x for x in reversed(vs) if x != float("inf")) | ||
|
||
width = v_max - v_min | ||
if width <= 0: | ||
# width == 0 or width == -inf | ||
width = 1.0 | ||
|
||
for j in range(len(population)): | ||
# inf - inf and (-inf) - (-inf) is considered to be zero. | ||
gap = 0.0 if vs[j] == vs[j + 2] else vs[j + 2] - vs[j] | ||
manhattan_distances[population[j].number] += gap / width | ||
return manhattan_distances | ||
|
||
|
||
def _crowding_distance_sort(population: list[FrozenTrial]) -> None: | ||
manhattan_distances = _calc_crowding_distance(population) | ||
population.sort(key=lambda x: manhattan_distances[x.number]) | ||
population.reverse() | ||
|
||
|
||
def _fast_non_dominated_sort( | ||
population: list[FrozenTrial], | ||
directions: list[optuna.study.StudyDirection], | ||
dominates: Callable[[FrozenTrial, FrozenTrial, list[optuna.study.StudyDirection]], bool], | ||
) -> list[list[FrozenTrial]]: | ||
dominated_count: defaultdict[int, int] = defaultdict(int) | ||
dominates_list = defaultdict(list) | ||
|
||
for p, q in itertools.combinations(population, 2): | ||
if dominates(p, q, directions): | ||
dominates_list[p.number].append(q.number) | ||
dominated_count[q.number] += 1 | ||
elif dominates(q, p, directions): | ||
dominates_list[q.number].append(p.number) | ||
dominated_count[p.number] += 1 | ||
|
||
population_per_rank = [] | ||
while population: | ||
non_dominated_population = [] | ||
i = 0 | ||
while i < len(population): | ||
if dominated_count[population[i].number] == 0: | ||
individual = population[i] | ||
if i == len(population) - 1: | ||
population.pop() | ||
else: | ||
population[i] = population.pop() | ||
non_dominated_population.append(individual) | ||
else: | ||
i += 1 | ||
|
||
for x in non_dominated_population: | ||
for y in dominates_list[x.number]: | ||
dominated_count[y] -= 1 | ||
|
||
assert non_dominated_population | ||
population_per_rank.append(non_dominated_population) | ||
|
||
return population_per_rank |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for confirmation. The loose coupling of the elite population selection for the
NSGAIIISampler
will be done as a follow-up?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I am going to do it as a follow-up.