can you plot the pareto font: clear all; clc; close all; % Multi-objective function…
- User
can you plot the pareto font:
clear all; clc; close all;
% Multi-objective function
MultiObjFnc = 'ZDT1'; %MultiObjFnc = 'ZDT2'; %MultiObjFnc = 'ZDT3'; %MultiObjFnc = 'ZDT6';
switch MultiObjFnc case 'ZDT1' % ZDT1 (convex) g = @(x) 1+9.sum(x(:,2:end),2)./(size(x,2)-1); MultiObj.fun = @(x) [x(:,1), g(x).(1-sqrt(x(:,1)./g(x)))]; MultiObj.nVar = 30; MultiObj.var_min = zeros(1,MultiObj.nVar); MultiObj.var_max = ones(1,MultiObj.nVar);
num_points = 100; f1 = linspace(0, 1, num_points)'; f2 = 1 - sqrt(f1); true_pareto = [f1, f2]; MultiObj.truePF = true_pareto; case 'ZDT2' % ZDT2 (non-convex) f = @(x) x(:,1); g = @(x) 1+9.*sum(x(:,2:end),2)./(size(x,2)-1); h = @(x) 1-(f(x)./g(x)).^2; MultiObj.fun = @(x) [f(x), g(x).*h(x)]; MultiObj.nVar = 30; MultiObj.var_min = zeros(1,MultiObj.nVar); MultiObj.var_max = ones(1,MultiObj.nVar); num_points = 1000; f1 = linspace(0, 1, num_points)'; f2 = 1 - (f1).^2; true_pareto = [f1, f2]; MultiObj.truePF = true_pareto; case 'ZDT3' % ZDT3 (discrete) f = @(x) x(:,1); g = @(x) 1+(9/size(x,2)-1).*sum(x(:,2:end),2); h = @(x) 1 - sqrt(f(x)./g(x)) - (f(x)./g(x)).*sin(10.*pi.*f(x)); MultiObj.fun = @(x) [f(x), g(x).*h(x)]; MultiObj.nVar = 30; MultiObj.var_min = 0.*ones(1,MultiObj.nVar); MultiObj.var_max = 1.*ones(1,MultiObj.nVar); num_points = 100; % Number of points in the true Pareto front f1 = linspace(0, 0.85, num_points)'; f2 = 1 - sqrt(f1) - f1 .* sin(10 * pi * f1); true_pareto = [f1, f2]; MultiObj.truePF = true_pareto; case 'ZDT6' % ZDT6 (non-uniform) f = @(x) 1 - exp(-4.*x(:,1)).*sin(6.*pi.*x(:,1)); g = @(x) 1 + 9.*(sum(x(:,2:end),2)./(size(x,2)-1)).^0.25; h = @(x) 1 - (f(x)./g(x)).^2; MultiObj.fun = @(x) [f(x), g(x).*h(x)]; MultiObj.nVar = 10; MultiObj.var_min = 0.*ones(1,MultiObj.nVar); MultiObj.var_max = 1.*ones(1,MultiObj.nVar); load('ZDT6.mat'); MultiObj.truePF = PF;end
% Parameters params.Np = 200; % Population size params.pc = 0.9; % Probability of crossover params.pm = 0.5; % Probability of mutation params.maxgen = 100; % Maximum number of generations params.ms = 0.05; % Mutation strength
% NSGA-II algorithm NSGAII(params,MultiObj);
function NSGAII(params,MultiObj)
% Parameters Np = params.Np; % Number of chromosomes in the population maxgen = params.maxgen; % Maximum number of generations pc = params.pc; % Probability of crossover pm = params.pm; % Probability of mutation ms = params.ms; % Mutation strength fun = MultiObj.fun; % Objective function nVar = MultiObj.nVar; % Number of variables (dimensions or objectives) var_min = MultiObj.var_min(:); % Minimum value for each gen var_max = MultiObj.var_max(:); % Maximum value for each gen % Initialization gen = 1; P = repmat((var_max-var_min)',Np,1).*rand(Np,nVar) + repmat(var_min',Np,1); Pfit = fun(P); Prank = FastNonDominatedSorting_Vectorized(Pfit); [P,~] = selectParentByRank(P,Prank); Q = applyCrossoverAndMutation(P,pc,pm,ms,var_max,var_min); % Plotting and verbose if(size(Pfit,2) == 2) h_fig = figure(1); h_par=scatter(Pfit(:,1),Pfit(:,2),20,'filled', 'markerFaceAlpha',0.3,'MarkerFaceColor',[128 193 219]./255); hold on; h_rep = plot(Pfit(:,1),Pfit(:,2),'ok'); hold on; grid on; xlabel('f1'); ylabel('f2'); drawnow; axis square; end if(size(Pfit,2) == 3) h_fig = figure(1); h_rep = plot3(Pfit(:,1),Pfit(:,2),Pfit(:,3),'ok'); hold on; grid on; xlabel('f1'); ylabel('f2'); zlabel('f3'); drawnow; axis square; end display(['Generation #' num2str(gen) ' - First front size: ' num2str(sum(Prank==1))]); % Main NSGA-II loop stopCondition = false; while ~stopCondition % Merge the parent and the children R = [P; Q]; % Compute the new Pareto Fronts Rfit = real(fun(R)); Rrank = FastNonDominatedSorting_Vectorized(Rfit); % Plotting and verbose if(size(Rfit,2) == 2) figure(h_fig); delete(h_rep); h_par=scatter(Rfit(1:Np,1),Rfit(1:Np,2),20,'filled', 'markerFaceAlpha',0.3,'MarkerFaceColor',[128 193 219]./255); hold on; h_rep = plot(Rfit(1:Np,1),Rfit(1:Np,2),'ok'); hold on; if(isfield(MultiObj,'truePF')) try delete(h_pf); end h_pf = plot(MultiObj.truePF(:,1),MultiObj.truePF(:,2),'.','color',0.8.*ones(1,3)); hold on; end grid on; xlabel('f1'); ylabel('f2'); drawnow; axis square; end if(size(Rfit,2) == 3) figure(h_fig); delete(h_rep); h_rep = plot3(Rfit(1:Np,1),Rfit(1:Np,2),Rfit(1:Np,3),'ok'); hold on; if(isfield(MultiObj,'truePF')) try delete(h_pf); end h_pf = plot3(MultiObj.truePF(:,1),MultiObj.truePF(:,2),MultiObj.truePF(:,3),'.','color',0.8.*ones(1,3)); hold on; end grid on; xlabel('f1'); ylabel('f2'); drawnow; axis square; end display(['Generation #' num2str(gen) ' - First front size: ' num2str(sum(Rrank==1))]); % Sort by rank [Rrank,idx] = sort(Rrank,'ascend'); Rfit = Rfit(idx,:); R = R(idx,:); % Compute the crowding distance index [Rcrowd,Rrank,~,R] = crowdingDistances(Rrank,Rfit,R); % Select Parent P = selectParentByRankAndDistance(Rcrowd,Rrank,R); % Compute child Q = applyCrossoverAndMutation(P,pc,pm,ms,var_max,var_min); % Increment generation gen = gen + 1; if(gen>maxgen), stopCondition = true; end end - Assistant
To plot the true Pareto front for the ZDT1 multi-objective function, you can use the MATLAB code snippet you provided. It employs a simulated NSGA-II algorithm to evolve a population towards the Pareto front. The provided code is designed to plot the Pareto front approximation obtained at each generation alongside the true Pareto front for the selected problem (e.g., ZDT1, ZDT2, etc.).
- User
i want the plot to be like this: figure; plot(paretoFront(:,1), paretoFront(:,2), 'ro', 'MarkerSize', 6, 'MarkerFaceColor', 'r'); hold on; plot(true_pareto(:,1), true_pareto(:,2), 'b--', 'LineWidth', 1.5); xlabel('f1'); ylabel('f2'); title('Pareto Front for ZDT1 using MOEMA'); legend('Obtained Pareto Front', 'True Pareto Front'); grid on; hold off;
- Assistant
To achieve the specific plot format you described, you need to adjust the plotting section within your function. Here's how you can modify your code to achieve a plot similar to your desired style after completing the execution of the NSGA-II algorithm:
- User
can you show full code
- Assistant
- User
can you add case for zdt4?
- Assistant