⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 psoprocess.m

📁 粒子群算法的详细实现以及在大肠杆菌代谢过程中参数优化的应用。
💻 M
字号:
function [Result,OnLine,OffLine,MinMaxMeanAdapt]=PsoProcess(SwarmSize,ParticleSize,ParticleScope,InitFunc,StepFindFunc,AdaptFunc,IsStep,IsDraw,LoopCount,IsPlot)%function description:an n loop entire PSO, returen this min and max%fitness value%[Result,OnLine,OffLine,MinMaxMeanAdapt]=PsoProcess(SwarmSize,ParticleSize,ParticleScope,InitFunc,StepFindFunc,AdaptFunc,IsStep,IsDraw,LoopCount,IsPlot)%inputparameter:SwarmSize: swarm size%inputparameter:ParticleSize:dimension of one particle%inputparameter:ParticleScope;%         ParticleScope format:%           3-D ParticleScope format:%                                   [x1Min,x1Max%                                    x2Min,x2Max%                                    x3Min,x3Max]%%input parameter:InitFunc:initialize PSO fucntion%inputparameter:StepFindFunc:one_step update v,position%inputparameter:AdaptFunc:fitness function%inputparameter:IsStep;IsStep=0,iteration continues,or stop。default%continue%inputparameter:IsDraw;IsDraw=0,draw iterations,or dont draw,default dont%draw%inputparameter:LoopCount:iteration times, 100%inputparameter:IsPlot%                 IsPlot=1;%%return:Result best solution after iterations%return:OnLine data online%return:OffLine data offline%return:MinMaxMeanAdapt mean fitness value%%usage:[Result,OnLine,OffLine,MinMaxMeanAdapt]=PsoProcess(SwarmSize,ParticleSize,ParticleScope,InitFunc,StepFindFunc,AdaptFunc,IsStep,IsDraw,LoopCount,IsPlot);%faults controlif nargin<4    error('fault number of input parameter')end[row,colum]=size(ParticleSize);if row>1|colum>1    error('fault of particle dimension, is a 1*1 data.');end[row,colum]=size(ParticleScope);if row~=ParticleSize|colum~=2    error('fualt particle dimension scope');end%set default valueif nargin<7    IsPlot=1;    LoopCount=100;    IsStep=0;    IsDraw=0;endif nargin<8    IsPlot=1;    IsDraw=0;    LoopCount=100;endif nargin<9    LoopCount=100;    IsPlot=1;endif nargin<10    IsPlot=1;end%control if give below-2-D particle searching processif IsDraw~=0    DrawObjGraphic(ParticleSize,ParticleScope,AdaptFunc);end%initialize swarm      [ParSwarm,OptSwarm]=InitFunc(SwarmSize,ParticleSize,ParticleScope,AdaptFunc)%draw initial swarm position on the best functionif IsDraw~=0    if 1==ParticleSize    for ParSwarmRow=1:SwarmSize        plot([ParSwarm(ParSwarmRow,1),ParSwarm(ParSwarmRow,1)],[ParSwarm(ParSwarmRow,3),0],'r*-','markersize',8);        text(ParSwarm(ParSwarmRow,1),ParSwarm(ParSwarmRow,3),num2str(ParSwarmRow));    endend    if 2==ParticleSize        for ParSwarmRow=1:SwarmSize            stem3(ParSwarm(ParSwarmRow,1),ParSwarm(ParSwarmRow,2),ParSwarm(ParSwarmRow,5),'r.','markersize',8);        end    endend    %pause catch picsif IsStep~=0    disp('begin iteration, press any key')    pauseend%begin update algorithmfor k=1:LoopCount    %show iteration time:    disp('----------------------------------------------------------')    TempStr=sprintf('%g iterations',k);    disp(TempStr);    disp('----------------------------------------------------------')        %transfer one iteration algorithm     [ParSwarm,OptSwarm]=StepFindFunc(ParSwarm,OptSwarm,AdaptFunc,ParticleScope,0.95,0.4,LoopCount,k)        %draw below-2-D particle's new position on the aim function    if IsDraw~=0        if 1==ParticleSize            for ParSwarmRow=1:SwarmSize                plot([ParSwarm(ParSwarmRow,1),ParSwarm(ParSwarmRow,1)],[ParSwarm(ParSwarmRow,3),0],'r*-','markersize',8);                text(ParSwarm(ParSwarmRow,1),ParSwarm(ParSwarmRow,3),num2str(ParSwarmRow));            end        end        if 2==ParticleSize            for ParSwarmRow=1:SwarmSize                stem3(ParSwarm(ParSwarmRow,1),ParSwarm(ParSwarmRow,2),ParSwarm(ParSwarmRow,5),'r.','markersize',8);            end        end    end        XResult=OptSwarm(SwarmSize+1,1:ParticleSize);    YResult=AdaptFunc(XResult);        if IsStep~=0        XResult=OptSwarm(SwarmSize+1,1:ParticleSize);        YResult=AdaptFunc(XResult);            str=sprintf('%g iteration the best aim value%g',k,YResult);        disp(str);        disp('next iteration, press anykey');        pause    end        %record every step mean fitness    MeanAdapt(1,k)=mean(ParSwarm(:,2*ParticleSize+1));end%for loop end%record min and max mean fitnessMinMaxMeanAdapt=[min(MeanAdapt),max(MeanAdapt)];%calculate online and offline performancefor k=1:LoopCount    OnLine(1,k)=sum(MeanAdapt(1,1:k))/k;    OffLine(1,k)=max(MeanAdapt(1,1:k));    %OffLine(1,k)=min(MeanAdapt(1,1:k));endfor k=1:LoopCount    OffLine(1,k)=sum(OffLine(1,1:k))/k;end%draw offline and online performance curveif 1==IsPlot    figure    hold on    title('offline performance curve')    xlabel('iteration times');    ylabel('offline performance');    grid on    plot(OffLine);    figure    hold on    title('online performance curve')    xlabel('iteration times');    ylabel('online performance');    grid on    plot(OnLine);end%record this time iteration best solutionXResult=OptSwarm(SwarmSize+1,1:ParticleSize);YResult=AdaptFunc(XResult);Result=[XResult,YResult];

⌨️ 快捷键说明

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