Skip to main content

bit_flip_mutation

Function bit_flip_mutation 

Source
pub fn bit_flip_mutation<B: Backend>(
    population: Tensor<B, 2, Int>,
    p: f32,
    rng: &mut dyn Rng,
    device: &<B as BackendTypes>::Device,
) -> Tensor<B, 2, Int>
Expand description

Bit-flip mutation on a binary Tensor<B, 2, Int> population.

Each gene is independently flipped with probability p. The flip is computed arithmetically as 1 − x, so the input tensor must hold values exclusively in {0, 1}; any value outside that set will produce out-of-range results silently.

p = 0.0 is an identity; p = 1.0 inverts every gene. The conventional mutation rate for bit-string GAs is 1 / D (one expected flip per individual), but this function does not enforce any particular rate.

All n·d Bernoulli coin flips are taken from the caller-supplied host rng and loaded onto the device via Tensor::from_data; no backend-global RNG state is touched.

The input tensor must have shape (N, D); the returned tensor has the same shape and element type.