Matlab微分進化演算法及最佳化函數測試,matlab進化演算法

來源:互聯網
上載者:User

Matlab微分進化演算法及最佳化函數測試,matlab進化演算法

微分進化(Difference Evolution,DE)演算法是一種最佳化演算法,據稱其比GA(遺傳演算法)等更為優秀。

借鑒網上實現的DE演算法,用Matlab實現了對若干函數最佳化問題的解法,代碼如下:

function [] = de_testclear all;close all;clc;%解:X = [0, 0, ...]%nVar = 30%dims: [-30, 30]    function fitness = sphere(vals)        prod = vals .* vals;        fitness = sum(prod, 2);    end%f(x) = 1/4000 * sum^n_1(x_i)^2 - prod^n_1 * cos(x_i/sqrt(i)) + 1%f* = 0, x* = [0, 0, ...];%nVar = 30%dims: [-600, 600]    function fitness = griewank(vals)        [h w] = size(vals);        p1 = vals.^2;        p1 = 1/4000 .* sum(p1, 2);                t = sqrt([1:w]);        p2 = vals ./ repmat(t, h, 1);        p2 = cos(p2);        p2 = prod(p2, 2);        fitness = p1 - p2 + 1;    end%解: X = [1, 1, 1, ...]    function fitness = RosenBroek(vals)        [k n] = size(vals);        vals1 = vals(:, 1:n-1);        vals2 = vals(:, 2:n);        tmp = vals2 - vals1 .* vals1;        tmp = 100 * tmp .* tmp;        tmp1 = (vals1 - 1).^2;        fitness = sum(tmp, 2) + sum(tmp1, 2);    end%許多局部最小值,最優解:X = [0, 0], 固定2個變數    function fitness = Rastrigin (X)        v = X.^2 - 10 .* cos(2 * pi .* X) + 10;        fitness = sum(v, 2);        %fitness = 20 + X(:, 1).^2 + X(:, 2).^2 - 10 .* (cos(2 * pi * X(:, 1)) + cos(2 * pi * X(:, 2)));    end%參數popSize = 100; % 群規模F = 0.5;                    % 突變因子C = 0.9;                    % 交叉率iterTimes = 300;% 迭代次數Run = 4;        % 測試輪次id = [1:popSize];%結果bestFit = 1e5;  %設取最小值bestGene = [];for r = 1:Run  %輪次    Func = r;     % 測試適應度函數    switch Func        case 1            Func = @sphere;            Xmin = -100;            Xmax = 100;            nVar = 30;        case 2            Func = @griewank;            Xmin = -600;            Xmax = 600;            nVar = 2;        case 3            nVar = 2;            Func = @RosenBroek;            Xmin = -100;            Xmax = 100;        case 4            nVar = 2;            Func = @Rastrigin            Xmin = -5.12;            Xmax = 5.12;    end;    Func    tic;        %1.初始化種群    X = Xmin + rand(popSize, nVar) * Xmax;        %2.每輪迭代    for i = 1:iterTimes        X0 = X;        %             F = 2 * (1 - (i-1)/iterTimes);        %個體突變得到V        for j = 1:popSize            ids = id;            ids(j) = [];                        rids = randperm(popSize - 1, 3);            rids = ids(rids);            V = X(rids(1), :) + F * (X(rids(2), :) - X(rids(3), :));                        %對V(j, :)範圍檢查            ids = find(V < Xmin);            if length(ids) > 0                V(ids) = Xmin;            end;            ids = find(V > Xmax);            if length(ids) > 0                V(ids) = Xmax;            end;                        %對每個X和V的配對,進行交叉操作,結果存在U中            jrand = floor(rand() * nVar + 1);  %必交叉項,保證至少一個交叉            for n = 1:nVar                R1 = rand();                if (R1 < C || n == jrand)                    U = V;    %保留子代基因                else                    U = X(j, :);    %保留父代基因                end            end                        %在子代和父代間做選擇運算            if Func(U) < Func(X(j, :))                Tr = U;            else                Tr=X(j, :);            end            % 更新種群基因            X(j,:) = Tr;            %計算新的適應度            fitness = Func(X(j,:));                        %記錄全域最優解            if fitness < bestFit                bestFit = fitness;                bestGene = X(j, :);            end;        end  %結束個體更新    end    bestGene    bestFit    tocend end
運行結果如下:



Func = 


    @de_test/sphere




bestGene =


  Columns 1 through 15


   -7.5570  -11.9099    9.9957  -37.8403  -17.9445   -7.1438  -21.4304  -33.6260  -22.1812  -66.1438    0.9014  -39.6724   -0.3175  -56.4815   26.5422


  Columns 16 through 30


    6.5446  -31.9653   -9.3640  -37.1629  -23.7325    2.7271   -6.3413  -21.3204  -13.1450   28.7402  -28.8170  -22.6226   10.9031  -16.6128  -14.7637




bestFit =


   2.0583e+04  (從此結果看,演算法效果不佳)


時間已過 2.285354 秒。


Func = 


    @de_test/griewank




bestGene =


   1.0e-08 *


    0.1702    0.1424




bestFit =


     0


時間已過 3.337347 秒。


Func = 


    @de_test/RosenBroek




bestGene =


   1.0e-08 *


    0.1702    0.1424




bestFit =


     0


時間已過 1.747765 秒。


Func = 


    @de_test/Rastrigin




Func = 


    @de_test/Rastrigin




bestGene =


   1.0e-08 *


    0.1702    0.1424




bestFit =


     0


時間已過 1.403871 秒。


微分進化演算法 MATLAB來源程式

可否轉寄我一份 841492215@qq.com
非常感謝!
 
要在matlab上用PSO(粒子群最佳化)演算法某測試函數的最優解,但是不知道函數寫對沒有

F2=0有問題,應該是F2=1
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.