moocore.

single_point_crossover#

moocore.single_point_crossover(parents, /, *, pair_prob=1.0, seed=None)[source]#

Single-point crossover.

Each crossover operation on a pair of parents produces two offspring. With probability pair_prob, a cut point \(k\) is drawn uniformly from \(\{1, \dots, n_\text{var} - 1\}\). The first offspring takes the first \(k\) variables from the first parent and the rest from the second parent, and the second offspring takes the complement. 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. It must have at least 2 columns.

  • 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

uniform_crossover

Swap each variable independently.

Notes

Single-point crossover is the classical crossover of genetic algorithms [1][2].

References

Examples

>>> parents = np.array([[0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1]])
>>> moocore.single_point_crossover(parents, seed=4)
array([[0, 0, 0, 1, 1, 1],
       [1, 1, 1, 0, 0, 0]])