i need help to make mathlab code step 4 and step 5?
11 views (last 30 days)
Show older comments
i have mathlab code about genetic algorithm ..
my mathlab code is only at the chromosome selection stage
pop_size = 5;
%sigma=0
%q=0
all_Evals = zeros(pop_size,1);
for count = 1:pop_size
%fprintf('count =%d\n',count);
c = [10 2 20 11
12 7 9 20
4 14 16 18];
[m,n] = size(c);
phi = 1:m*n;
s = [15
25
10
];
d = [5 15 15 15];
for count2=1:length(phi)
% fprintf('count2=%d\n',count2);
used_idx = randi(length(phi));
trial = phi(used_idx);
phi(used_idx) = [];
i = [1+mod((trial-1),m)];
j = [1+mod((trial-1),n)];
x(i,j) = min(s(i),d(j));
s(i) = s(i)-x(i,j);
d(j) = d(j)-x(i,j);
Eval=0;
for j=1:n
for i=1:m
if x(i,j)>0
Eval=Eval+c(i,j)*x(i,j);
%fprintf('Eval=%d\n',Eval);
end
end
end
end
disp(Eval)
all_Evals(count) = Eval
end
sum_of_Evals = sum(all_Evals)
all_p=zeros(pop_size,1)
for count=1:pop_size
%probabilitas_eval = all_Evals(count)/sum_of_Evals
%kumulatif_eval = cumsum(probabilitas_eval)
p=(sum_of_Evals-all_Evals(count))/(sum_of_Evals*(pop_size-1))
%fprintf('all_Evals(count)=%d\n',all_Evals(count));
all_p(count)=p
end
kumulatif_eval=cumsum(all_p)
%all_p=zeros(pop_size,1)
all_roulate=zeros(pop_size,1);
%%newInd = x(i,j)
%r=rand()
for count=1:pop_size
roulete = rand()
all_roulate(count)= roulete
end
for a=1:pop_size
for b=1:pop_size
if roulete <= kumulatif_eval(b)
fprintf('kumulatif_eval(b)=%d\n',kumulatif_eval(b));
x = b
end
end
end
Step 4: Generate a random number roullate from range [0,1]
Step 5: if kumulatif_eval(b-1)< roulete<= kumulatif_eval(b) then choromosome X(b) is selected
can anyone help fix my mathlab code this? Please help
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!