moptipy.algorithms.modules package¶
A set of reuseable algorithm modules. Selection algorithms are common modules that choose n out of N objects. Selection algorithms are modules of the fully-configurable Evolutionary Algorithm The following selection algorithms have currently been implemented: Bases: A fitness record stores data together with a fitness. Bases: The base class for selections algorithms. Select n records from source and pass them to dest. When choosing the n records from source to be passed to dest, only the source ( dest ( n ( random ( A temperature schedule as needed by Simulated Annealing. The Simulated Annealing algorithm implemented in The temperature schedule receives an iteration index tau as input and returns the current temperature via Bases: The exponential temperature schedule. The current temperature is computed as t0 * (1 - epsilon) ** tau. Log all parameters of the exponential temperature schedule. logger ( Compute the temperature at iteration tau. tau ( the temperature Bases: The logarithmic temperature schedule. The temperature is computed as t0 / log(e + (tau * epsilon)). Log all parameters of the logarithmic temperature schedule. logger ( Compute the temperature at iteration tau. tau ( the temperature Bases: The base class for temperature schedules. Log all parameters of this temperature schedule as key-value pairs. logger (Subpackages¶
Submodules¶
moptipy.algorithms.modules.selection module¶
GeneralEA
. They can utilize fitness values computed by the fitness assignment processes (Fitness
). Of course, they can also be applied in different contexts and are not bound to single-objective optimization.Selection
is especially important in Evolutionary Algorithms (~moptipy.algorithms.so.general_ea.GeneralEA), where it is used in two places: As survival selection, it chooses which points will be allowed to remain in the population and, hence, survive into the mating pool for the next generation. As mating selection methods, they choose the inputs of the search operations from the mating pool.Selection
algorithms must only use the fitness
of a solution record (and random numbers) to make their decisions. These fitness values are subject to minimization. They can equal to the objective values in optimization or stem from a Fitness
Assignment Process.Best
selection selects the best n solutions without replacement. This is a common strategy for survival selection, especially in (mu+lambda) EAs (compatible to EA
).RandomWithoutReplacement
selects random solutions without replacement. It is a common strategy for mating selection.FitnessProportionateSUS
performs fitness proportionate selection for minimization using stochastic uniform sampling and, optionally, a minimum selection probability threshold. It is the classic survival selection algorithm in Genetic Algorithm.TournamentWithReplacement
performs tournament selection with a specified tournament size with replacement.TournamentWithoutReplacement
performs tournament selection with a specified tournament size without replacement.Protocol
Component
fitness
attribute of the records and the random numbers from random must be used as decision criteria.list
[FitnessRecord
]) – the list with the records to select fromCallable
[[FitnessRecord
], Any
]) – the destination collector Callable to invoke for each selected record, can be list
.`append`.int
) – the number of records to selectGenerator
) – the random number generatormoptipy.algorithms.modules.temperature_schedule module¶
simulated_annealing
performs a local search that always accepts a non-worsening move, i.e., a solution which is not worse than the currently maintained one. However, it will also sometimes accept one that is worse. The probability of doing so depends on how much worse that solution is and on the current temperature of the algorithm. The higher the temperature, the higher the acceptance probability. The temperature changes over time according to the TemperatureSchedule
.temperature()
. Notice that tau is zero-based for simplicity reason, meanings that the first objective function evaluation is at index 0.TemperatureSchedule
>>> ex = ExponentialSchedule(10.0, 0.05)
>>> print(f"{ex.t0} - {ex.epsilon}")
10.0 - 0.05
>>> ex.temperature(0)
10.0
>>> ex.temperature(1)
9.5
>>> ex.temperature(2)
9.025
>>> ex.temperature(1_000_000_000_000_000_000)
0.0
KeyValueLogSection
) – the logger for the parameters>>> from moptipy.utils.logger import InMemoryLogger
>>> with InMemoryLogger() as l:
... with l.key_values("C") as kv:
... ExponentialSchedule(0.2, 0.6).log_parameters_to(kv)
... text = l.get_log()
>>> text[1]
'name: exp0d2_0d6'
>>> text[3]
'T0: 0.2'
>>> text[5]
:rtype: :sphinx_autodoc_typehints_type:`\:py\:obj\:\`None\``
'e: 0.6'
>>> len(text)
8
int
) – the iteration index, starting with 0 at the first comparison of two solutions, at which point the starting temperature t0
should be returned>>> s = ExponentialSchedule(100.0, 0.5)
>>> s.temperature(0)
100.0
>>> s.temperature(1)
50.0
>>> s.temperature(10)
0.09765625
TemperatureSchedule
>>> lg = LogarithmicSchedule(10.0, 0.1)
>>> print(f"{lg.t0} - {lg.epsilon}")
10.0 - 0.1
>>> lg.temperature(0)
10.0
>>> lg.temperature(1)
9.651322627630812
>>> lg.temperature(1_000_000_000_000_000_000_000_000_000_000_000_000_000)
0.11428802155348732
KeyValueLogSection
) – the logger for the parameters>>> from moptipy.utils.logger import InMemoryLogger
>>> with InMemoryLogger() as l:
... with l.key_values("C") as kv:
... LogarithmicSchedule(0.2, 0.6).log_parameters_to(kv)
... text = l.get_log()
>>> text[1]
'name: ln0d2_0d6'
>>> text[3]
'T0: 0.2'
>>> text[5]
:rtype: :sphinx_autodoc_typehints_type:`\:py\:obj\:\`None\``
'e: 0.6'
>>> len(text)
8
int
) – the iteration index, starting with 0 at the first comparison of two solutions, at which point the starting temperature t0
should be returned>>> s = LogarithmicSchedule(100.0, 0.5)
>>> s.temperature(0)
100.0
>>> s.temperature(1)
85.55435113150568
>>> s.temperature(10)
48.93345190925178
Component
KeyValueLogSection
) – the logger for the parameters>>> from moptipy.utils.logger import InMemoryLogger
>>> with InMemoryLogger() as l:
... with l.key_values("C") as kv:
... TemperatureSchedule(0.1).log_parameters_to(kv)
... text = l.get_log()
>>> text[1]
'name: TemperatureSchedule'
>>> text[3]
:rtype: :sphinx_autodoc_typehints_type:`\:py\:obj\:\`None\``
'T0: 0.1'
>>> len(text)
6