版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37360631/article/details/86607215
1.遗传算法
以下方法求得是区间范围内的最小值。
要想求得区间范围内的最大值,只需要在lbw.m中将f的变化为-f。
main.m
clear all
clc
%设置遗传算法参数
options=gaoptimset('Generations',1500,'StallGenLimit',300,'PlotFcns',@gaplotbestf);
[x,f]=ga(@lbw,10,options) %10是变量的个数
lbw.m
%目标函数定义
function f=lbw(x)
if x(1)>30|x(1)<-30 x(2)>30|x(2)<-30|x(3)>30|x(3)<-30|x(4)>30|x(4)<-30|x(5)>30|x(5)<-30 |x(6)>30|x(6)<-30| x(7)>30|x(7)<-30 |x(8)>30|x(8)<-30 |x(9)>30|x(9)<-30 |x(10)>30|x(10)<-30
f=300;
else
f=-2*pi*exp(-0.2*sqrt((x(1)^2+x(2)^2+x(3)^2+x(4)^2+x(5)^2+x(6)^2+x(7)^2+x(8)^2+x(9)^2+x(10)^2)/10))-exp(0.1*(cos(2*pi*x(1))+cos(2*pi*x(2))+cos(2*pi*x(3))+cos(2*pi*x(4))+cos(2*pi*x(5))+cos(2*pi*x(6))+cos(2*pi*x(7))+cos(2*pi*x(8))+cos(2*pi*x(9))+cos(2*pi*x(10))))+2*pi;
end