遗传算法Word格式文档下载.docx
《遗传算法Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《遗传算法Word格式文档下载.docx(17页珍藏版)》请在冰点文库上搜索。
(
一般取0.0001-0.1)随机地对个体中某个字符进行补运算(即若原来字符为0,则改为1;
若原来为1则改为0)。
具体的,针对每个个体的每个自负产生一个[0,1]区间上的均匀分布的随机数
,若
,则将该字符进行变异,否则不进行变异。
变异运算具有增加群体多样性的效果。
(7)终止反复进行(3)—(6)项工作,直至得到满意解。
终止的条件也可用事先规定进化的代数,一般取100—500。
二、算法框图
图1
三、算法程序
function[x,fval,exitFlag,output,population,scores]=ga(fun,nvars,Aineq,bineq,Aeq,beq,lb,ub,nonlcon,options)
%GAConstrainedoptimizationusinggeneticalgorithm.
%GAattemptstosolveproblemsoftheform:
%minF(X)subjectto:
A*X<
=B,Aeq*X=Beq(linearconstraints)
%XC(X)<
=0,Ceq(X)=0(nonlinearconstraints)
%LB<
=X<
=ub
%
%X=GA(FITNESSFCN,NVARS)findsalocalunconstrainedminimumXtothe
%FITNESSFCNusingGA.NVARSisthedimension(numberofdesign
%variables)oftheFITNESSFCN.FITNESSFCNacceptsavectorXofsize
%1-by-NVARS,andreturnsascalarevaluatedatX.
%X=GA(FITNESSFCN,NVARS,A,b)findsalocalminimumXtothefunction
%FITNESSFCN,subjecttothelinearinequalitiesA*X<
=B.Linear
%constraintsarenotsatisfiedwhenthePopulationTypeoptionissetto
%'
bitString'
or'
custom'
.Seethedocumentationfordetails.
%X=GA(FITNESSFCN,NVARS,A,b,Aeq,beq)findsalocalminimumXtothe
%functionFITNESSFCN,subjecttothelinearequalitiesAeq*X=beqas
%wellasA*X<
=B.(SetA=[]andB=[]ifnoinequalitiesexist.)Linear
%X=GA(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub)definesasetoflowerand
%upperboundsonthedesignvariables,X,sothatasolutionisfoundin
%therangelb<
=ub.Useemptymatricesforlbandubifnobounds
%exist.Setlb(i)=-InfifX(i)isunboundedbelow;
setub(i)=Infif
%X(i)isunboundedabove.Linearconstraintsarenotsatisfiedwhenthe
%PopulationTypeoptionissetto'
.Seethe
%documentationfordetails.
%X=GA(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub,NONLCON)subjectsthe
%minimizationtotheconstraintsdefinedinNONLCON.Thefunction
%NONLCONacceptsXandreturnsthevectorsCandCeq,representingthe
%nonlinearinequalitiesandequalitiesrespectively.GAminimizes
%FITNESSFCNsuchthatC(X)<
=0andCeq(X)=0.(Setlb=[]and/orub=[]if
%noboundsexist.)Nonlinearconstraintsarenotsatisfiedwhenthe
%X=GA(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub,NONLCON,options)minimizes
%withthedefaultoptimizationparametersreplacedbyvaluesinthe
%structureOPTIONS.OPTIONScanbecreatedwiththeGAOPTIMSETfunction.
%SeeGAOPTIMSETfordetails.
%X=GA(PROBLEM)findstheminimumforPROBLEM.PROBLEMisastructure
%thathasthefollowingfields:
%fitnessfcn:
<
Fitnessfunction>
%nvars:
Numberofdesignvariables>
%Aineq:
Amatrixforinequalityconstraints>
%bineq:
bvectorforinequalityconstraints>
%Aeq:
Aeqmatrixforequalityconstraints>
%beq:
beqvectorforequalityconstraints>
%lb:
LowerboundonX>
%ub:
UpperboundonX>
%nonlcon:
nonlinearconstraintfunction>
%options:
OptionsstructurecreatedwithGAOPTIMSET>
%rngstate:
Stateoftherandomnumbergenerator>
%[X,FVAL]=GA(FITNESSFCN,...)returnsFVAL,thevalueofthefitness
%functionFITNESSFCNatthesolutionX.
%[X,FVAL,EXITFLAG]=GA(FITNESSFCN,...)returnsEXITFLAGwhich
%describestheexitconditionofGA.PossiblevaluesofEXITFLAGandthe
%correspondingexitconditionsare
%1Averagechangeinvalueofthefitnessfunctionover
%options.StallGenLimitgenerationslessthanoptions.TolFunand
%constraintviolationlessthanoptions.TolCon.
%3Thevalueofthefitnessfunctiondidnotchangein
%options.StallGenLimitgenerationsandconstraintviolationless
%thanoptions.TolCon.
%4Magnitudeofstepsmallerthanmachineprecisionandconstraint
%violationlessthanoptions.TolCon.Thisexitconditionapplies
%onlytononlinearconstraints.
%5Fitnesslimitreachedandconstraintviolationlessthan
%options.TolCon.
%0Maximumnumberofgenerationsexceeded.
%-1Optimizationterminatedbytheoutputorplotfunction.
%-2Nofeasiblepointfound.
%-4Stalltimelimitexceeded.
%-5Timelimitexceeded.
%[X,FVAL,EXITFLAG,OUTPUT]=GA(FITNESSFCN,...)returnsa
%structureOUTPUTwiththefollowinginformation:
StateoftherandomnumbergeneratorbeforeGAstarted>
%generations:
Totalgenerations,excludingHybridFcniterations>
%funccount:
Totalfunctionevaluations>
%maxconstraint:
Maximumconstraintviolation>
ifany
%message:
GAterminationmessage>
%[X,FVAL,EXITFLAG,OUTPUT,POPULATION]=GA(FITNESSFCN,...)returnsthe
%finalPOPULATIONattermination.
%[X,FVAL,EXITFLAG,OUTPUT,POPULATION,SCORES]=GA(FITNESSFCN,...)returns
%theSCORESofthefinalPOPULATION.
%Example:
%Unconstrainedminimizationof'
rastriginsfcn'
fitnessfunctionof
%numberOfVariables=2
%x=ga(@rastriginsfcn,2)
%DisplayplottingfunctionswhileGAminimizes
%options=gaoptimset('
PlotFcns'
...
%{@gaplotbestf,@gaplotbestindiv,@gaplotexpectation,@gaplotstopping});
%[x,fval,exitflag,output]=ga(@rastriginsfcn,2,[],[],[],[],[],[],[],options)
%Anexamplewithinequalityconstraintsandlowerbounds
%A=[11;
-12;
21];
b=[2;
2;
3];
lb=zeros(2,1);
%%Usemutationfunctionwhichcanhandleconstraints
MutationFcn'
@mutationadaptfeasible);
%[x,fval,exitflag]=ga(@lincontest6,2,A,b,[],[],lb,[],[],options);
%FITNESSFCNcanalsobeananonymousfunction:
%x=ga(@(x)3*sin(x
(1))+exp(x
(2)),2)
%IfFITNESSFCNorNONLCONareparameterized,youcanuseanonymous
%functionstocapturetheproblem-dependentparameters.Supposeyouwant
%tominimizethefitnessgiveninthefunctionmyfit,subjecttothe
%nonlinearconstraintmyconstr,wherethesetwofunctionsare
%parameterizedbytheirsecondargumenta1anda2,respectively.Here
%myfitandmyconstrareMATLABfilefunctionssuchas
%functionf=myfit(x,a1)
%f=exp(x
(1))*(4*x
(1)^2+2*x
(2)^2+4*x
(1)*x
(2)+2*x
(2)+a1);
%and
%function[c,ceq]=myconstr(x,a2)
%c=[1.5+x
(1)*x
(2)-x
(1)-x
(2);
%-x
(1)*x
(2)-a2];
%%Nononlinearequalityconstraints:
%ceq=[];
%Tooptimizeforspecificvaluesofa1anda2,firstassignthevalues
%tothesetwoparameters.Thencreatetwoone-argumentanonymous
%functionsthatcapturethevaluesofa1anda2,andcallmyfitand
%myconstrwithtwoarguments.Finally,passtheseanonymousfunctionsto
%GA:
%a1=1;
a2=10;
%defineparametersfirst
%%Mutationfunctionforconstrainedminimization
%x=ga(@(x)myfit(x,a1),2,[],[],[],[],[],[],@(x)myconstr(x,a2),options)
%SeealsoGAOPTIMSET,FITNESSFUNCTION,GAOUTPUTFCNTEMPLATE,PATTERNSEARCH,@.
%Copyright2003-2010TheMathWorks,Inc.
%$Revision:
1.1.6.3$$Date:
2010/02/0822:
34:
19$
%Ifthefirstargisnotagaoptimset,thenit'
safitnessfunctionfollowedbyagenome
%length.Herewemakeagaoptimsetfromtheargs.
defaultopt=struct('
PopulationType'
'
doubleVector'
...
'
PopInitRange'
[0;
1],...
PopulationSize'
20,...
EliteCount'
2,...
CrossoverFraction'
0.8,...
MigrationDirection'
'
forward'
MigrationInterval'
20,...
MigrationFraction'
0.2,...
Generations'
100,...
TimeLimit'
inf,...
FitnessLimit'
-inf,...
StallGenLimit'
50,...
StallTimeLimit'
TolFun'
1e-6,...
TolCon'
InitialPopulation'
[],...
InitialScores'
[],...
InitialPenalty'
10,...
PenaltyFactor'
PlotInterval'
1,...
CreationFcn'
@gacreationuniform,...
FitnessScalingFcn'
@fitscalingrank,...
SelectionFcn'
@selectionstochunif,...
CrossoverFcn'
@crossoverscattered,...
{{@mutationgaussian11}},...
HybridFcn'
Display'
final'
OutputFcns'
Vectorized'
off'
UseParallel'
never'
);
%Checknumberofinputarguments
errmsg=nargchk(1,10,nargin);
if~isempty(errmsg)
error('
globaloptim:
ga:
numberOfInputs'
[errmsg,'
GArequiresatleast1inputargument.'
]);
end
%Ifjust'
defaults'
passedin,returnthedefaultoptionsinX
ifnargin==1&
&
nargout<
=1&
isequal(fun,'
)
x=defaultopt;
return
ifnargin<
10,options=[];
ifnargin<
9,nonlcon=[];
8,ub=[];
7,lb=[];
6,beq=[];
5,Aeq=[];
4,bineq=[];
3,Aineq=[];
end
%Isthirdargumentastructure
ifnargin==3&
isstruct(Aineq)%Oldsyntax
options=Aineq;
Aineq=[];
%Oneinputargumentisforproblemstructure
ifnargin==1
ifisa(fun,'
struct'
[fun,nvars,Aineq,bineq,Aeq,beq,lb,ub,nonlcon,rngstate,options]=separateOptimStruct(fun);
%Resettherandomnumbergenerators
resetDfltRng(rngstate);
else%Singlei