Skip to content
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

Check tournament_size <= n_parents in hl_api #268

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions cgp/ea/mu_plus_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class MuPlusLambda:
def __init__(
self,
n_offsprings: int,
tournament_size: int,
*,
tournament_size: Union[None, int] = None,
n_processes: int = 1,
local_search: Callable[[IndividualBase], None] = lambda combined: None,
k_local_search: Union[int, None] = None,
Expand All @@ -34,8 +34,8 @@ def __init__(
----------
n_offsprings : int
Number of offspring in each iteration.
tournament_size : int
Tournament size in each iteration.
tournament_size : int, optional
Tournament size in each iteration. Defaults to the number of parents in the population
n_processes : int, optional
Number of parallel processes to be used. If greater than 1,
parallel evaluation of the objective is supported. Defaults to 1.
Expand Down Expand Up @@ -159,6 +159,13 @@ def step(
def _create_new_offspring_generation(self, pop: Population) -> List[IndividualBase]:
# use tournament selection to randomly select individuals from
# parent population

if self.tournament_size is None:
self.tournament_size = pop.n_parents

if self.tournament_size > pop.n_parents:
raise ValueError("tournament_size must be less or equal n_parents")

offsprings: List[IndividualBase] = []
while len(offsprings) < self.n_offsprings:
tournament_pool = pop.rng.permutation(pop.parents)[: self.tournament_size]
Expand Down
1 change: 0 additions & 1 deletion examples/example_hurdles.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ def objective_two(individual):
# the (n+1)th objective by a list of numbers between 0 and 1.
ea_params = {
"n_offsprings": 4,
"tournament_size": 2,
"n_processes": 1,
"hurdle_percentile": [0.5, 0.0],
}
Expand Down
2 changes: 1 addition & 1 deletion examples/example_minimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def objective(individual):
"primitives": (cgp.Add, cgp.Sub, cgp.Mul, cgp.ConstantFloat),
}

ea_params = {"n_offsprings": 4, "tournament_size": 2, "n_processes": 2}
ea_params = {"n_offsprings": 4, "n_processes": 2}

evolve_params = {"max_generations": int(args["--max-generations"]), "min_fitness": 0.0}

Expand Down
2 changes: 1 addition & 1 deletion examples/example_multi_genome.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def objective(individual):
}
genome_params = [single_genome_params, single_genome_params]

ea_params = {"n_offsprings": 4, "tournament_size": 2, "n_processes": 1}
ea_params = {"n_offsprings": 4, "n_processes": 1}

evolve_params = {"max_generations": int(args["--max-generations"]), "min_fitness": 0.0}

Expand Down
2 changes: 1 addition & 1 deletion examples/example_piecewise_target_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def objective(individual, rng):
"primitives": (cgp.IfElse, cgp.Mul, cgp.Add, cgp.Sub, cgp.ConstantFloat,),
}

ea_params = {"n_offsprings": 4, "tournament_size": 2, "n_processes": 2}
ea_params = {"n_offsprings": 4, "n_processes": 2}

evolve_params = {"max_generations": int(args["--max-generations"]), "min_fitness": 0.0}

Expand Down
3 changes: 1 addition & 2 deletions examples/example_reorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ def objective(individual):
"primitives": (cgp.Add, cgp.Sub, cgp.Mul, cgp.ConstantFloat),
}

ea_params = {"n_offsprings": 4, "tournament_size": 2, "n_processes": 2}
ea_params = {"n_offsprings": 4, "n_processes": 2}
ea_params_with_reorder = {
"n_offsprings": 4,
"tournament_size": 2,
"n_processes": 2,
"reorder_genome": True,
}
Expand Down
2 changes: 1 addition & 1 deletion test/test_ea_mu_plus_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def objective(ind):
return ind

population_params["n_parents"] = 1

pop = cgp.Population(**population_params, genome_params=genome_params)

ea_params["tournament_size"] = 1
ea = cgp.ea.MuPlusLambda(**ea_params)
ea.initialize_fitness_parents(pop, objective)

Expand Down