基於Matlab用遺傳算法求一元函數最值問題(附源碼)


問題:求y=10cos(5xx)+7sin(x-5)+10xx的最小值
要求:(1)用遺傳算法編程求解問題
(2)編程語言用MATLAB 或C
(3)輸出問題的最優解及最大值,並繪圖顯示

方法一

function.m

clear all;
close all;
clc;
x=-1:0.01:0;
y=10.*cos(5.*x.*x)+7.*sin(x-5.0)+10.*x.*x;
figure
plot(x,y)
grid on
xlabel('x')
ylabel('f(x)')
title('f(x)=10*cos(5*x*x)+7*sin(x-5)+10*x*x')
%%f(x)=10*cos(5*x*x)+7*sin(x-5)+10*x*x

1)運行結果
函數取(-1,0)定義域,能夠顯示出的X=-0.7733時,Y=-0.4888,圖像如下

方法二

func.m

clear all;
close all;
clc;
x=-1:0.01:0;
y=10.*cos(5.*x.*x)+7.*sin(x-5.0)+10.*x.*x;
figure
plot(x,y)
grid on
xlabel('x')
ylabel('f(x)')
title('f(x)=10*cos(5*x*x)+7*sin(x-5)+10*x*x')
%%f(x)=10*cos(5*x*x)+7*sin(x-5)+10*x*x

main.m

clear all;      %清除所有變量
close all;      %清圖
clc;            %清屏
nvars = 1;
LB = -1;
UB = 0;
[t,fval] =ga(@test,1,[],[],[],[],LB,UB)

fplot(@(x)(10.*cos(5.*x.*x)+7.*sin(x-5)+10.*x.*x),[-1 0]);
hold on;
plot(t,fval,'*');
function y = test(x)
y = 10*cos(5*x*x)+7*sin(x-5)+10*x*x
end

simple_fitness.m

%目標函數
x = -1:0.01:0;
%y=10*cos(5*x*x)+7*sin(x-5)+10*x*x;
y=10.*cos(5.*x.*x)+7.*sin(x-5)+10.*x.*x;
plot(x,y);
%%%%%%%%%%%%%%%初始化參數%%%%%%%%%%%%%%%
clear all;      %清除所有變量
close all;      %清圖
clc;            %清屏
NP=50;          %種群規模(數量)
L = 20;         %二進制位串長度
Pc = 0.8;       %交叉率
Pm = 0.1;       %變異率
G = 100;        %最大遺傳代數
Xs = 1;        %上限
Xx = -0;         %下限
f = randi([0,1],NP,L);%隨機獲得初始種群
xB =[];
%%%%%%%%%%%%%%%遺傳算法循環%%%%%%%%%%%%%%%
for k = 1:G
    %%%%%%%%%%%%%%%將二進制解碼為定義域范圍內十進制%%%%%%%%%%%%%%%
    for i = 1:NP
        U = f(i,:);
        m = 0;
        for j = 1:L
            m = U(j)*2^(j-1)+m;
        end
        x(i) = Xx+m*(Xs-Xx)/(2^L-1);
        Fit(i) = 1/func1(x(i));
    end
    maxFit = max(Fit);
    minFit = min(Fit);
    rr = find(Fit==maxFit);
    fBest = f(rr(1,1),:);
    xBest = x(rr(1,1));
    xB(i)=xBest;
    Fit = (Fit-minFit)/(maxFit-minFit);
    %%%%%%%%%%%%%%%基於輪盤賭的復制操作%%%%%%%%%%%%%%%
    sum_Fit = sum(Fit);
    fitvalue = Fit./sum_Fit;
    fitvalue = cumsum(fitvalue);
    ms = sort(rand(NP,1));
    fiti = 1;
    newi = 1;
    while newi <= NP
        if (ms(newi)) < fitvalue(fiti)
            nf(newi,:) = f(fiti,:);
            newi = newi + 1;
        else
            fiti = fiti+1;
        end
    end
    %%%%%%%%%%%%%%%基於概率的交叉操作%%%%%%%%%%%%%%%
    for i=1:2:NP
        p = rand;
        if p < Pc
            q = randi(1,1,L);
            for j = 1:L
                if q(j)==1;
                    temp = nf(i+1,j);
                    nf(i+1,j) = nf(i,j);
                    nf(i,j) = temp;
                end
            end
        end
    end
    %%%%%%%%%%%%%%%基於概率的變異操作%%%%%%%%%%%%%%%
    i= 1;
    while i<= round(NP*Pm)
        h = randi([1,NP]);
        for j = 1:round(L*Pm)
            g = randi([1,L]);
            nf(h,g) =~ nf(h,g);
        end
        i=i+1;
    end
    f=nf;
    f(1,:) = fBest;
    trace(k) = maxFit;
end
xBest;
fBestt=func1(xBest);
subplot(1,2,1)
plot(trace)
xlabel('迭代次數')
ylabel('目標函數值')
title('適應度進化曲線')
subplot(1,2,2)
fplot(@(x)(10.*cos(5.*x.*x)+7.*sin(x-5)+10.*x.*x),[-1 0]);
hold on;
plot(xBest,func1(xBest),'*');
%%%%%%%%%%%%%%%適應度函數%%%%%%%%%%%%%%%
function result = func1(x)
%fit = x+10*sin(5*x)+7*cos(4*x);
fit = 10.*cos(5.*x.*x)+7.*sin(x-5)+10.*x.*x;
result = fit;
end

1)運行結果

                                 適應度曲線

                                  函數圖像


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM