Generation#
- class empulse.optimizers.Generation(population_size=None, crossover_rate=0.8, mutation_rate=0.1, elitism=0.05, verbose=False, logging_fn=<built-in function print>, random_state=None, n_jobs=1)[source]#
A single generation of a Real-coded Genetic Algorithm (RGA).
optimizealways maximizes the objective function passed to it. Read more in the User Guide.- Parameters:
- population_sizeint or None, default=None
Number of individuals in the population. If
None, population size is set to10 * n_features.- crossover_ratefloat, default=0.8
Probability of crossover. Must be in [0, 1].
- mutation_ratefloat, default=0.1
Probability of mutation. Must be in [0, 1].
- elitismfloat, default=0.05
Fraction of the population that transferred to the next generation without change. Must be in [0, 1].
- verbosebool, default=False
If
True, print status messages.- logging_fncallable, default=print
Function to use for logging.
- random_stateint, RandomState or None, default=None
Random seed. Accepts an
int, anumpy.random.RandomStateinstance, orNone(uses the global NumPy random state).- n_jobsint or None, default=1
Number of jobs to run in parallel. If
-1, use all available processors. IfNone, use 1 processor.
- Attributes:
- namestr
Name of the optimizer.
- directionDirection
Optimization direction, always
Direction.MAXIMIZE. Callers handing this class a loss (which is minimized by convention) must negate it first.- populationndarray, shape (population_size, n_dim)
Current population.
- population_sizeint or None
The population_size constructor argument, unchanged by
optimize.Nonemeans the actual size used each run is resolved from10 * n_featuresand kept internally.- crossover_ratefloat
Probability of crossover.
- mutation_ratefloat
Probability of mutation.
- elitismint
The number of individuals of the population that are transferred to the next generation without change. Set to
0untiloptimizeresolves it from elitism_fraction once the population size is known.- elitism_fractionfloat
The elitism constructor argument, stored under its own name since elitism itself is repurposed as the resolved individual count above.
- verbosebool
If
True, print status messages.- logging_fncallable
Function to use for logging.
- rngRandomState
Random state object.
- n_jobsint
Number of jobs to run in parallel. If
-1, use all available processors. IfNone, use 1 processor.- fx_bestlist
List of best fitness values.
- fitnessndarray, shape (population_size,)
Fitness values of the current population.
- resultOptimizeResult
Result of the optimization.
- lower_boundsndarray, shape (n_dim,)
Lower bounds of the search space.
- upper_boundsndarray, shape (n_dim,)
Upper bounds of the search space.
- delta_boundsndarray, shape (n_dim,)
Difference between upper and lower bounds.
- n_dimint
Number of dimensions.
- _n_mating_pairsint
Number of mating pairs.
- elite_poollist
List of elite individuals.
- optimize(objective, bounds)[source]#
Optimize the objective function.
- Parameters:
- objectiveCallable
Objective function to optimize. Should be of signature
objective(weights) -> float.- boundslist[tuple[float, float]]
List of tuples of lower and upper bounds for each weight.
- Yields:
- selfGeneration
Current instance of the optimizer.
Notes
This is an infinite generator. The caller is responsible for stopping iteration (e.g. via
breakoritertools.islice). Callingoptimizeon the same instance a second time resets all accumulated state (fx_best,result,elite_pool, etc.).