matlab遗传算法学习和全局化算法Word文档格式.docx
《matlab遗传算法学习和全局化算法Word文档格式.docx》由会员分享,可在线阅读,更多相关《matlab遗传算法学习和全局化算法Word文档格式.docx(29页珍藏版)》请在冰豆网上搜索。
已知某一世物的总量y(单位:
万个)与时间t(月)之间的关系为y=k0(1-exp(-k1*t)),统计十个月获得数据见表1,试求关系式中的k0,k1。
先编写目标函数,并以文件名
存盘。
functiony=myfung(x)
TOT=[2.05673.69044.98816.01896.83717.48818.00478.41518.74119.0000];
t=1:
10;
[r,s]=size(TOT);
y=0;
fori=1:
s
y=y+(TOT(i)-x(:
1)*(1-exp(-x(:
2)*t(i))))^2%最小预计原则
end
翻开遗传算法的GUI,在Fitnessfunction窗格中输入@myfung,在Numberofvariables窗格中输入数字2,在Stoppingcriteria选项中设置generations为300,fitnesslimit为,stallgenerations为100,其余参数为缺省值,而后单击Start运转遗传算法获得,
,即
例子2
2matlab7GA工具箱_木子一车(转载)
例子1求fx1sin(4pix1)x2sin(20pix2)的最大值;
也就是求负函数的最小值
最大值为-38.8503,在点xmin=[11.62555.7250];
clear
f=@(x1,x2)(-(21.5+x1.*sin(4*pi*x1)+x2.*sin(20*pi*x2)))
t1=-3:
0.1:
12.1;
t2=4:
1.8/(length(t1)-1):
5.8;
[x,y]=meshgrid(t1,t2);
mesh(x,y,f(x,y))
方法1遗传算法
f=@(x)-(21.5+x
(1)*sin(4*pi*x
(1))+x
(2)*sin(20*pi*x
(2)));
opt1=gaoptimset;
opt1.PopInitRange=[[-3.04.1];
[12.15.8]];
opt1.PopulationSize=1000;
opt1.MutationFcn=@mutationuniform;
[x,fval]=ga(f,2,opt1)
[x,fval]=ga(f,2,[],[],[],[],[-3.0;
4.1],[12.1;
5.8]);
方法2gatool的用法
在matlab7命令行输入gatool,见附图。
在PopulationSize=10000;
请注意Mutation函数的选择。
f(x1*,x2*)=-my_func1(x1*,x2*)=38.84741978236206,
方法3全局优化算法
gs=GlobalSearch('
Display'
'
iter'
);
f=@(x)-(21.5+x
(1)*sin(4*pi*x
(1))+x
(2)*sin(20*pi*x
(2)));
opts=optimset('
Algorithm'
interior-point'
problem=createOptimProblem('
fmincon'
objective'
f,'
x0'
[1/21/3],'
lb'
[-3
4.1],'
ub'
[12.15.8],'
options'
opts);
[xming,fming,flagg,outptg,manyminsg]=run(gs,problem)
方法4multistart方法
ms=MultiStart('
TolFun'
1e-10,'
TolX'
1e-10);
opts=optimset('
'
problem=createOptimProblem('
[0,0],'
[-3,4.1],'
u
b'
[12.1,5.8],'
[xminm,fminm,flagm,outptm,someminsm]=run(ms,problem,300);
%stpoints=RandomStartPointSet;
%默认产生10个开端点
此方法得不到最优解;
查察局部解的散布范围enterhist([someminsm.Fval]).
方法4.1对上个方法的改良;
第一依据上个方法搜寻的最正确点,取此刻的方法的搜寻范围为上个最优解的四周地区,减小搜寻范围
ms=MultiStart;
opts=optimset('
f=@(x)(-(21.5+x
(1).*sin(4*pi*x
(1))+x
(2).*sin(20*pi*x
(2))));
problem=createOptimProblem('
[12,5],'
[10,4],'
ub
'
[xminm,fminm,flagm,outptm,manyminsm]=run(ms,problem,200)
xminm=
flagm=1
outptm=funcCount:
8660
localSolverTotal:
200
localSolverSuccess:
localSolverIncomplete:
0
localSolverNoSolution:
message:
[1x129char]
manyminsm=1x78GlobalOptimSolution
Properties:
X
Fval
Exitflag
Output
X0
方法
pts=-4*rand(200,2)+13*rand(200,2);
tpoints=CustomStartPointSet(pts);
rpts=RandomStartPointSet('
NumStartPoints'
200);
allpts={tpoints,rpts};
LargeScale'
off'
f=@(x)(-(21.5+x
(1).*sin(4*pi*x
(1))+x
(2).*sin(20*pi*x
(2))));
[12.1,5.6],'
[9,4],
[xmin,fmin,flag,outpt,allmins]=run(ms,problem,allpts)
3【问题】求f(x)=x+10*sin(5x)+7*cos(4x)的最大值,此中0<
=x<
=9
f=@(x)-(x+10*sin(5*x)+7*cos(4*x));
fplot(f,[09]);
[x,fv]=ga(f,[0;
9])
options=gaoptimset('
PopulationSize'
100)
[xfval]=ga(@fitnessfun,nvars,[],[],[],[],[],[lb],[ub],options);
x=ga(fitnessfcn,nvars,A,b,Aeq,beq,LB,UB,nonlcon,options);
nvars为变量数量,
5全局化算法(GlobalSearch)
5.1createOptimProblem
problem
=createOptimProblem('
solverNameParameterName
ParameterValue
...)
ParameterName/ValuePairs
Aeq
Matrix
for
linear
equality
constraints.
The
constraintshavetheform:
Aeqx=beq
Aineq
inequality
constraints.The
Aineqx≤bineq
beq
Vector
bineq
lb
Vectoroflowerbounds.
nonlcon
Functionhandletothenonlinearconstraint
function.Theconstraintfunctionmustaccepta
vectorxandreturntwovectors:
c,thenonlinear
constraints,andceq,thenonlinear
constraints.If
oneof
theseconstraint
functionsisempty,nonlconmustreturn[]forthat
function.
IftheGradConstroptionis'
on'
theninaddition
nonlconmustreturntwoadditionaloutputs,gradc
andgradceq.Thegradcparameterisamatrixwith
onecolumnforthegradientofeachconstraint,as
isgradceq.
Formoreinformation,seeConstraints.
objective
Functionhandletotheobjectivefunction.Forall
solvers
exceptlsqnonlinand
lsqcurvefit,
the
objectivefunctionmustacceptavectorxand
returnascalar.IftheGradObjoptionis'
then
theobjectivefunctionmustreturnasecondoutput,
avector,representingthegradientoftheobjective.
Forlsqnonlin,theobjectivefunctionmustaccepta
vectorx
andreturnavector.lsqnonlinsumsthe
squaresof
theobjective
functionvalues.For
lsqcurvefit,theobjectivefunctionmustaccepttwo
inputs,xandxdata,andreturnavector.
Formoreinformation,
seeComputingObjective
Functions.
options
Options
structure.Create
this
structure
with
optimset,orbyexportingfromtheOptimization
Tool.
Vectorofupperbounds.
x0
A
vector,
apotential
starting
pointfor
optimization.Givesthedimensionalityof
problem.
xdata
Vectorofdatapointsforlsqcurvefit.
ydata
wheresolveristhenameofyourlocalsolver:
?
ForGlobalSearch:
'
o
?
ForMultiStartthechoicesare:
fminunc'
lsqcurvefit'
lsqnonlin'
例子1f=@(x)(100*(x
(2)-x
(1)^2)^2+(1-x
(1))^2);
求最小值gs=GlobalSearch;
anonrosen=@(x)(100*(x
(2)-x
(1)^2)^2+(1-x
(1))^2);
randn(2,1),'
anonrosen,'
lb'
[-2;
-2],'
[2;
2],'
[x,fval,exitflag,output,solutions]=run(gs,problem)
例子2:
有拘束遗传优化
%sixmin=4x2–2.1x4+x6/3+xy–4y2+4y4.x1+2x2≥4.
gs=GlobalSearch;
sixmin=@(x)(4*x
(1)^2-2.1*x
(1)^4+x
(1)^6/3+x
(1)*x
(2)-4*x
(2)^2+4*x
(2)^4);
A=[-1,-2];
b=-4;
3],'
sixmin,'
Aineq'
A,'
bineq'
b,'
[x,fval,exitflag,output,solutions]=run(gs,problem)
例子3鉴于模拟退火算法minf(x)=(4-2.1*x1^2+x1^4/3)*x1^2+x1*x2+(-4+4*x2^2)*x2^2;
functiony=simple_objective(x)
y=(4-2.1*x
(1)^2+x
(1)^4/3)*x
(1)^2+x
(1)*x
(2)+(-4+4*x
(2)^2)*x
(2)^2;
ObjectiveFunction=@simple_objective;
X0=[0.50.5];
%Startingpoint
lb=[];
ub=[]
[x,fval,exitFlag,output]=simulannealbnd(ObjectiveFunction,X0,lb,ub)
参数化的最小
functiony=parameterized_objective(x,a,b,c)
y=(a-b*x
(1)^2+x
(1)^4/3)*x
(1)^2+x
(1)*x
(2)+(-c+c*x
(2)^2)*x
(2)^2;
a=4;
b=2.1;
c=4;
%defineconstantvaluesObjectiveFunction=@(x)parameterized_objective(x,a,b,c);
[x,fval]=simulannealbnd(ObjectiveFunction,X0)
例子4使用工具箱
Thisexampleminimizes
thefunction
fromRuntheSolver,
subjecttotheconstraint
x1+2x2
≥
4.Theobjectiveis
sixmin=4
x
2
–2.1x
4
+
6
xy–4y
x/3+
+4y.
sixmin=@(x)(4*x
(1)^2
-
2.1*x
(1)^4
+x
(1)^6/3
+x
(1)*x
(2)
-4*x
(2)^2+4*x
(2)^4);
A=[-1,-2];
b=-4;
1.Bestpractice:
runtheproblemtoverifythesetup.
Theproblemrunssuccessfully.
2.ChooseFile>
ExporttoWorkspaceandselectExportproblemandoptionsto
aMATLABstructurenamed
例子5多开端点优化(MultiStartGlobalOptimization))
ms=MultiStart;
xstart=randn(3,1);
options=optimset('
假定你想解决一个问题并假定局部解相邻0.01以内,而且函数值在函数精度以内;
求解时间少于2000s;
0.01,'
MaxTime'
2000);
[xmin,fmin,flag,outpt,allmins]=run(ms,problem,k);
%k为要使用的起点数量,k能够由RandomStartPointSet函数产生;
5.1RandomStartPointSetObjectforStartPoints
stpoints=RandomStartPointSet;
%默认产生10个开端点,假如想产生
stpoints=RandomStartPointSet('
40);
RunningasolverisnearlyidenticalforGlobalSearchandMultiStart.Theonly
differenceinsyntaxisMultiStarttakesanadditionalin