A crossover function for binary code
[Crossed_Indiv1, Crossed_Indiv2, mix] = crossover_ga_binary(Indiv1, Indiv2, param)
A string
the first individual (here a binary code) to be crossed-over.
A string
the second individual to be crossed-over.
a list of parameters.
"binary_length"
: an integer, the length of the binary
code (default 8).
"multi_cross"
: a boolean. If %T
then we allow several
cuts in the binary code (default %F
).
"multi_cross_nb"
: an integer, the number of cuts in the binary code.
Only used when multi_cross is set to %T (default 2).
A string
The first individual obtained by the cross-over function.
A string
The second individual obtained by the cross-over function.
A vector of integers
The positions the crossover occurred.
This function implements a classical binary cross-over.
crossover_ga_binary(Indiv1, Indiv2)
generates
the crossover between Indiv1
and Indiv2
by merging the characters from each string.
A position i
is chosen randomly
between 1 and the length of the binary code.
Then Indiv1
and Indiv2
are split in two parts:
the first i
characters (the head),
and the remaining characters (the tail).
The crossover swaps the tails of the binary codes.
The following schema presents the crossover:
The behaviour of the function can be modified with the use of param
:
changes the minimal length of the binary code, by default 8 characters.
Binary code for Indiv1
or Indiv2
of lower length are zero padded to the right to be of
binary_length
length or max([length(Indiv1), length(Indiv2)])
whichever is greater.
if set to %T
multiple crossovers
can happen (default %F
)
the number of locations for crossovers.
(default 2 if
multi_cross is set to %T
, 1 otherwise)
crossover_ga_binary
is based
on grand
for generating the random samples.
Use grand("setsd", seed)
to change the seed
for crossover_ga_binary
.
A = "11100000" B = "00011111" [A_crossed, B_crossed, mix] = crossover_ga_binary(A, B) C = dec2bin(2^16 - 1, 16) D = "0" param = init_param(); param = add_param(param, "binary_length", 16); // Code of length 16 param = add_param(param, "multi_cross", %T); // Multiple Crossover param = add_param(param, "multi_cross_nb", 3); // Occurs over 3 locations [C_crossed, D_crossed, mix] = crossover_ga_binary(C, D, param) | ![]() | ![]() |