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

📄 ps.m

📁 基本种群规模改进与数值实验的PSO算法源程序
💻 M
字号:
%RunExp >> Automation function
% Usage     : RunExp(noRuns, ExitAction) %e.g. RunExp(25, 1); -> Runs each experminet for 25 trials and Exits matlab when doen.
% Arguments : (optional) noRuns     -> Integer -> Number of trials per experiment
%             (optional) ExitAction -> Integer -> Action to perform on completion of experiments
%                                       NOTE: Argument noRuns is required if ExitAction is to be specified.
%                                       Accepted values and their meaning.
%                                       0 = Do Nothing
%                                       1 = Exit Matlab
%                                       2 = Exit Matlab and Shutdown (Windows XP only)
%                                       3 = Exit Matlab and Logoff   (Windows XP only)
%                                       4 = Exit Matlab and Shutdown (Win98 and Me)
%                                       5 = Exit Matlab and Logoff (Win98 and Me)

function RunExp(noRuns, ExitAction)
numberofRuns = 30;          %number of trials per experiment
if nargin >= 1
    numberofRuns = noRuns;
    if nargin < 2
        ExitAction = 0; 
    end
end
        
psoOptions = get_psoOptions;

psoOptions.Vars.ErrGoal = 1e-10;

% Parameters common across all functions
psoOptions.SParams.c1 = 2;
psoOptions.SParams.c2 = 2;
psoOptions.SParams.w_start = 0.9;
psoOptions.SParams.w_end = 0.4;
psoOptions.SParams.w_varyfor = 1;

psoOptions.Vars.Iterations = 2000;

psoOptions.Flags.ShowViz = 0;
psoOptions.Flags.Neighbor = 0;
psoOptions.Save.Interval = 0;
psoOptions.Disp.Interval = 0;

% Run experiments for the three complex functions
psoOptions.Obj.f2eval = 'DeJong';
psoOptions.Obj.lb = -100;
psoOptions.Obj.ub = 50;
psoOptions.SParams.Vmax = 50;
psoOptions.Vars.Dim = 30;
GenExpData(numberofRuns, psoOptions);

psoOptions.Obj.f2eval = 'Rastrigrin';
psoOptions.Obj.lb = -5.12;
psoOptions.Obj.ub = 2;
psoOptions.SParams.Vmax = 5.12;
psoOptions.Vars.Dim = 30;
GenExpData(numberofRuns, psoOptions);

psoOptions.Obj.f2eval = 'Rosenbrock';
psoOptions.Obj.lb = -2.048;
psoOptions.Obj.ub = 2.048;
psoOptions.SParams.Vmax = 2.048;
psoOptions.Vars.Dim = 30;
GenExpData(numberofRuns, psoOptions);

psoOptions.Obj.f2eval = 'Griewank';
psoOptions.Obj.lb = 200;
psoOptions.Obj.ub = 600;
psoOptions.SParams.Vmax = 600;
psoOptions.Vars.Dim = 30;
GenExpData(numberofRuns, psoOptions);

psoOptions.Obj.f2eval = 'Ackley';
psoOptions.Obj.lb = -32.768;
psoOptions.Obj.ub = 16;
psoOptions.SParams.Vmax = 16;
psoOptions.Vars.Dim = 30;
GenExpData(numberofRuns, psoOptions);

psoOptions.Obj.f2eval = 'Schaffer';
psoOptions.Obj.lb = -100;
psoOptions.Obj.ub = 100;
psoOptions.SParams.Vmax = 100;
psoOptions.Vars.Dim = 2;
GenExpData(numberofRuns, psoOptions);

% MANAGE EXIT ACTIONS
if ExitAction
    exitString = sprintf('\n\n\t EXITING MATLAB in 10 seconds. PLEASE SAVE OPEN FILES');
    logoffStr = sprintf('\n Your COMPUTER WILL LOG OFF IN 30 seconds. PLEASE SAVE DATA');
    shutdownStr = sprintf('\n Your COMPUTER WILL SHUTDOWN IN 30 seconds. PLEASE SAVE DATA');
    
    disp( exitString );
    errordlg(exitString)
    pause(5);
    if ExitAction == 1  %Just Exit Matlab
        pause(5);
        exit;
    elseif ExitAction == 2  %Exit and Shutdown WinXP.
        disp(shutdownStr);
        errordlg(shutdownStr);
        dos('shutdown -s -f -t 30 -c "MATLAB:RunExp: The function has finished and the system will go into a planned shutdown"');
        pause(5);
        exit;
    elseif ExitAction == 3
        disp(logoffStr);
        errordlg(logoffStr);
        dos('shutdown -l -f -t 30 -c "MATLAB:RunExp: The function has finished and the system will go into a planned shutdown"');
        pause(5);
        exit;
    elseif ExitAction == 4
        disp(shutdownStr);
        errordlg(shutdownStr);
        dos('rundll32.exe shell32.dll,SHExitWindowsEx 8');
        pause(5);
        exit;
    elseif ExitAction == 4
        disp(logoffStr);
        errordlg(logoffStr);
        dos('rundll32.exe shell32.dll,SHExitWindowsEx 0');
        pause(5);
        exit;
    end

end
    
    

%-----------------------------------------------------------%
%--Run Experiments for different dimensions and SwarmSizes--%
%-----------------------------------------------------------%
function GenExpData(numberofRuns, psoOptions)
%	DimIters = [30; ...   %Dimensions
%              2000];    %Corresponding iterations
%	for x = DimIters;
%       psoOptions.Vars.Dim = x(1,:);
%        psoOptions.Vars.Iterations = x(2,:);
%       for swarmsize = [30]
%          psoOptions.Vars.SwarmSize = swarmsize;
%         RnS(numberofRuns, psoOptions);
%    end
%    end

%	DimIters = [10, 20, 30; ...   %Dimensions
%              2000, 3000, 4000];    %Corresponding iterations
%	for x = DimIters;
%        psoOptions.Vars.Dim = x(1,:);
%        psoOptions.Vars.Iterations = x(2,:);
        for swarmsize = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150]
            psoOptions.Vars.SwarmSize = swarmsize;
            RnS(numberofRuns, psoOptions);
        end
%    end
    
%----------------%
%---Run & save---%
%----------------%
function RnS(numberofRuns, psoOptions)

disp(sprintf('This experiment will optimize %s function for %d times.', psoOptions.Obj.f2eval, numberofRuns));
disp(sprintf('Population Size: %d\t\tDimensions: %d.', psoOptions.Vars.SwarmSize, psoOptions.Vars.Dim));
fVal = 0;
History=[];
disp(sprintf('\nRun \t\t Best objVal\t\tIter'));
for i = 1:numberofRuns
    [tfxmin, xmin, Swarm, t, tHistory] = pso(psoOptions);
    fVal(i,:) = tfxmin;
    iter(i,:) = t;
    History(:,i) = tHistory;
    disp(sprintf('%4d \t\t%10f \t\t%d', i, tfxmin, t));
%    disp(sprintf('%4d \t\t%10f', i, t));
end
Avg = sum(fVal)/numberofRuns;
AvgIter = sum(iter)/numberofRuns;
disp(sprintf('\nAvg. \t\t%10f\n\n', Avg))
disp(sprintf('\nAvg. \t\t%10f\n\n', AvgIter))

fFile = strcat('f_', psoOptions.Obj.f2eval,  'd', int2str(psoOptions.Vars.SwarmSize), 'p');
%fFile = strcat('f_', psoOptions.Obj.f2eval, '_', int2str(psoOptions.Vars.Dim), 'd', int2str(psoOptions.Vars.SwarmSize), 'p'); %e.g. f_Rastrigrin_10d20p
%hFile = strcat('h_', psoOptions.Obj.f2eval, '_', int2str(psoOptions.Vars.Dim), 'd', int2str(psoOptions.Vars.SwarmSize), 'p');
save(fFile, 'fVal', '-ascii');
%save(hFile, 'History', '-ascii');

⌨️ 快捷键说明

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