【Matlab学习手记】离散差分进化法求最小值

xiaoxiao2021-03-01  57

求函数f(x,y)=-((x^2+y-1)+(x+y^2-7)^2)/200+10的最大值,x和y取值区间[-100,100];经过下面的优化过程,得到结果:x=-2,y=-3,最大值为10。

原理

源码 %%%%%%%%%%%%% 离散差分进化算法求函数极值 %%%%%%%%%%%%% %%%%%%%%%%%%% 初始化 %%%%%%%%%%%%% clear;clc; NP = 20; % 种群数量 D = 2; % 变量维度 G = 200; % 最大进化代数 F = 0.5; % 初始编译算子 CR = 0.1; % 交叉算子 Xs = 100; % 上限 Xx = -100; % 下限 %%%%%%%%%%%%% 赋初值 %%%%%%%%%%%%% Ob = zeros(NP, 1); Ob1 = zeros(NP, 1); trace = zeros(G + 1, 1); v = zeros(D, NP); % 变异种群 u = zeros(D, NP); % 选择种群 x = randi([Xx, Xs], D, NP); %%%%%%%%%%%%% 计算目标函数 %%%%%%%%%%%%% for m = 1:NP Ob(m) = func3(x(:, m)); end trace(1) = min(Ob); %%%%%%%%%%%%% 差分进化循环 %%%%%%%%%%%%% for gen = 1:G %%%%%%%%%%%%% 变异操作 %%%%%%%%%%%%% %%%%%%%%%% r1、r2、r3和m互不相同 %%%%%%%%%% for m = 1:NP r1 = randi([1, NP], 1); while r1 == m r1 = randi([1, NP], 1); end r2 = randi([1, NP], 1); while r2 == m || r2 == r1 r2 = randi([1, NP], 1); end r3 = randi([1, NP], 1); while r3 == m || r3 == r2 || r3 == r1 r3 = randi([1, NP], 1); end v(:, m) = floor(x(:, r1) + F*(x(:, r2) - x(:, r3))); end %%%%%%%%%%%%% 交叉操作 %%%%%%%%%%%%% r = randi([1, D], 1); for n = 1:D cr = rand; if cr <= CR || n == r u(n, :) = v(n, :); else u(n, :) = x(n, :); end end %%%%%%%%%%%%% 边界条件的处理 %%%%%%%%%%%%% %%%%%%%%%%%%% 边界吸收 %%%%%%%%%%%%% for n = 1:D for m = 1:NP if u(n, m) < Xx u(n, m) = Xx; end if u(n, m) > Xs u(n, m) = Xs; end end end %%%%%%%%%%%%% 选择操作 %%%%%%%%%%%%% for m = 1:NP Ob1(m) = func3(u(:, m)); end for m = 1:NP if Ob1(m) > Ob(m) x(:, m) = u(:, m); end end for m = 1:NP Ob(m) = func3(x(:, m)); end trace(gen + 1) = min(Ob); end [SortOb, Index] = sort(Ob); x = x(:, Index); X = x(:, 1); % 最优变量 Y = min(Ob); % 最优值 disp(['最优x:' num2str(X')]); disp(['最优y:' num2str(Y)]); plot(trace); xlabel('迭代次数'); ylabel('目标函数值'); title('DE目标函数曲线'); %%%%%%%%%%%%% 适应度函数 %%%%%%%%%%%%% function result = func3(x) result = -((x(1).^2 + x(2) - 1).^2 + (x(1) + x(2).^2 - 7).^2)/200 + 10;

 

转载请注明原文地址: https://www.6miu.com/read-4149977.html

最新回复(0)