matlab遗传算法学习和全局化算法精品毕业设计完整版.docx
《matlab遗传算法学习和全局化算法精品毕业设计完整版.docx》由会员分享,可在线阅读,更多相关《matlab遗传算法学习和全局化算法精品毕业设计完整版.docx(22页珍藏版)》请在冰豆网上搜索。
matlab遗传算法学习和全局化算法精品毕业设计完整版
1遗传算法步骤
1根据具体问题选择编码方式,随机产生初始种群,个体数目一定,每个个体表现为染色体的基因编码
2选择合适的适应度函数,计算并评价群体中各个体的适应。
3选择(selection)。
根据各个个体的适应度,按照一定的规则或方法,从当前群体中选择出一些优良的个体遗传到下一代群体
4交叉(crossover)。
将选择过后的群体内的各个个体随机搭配成对,对每一对个体,以一定概率(交叉概率)交换它们中的部分基因。
5变异(mutation)。
对交叉过后的群体中的每一个个体,以某个概率(称为变异概率)改n变某一个或某一些基因位上的基因值为其他的等位基因
6终止条件判断。
若满足终止条件,则以进化过程中得到的具有最大适应度的个体作为最优解输出,终止运算。
否则,迭代执行Step2至Step5。
适应度是评价群体中染色体个体好坏的标准,是算法进化的驱动力,是自然选择的唯一依据,改变种群结构的操作皆通过适应度函数来控制。
在遗传算法中,以个体适应度的大小来确定该个体被遗传到下一代群体中的概率。
个体的适应度越大,被遗传到下一代的概率就越大,相反,被遗传到下一代的概率就越小。
1[a,b,c]=gaopt(bound,fun)其中,bound=[xm,xM]为求解区间上届和下届构成的矩阵。
Fun为用户编写的函数。
a为搜索的结果向量,由搜索的出的最优x向量与目标函数构成,b为最终搜索种群,c为中间搜索过程变参数,其第一列为代数,后边列分别为该代最好的的个体与目标函数的值,可以认为寻优的中间结果。
2ga函数。
[X,F,FLAG,OUTPUT]=GA(fun,n,opts).n为自变量个数,opts为遗传算法控制选项,用gaoptimset()函数设置各种选项,InitialPopulation可以设置初始种群,用PopulationSize可以设置种群规模,SelectionFcn可以定义选择函数,
3gatool函数用于打开,GATOOLisnowincludedinOPTIMTOOL。
2.2通过GUI使用遗传算法
在Matlab工作窗口键入下列命令>>gatool,或通过Start打开其下子菜单GeneticAlgorithmTool,如图1。
只要在相应的窗格选择相应的选项便可进行遗传算法的计算。
其中fitnessfun窗格为适应度函数,填写形式为@fitnessfun,Numberofvariable窗格为变量个数。
其它窗格参数根据情况填入。
填好各窗格内容,单击Start按钮,便可运行遗传算法
例子1应用实例
已知某一生物的总量y(单位:
万个)与时间t(月)之间的关系为y=k0(1-exp(-k1*t)),
统计十个月得到数据见表1,试求关系式中的k0,k1。
先编写目标函数,并以文件名myfung.m存盘。
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为0.001,stallgenerations为100,其它参数为缺省值,然后单击Start运行遗传算法得到k0=9.99559,k1=0.23018,即
例子2
2matlab7GA工具箱_木子一车(转载)
例子1求
的最大值;也就是求负函数的最小值
最大值为-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,
wherex1*=11.62378;x2*=5.72501
方法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',[-34.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('Algorithm','interior-point');
f=@(x)-(21.5+x
(1)*sin(4*pi*x
(1))+x
(2)*sin(20*pi*x
(2)));
problem=createOptimProblem('fmincon','x0',[0,0],'objective',f,'lb',[-3,4.1],'ub',[12.1,5.8],'options',opts);
[xminm,fminm,flagm,outptm,someminsm]=run(ms,problem,300);
%stpoints=RandomStartPointSet;%默认产生10个起始点
此方法得不到最优解;
查看局部解的分布范围enterhist([someminsm.Fval]).
方法4.1对上个方法的改进;首先根据上个方法搜索的最佳点,取现在的方法的搜索范围为上个最优解的周围区域,缩小搜索范围
clear
ms=MultiStart;
opts=optimset('Algorithm','interior-point');
f=@(x)(-(21.5+x
(1).*sin(4*pi*x
(1))+x
(2).*sin(20*pi*x
(2))));
problem=createOptimProblem('fmincon','x0',[12,5],'objective',f,'lb',[10,4],'ub',[12.1,5.8],'options',opts);
[xminm,fminm,flagm,outptm,manyminsm]=run(ms,problem,200)
xminm=11.62555.7250
fminm=-38.8503
flagm=1
outptm=funcCount:
8660
localSolverTotal:
200
localSolverSuccess:
200
localSolverIncomplete:
0
localSolverNoSolution:
0
message:
[1x129char]
manyminsm=1x78GlobalOptimSolution
Properties:
X
Fval
Exitflag
Output
X0
方法4.2
pts=-4*rand(200,2)+13*rand(200,2);
tpoints=CustomStartPointSet(pts);
rpts=RandomStartPointSet('NumStartPoints',200);
allpts={tpoints,rpts};
ms=MultiStart;
opts=optimset('Algorithm','interior-point','LargeScale','off');
f=@(x)(-(21.5+x
(1).*sin(4*pi*x
(1))+x
(2).*sin(20*pi*x
(2))));
problem=createOptimProblem('fmincon','x0',[12.1,5.6],'objective',f,'lb',[9,4],'ub',[12.1,5.8],'options',opts);
[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('solverName','ParameterName',ParameterValue,...)
ParameterName/ValuePairs
Aeq
Matrixforlinearequalityconstraints.Theconstraintshavetheform:
Aeqx = beq
Aineq
Matrixforlinearinequalityconstraints.Theconstraintshavetheform:
Aineqx ≤ bineq
beq
Vectorforlinearequalityconstraints.Theconstraintshavetheform:
Aeqx = beq
bineq
Vectorforlinearinequalityconstraints.Theconstraintshavetheform:
Aineqx ≤ bineq
lb
Vectoroflowerbounds.
nonlcon
Functionhandletothenonlinearconstraintfunction.Theconstraintfunctionmustacceptavectorxandreturntwovectors:
c,thenonlinearinequalityconstraints,andceq,thenonlinearequalityconstraints.Ifoneoftheseconstraintfunctionsisempty,nonlconmustreturn[]forthatfunction.
IftheGradConstroptionis'on',theninadditionnonlconmustreturntwoadditionaloutputs,gradcandgradceq.Thegradcparameterisamatrixwithonecolumnforthegradientofeachconstraint,asisgradceq.
Formoreinformation,seeConstraints.
objective
Functionhandletotheobjectivefunction.Forallsolversexceptlsqnonlinandlsqcurvefit,theobjectivefunctionmustacceptavectorxandreturnascalar.IftheGradObjoptionis'on',thentheobjectivefunctionmustreturnasecondoutput,avector,representingthegradientoftheobjective.Forlsqnonlin,theobjectivefunctionmustacceptavectorxandreturnavector.lsqnonlinsumsthesquaresoftheobjectivefunctionvalues.Forlsqcurvefit,theobjectivefunctionmustaccepttwoinputs,xandxdata,andreturnavector.
Formoreinformation,seeComputingObjectiveFunctions.
options
Optionsstructure.Createthisstructurewithoptimset,orbyexportingfromtheOptimizationTool.
ub
Vectorofupperbounds.
x0
Avector,apotentialstartingpointfortheoptimization.Givesthedimensionalityoftheproblem.
xdata
Vectorofdatapointsforlsqcurvefit.
ydata
Vectorofdatapointsforlsqcurvefit.
wheresolveristhenameofyourlocalsolver:
o
•ForGlobalSearch:
'fmincon'
•ForMultiStartthechoicesare:
o'fmincon'
o'fminunc'
o'lsqcurvefit'
o'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);
opts=optimset('Algorithm','interior-point');
problem=createOptimProblem('fmincon','x0',randn(2,1),'objective',anonrosen,'lb',[-2;-2],'ub',[2;2],'options',opts);
[x,fval,exitflag,output,solutions]=run(gs,problem)
ansx=[1.0000;1.0000];fval=2.3801e-011
例子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;
opts=optimset('Algorithm','interior-point');
problem=createOptimProblem('fmincon','x0',[2;3],'objective',sixmin,'Aineq',A,'bineq',b,'options',opts);
[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)
3.2参数化的最小
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;%defineconstantvalues
ObjectiveFunction=@(x)parameterized_objective(x,a,b,c);
X0=[0.50.5];
[x,fval]=simulannealbnd(ObjectiveFunction,X0)
例子4使用工具箱
ThisexampleminimizesthefunctionfromRuntheSolver,subjecttotheconstraintx1+2x2 ≥ 4.Theobjectiveis
sixmin=4x2–2.1x4+x6/3+xy–4y2+4y4.
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>ExporttoWorkspaceandselectExportproblemandoptionstoaMATLABstructurenamed
例子5多起始点优化(MultiStartGlobalOptimization))
gs=GlobalSearch;
ms=MultiStart;
xstart=randn(3,1);
options=optimset('Algorithm','interior-point');
假设你想解决一个问题并假设局部解相邻0.01之内,并且函数值在函数精度之内;求解时间少于2000s;
gs=GlobalSearch('TolX',0.01,'MaxTime',2000);
gs=GlobalSearch;
ms=MultiStart;
[xmin,fmin,flag,outpt,allmins]=run(ms,problem,k);%k为要使用的起点数目,k可以由RandomStartPointSet函数产生;
5.1RandomStartPointSetObjectforStartPoints
stpoints=RandomStartPointSet;%默认产生10个起始点,如果想产生
stpoints=RandomStartPointSet('NumStartPoints',40);
RunningasolverisnearlyidenticalforGlobalSearchandMultiStart.TheonlydifferenceinsyntaxisMultiStarttakesanadditionalinputdescribingthestartpoints.
startpts=RandomStartPointSet('ArtificialBound',100,'NumStartPoints',50);
[xfvaleflagoutputmanymins]=run(ms,problem,startpts)
5.2CustomStartPointSetObjectforStartPoints
Touseaspecificsetofstartingpoints,packagetheminaCustomStartPointSetasfollows:
Placethestartingpointsinamatrix.Eachrowofthematrixrepresentsonestartingpoint.MultiStartrunsalltherowsofthematrix,subjecttofilteringwiththeStartPointsToRunproperty.Formoreinformation,seeMultiStartAlgorithm.
CreateaCustomStartPointSetobjectfromthematrix:
tpoints=CustomStartPointSet(ptmatrix);