4058555e0619d21a67b561d5cfdf1d80d918d127
[scilab.git] / scilab / modules / genetic_algorithms / macros / crossover_ga_binary.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2008 - Yann COLLETTE <yann.collette@renault.com>
3 //
4 // This file must be used under the terms of the CeCILL.
5 // This source file is licensed as described in the file COPYING, which
6 // you should have received as part of this distribution.  The terms
7 // are also available at
8 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
9
10 function [Crossed_Indiv1, Crossed_Indiv2] = crossover_ga_binary(Indiv1,Indiv2,param)
11     if ~isdef("param","local") then
12         param = [];
13     end
14
15     // We deal with some parameters to take into account the boundary of the domain and the neighborhood size
16     [BinLen,err]       = get_param(param,"binary_length",8);
17     [MultiCross,err]   = get_param(param,"multi_cross",%F);
18     [MultiCrossNb,err] = get_param(param,"multi_cross_nb",2);
19
20     if ~MultiCross then
21         mix = ceil((length(Indiv1)-1)*grand(1,1,"def"))+1;
22
23         part1_1 = part(Indiv1,1:mix);
24         part1_2 = part(Indiv1,mix+1:length(Indiv1));
25         part2_1 = part(Indiv2,1:mix);
26         part2_2 = part(Indiv2,mix+1:length(Indiv2));
27
28         Crossed_Indiv1 = strcat([part1_1 part2_2]);
29         Crossed_Indiv2 = strcat([part1_2 part2_1]);
30     else
31         mix = ceil((length(Indiv1)-1)*grand(MultiCrossNb,1,"def"))+1;
32         mix = -unique(gsort(-mix));
33         Crossed_Indiv1 = Indiv1;
34         Crossed_Indiv2 = Indiv2;
35         for i=1:length(mix)
36             part1_1 = part(Crossed_Indiv1,1:mix(i));
37             part1_2 = part(Crossed_Indiv1,mix(i)+1:length(Crossed_Indiv1));
38             part2_1 = part(Crossed_Indiv2,1:mix(i));
39             part2_2 = part(Crossed_Indiv2,mix(i)+1:length(Crossed_Indiv2));
40
41             Crossed_Indiv1 = strcat([part1_1 part2_2]);
42             Crossed_Indiv2 = strcat([part1_2 part2_1]);
43         end
44     end
45 endfunction