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

📄 plotpareto.m

📁 遗传规划的matlab工具箱
💻 M
字号:
function plotpareto(vars)
%PLOTPARETO    Plots the pareto front in GPLAB.
%   PLOTPARETO(VARS) draws a plot which shows the best fitness
%   found for each tree size, i.e. number of nodes, in blue.
%   The red graph is the pareto front, i.e. the set of solutions
%   for which no other solution was found which both has a smaller
%   tree and better fitness. The sizes and fitnesses of the current
%   population are plotted in green.
%
%   Input arguments:
%      VARS - all the variables of the algorithm (struct)
%   Output arguments:
%      none
%
%   See also DESIRED_OBTAINED, OPERATOR_EVOLUTION, ACCURACY_COMPLEXITY;
%
%   Created (2003) by SINTEF (hso@sintef.no,jtt@sintef.no,okl@sintef.no)
%   Modified (2004) by Sara Silva (sara@dei.uc.pt)
%   - moved the paretofront-related code of 'updatestate.m' into here
%   - slight modifications to ensure compatibility with version 2
%   This file is part of the GPLAB toolbox
%
%   Modified (September 2004) by Sara Silva (sara@dei.uc.pt)
%   Temporary bug fix, added lines 43-47 (will be optimized in the future):
%   - test fitness was not correctly drawn (and sometimes an error was issued)

% ensure #nodes are computed for current population:
for i=1:length(vars.pop)
    if isempty(vars.pop(i).nodes)
        vars.pop(i).nodes=nodes(vars.pop(i).tree);
    end
end

% build pareto front variable:
paretofront=[];
paretofront(1).fitness=[];
for solution=vars.pop
   while length(paretofront)<solution.nodes
      paretofront(end+1).ind=[];
   end
   if isempty(paretofront(solution.nodes).fitness) | ((vars.params.lowerisbetter & solution.fitness<paretofront(solution.nodes).fitness) | (~vars.params.lowerisbetter & solution.fitness>paretofront(solution.nodes).fitness))
      % cross validation
      if vars.params.usetestdata
        % make a temporary 'varsvals', because the real one contains training data:
        for t=1:vars.params.numvars
		    % for all variables (which are first in list of inputs), ie, X1,X2,X3,...
   	        vars.state.varsvals{t}=mat2str(vars.data.test.example(:,t));
	    end
         [testfitness,testresultind]=calcfitness(solution.str,vars.params,vars.data.test,vars.state);
         solution.testfitness=testfitness;
      end
      paretofront(solution.nodes).fitness=solution.fitness;
      paretofront(solution.nodes).ind=solution;
      if vars.params.usetestdata
         paretofront(solution.nodes).testfitness=solution.testfitness;
      end
   end
end
 
% collect fitness and #nodes:
y=[paretofront.fitness];
x=[];
for i=1:size(paretofront,2)
   if ~isempty(paretofront(i).ind)
      x=[x i];
   end
end
if length(y)~=length(x)
    error('','internal error');
end

% compute pareto front:
best=[];
bestind=[];
sofar=[];
for i=1:length(y)
    if i==1 | y(i) > sofar
        sofar=y(i);
        best=[best sofar];
        bestind=[bestind x(i)];
    end
end

hold on
title('Pareto front');
xlabel('nodes');
ylabel('fitness');

% plot:
plot(x,y,'o-',bestind,best,'ro-');
plot([vars.pop.nodes],[vars.pop.fitness],'g*');
if vars.params.usetestdata
   plot(x,[paretofront.testfitness],'m-');
   legend('best for #nodes','pareto front','current population','test fitness');
else
   legend('best for #nodes','pareto front','current population');
end

hold off

⌨️ 快捷键说明

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