[英]Code for Genetic Algorithm Cross-Over in Matlab
Let suppose I have two Parents rows. 假设我有两个Parents行。
parent 1: 3-1-2-5-4 父母1:3-1-2-5-4
parent 2: 1-4-5-2-3 父母2:1-4-5-2-3
Now after Cross-Over, I want to have following child rows: 现在,在交叉之后,我要具有以下子行:
Child 1: 1-4-|2-5|-3 儿童1:1-4- | 2-5 | -3
Child 2: 3-1-|5-2|-4 儿童2:3-1- | 5-2 | -4
A single position cross-Over is denoted by "|". 单个位置交叉点用“ |”表示。 Kindly if there is any code that gives me the above child sequences. 请给我上面的子序列的代码。
Use: 采用:
%initilizes parents
parent1 = [3 1 2 5 4];
parent2 = [1 4 5 2 3];
%determines which rows should be swapped
rowsToSwap = [3 4];
%generates child1 and child2
child1 = parent2;
child1(rowsToSwap) = parent1(rowsToSwap);
child2 = parent1;
child2(rowsToSwap) = parent2(rowsToSwap);
Results: 结果:
child1 =
3 1 5 2 4
child2 =
1 4 2 5 3
In this code snippet, rowsToSwap are determined hard-codded. 在此代码段中,将rowToSwap确定为硬编码。 if you want, you can choose them randomly by using randsample function: 如果需要,可以使用randsample函数随机选择它们:
rowsToSwap = randsample(1:length(parent1),2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.