Operators¶
Guide: Native operators.
Operators for the algorithms that recombine.
crossover= and mutation= settings take the operators of ypea.operators.library
(also available here): SBX and PolynomialMutation in the [0, 1] code space, and
NativeCrossover and NativeMutation for native values. Each one declares its
space, and the algorithm checks that it fits before the run.
The rest of this module holds the native operators of GA, SA and, for all types but Real, NSGA-II, NSGA-III, SPEA2 and MOEA/D.
A native operator works on decoded values of one variable: a batch of shape (n, *shape).
A crossover takes two parent batches and returns two child batches. A mutation takes one
batch and returns one. Every operator keeps the invariants of its type (a permutation
stays a permutation, k ones stay k ones, values stay in their bounds), so
decode(encode(y)) == y holds for every output (exactly, except for Real values, which
come back up to floating point rounding).
In a problem with several variables, a crossover applies to every variable, and a mutation
changes each variable with probability 1/k (k variables), with at least one per row. The
settings rate and sigma (GA mutation_rate and mutation_step_size) only affect
Real and Integer variables. The ordering, selection and partition operators ignore them
(but GA with mutation_rate=0 makes no mutants at all).
The algorithm decodes the parents' codes, applies each variable's operator, and encodes the result back. Partition and BinaryPartition use the permutation operators on their ordering of items and separators. FixedSum has no native operator and keeps using the algorithm's [0, 1] code operators on its own slice of the codes.
All random numbers come from the rng argument, and only from the methods that
ypea.StudioRng offers.
SBX ¶
SBX(eta=20.0, prob=0.9, prob_var=0.5)
Simulated binary crossover (SBX) in the code space, bounded to [0, 1].
Each pair of parents is crossed with probability prob, and then each component
with probability prob_var. A crossed component spreads the two parent values by a
factor drawn from a polynomial distribution with index eta (larger values keep the
children closer to the parents). The bounded form takes the distance to the bounds
into account, so no child leaves [0, 1], and the two children are swapped with
probability 1/2. Components whose parent values differ by less than 1e-14 are copied.
The defaults (eta=20, prob=0.9) are the NSGA-II settings of Deb et al. (2002).
The bounded form and prob_var=0.5 follow Deb's NSGA-II reference code.
References: Deb and Agrawal (1995), Simulated binary crossover for continuous search space, Complex Systems 9, 115 to 148. Deb, Pratap, Agarwal and Meyarivan (2002), IEEE Transactions on Evolutionary Computation 6(2), 182 to 197.
PolynomialMutation ¶
PolynomialMutation(eta=20.0, prob=None)
Polynomial mutation in the code space, bounded to [0, 1].
Each component changes with probability prob (default 1/d for d code components).
The change follows a polynomial distribution with index eta (larger values give
smaller changes), scaled so that the result stays in [0, 1].
The defaults (eta=20, prob=1/d) are the NSGA-II settings of Deb et al. (2002).
The bounded form follows Deb's NSGA-II reference code.
References: Deb and Goyal (1996), A combined genetic adaptive search (GeneAS) for engineering design, Computer Science and Informatics 26(4), 30 to 45. Deb et al. (2002).
NativeCrossover ¶
NativeCrossover(fn, types)
A crossover on the decoded values of the variables whose type is in types.
fn(x1, x2, rng) takes two decoded batches of one variable and returns two, for
example NativeCrossover(ypea.operators.uniform_crossover, types=(ypea.Integer,)).
NativeMutation ¶
NativeMutation(fn, types)
A mutation on the decoded values of the variables whose type is in types.
fn(x, rng) takes a decoded batch of one variable and returns one.
Operator ¶
Base class of all operators. space is "code" or "native", and types lists
the variable types of a native operator.
Crossover ¶
Base class of crossover operators, called as op(x1, x2, rng).
Mutation ¶
Base class of mutation operators, called as op(x, rng).
NativeOperators ¶
NativeOperators(problem, operators=None, *, crossover=None, mutation=None, code_types=(), **params)
The operators of one run: the user's ones where given (operators per variable,
then the native crossover and mutation operators for the variable types they
list), otherwise the defaults, otherwise the algorithm's code operators on that
variable's slice of the codes.
apply ¶
apply(method, parents, rng, fallback)
Apply method ("crossover" or "mutate") to code batches parents and return
code batches. fallback(*code_slices) returns a tuple of code slices.
RealOperators ¶
RealOperators(var, alpha=0.1, rate=0.1, sigma=0.1)
Blend crossover (BLX alpha) and Gaussian mutation with a step of sigma * (ub - lb).
IntegerOperators ¶
IntegerOperators(var, alpha=0.1, rate=0.1, sigma=0.1, creep=0.5)
Uniform crossover. A changed element creeps (with probability creep) by up to
sigma * (ub - lb) steps, at least one, or is reset to a different value. For
Binary the reset is a bit flip.
PermutationOperators ¶
PermutationOperators(var, alpha=0.1, rate=0.1, sigma=0.1, crossovers=(order_crossover, pmx, cycle_crossover), mutations=(swap, inversion, insertion))
For each pair one crossover, and for each row one move, picked at random from the lists.
PartitionOperators ¶
PartitionOperators(var, alpha=0.1, rate=0.1, sigma=0.1, crossovers=(order_crossover, pmx, cycle_crossover), mutations=(swap, inversion, insertion))
Partition and BinaryPartition: the permutation operators on the ordering of n items and k - 1 separators that the groups come from.
The ordering is rebuilt as the variable's encode builds it: the items of group 0, then
separator n, the items of group 1, separator n + 1, and so on. So separators are numbered
by group position, and order crossover and PMX treat them as distinct values.
SelectionOperators ¶
SelectionOperators(var, alpha=0.1, rate=0.1, sigma=0.1)
Ordered selection of k distinct items. Crossover takes an item from the other parent at a random half of the positions, when the child does not hold it yet. Mutation replaces one item with an unused one, or swaps two positions.
BinarySelectionOperators ¶
BinarySelectionOperators(var, alpha=0.1, rate=0.1, sigma=0.1)
0/1 mask with exactly k ones. Crossover keeps the common ones and shares the others at random between the two children. Mutation swaps a one and a zero.
check_operator ¶
check_operator(op, kind, space, problem=None)
Check that op is a kind ("crossover" or "mutation") operator that fits the
algorithm's space ("encoded" or "native") and, when given, the problem. None passes.
default_operators ¶
default_operators(var, **params)
The native operators of a variable, or None if its type has none.
uniform_crossover ¶
uniform_crossover(x1, x2, rng)
Each element comes from either parent with probability 1/2.
one_point_crossover ¶
one_point_crossover(x1, x2, rng)
The children swap the tails of the flattened parents after a random cut.
order_crossover ¶
order_crossover(p1, p2, i, j)
Order crossover (OX): keep p1[i:j], fill the rest in the order of p2 after j.
pmx ¶
pmx(p1, p2, i, j)
Partially mapped crossover (PMX): keep p1[i:j], place the rest of p2 through the mapping.
cycle_crossover ¶
cycle_crossover(p1, p2, i=None, j=None)
Cycle crossover (CX): alternate cycles come from p1 and p2. The cut points are unused.