Skip to content

Commit

Permalink
Merge pull request #295 from Happy-Algorithms-League/api/rename_min_f…
Browse files Browse the repository at this point in the history
…itness

Replace min_fitness by termination_fitness
  • Loading branch information
mschmidt87 committed Mar 25, 2021
2 parents caeb946 + 342e445 commit 9e4689b
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 14 deletions.
4 changes: 4 additions & 0 deletions cgp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
__url__ = "https://happy-algorithms-league.github.io/hal-cgp/"
__doc__ = f"{__description__} <{__url__}>"

import warnings

from . import ea, local_search, utils
from .cartesian_graph import CartesianGraph, atomic_operator
from .genome import Genome
Expand All @@ -14,3 +16,5 @@
from .node import OperatorNode
from .node_impl import Add, ConstantFloat, Div, IfElse, Mul, Parameter, Pow, Sub
from .population import Population

warnings.simplefilter("always", DeprecationWarning)
27 changes: 25 additions & 2 deletions cgp/hl_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
from typing import Callable, Optional

import numpy as np
Expand All @@ -11,7 +12,8 @@ def evolve(
pop: Population,
objective: Callable[[IndividualBase], IndividualBase],
ea: MuPlusLambda,
min_fitness: float,
min_fitness: float = np.inf,
termination_fitness: float = np.inf,
max_generations: int = np.inf,
max_objective_calls: int = np.inf,
print_progress: Optional[bool] = False,
Expand All @@ -33,6 +35,10 @@ def evolve(
`initialize_fitness_parents` and `step` method.
min_fitness : float
Minimum fitness at which the evolution is stopped.
Warning: This argument is deprecated and will be removed in the 0.4
release. Please use `termination_fitness` instead.
termination_fitness : float
Minimum fitness at which the evolution is terminated
max_generations : int
Maximum number of generations.
Defaults to positive infinity.
Expand All @@ -50,6 +56,23 @@ def evolve(
-------
None
"""
if np.isfinite(min_fitness) and np.isfinite(termination_fitness):
raise RuntimeError(
"Both `min_fitness` and `termination_fitness` have been set. The "
"`min_fitness` argument is deprecated and will be removed in the 0.4 "
"release. Please use `termination_fitness` instead."
)

if np.isfinite(min_fitness):
warnings.warn(
DeprecationWarning(
"The `min_fitness` argument is deprecated and "
"will be removed in the 0.4 release. Please use "
"`termination_fitness` instead."
)
)
termination_fitness = min_fitness

if np.isinf(max_generations) and np.isinf(max_objective_calls):
raise ValueError("Either max_generations or max_objective_calls must be finite.")

Expand Down Expand Up @@ -91,7 +114,7 @@ def evolve(
if callback is not None:
callback(pop)

if pop.champion.fitness + 1e-10 >= min_fitness:
if pop.champion.fitness + 1e-10 >= termination_fitness:
break

if print_progress:
Expand Down
2 changes: 1 addition & 1 deletion examples/example_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def objective(individual):
"levels_back": 2,
"primitives": (cgp.Add, cgp.Sub, cgp.Mul, cgp.ConstantFloat),
},
"evolve_params": {"max_generations": 200, "min_fitness": -1e-12},
"evolve_params": {"max_generations": 200, "termination_fitness": -1e-12},
}

# %%
Expand Down
2 changes: 1 addition & 1 deletion examples/example_differential_evo_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def objective(individual, seed):
"k_local_search": 2,
}

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

# use an uneven number of gradient steps so they can not easily
# average out for clipped values
Expand Down
2 changes: 1 addition & 1 deletion examples/example_evo_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def evolution(f_target):

ea_params = {"n_offsprings": 10, "tournament_size": 2, "mutation_rate": 0.03, "n_processes": 2}

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

# create population that will be evolved
pop = cgp.Population(**population_params, genome_params=genome_params)
Expand Down
2 changes: 1 addition & 1 deletion examples/example_fec_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def objective(individual):
"levels_back": 2,
"primitives": (cgp.Add, cgp.Sub, cgp.Mul, cgp.ConstantFloat),
},
"evolve_params": {"max_generations": 200, "min_fitness": -1e-12},
"evolve_params": {"max_generations": 200, "termination_fitness": -1e-12},
}

# %%
Expand Down
2 changes: 1 addition & 1 deletion examples/example_hurdles.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def objective_two(individual):
"hurdle_percentile": [0.5, 0.0],
}

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

# %%
# We create a population that will be evolved
Expand Down
2 changes: 1 addition & 1 deletion examples/example_local_search_evolution_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def objective(individual, seed):
"k_local_search": 2,
}

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

# restrict the number of steps in the local search; since parameter
# values are propagated from parents to offsprings, parameter values
Expand Down
2 changes: 1 addition & 1 deletion examples/example_minimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def objective(individual):

ea_params = {"n_offsprings": 4, "mutation_rate": 0.03, "n_processes": 2}

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

# %%
# We create a population that will be evolved
Expand Down
5 changes: 4 additions & 1 deletion examples/example_mountain_car.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ def evolve(seed):

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

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

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

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 @@ -84,7 +84,7 @@ def objective(individual):

ea_params = {"n_offsprings": 4, "mutation_rate": 0.03, "n_processes": 1}

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

# %%
# We create a population that will be evolved
Expand Down
2 changes: 1 addition & 1 deletion examples/example_parametrized_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def objective(individual, seed):

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

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

local_search_params = {"lr": 1e-3, "gradient_steps": 9}

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 @@ -90,7 +90,7 @@ def objective(individual, rng):

ea_params = {"n_offsprings": 4, "mutation_rate": 0.03, "n_processes": 2}

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

# create population that will be evolved
pop = cgp.Population(**population_params, genome_params=genome_params)
Expand Down
2 changes: 1 addition & 1 deletion examples/example_reorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def objective(individual):
"reorder_genome": True,
}

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

# %%
# We create two populations that will be evolved
Expand Down
16 changes: 16 additions & 0 deletions test/test_hl_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,19 @@ def test_speedup_parallel_evolve(population_params, genome_params, ea_params):
else:
# assert that multiprocessing roughly follows a linear speedup.
assert T == pytest.approx(T_baseline / n_processes, rel=0.25)


def test_min_fitness_deprecation(population_params, genome_params, ea_params):
def objective(individual):
individual.fitness = 1.0
return individual

pop = cgp.Population(**population_params, genome_params=genome_params)
ea = cgp.ea.MuPlusLambda(**ea_params)
with pytest.warns(DeprecationWarning):
cgp.evolve(pop, objective, ea, min_fitness=2.0, max_generations=10)

with pytest.raises(RuntimeError):
cgp.evolve(
pop, objective, ea, min_fitness=2.0, termination_fitness=1.5, max_generations=10
)

0 comments on commit 9e4689b

Please sign in to comment.