A list,
Firstly, the flow chart of NSGA2 genetic algorithm is introduced.
Ii. Source code
clc;
clear;
close all;
%% Problem Definition
load CastingData Jm T JmNumber DeliveryTime IntervalTime
CostFunction=@(x,Jm ,T ,JmNumber ,DeliveryTime, IntervalTime) MyCost(x,Jm ,T ,JmNumber ,DeliveryTime, IntervalTime);
nVar=3;
VarSize=[1 nVar];
VarMin=4 -;
VarMax= 4;
pfmax=0.9;
pfmin=0.2;
VarRange=[VarMin VarMax];
%% NSGA-II Parameters
MaxIt=500;
nPop=50;
pCrossover=0.8;
nCrossover=round(pCrossover*nPop/2) *2;
pMutation=0.3;
nMutation=round(pMutation*nPop);
mu=0.3; %% Initialization tic; % PNumber number of castings MNumber number of processes Array The number of processes for each part may be different PNumber=size(Jm,1);
trace=zeros(2, MaxIt); % Initial value of search result MNumber=[];for i=1:size(Jm,1)
sumTemp=0;
for j=1:size(Jm,2)
if(length(Jm{i,j}))>0
sumTemp=sumTemp+1; end end MNumber=[MNumber,sumTemp]; end WNumber=sum(MNumber); % total Number of operations %% Initialization Number=MNumber; D=WNumber*2; % Particle swarm dimension empty_individual.position =[]; empty_individual.Cost=[]; empty_individual.Rank=[]; empty_individual.CrowdingDistance=[]; empty_individual.DominatedCount=[]; empty_individual.DominationSet=[]; % pop=repmat(empty_individual,nPop,1);
for i=1:nPop
WPNumberTemp=Number;
if i<nPop/2
for j=1:WNumber % random production process val=unidrnd(PNumber);while WPNumberTemp(val)= =0val=unidrnd(PNumber); End % pop(I).position (j)=val; % WPNumberTemp(val)=WPNumberTemp(val)- 1; % of the first2TempT=T{val,MNumber(val) -wpNumberTemp (val)}; % TempT =min(TempT); % mindex= UNIDRND (LENGTH (TempT)); % pop(I).Position(j+WNumber)=mindex; endelse
for j=1:WNumber % random production process val=unidrnd(PNumber);while WPNumberTemp(val)= =0val=unidrnd(PNumber); End % pop(I).position (j)=val; % WPNumberTemp(val)=WPNumberTemp(val)- 1; % of the first2TempT=T{val,MNumber(val) -wpNumberTemp (val)}; % machine time minimum initialization [~,minTimeIndex]=min(TempT); % MINdex = UNIDRND (LENGTH (TempT)); % pop(I).Position(j+WNumber)=minTimeIndex; end end endfor i=1:nPop
pop(i).Cost=CostFunction(pop(i).Position,Jm ,T ,JmNumber ,DeliveryTime, IntervalTime);
end
% Non-dominated Sorting
[pop ,F]=NonDominatedSorting(pop);
% Calculate Crowding Distances
pop=CalcCrowdingDistance(pop,F);
%% NSGA-II Loop
for it=1:MaxIt
% Crossover
popc=repmat(empty_individual,nCrossover,1);
pf=pfmax-(pfmax-pfmin)*it/MaxIt;
for k=1:nCrossover
i1=BinaryTournamentSelection(pop);
i2=BinaryTournamentSelection(pop);
% [popc(k,1).Position, popc(k,2).Position]=Crossover(pop(i1).Position,pop(i2).Position,VarRange);
popc(k,1).Position= CrossParticle(pop(i1).Position,pop(i2).Position,Jm,pf);
popc(k,1).Cost=CostFunction(popc(k,1).Position,Jm ,T ,JmNumber ,DeliveryTime, IntervalTime);
end
popc=popc(:);
% Mutation
popm=repmat(empty_individual,nMutation,1);
for k=1:nMutation
i=BinaryTournamentSelection(pop);
if rand(a)<mu
popm(k).Position=Swap(pop(i).Position,Jm);
popm(k).Cost=CostFunction(popm(k).Position,Jm ,T ,JmNumber ,DeliveryTime, IntervalTime);
else
popm(k).Position=pop(i).Position;
popm(k).Cost=pop(i).Cost;
end
end
% Merge Pops
pop=[pop
popc
popm];
% Non-dominated Sorting
[pop, F]=NonDominatedSorting(pop);
% Calculate Crowding Distances
pop=CalcCrowdingDistance(pop,F);
% Sort Population
pop=SortPopulation(pop);
% Delete Extra Individuals
pop=pop(1:nPop);
% Non-dominated Sorting
[pop, F]=NonDominatedSorting(pop);
% Calculate Crowding Distances
pop=CalcCrowdingDistance(pop,F);
% Plot F1
PF=pop(F{1});
PFCosts=[PF.Cost];
popCosts=[pop.Cost];
firstObj=popCosts(1, :); secondObj=popCosts(2, :); trace(1, it)=min(firstObj);
trace(2, it)=min(secondObj); % drawing FIG = figure (1);
set(fig,'NAME'.'NSGA-MultiObj');
plot(PFCosts(1,:),PFCosts(2, :).'ro');
xlabel('Interval time drag');
ylabel('Late delivery');
% Show Iteration Information
disp(['Iteraion ' num2str(it) ': Number of F1 Members = ' num2str(numel(PF))]);
end
Copy the code
3. Operation results
Fourth, note
Version: 2014 a