Skip to content

Commit

Permalink
Merge pull request #96 from rodrigo-arenas/0.9.0
Browse files Browse the repository at this point in the history
Add ConstantAdapter
  • Loading branch information
rodrigo-arenas committed Jun 5, 2022
2 parents d7cb37a + dad16a3 commit 086eb59
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 265 deletions.
5 changes: 5 additions & 0 deletions docs/api/schedules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Schedules

.. autosummary::
base.BaseAdapter
ConstantAdapter
ExponentialAdapter
InverseAdapter
PotentialAdapter
Expand All @@ -13,6 +14,10 @@ Schedules
:members:
:undoc-members: False

.. autoclass:: ConstantAdapter
:members:
:undoc-members: False

.. autoclass:: ExponentialAdapter
:members:
:undoc-members: False
Expand Down
3 changes: 3 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@

master_doc = "index"

# Ignore Jupyter notebooks error
nbsphinx_allow_errors = True

# generate autosummary even if no references
autosummary_generate = True
autosummary_imported_members = True
Expand Down
188 changes: 0 additions & 188 deletions docs/notebooks/Adaptive_Learning.ipynb

This file was deleted.

1 change: 1 addition & 0 deletions docs/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Features:
* Introducing Adaptive Schedulers to enable adaptive mutation and crossover probabilities;
currently, supported schedulers are:

- :class:`~sklearn_genetic.schedules.ConstantAdapter`
- :class:`~sklearn_genetic.schedules.ExponentialAdapter`
- :class:`~sklearn_genetic.schedules.InverseAdapter`
- :class:`~sklearn_genetic.schedules.PotentialAdapter`
Expand Down
17 changes: 16 additions & 1 deletion docs/tutorials/adapters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ If :math:`p_0 > p_f`, you are performing a decay towards :math:`p_0`.

If :math:`p_0 < p_f`, you are performing an ascend towards :math:`p_f`.

All the adapters :math:`p(t; \alpha)`, for :math:`\alpha \in (0,1)`,
All the non-constant adapters :math:`p(t; \alpha)`, for :math:`\alpha \in (0,1)`,
have the following properties:

.. math::
Expand All @@ -49,10 +49,25 @@ have the following properties:
The following adapters are available:

* ConstantAdapter
* ExponentialAdapter
* InverseAdapter
* PotentialAdapter


ConstantAdapter
---------------

This adapter is meant to be used internally by the package; when the user doesn't create an adapter but
instead defines the crossover or mutation probability as a real number, the package will convert it
to a `ConstantAdapter`, so the library can use the internal API with the same methods in both cases.
Because of this, its definition is:

.. math::
p(t; \alpha) = p_0
ExponentialAdapter
------------------

Expand Down
8 changes: 7 additions & 1 deletion sklearn_genetic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
LogbookSaver,
)

from .schedules import ExponentialAdapter, InverseAdapter, PotentialAdapter
from .schedules import (
ConstantAdapter,
ExponentialAdapter,
InverseAdapter,
PotentialAdapter,
)

from ._version import __version__

Expand All @@ -18,6 +23,7 @@
"ConsecutiveStopping",
"DeltaThreshold",
"LogbookSaver",
"ConstantAdapter",
"ExponentialAdapter",
"InverseAdapter",
"PotentialAdapter",
Expand Down
51 changes: 13 additions & 38 deletions sklearn_genetic/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ def eaSimple(
toolbox: A :class:`~deap.base.Toolbox`
Contains the evolution operators.
cxpb: float or a Scheduler, default=None
The probability of mating two individuals.
cxpb: Scheduler, default=None
An adaptive scheduler representing the probability of mating two individuals.
mutpb: float or a Scheduler, default=None
The probability of mutating an individual.
mutpb: Scheduler, default=None
An adaptive scheduler representing the probability that an offspring is produced by mutation.
ngen: int, default=None
The number of generation.
Expand Down Expand Up @@ -124,22 +124,13 @@ def eaSimple(
print("INFO: Stopping the algorithm")
return population, logbook, n_gen

# Begin the generational process
mutpb_value = mutpb
cxpb_value = cxpb
for gen in range(1, ngen + 1):
try:
# Select the next generation individuals
offspring = toolbox.select(population, len(population) - hof_size)

# Check adaptive rates
if isinstance(mutpb, BaseAdapter):
mutpb_value = mutpb.step()
if isinstance(cxpb, BaseAdapter):
cxpb_value = cxpb.step()

# Vary the pool of individuals
offspring = varAnd(offspring, toolbox, cxpb_value, mutpb_value)
offspring = varAnd(offspring, toolbox, cxpb.step(), mutpb.step())

# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
Expand Down Expand Up @@ -233,11 +224,11 @@ def eaMuPlusLambda(
lambda\_: int, default=None
The number of children to produce at each generation.
cxpb: float or a Scheduler, default=None
cxpb: Scheduler, default=None
The probability that an offspring is produced by crossover.
mutpb: float or a Scheduler, default=None
The probability that an offspring is produced by mutation.
mutpb: Scheduler, default=None
An adaptive scheduler representing the probability that an offspring is produced by mutation.
ngen: int, default=None
The number of generation.
Expand Down Expand Up @@ -324,19 +315,11 @@ def eaMuPlusLambda(
print("INFO: Stopping the algorithm")
return population, logbook, n_gen

# Begin the generational process
mutpb_value = mutpb
cxpb_value = cxpb
for gen in range(1, ngen + 1):
try:
# Check adaptive rates
if isinstance(mutpb, BaseAdapter):
mutpb_value = mutpb.step()
if isinstance(cxpb, BaseAdapter):
cxpb_value = cxpb.step()

# Vary the population
offspring = varOr(population, toolbox, lambda_, cxpb_value, mutpb_value)
offspring = varOr(population, toolbox, lambda_, cxpb.step(), mutpb.step())

# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
Expand Down Expand Up @@ -426,11 +409,11 @@ def eaMuCommaLambda(
lambda\_: int, default=None
The number of children to produce at each generation.
cxpb: float or a Scheduler, default=None
cxpb: Scheduler, default=None
The probability that an offspring is produced by crossover.
mutpb: float or a Scheduler, default=None
The probability that an offspring is produced by mutation.
mutpb: Scheduler, default=None
An adaptive scheduler representing the probability that an offspring is produced by mutation.
ngen: int, default=None
The number of generation.
Expand Down Expand Up @@ -520,19 +503,11 @@ def eaMuCommaLambda(
print("INFO: Stopping the algorithm")
return population, logbook, n_gen

# Begin the generational process
mutpb_value = mutpb
cxpb_value = cxpb
for gen in range(1, ngen + 1):
try:
# Check adaptive rates
if isinstance(mutpb, BaseAdapter):
mutpb_value = mutpb.step()
if isinstance(cxpb, BaseAdapter):
cxpb_value = cxpb.step()

# Vary the population
offspring = varOr(population, toolbox, lambda_, cxpb_value, mutpb_value)
offspring = varOr(population, toolbox, lambda_, cxpb.step(), mutpb.step())

# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
Expand Down

0 comments on commit 086eb59

Please sign in to comment.