필터 지우기
필터 지우기

a simple evoulutionary algorithm 1. the user can enter any user defined string as the evolutionary tareget up to 30 characters max

조회 수: 2 (최근 30일)
clc
clear
t= 'Hell world!';
numRands=length(t);
tlength=numRands;
source=t(ceil(rand(1,tlength)*numRands));
fitval = fitness(source, t);
i = 0;
while 1
i = i + 1;
m = mutate(source);
fitval_m = fitness(m, t);
if fitval_m < fitval
fitval = fitval_m;
source = m;
fprintf('%5i %5i %14s', i, fitval_m, m);
end
if fitval == 0
break
end
end
function fitval = fitness(source, t)
fitval = 0
for i = 1 : length(source)
fitval = fitval + (double(t(i)) - double(source(i))) ^ 2;
end
end
function parts = mutate(source)
parts = source;
charpos = randi(length(source)) - 1;
parts(charpos) = char(double(parts(charpos)) + (randi(3)-2));
end
  댓글 수: 2
yared Zeleke
yared Zeleke 2018년 3월 30일
편집: Walter Roberson 2018년 3월 31일
The quation is
1. The user can enter any user defined string as the evolutionary target up to 30 characters max.
2. The program should randomly generate the first ‘source’ from which to mutate.
3. The program should print-out every 100th iteration the iteration and the current mutated source and its fitness in the command window.
I tried but I have some eror I need some one to correct it to me finally the result should be the same as t

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2018년 3월 31일
You have
charpos = randi(length(source)) - 1;
The result of that can be 0 because randi might have generated a 1. You then have
parts(charpos) = char(double(parts(charpos)) + (randi(3)-2));
but charpos can be 0 so you can be attempting to index at location 0, which is not defined.
I do not know why you are subtracting 1 at the point?

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by