GASolver¶
-
class
ga_solver.
GASolver
(initial_pop, goal, target_value, mutation, prob_mutation, crossover_, selector, selection_rate=0.5, min_select=0, max_steps=0, random_seed=None)¶ A simple Genetic Algorithm Solver. It accepts an initial population, and several callables to define goal, mutation and crossovers.
Parameters: - initial_pop (list) – The elements of the initial population.
- goal (function) – A function that accepts one member of the population and returns its current value.
- target_value (numeric) – The value you’re looking for. When the goal(x) for any member x of the population is equal to this, the solution has been found.
- mutation (function) – A callable that accepts one member of the population and returns a mutated value of that member.
- prob_mutation (float, 0 <= prob_mutation <= 1)) – The probability of a
mutation occur in any individual. Every time that the
mutation
function is invoked, there’sprob_mutation
chance of it actually DOING anything. - crossover – (function): A callable that takes 2 arguments that are current solutions and crosses them over, returning a new one
- selector – (function): A function that receives a dictionary containing
the current population and their fitness values and returns one
selected individual. Some of the classic selection functions
can be found in the
pop_selectors
submodule - selection_rate (float, 0 <= selection_rate <= 1) – the rate of individuals in the current population that will be selected to reproduce and compose the next. Defaults to 0.5
- min_select (integer, optional) – if set,
selector
will select at leastmin_select
members of the current population to form the next - max_steps (int, optional) – If supplied, the solver will iterate for at
most
max_steps
. The default is 0, which is the value that disable the limit in steps. - random_seed (int, optional) – If provided, the seed of Python’s PRNG will be set to this. In practice you _only_ want to set this value when you need reproducible runs. Useful for testing
-
__init__
(initial_pop, goal, target_value, mutation, prob_mutation, crossover_, selector, selection_rate=0.5, min_select=0, max_steps=0, random_seed=None)¶ Initialize self. See help(type(self)) for accurate signature.
-
__iter__
()¶ Implementing the iterable protocol. GASolver is an iterable itself!
-
__len__
()¶ A solvers len() is its population’s len()
-
__next__
()¶ GASolver is an iterable that will execute until a solution is found or, optionally, until a max number of steps is reached. That max number may be defined in the max_steps property.
A step is built as follows:
- The current population is selected by the selector function. This will select a fixed selection_rate of the current population and replace it by the selected members
- Random couples are selected to reproduce using the crossover function. We select as much couples as needed to replace the size of the population. That is, if selection_rate is 0.3, the previous step will have selected 30% of the original population and we need as much as 70% of the original to restore the original size.
- All the selected couples are crossed over and create new siblings
- The selected members of the current population and the siblings make the new population
- All the population suffers mutation by the mutate function
-
best_fit
¶ Returns the individuals with the best fitness value and that value.
-
crossover
(sol_a, sol_b)¶ Calls the crossover function with the parameters sol_a and sol_b and returns the given sibling
-
current_state
¶ Current state computes the status of the current population by applying the goal function to all of its members
-
mutate
(individual)¶ Mutate tries to apply the mutation function, but it actually does anything only a few times, determined by prob_mutation
-
mutate_pop
()¶ Runs the entire population through mutate. Note that not _every_ time that mutate is called it actually does anything. Check mutate’s doc for details
-
select
(replace=True)¶ Selects a new population using the given selector function. This new population will have
size = ceil(current_population * selection_rate)If replace is True, the current population will be replaced by the new one.
-
solution_found
¶ Returns True if any member of the current population has goal(x) == target_value
-
solutions
¶ Returns the member of the current population where goal(individual) == target_value