📄 init.asv
字号:
function [ x q ] = Init(PopSize)%UNTITLED1 Summary of this function goes here% Detailed explanation goes herex = rand(PopSize, 3);a = 0.05;q(1) = 0.05;for i=2:PopSize+1 a = a*0.95; q(i) = q(i-1)+a;end;for i=2:PopSize+1 while x(i,1)<0 || x(i,2)<0 || x(i,3)<0 || power(x(i,1),2)+2*power(x(i,2),2)+3*power(x(i,3),2) > 1 x(i,:) = rand(1,3); endend%目标函数function [Objective] = objective_function(PopSize, chromosome)for i=2:PopSize+1 x1 = chromosome(i,1); x2 = chromosome(i,2); x3 = chromosome(i,3); Objective(i,2) = sqrt(x1)+sqrt(x2)+sqrt(x3); endfor i=2:PopSize+1 Objective(i,1) = max(Objective(i,:));end%评估函数function evaluation(gen, PopSize, chromosome)Objective = Objective_function(PopSize, chromosome);if gen == 0 Objective(1,1) = Objective(2,1); chromosome(1,:) = chromosome(2,:);endfor i=1:PopSize label = 1; a = Objective(i,1); for j=i+1:PopSize+1 if a < Objective(j,1) a = Objective(j,1); label = j; end end if lable ~= 1 a = Objective(i,1) endend%选择函数function selection(PopSize, chromosome)for i=2:PopSize+1 r = rand(1,1)*q(PopSize); for j=2:PopSize+1 if r <= q(j) temp = chromosome(j,:); break; end end for i=2:PopSize+1 chromosome(i,:) = temp(i,:); endend%交叉函数function crossover(PopSize, P_Crossover, chromosome)pop = PopSize/2;for i=2:pop+1 if rand() > P_Crossover continue; end j = round(rand()*PopSize); jj = round(rand()*PopSize); r = rand(); x = r*chromosome(j,:)+(1-r)*chromosome(jj,:); y = r*chromosome(jj,:)+(1-r)*chromosome(j,:); if power(x(i,1),2)+2*power(x(i,2),2)+3*power(x(i,3),2) <= 1 chromosome(j,:) = x; elseif power(y(i,1),2)+2*power(y(i,2),2)+3*power(y(i,3),2) <= 1 chromosome(jj,:) = y; endend%变异函数function mutation(PopSize, P_Mutation, chromosome)INFTY = 10;precision = 0.0001;for i=2:PopSize+1 if rand() > P_Mutation continue; end x = chromosome(i,:); for k=1:3 if rand() < 0.5 direction(k) = rand()*2-1; else direction(k) = 0; end end infty = rand()*INFTY; while infty > precision for j=1:3 y(j) = x(j)+infty*direction(j); end if power(y(i,1),2)+2*power(y(i,2),2)+3*power(y(i,3),2) <= 1 chromosome(i,:) = y; break; end infty = rand()*infty; endend%主函数function Main(PopSize, GEN, P_Crossover, P_Mutation)[x q] = Init(PopSize);evaluation(0, PopSize, x);for i=1:GEN selection(PopSize, x); crossover(PopSize, P_Crossover, x); mutation(PopSize, P_Mutation, x); evaluation(i, PopSize, x);end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -