QDpy
Loading...
Searching...
No Matches
tools.py File Reference

Selection, mutation, and trusted result-file helpers. More...

Go to the source code of this file.

Classes

class  qdpy.tools._ResultUnpickler

Namespaces

namespace  qdpy

Functions

Sequence[Any] qdpy.tools.sel_random (Sequence[Any] collection)
 SELECTION ########### {{{1.
Any qdpy.tools.non_trivial_sel_random (containers.Container container)
Sequence[Any] qdpy.tools.sel_roulette (Sequence[Any] collection)
 qdpy.tools.sel_roulette_score (collection, str score_name="curiosity", float default_score=0.0)
 Draw one candidate with weights derived from a named scalar score.
Sequence[Any] qdpy.tools.sel_grid_roulette (Sequence[Any] collection)
 qdpy.tools.iso_line_dd (first, second, *, iso_sigma=0.01, line_sigma=0.2, bounds=None, rng=None)
 MUTATIONS ########### {{{1.
 qdpy.tools.iso_line_cross (first, second, *, iso_sigma=0.005, line_sigma=0.05, crossover_probability=0.5, crossover_rate=0.1, bounds=None, rng=None)
 Combine independently varied parents using a segment crossover mask.
MutableSequence[Any] qdpy.tools.mut_gaussian (MutableSequence[Any] individual, float mu, float sigma, float mut_pb, Optional[Sequence[float]] bounds=None)
MutableSequence[Any] qdpy.tools.mut_polynomial_bounded (MutableSequence[Any] individual, float eta, float low, float up, float mut_pb, *, rng=None)
 qdpy.tools.sel_or_init (Sequence[IndividualLike] collection, IndividualLike base_ind, Callable sel_fn, float sel_pb, Callable init_fn, float init_pb=0., bool return_flag=True)
 COMBINED OPERATORS ########### {{{1.
Sequence[IndividualLike] qdpy.tools.mut_or_cx (Union[IndividualLike, Sequence[IndividualLike]] individuals, Callable mut_fn, Callable cx_fn)
 qdpy.tools.open_pickle_file (filename)

Detailed Description

Selection, mutation, and trusted result-file helpers.

Definition in file tools.py.

Function Documentation

◆ iso_line_cross()

qdpy.tools.iso_line_cross ( first,
second,
* ,
iso_sigma = 0.005,
line_sigma = 0.05,
crossover_probability = 0.5,
crossover_rate = 0.1,
bounds = None,
rng = None )

Combine independently varied parents using a segment crossover mask.

Return one IsoLineCross offspring without changing its parents.

Apply Iso+LineDD in both directions and alternate parental segments at
the scaled cumulative exponential breakpoints. Set line_sigma to zero
for IsoCross. A zero crossover probability consumes the same random
draws as Iso+LineDD.

:param first: Base parent vector.
:param second: Other parent vector with the same shape.
:param iso_sigma: Isotropic noise scale.
:param line_sigma: Directional noise scale.
:param crossover_probability: Probability of mixing the two varied vectors.
:param crossover_rate: Number of sampled breakpoints per gene, in [0, 1].
:param bounds: Optional clipping bounds.
:param rng: Generator or the default NumPy global stream.

Definition at line 152 of file tools.py.

◆ iso_line_dd()

qdpy.tools.iso_line_dd ( first,
second,
* ,
iso_sigma = 0.01,
line_sigma = 0.2,
bounds = None,
rng = None )

MUTATIONS ########### {{{1.

Generate real-valued offspring with independent and directional noise.

Return an Iso+LineDD offspring as a new NumPy vector.

Each coordinate receives independent isotropic noise. One Gaussian draw
scales the difference between the parents for the entire offspring.

:param first: Base parent vector.
:param second: Parent defining the direction from the base.
:param iso_sigma: Standard deviation of independent coordinate noise.
:param line_sigma: Standard deviation of the shared direction coefficient.
:param bounds: Optional lower and upper coordinate bounds for clipping.
:param rng: Random generator. By default use NumPy's global random stream.

Definition at line 127 of file tools.py.

◆ mut_gaussian()

MutableSequence[Any] qdpy.tools.mut_gaussian ( MutableSequence[Any] individual,
float mu,
float sigma,
float mut_pb,
Optional[Sequence[float]] bounds = None )
Add Gaussian noise to selected coordinates in place.

:param individual: Mutable genome to vary.
:param mu: Mean of the perturbation.
:param sigma: Standard deviation of the perturbation.
:param mut_pb: Per-coordinate selection probability.
:param bounds: Optional common lower and upper clipping bounds.
:returns: The same mutated genome object.

Definition at line 186 of file tools.py.

◆ mut_or_cx()

Sequence[IndividualLike] qdpy.tools.mut_or_cx ( Union[IndividualLike, Sequence[IndividualLike]] individuals,
Callable mut_fn,
Callable cx_fn )
Either perform a mutation (using function `mut_fn`) or a crossover (using function `cx_fn`)
depending on the nature and length of `individuals`.
If `individuals` is an IndividualLike or a Sequence of one IndividualLike, a mutation will be performed.
If `individuals` is a Sequence of two IndividualLike, a crossover will be performed.

Parameters
----------
:param individuals: Union[IndividualLike, Sequence[IndividualLike]]
    The individual(s) to mutate or crossover.
:param mut_fn: Callable
    The mutation function.
:param cx_fn: Callable
    The crossover function.

Return
------
The resulting individual(s).

Definition at line 290 of file tools.py.

◆ mut_polynomial_bounded()

MutableSequence[Any] qdpy.tools.mut_polynomial_bounded ( MutableSequence[Any] individual,
float eta,
float low,
float up,
float mut_pb,
* ,
rng = None )
Apply bounded polynomial mutation to selected coordinates in place.

Use a strictly ordered interval and start with a genome inside
its bounds. The operator uses Deb's polynomial distribution and
clips the mutated coordinates to that interval.

:param individual: Mutable genome to vary.
:param eta: Distribution index controlling typical change size.
:param low: Lower bound shared by the coordinates.
:param up: Upper bound shared by the coordinates.
:param mut_pb: Per-coordinate selection probability.
:param rng: Optional random generator. Defaults to Python's random module.
:returns: The same mutated genome object.

Definition at line 205 of file tools.py.

◆ non_trivial_sel_random()

Any qdpy.tools.non_trivial_sel_random ( containers.Container container)
Select and return one individual at random among `container`.
Ignore individuals with a trivial fitness (i.e. equal to the minimum fitness value),
except if there are only individuals with trivial fitness.

Parameters
----------
:param container: Container
    The individuals to select from.

Definition at line 46 of file tools.py.

◆ open_pickle_file()

qdpy.tools.open_pickle_file ( filename)
Deserialize a trusted result file.

Standard native result dictionaries include the container and may
include evaluation and iteration tables. Recreate older runs with
the current version when their fitness snapshot format is unsupported.

Definition at line 329 of file tools.py.

◆ sel_grid_roulette()

Sequence[Any] qdpy.tools.sel_grid_roulette ( Sequence[Any] collection)
Select a grid member and use its cell for roulette selection.

The first draw is uniform over stored entries, so cells with more
members are visited more often. The second draw applies roulette
weights to the chosen cell's population.

Definition at line 107 of file tools.py.

◆ sel_or_init()

qdpy.tools.sel_or_init ( Sequence[IndividualLike] collection,
IndividualLike base_ind,
Callable sel_fn,
float sel_pb,
Callable init_fn,
float init_pb = 0.,
bool return_flag = True )

COMBINED OPERATORS ########### {{{1.

Either select an individual from `collection` by using function `sel_pb`,
or initialise a new individual by using function `init_pb`.
If `collection` is empty, it will always initialise a new individual, not perform selection.

Parameters
----------
:param collection: Sequence[IndividualLike]
    The individuals to select from.
:param base_ind: IndividualLike
    The base individual to initialise.
:param sel_fn: Callable
    The selection function.
:param sel_pb: float
    The probability of performing selection.
:param init_fn: Callable
    The initialisation function.
:param init_pb: float
    The probability of performing initialisation.
:param return_flag: bool
    If set to True, the function will return a Tuple[IndividualLike, bool] with a first item corresponding
    to the selected or initialised individual, and the second item a flag set to True if the first item
    was selected, and set to False if it was initialised.
    If set to False, the function will return the selected or initialised IndividualLike.

Definition at line 249 of file tools.py.

◆ sel_random()

Sequence[Any] qdpy.tools.sel_random ( Sequence[Any] collection)

SELECTION ########### {{{1.

Select and return one individual at random among `collection`.

Parameters
----------
:param collection: Sequence[Any]
    The individuals to select from.

Definition at line 35 of file tools.py.

◆ sel_roulette()

Sequence[Any] qdpy.tools.sel_roulette ( Sequence[Any] collection)
Select a member by its sum of raw objective values.

The supplied values should define valid nonnegative roulette
probabilities. A total of zero falls back to a uniform choice.

Definition at line 67 of file tools.py.

◆ sel_roulette_score()

qdpy.tools.sel_roulette_score ( collection,
str score_name = "curiosity",
float default_score = 0.0 )

Draw one candidate with weights derived from a named scalar score.

Parameters
collectionNonempty sequence of candidates.
score_nameCandidate score used for weighting.
default_scoreScore for candidates that lack the named measurement.
Returns
A member of the input sequence.
Select by a finite named score, with support for negative scores.

Shift all scores by the same amount when the minimum is negative.
If every resulting weight is zero, draw uniformly.

Definition at line 88 of file tools.py.