Matlab7.0编写的变异过程程序3:
function new_pop = mutation(new_pop, stringlength, dimension, pm)
new_popsize = size (new_pop, 1);
for i = 1: new_popsize
if rand
mpoint = round (rand (1, dimension) * (stringlength -1))+1;
for j =1: dimension
new_pop (i, (j-1)* stringlength+ mpoint (j)) =
1- new_pop (i, (j-1)* stringlength+ mpoint (j));
end
end
end
Matlab7.0编写的解码程序4:
function pop = decoding(pop, stringlength, dimension, x_bound)
popsize = size (pop, 1) ;
temp = 2. ^ (stringlength-1: -1: 0) / (2^ stringlength-1) ;
for i = 1: dimension
bound (i) = x_bound (i,2) -x_bound (i,1) ;
end
for i = 1:popsize
for j = 1:dimension
m (:, j) = pop (i, stringlength* (j-1)+1: stringlength*j) ;
end
x=temp*m;
x=x.*bound+ x_bound (:, 1)’ ;
pop (i, dimension* stringlength+1) = funname (x);
end
选择程序5:
function
[temp_generation, k, sign] = constraint_choising (pop, stringlength, dimension, x_bound)
popsize = size (pop, 1) ;
mark = k ;
sign = 1 ;
ss = 1 ;
temp = 2. ^ (stringlength-1: -1: 0) / (2^ stringlength-1) ;
for i = 1: dimension
bound (i) = x_bound (i,2) -x_bound (i,1) ;
for i = 1: popsize
for j = 1: dimension
m (:, j) = pop (i, stringlength* (j-1)+1: strength*j) ;
end
x = temp* m ;
x = x. *bound+ x_ound (:, 1)’ ;
%x = round(x);%四舍五入取整
if constraint_fun (x) = = 1
temp_generation (ss, :) = pop (i, :) ;
ss = ss+1;
k = l +1;
end
end
if mark = = k
temp_generation = 0;
sign = 0;
end
function
new_generation = producing (pop, popsize, stringlength, dimension, x_bound, pm)
k = 1;
signk = 1;
while k <= popsize
new_pop = cross_over (pop, popsize, stringlength, dimension);
pop = mutation (new_pop, stinglength, dimension, pm);
[temp_generation, k, sign] = constraint_choising (pop, k, stringlength, dimension, x_bound);
if sign = =1
new_generation (signk: k-1, :) = temp_generation;
signk = k;
end
end
function selected = selection (pop, popsize, stringlength, dimension)
popsize_new = size (pop, 1) ;
r = rand (1, popsize) ;
fitness = pop(:, dimension* stringlength+1) ;
fitness = fitness / sum (fitness);
fitness = cumsum (fitness);
for i = 1: popsize
for j = 1: popsize_new
if r(i) <= fitness(j)
selected (i, :) = pop (j, :);
break;
end
end
end