moocore.

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 probability swap_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. Rows 2i and 2i + 1 are 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 seed numpy.random.default_rng(), a Numpy default random number generator (RNG) or an instance of a Numpy-compatible RNG. None uses the equivalent of a random seed, as in numpy.random.default_rng(). (default: None)

Returns:

ndarray – Array with the same shape as parents, where rows 2i and 2i + 1 are the two offspring of rows 2i and 2i + 1 of parents. 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_crossover

Exchange 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]])