1. 程式人生 > >Matlab遺傳演算法優化問題求解的示例程式碼

Matlab遺傳演算法優化問題求解的示例程式碼

程式碼如下:

function m_main()
clear
clc
Max_gen = 100;% 執行代數
pop_size = 100;%種群大小
chromsome = 10;%染色體的長度
pc = 0.9;%交叉概率
pm = 0.25;%變異概率
gen = 0;%統計代數


%初始化
init = 40*rand(pop_size, chromsome)-20;
pop = init;
fit = obj_fitness(pop);
[max_fit, index_max] = max(fit);
maxfit = max_fit;
[min_fit, index_min] = min(fit);
best_indiv = pop(index_max, :);
%迭代操作
while gen<Max_gen
    gen = gen+1;  
    bt(gen) = max_fit;
    if  maxfit<max_fit;
        maxfit = max_fit;
        pop(index_min, :) = pop(index_max, :);
        best_indiv = pop(index_max, :);
    end
    best_indiv_tmp(gen) = pop(index_max);
    newpop = ga(pop, pc, pm, chromsome, fit);
    fit = obj_fitness(newpop);
    [max_fit, index_max] = max(fit);
    [min_fit, index_min] = min(fit);
    pop = newpop;
    trace(1, gen) = max_fit;
    trace(2, gen) = sum(fit)./length(fit);
end


%執行結果
[f_max gen_ct] = max(bt)%求的最大值以及代數
maxfit
best_indiv
%畫圖
% bt
hold on
plot(trace(1, :), '.g:');
plot( trace(2, :), '.r-');
title('實驗結果圖')
xlabel('迭代次數/代'), ylabel('最佳適應度(最大值)');%座標標註
plot(gen_ct-1, 0:0.1:f_max+1, 'c-');%畫出最大值
text(gen_ct, f_max+1,   '最大值')
hold off


    function  [fitness] = obj_fitness(pop)
        %適應度計算函式
        [r c] = size(pop);
        x = pop;
        fitness = zeros(r, 1);
        for i = 1:r
            for j = 1:c
                fitness(i, 1) = fitness(i, 1)+sin(sqrt(abs(40*x(i))))+1-abs(x(i))/20.0;
            end
        end
    end


    function newpop = ga(pop, pc, pm, chromsome, fit)
        pop_size = size(pop, 1);
        %輪盤賭選擇
        ps = fit/sum(fit);
        pscum = cumsum(ps);%size(pscum)
        r = rand(1, pop_size);
        qw = pscum*ones(1, pop_size);
        selected = sum(pscum*ones(1, pop_size)<ones(pop_size, 1)*r)+1;
        newpop = pop(selected, :);
        %交叉
        if pop_size/2 ~= 0
            pop_size = pop_size-1;
        end        
        for i = 1:2:pop_size-1
            while pc>rand
                c_pt = round(8*rand+1);
                pop_tp1 = newpop(i, :);pop_tp2 = newpop(i+1, :);
                newpop(i+1, 1:c_pt) = pop_tp1(1, 1:c_pt);
                newpop(i, c_pt+1:chromsome) = pop_tp2(1, c_pt+1:chromsome);
            end
            
        end
        % 變異
        for i = 1:pop_size
            if pm>rand
                m_pt = 1+round(9*rand);
                newpop(i, m_pt) = 40*rand-20;
            end
        end
    end
end