sga.m

来自「基本遗传算法的matlab源程序」· M 代码 · 共 48 行

M
48
字号
function [indivBest,fitValue]=sga(eevalFN,bounds,sizePop,accuracy,generMax,proCro,proMut)
%  running a genetic algorithm
%  syntax:[indivBest,fitValue]=sga(eevalFN,bounds,sizePop,accuracy,generMax,proCro,proMut)
%
%  Output Arguments:
%      indivBest  ---- the best solution found during the course of the run
%      fitValue   ---- the fitness value corresponding to indivBest
%  Input Arguments:
%      eevalFN    ---- the expression of fitness function
%      bounds     ---- a matrix of upper and lower bounds on the variables
%      sizePop    ---- the population size
%      accuracy   ---- the calculation accuracy
%      generMax   ---- the largest genetic generation
%      proCro     ---- the probability of cross-gene
%      proMut     ---- the probability of gene mutation
%
%  Author:Yan Anxin
%  ID number:081810
%  Date:finished on 2008.11.29
%  This program is free software; you can redistribute it and/or modify it
%  Yax235 DreamWorks, SEE, SEU, 2# Sipailou Nanjing, 210096, P.R.China 

[varNum,]=size(bounds);
x_vector=sym([]);
for xk=1:varNum            %definition of variables
    x_vector(xk)=sym(['x' num2str(xk)]);    
end
eevalFN=sym(eevalFN);      %access to fitness function

[geneCod,genek_bitnum]=createPop(bounds,sizePop,accuracy);%initial generation
indiv=bstr2rval(geneCod,genek_bitnum,bounds);             %initial individual
eval=indiEval(eevalFN,x_vector,indiv);                    %initial fitness eval
generation=1;
while (generation~=generMax)                              %current generation
    geneCodSel=selectOper(geneCod,indiv,eval,bounds);
    geneCodCro=crossOper(geneCodSel,proCro);
    geneCodMut=mutateOper(geneCodCro,proMut);
    indivMut=bstr2rval(geneCodMut,genek_bitnum,bounds);
    evalMut=indiEval(eevalFN,x_vector,indivMut);
    geneCod=geneCodMut;indiv=indivMut;eval=evalMut;       %update generation
    generation=generation+1;
end
geneCodSel=selectOper(geneCod,indiv,eval,bounds);
indiv=bstr2rval(geneCodSel,genek_bitnum,bounds);
eval=indiEval(eevalFN,x_vector,indiv);
[fitValue,indivk]=max(eval);
indivBest=indiv(indivk,:);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?