can you try this on zdt1: % Parameters params.Np = 200; % Population size params.Nr =…
- User
can you try this on zdt1:
% Parameters params.Np = 200; % Population size params.Nr = 200; % Repository size params.maxgen = 100; % Maximum number of generations params.W = 0.4; % Inertia weight params.C1 = 2; % Individual confidence factor params.C2 = 2; % Swarm confidence factor params.ngrid = 20; % Number of grids in each dimension params.maxvel = 5; % Maxmium vel in percentage params.u_mut = 0.5; % Uniform mutation percentage
% MOPSO REP = MOPSO(params,MultiObj);
% Display info display('Repository fitness values are stored in REP.pos_fit'); display('Repository particles positions are store in REP.pos');
function REP = MOPSO(params,MultiObj)
% Parameters Np = params.Np; Nr = params.Nr; maxgen = params.maxgen; W = params.W; C1 = params.C1; C2 = params.C2; ngrid = params.ngrid; maxvel = params.maxvel; u_mut = params.u_mut; fun = MultiObj.fun; nVar = MultiObj.nVar; var_min = MultiObj.var_min(:); var_max = MultiObj.var_max(:); % Initialization POS = repmat((var_max-var_min)',Np,1).*rand(Np,nVar) + repmat(var_min',Np,1); VEL = zeros(Np,nVar); POS_fit = fun(POS); if size(POS,1) ~= size(POS_fit,1) warning(['The objective function is badly programmed. It is not returning' ... 'a value for each particle, please check it.']); end PBEST = POS; PBEST_fit= POS_fit; DOMINATED= checkDomination(POS_fit); REP.pos = POS(~DOMINATED,:); REP.pos_fit = POS_fit(~DOMINATED,:); REP = updateGrid(REP,ngrid); maxvel = (var_max-var_min).*maxvel./100; gen = 1; % Plotting and verbose if(size(POS_fit,2)==2) h_fig = figure(1); h_par = plot(POS_fit(:,1),POS_fit(:,2),'or'); hold on; h_rep = plot(REP.pos_fit(:,1),REP.pos_fit(:,2),'ok'); hold on; try set(gca,'xtick',REP.hypercube_limits(:,1)','ytick',REP.hypercube_limits(:,2)'); axis([min(REP.hypercube_limits(:,1)) max(REP.hypercube_limits(:,1)) ... min(REP.hypercube_limits(:,2)) max(REP.hypercube_limits(:,2))]); grid on; xlabel('f1'); ylabel('f2'); end drawnow; end if(size(POS_fit,2)==3) h_fig = figure(1); h_par = plot3(POS_fit(:,1),POS_fit(:,2),POS_fit(:,3),'or'); hold on; h_rep = plot3(REP.pos_fit(:,1),REP.pos_fit(:,2),REP.pos_fit(:,3),'ok'); hold on; try set(gca,'xtick',REP.hypercube_limits(:,1)','ytick',REP.hypercube_limits(:,2)','ztick',REP.hypercube_limits(:,3)'); axis([min(REP.hypercube_limits(:,1)) max(REP.hypercube_limits(:,1)) ... min(REP.hypercube_limits(:,2)) max(REP.hypercube_limits(:,2))]); end grid on; xlabel('f1'); ylabel('f2'); zlabel('f3'); drawnow; axis square; end display(['Generation #0 - Repository size: ' num2str(size(REP.pos,1))]); % Main MPSO loop stopCondition = false; while ~stopCondition % Select leader h = selectLeader(REP); % Update speeds and positions VEL = W.*VEL + C1*rand(Np,nVar).*(PBEST-POS) ... + C2*rand(Np,nVar).*(repmat(REP.pos(h,:),Np,1)-POS); POS = POS + VEL; % Perform mutation POS = mutation(POS,gen,maxgen,Np,var_max,var_min,nVar,u_mut); % Check boundaries [POS,VEL] = checkBoundaries(POS,VEL,maxvel,var_max,var_min); % Evaluate the population POS_fit = fun(POS); % Update the repository REP = updateRepository(REP,POS,POS_fit,ngrid); if(size(REP.pos,1)>Nr) REP = deleteFromRepository(REP,size(REP.pos,1)-Nr,ngrid); end % Update the best positions found so far for each particle pos_best = dominates(POS_fit, PBEST_fit); best_pos = ~dominates(PBEST_fit, POS_fit); best_pos(rand(Np,1)>=0.5) = 0; if(sum(pos_best)>1) PBEST_fit(pos_best,:) = POS_fit(pos_best,:); PBEST(pos_best,:) = POS(pos_best,:); end if(sum(best_pos)>1) PBEST_fit(best_pos,:) = POS_fit(best_pos,:); PBEST(best_pos,:) = POS(best_pos,:); end % Plotting and verbose if(size(POS_fit,2)==2) figure(h_fig); delete(h_par); delete(h_rep); h_par = plot(POS_fit(:,1),POS_fit(:,2),'or'); hold on; h_rep = plot(REP.pos_fit(:,1),REP.pos_fit(:,2),'ok'); hold on; try set(gca,'xtick',REP.hypercube_limits(:,1)','ytick',REP.hypercube_limits(:,2)'); axis([min(REP.hypercube_limits(:,1)) max(REP.hypercube_limits(:,1)) ... min(REP.hypercube_limits(:,2)) max(REP.hypercube_limits(:,2))]); end 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(POS_fit,2)==3) figure(h_fig); delete(h_par); delete(h_rep); h_par = plot3(POS_fit(:,1),POS_fit(:,2),POS_fit(:,3),'or'); hold on; h_rep = plot3(REP.pos_fit(:,1),REP.pos_fit(:,2),REP.pos_fit(:,3),'ok'); hold on; try set(gca,'xtick',REP.hypercube_limits(:,1)','ytick',REP.hypercube_limits(:,2)','ztick',REP.hypercube_limits(:,3)'); axis([min(REP.hypercube_limits(:,1)) max(REP.hypercube_limits(:,1)) ... min(REP.hypercube_limits(:,2)) max(REP.hypercube_limits(:,2)) ... min(REP.hypercube_limits(:,3)) max(REP.hypercube_limits(:,3))]); end 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'); zlabel('f3'); drawnow; axis square; end display(['Generation #' num2str(gen) ' - Repository size: ' num2str(size(REP.pos,1))]); % Update generation and check for termination gen = gen + 1; if(gen>maxgen), stopCondition = true; end end hold off; - Assistant
The code you've provided is an implementation of a Multi-Objective Particle Swarm Optimization (MOPSO) algorithm that attempts to solve a multi-objective optimization problem, specifically targeting a standard test problem known as ZDT1. This test problem is widely used for benchmarking evolutionary algorithms and multi-objective optimization techniques because it has a known Pareto front.
- User
give me full code
- Assistant
- User
add pareto font
- Assistant