From: Shi on
I am a new matlab learner, and these matlab codes may not in good order, but it can run. However, there are some questions (in (((((()))))) I can not work out. Please help me for that.
herein after is the assignment and my code:
Use Ga to solve one-max problem, and the GA must have these parameters:
Representation: Binary strings of length equal to the length of the one-max problem.
Initialisation: Random.
Selection: Fitness Proportionate, Tournament Selection (default candidate list size of 3)
Recombination: One point crossover (default probability of 0.95 and random crossover position)
Bit flip mutation strategy (default mutation probability per bit of 0.001)
Elitism: Number of elite solutions copied automatically into the new population (default 1).
Replacement: Generational, i.e. generate an entirely new population to replace the last population.
Population size: 100.
Termination criteria: optimum found or no increase in quality for 50 generations.

code:

for l=1:10 %perfom 10 times to get a mean value of optimization times
clear all;
close all;
%set initial values
M=100; %population size100
CL=10; % chromsome length
G=round(rand(M,CL)); %generate a population

for k=1:10000
for j=1:M
rows1=randsample(1:M,3,true,sum(G,2));
a1=G(rows1,:) ; % choose 3 chromosomes from population
sum(a1,2) ;
max(sum(a1,2)) ; % find the chromosomes with max. sum of entries
b1=find(sum(a1,2)==max(sum(a1,2))) ;
rowsa=randsample(b1,true,1);
chrom1=a1(rowsa,:);
%%%%%%%%%%%
rows2=randsample(1:M,3,true,sum(G,2));
a2=G(rows2,:) ; % choose another 3 chromosomes from population
sum(a2,2);
max(sum(a2,2)) ; % find the chromosomes with max. sum of entries

b2=find(sum(a2,2)==max(sum(a2,2))) ;
rowsb=randsample(b2,true,1);
chrom2=a2(rowsb,:);

%%%%%
c=randsample(1:CL,1,true);
if randsample(1:100,1,true)<=95 %define crossover probablity 0.95
copy_chrom1=chrom1;
chrom1(1:c)=chrom2(1:c); %cross over genes [1,c] with chrom2
chrom2(1:c)=copy_chrom1(1:c); %cross over genes [1,c] with chrom1
else
end

for i=1:CL
if randsample(1:1000,1,true)<=1 % if meeting mutation probability 0.01
if chrom1(i)==0 % chrom1 genes mutates
chrom1(i)=1;
elseif chrom1(i)==1
chhrom1(i)=0;
end
if chrom2(i)==0 %chrom2 genes mutates
chrom2(i)=1;
elseif chrom2(i)==1
chrom2(i)=0;
end
else
end
end

v1=sum(chrom1);
v2=sum(chrom2); % sum the two chroms
if v1>=v2
elit=chrom1;
elseif v1<v2
elit=chrom2;
end %find out the best chrom to be the elitism
p(j,:)=elit;
end
G=p; %yield a new population G with all elitisms
((((((%how to write a code to express:Termination criteria: optimum found or no increase in quality for 50 generations))))))))

if mean(mean(G))==1
break
end
end
times(l)=k
end
optimesm=mean(times) %get the mean of optimization times
((((how to express optimesm(optimization times mean) here, I know there is some simple errors, but I dont know how to correct them, ))))
From: sscnekro on
> I am a new matlab learner, and these matlab codes may not in good order, but it can run. However, there are some questions (in (((((()))))) I can not work out. Please help me for that.

Hey Shi, have you read my reply to your previous post? It could be eventually .. useful. Another tip: Your code feels a bit lost-in-it. You may try to sketch the oveall frame of the code first, instead of writing down all details. I mean, e.g.

% ----------- Part 1: create initial population ---------
% FOR or WHILE loop
% do this stuff
%end

% ----------- Part 2: reapeat cross-over until ---------------
% WHILE loop: what conditions?
% ...
% ... what results to display?

Wish you fun with your GA :O)
From: Shi on
"sscnekro " <stiahni.mail(a)zoznam.sk> wrote in message <hvedou$qgv$1(a)fred.mathworks.com>...
> > I am a new matlab learner, and these matlab codes may not in good order, but it can run. However, there are some questions (in (((((()))))) I can not work out. Please help me for that.
>
> Hey Shi, have you read my reply to your previous post? It could be eventually .. useful. Another tip: Your code feels a bit lost-in-it. You may try to sketch the oveall frame of the code first, instead of writing down all details. I mean, e.g.
>
> % ----------- Part 1: create initial population ---------
> % FOR or WHILE loop
> % do this stuff
> %end
>
> % ----------- Part 2: reapeat cross-over until ---------------
> % WHILE loop: what conditions?
> % ...
> % ... what results to display?
>
> Wish you fun with your GA :O)

Thank you so much for your reply, I will try it per your advice.
From: Shi on
"sscnekro " <stiahni.mail(a)zoznam.sk> wrote in message <hvedou$qgv$1(a)fred.mathworks.com>...
> > I am a new matlab learner, and these matlab codes may not in good order, but it can run. However, there are some questions (in (((((()))))) I can not work out. Please help me for that.
>
> Hey Shi, have you read my reply to your previous post? It could be eventually .. useful. Another tip: Your code feels a bit lost-in-it. You may try to sketch the oveall frame of the code first, instead of writing down all details. I mean, e.g.
>
> % ----------- Part 1: create initial population ---------
> % FOR or WHILE loop
> % do this stuff
> %end
>
> % ----------- Part 2: reapeat cross-over until ---------------
> % WHILE loop: what conditions?
> % ...
> % ... what results to display?
>
> Wish you fun with your GA :O)

another question, easy to you I think:
the result are ten k value(generations for optimization) , how to calculate their mean value?
From: sscnekro on
> another question, easy to you I think:
> the result are ten k value(generations for optimization) , how to calculate their mean value?

I don't quite know to imagine the mean of what you need to calculate, but basically you need to know that mean works "columnwise". If your final population is stored e.g. in m x n matrix P, mean(P) gives a 1 x n result, with the mean computed for each column in P; mean(mean(P)) returns a 1 x 1 result, the mean of the column means. So, it's up to you if you need to check single columns (is the individual string all ones) or the overall population (are all strings all ones).

PS PLEASE, CONTINUE EVENTUAL POSTS UNDEER THIS THREAD!!!