Navigator
GA对peaks函数进行极值寻优
peaks
函数
f ( x , y ) = 3 ( 1 − x ) 2 exp ( − x 2 − ( y + 1 ) 2 ) − 10 ( x 5 − x 3 − y 5 ) exp ( − x 2 − y 2 ) − 1 3 exp ( − ( x + 1 ) 2 − y 2 ) f(x, y)=3(1-x)^2\exp(-x^2-(y+1)^2)-10(\frac{x}{5}-x^3-y^5)\exp(-x^2-y^2)-\frac{1}{3}\exp(-(x+1)^2-y^2) f(x,y)=3(1−x)2exp(−x2−(y+1)2)−10(5x−x3−y5)exp(−x2−y2)−31exp(−(x+1)2−y2)
使用GA Toolbox
进行寻优计算,对比fmincon
寻优结果
clc;
clear all;
close all;
peaks
problem = createOptimProblem('fmincon', 'objective', @(x) peaks(x(:, 1), x(:, 2)), 'nonlcon', @circularConstraint, ...
'x0', [-1, 1], 'lb', [-3, 3], 'ub', [3 3]);
% fmincon for the optimal values
[x1, f1] = fmincon(problem);
% GA for global optimal values
problem.solver = 'ga';
problem.fitnessfcn = problem.objective;
problem.nvars = 2;
problem.options = gaoptimset('PopInitRange', [-3; 3]);
GA-PSO寻优
PSO
在计算函数极值时,会出现早熟现象,导致求解函数极值存在较大的偏差,而遗传算法中对于函数采用了Select
, Mutation
, Cross
等算子操作,直接以目标函数作为搜索信息,可以提高PSO
算法的全局寻优能力,加快了算法的进化速度,且可以提高算法的收敛精度。
clc;
clear all;
close all;
%% initialize parameters
lenchrom = 3;
pc = 0.7; % probability of cross
pm = 0.3; % probability of mutation
% parameters of PSO
c1 = 1.49445;
c2 = 1.49445;
maxgen = 100;
popsize = 30;
% Velocity of update particles
Vmax = 1;
Vmin = -1;
% Population
popmax = 15;
popmin = 0;
% Range
bound = [popmin popmax; popmin popmax; popmin popmax];
% number of particles for optimizing
par_num = 3;
%% initial particles & velocity
for i=1:popsize
% generate a population randomly
pop(i, :) = 3*abs(rands(1, par_num));
V(i, :) = rands(1, par_num);
fitness(i) = func(pop(i, :));
end
% find the best chromosome
[bestfitness, bestindex] = min(fitness);
zbest = pop(bestindex, :);
gbest = pop;
fitnessgbest = fitness;
fitnesszbest = bestfitness;
% find the optimal value
for i=1:maxgen
i
for j = 1:popsize
% Velocity: PSO
V(j, :) = V(j, :)+c1*rand*(gbest(j, :)-pop(j, :))+c2*rand*(zbest- pop(j, :));
V(j, find(V(j, :)>Vmax)) = Vmax;
V(j, find(V(j, :)<Vmin)) = Vmin;
% Population: PSO
pop(j, :) = pop(j, :)+0.5*V(j, :);
pop(j, find(pop(j, :)>popmax)) = popmax;
pop(j, find(pop(j, :)<popmin)) = popmin;
% Cross: GA
GApop = Cross(pc, lenchrom, pop, popsize, bound);
% Mutation: GA
GApop = Mutation(pm, lenchrom, GApop, popsize, [i maxgen], bound);
pop = GApop;
% fitness --> constraint
if pop(j, 1)-pop(j, 2)+pop(j, 3)<=20
if 3*pop(j, 1)+2*pop(j, 2)+4*pop(j, 3)<=42
if 3*pop(j, 1)+2*pop(j, 2)<=30
fitness(j) = func(pop(j, :));
end
end
end
% update the individual optimal value
if fitness(j) < fitnessgbest(j)
gbest(j, :) = pop(j, :);
fitnessgbest(j) = fitness(j);
end
% update the population optimal value
if fitness(j) < fitnesszbest
zbest = pop(j, :);
fitnesszbest = fitness(j);
end
end
yy(i) = fitnesszbest;
end
%% results & plot
disp '*********************best particle number**********************'
zbest
plot(yy, 'linewidth', 1.5);
grid on;
title(['Fitness Curve ' 'Generations of Stopping: ' num2str(maxgen)]);
xlabel('Generation of evolution');
ylabel('Fitness');
Issue: Code
二进制编码的结构不能直接反映问题的固有结构,精度不高,个体长度大以及占用较多的内存。解决该问题的方法有:
- 动态编码,在保持串长不变的情况下减少搜索区域,当算法收敛到某局部最优时增加搜索精度,使得算法可以在全局最优点附近进行更加精细化的搜索。
- 对于问题变量时实向量的情况,可以直接采用实数编码
- 复数编码是为了描述和求解二维问题