uniform_crossover#
- moocore.uniform_crossover(parents, /, *, swap_prob=0.5, pair_prob=1.0, seed=None)[source]#
Uniform crossover.
Each crossover operation on a pair of parents produces two offspring. With probability
pair_prob, each variable is swapped between the two offspring with probabilityswap_prob. Otherwise, the offspring are copies of their parents.The offspring only take values from their parents, so this operator works for binary, integer and real-valued variables alike. Integer and Boolean inputs keep their dtype.
- Parameters:
parents (
ArrayLike) – 2D array of numerical values, where each row is a decision vector. Rows2iand2i + 1are crossed over. If the number of rows is odd, the last row is crossed over with the first one.swap_prob (
float) – Probability of swapping each variable between the two offspring. (default:0.5)pair_prob (
float) – Probability of applying the crossover operation to each pair of parents. (default:1.0)seed (
int|Generator|None) – Either an integer to seednumpy.random.default_rng(), a Numpy default random number generator (RNG) or an instance of a Numpy-compatible RNG.Noneuses the equivalent of a random seed, as innumpy.random.default_rng(). (default:None)
- Returns:
ndarray– Array with the same shape asparents, where rows2iand2i + 1are the two offspring of rows2iand2i + 1ofparents. If the number of rows is odd, the second offspring of the last pair is dropped, so the number of rows does not change.
See also
single_point_crossoverExchange the tails of two parents.
Notes
Uniform crossover was proposed by Syswerda[1]. See also Eiben and Smith[2].
References
Examples
>>> parents = np.array([[0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1]]) >>> moocore.uniform_crossover(parents, seed=5) array([[0, 1, 0, 1, 0, 0], [1, 0, 1, 0, 1, 1]])