当前位置:   article > 正文

【GA】曲面极值寻优&GA-PSO

ga-pso

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(1x)2exp(x2(y+1)2)10(5xx3y5)exp(x2y2)31exp((x+1)2y2)
peaks

使用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]);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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');
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99

GA-PSO

Issue: Code

二进制编码的结构不能直接反映问题的固有结构,精度不高,个体长度大以及占用较多的内存。解决该问题的方法有:

  1. 动态编码,在保持串长不变的情况下减少搜索区域,当算法收敛到某局部最优时增加搜索精度,使得算法可以在全局最优点附近进行更加精细化的搜索。
  2. 对于问题变量时实向量的情况,可以直接采用实数编码
  3. 复数编码是为了描述和求解二维问题
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/415868
推荐阅读
相关标签
  

闽ICP备14008679号