gusucode.com > matlab优化的遗传算法来优化支持向量机的参数源码程序 > 优化的遗传算法来优化支持向量机的参数/遗传优化向量机/genetic.m
function f = genetic(parent_chromosome,mu,mum,sizepop,pcross,pmutation,xmin,xmax) V=size(parent_chromosome,2); l_limit =xmin;u_limit =xmax; for i = 1 : sizepop if rand(1) < pcross(i)%交叉算子 child_1 = []; child_2 = []; parent_1 = round(sizepop*rand(1)); if parent_1 < 1 parent_1 = 1; end parent_2 = round(sizepop*rand(1)); if parent_2 < 1 parent_2 = 1; end nn=0; while isequal(parent_chromosome(parent_1,:),parent_chromosome(parent_2,:))%保证两个个体互不相同 parent_2 = round(sizepop*rand); if (parent_2 < 1) if rand>0.5 parent_2 = 1; else parent_2 = 2; end end nn=nn+1; if nn>sizepop break; end end parent_11 = parent_chromosome(parent_1,:); parent_22 = parent_chromosome(parent_2,:); for j = 1 : V % SBX (Simulated Binary Crossover)模拟二进制交叉 % Generate a random number u(j) = rand(1); if u(j) <= 0.5 bq(j) = (2*u(j))^(1/(mu+1)); else bq(j) = (1/(2*(1 - u(j))))^(1/(mu+1)); end child_1(j) = ... 0.5*(((1 + bq(j))*parent_11(j)) + (1 - bq(j))*parent_22(j)); child_2(j) = ... 0.5*(((1 - bq(j))*parent_11(j)) + (1 + bq(j))*parent_22(j)); if child_1(j) > u_limit(j) child_1(j) = u_limit(j); elseif child_1(j) < l_limit(j) child_1(j) = l_limit(j); end if child_2(j) > u_limit(j) child_2(j) = u_limit(j); elseif child_2(j) < l_limit(j) child_2(j) = l_limit(j); end end parent_chromosome(parent_1,:)=child_1; parent_chromosome(parent_2,:)=child_2; end end for i = 1 : sizepop if rand(1) < pmutation(i)%变异算子 parent_3 = round(sizepop*rand(1)); if parent_3 < 1 parent_3 = 1; end child_3 = parent_chromosome(parent_3,:); for j = 1 : V r(j) = rand(1); if r(j) < 0.5%SBX (Simulated Binary Crossover)实值变异(多项式变异) delta(j) = (2*r(j))^(1/(mum+1)) - 1; else delta(j) = 1 - (2*(1 - r(j)))^(1/(mum+1)); end child_3(j) = child_3(j) + delta(j); if child_3(j) > u_limit(j) child_3(j) = u_limit(j); elseif child_3(j) < l_limit(j) child_3(j) = l_limit(j); end end parent_chromosome(parent_3,:)=child_3; end end f = parent_chromosome; end