about single point crossover??

7 Ansichten (letzte 30 Tage)
eman ahmed
eman ahmed am 20 Nov. 2016
Bearbeitet: eman ahmed am 20 Nov. 2016
i'm writing aprog, convert word to numbers then binary then want to make crossover with random chromosome,the problem face me when the word is 3 letters output 4..and when 2 letters return error..can anyone help me plz
%convert the word 'THIS' to numbers according to the alphabetic sequence
prompt = 'Enter your text: ';
text= input(prompt,'s');
alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
[~,pos] = ismember(text,alphabet)
%convert each letter of the word to binary value
step3=de2bi(pos,'left-msb')
n = size(step3, 1);
l = size(step3, 2);
%generate the default chromosome (second parent)
Step4= randi([0, 1], 1,l)
Step4=[Step4;Step4;Step4;Step4];
%Choose a crossover point
cp = randperm(l-1, 1)
for i=1:n
breeders = [step3(i,:);Step4];
b1(i,:) = [breeders(1, 1:cp), breeders(2, cp+1:end)];
b2(i,:) = [breeders(2, 1:cp), breeders(1, cp+1:end)];
i=i+1;
end
b1=b1
b2=b2

Antworten (0)

Kategorien

Mehr zu Encryption / Cryptography finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by