### [workshop scheduling] solve the workshop scheduling problem based on MATLAB NSGA2 algorithm [including Matlab source code 893]

Matlab scientific research 2021-08-10 09:15:02 阅读数:724

workshop scheduling solve workshop scheduling

## One 、 brief introduction

Let's first introduce NSGA2 Flow chart of genetic algorithm .

## Two 、 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 Operation number array The number of operations corresponding to each workpiece may be different
PNumber=size(Jm,1);
trace=zeros(2, MaxIt); % The initial value of the optimization 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 processes
%% 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=[];
% Initial population
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)==0
val=unidrnd(PNumber);
end
% The first layer of code represents the operation
pop(i).Position(j)=val; % Random initialization position
WPNumberTemp(val)=WPNumberTemp(val)-1;
% The first 2 The layer code represents the machine
TempT=T{val,MNumber(val)-WPNumberTemp(val)};
% Minimum machine processing time
%[~,minTimeIndex]=min(TempT);
% Random machine initialization
mindex=unidrnd(length(TempT));
% Random production process machine
pop(i).Position(j+WNumber)=mindex;
end
else
for j=1:WNumber
% Random production process
val=unidrnd(PNumber);
while WPNumberTemp(val)==0
val=unidrnd(PNumber);
end
% The first layer of code represents the operation
pop(i).Position(j)=val; % Random initialization position
WPNumberTemp(val)=WPNumberTemp(val)-1;
% The first 2 The layer code represents the machine
TempT=T{val,MNumber(val)-WPNumberTemp(val)};
% Minimum machine processing time
[~,minTimeIndex]=min(TempT);
% Random machine initialization
%mindex=unidrnd(length(TempT));
% Random production process machine
pop(i).Position(j+WNumber)=minTimeIndex;
end
end
end
for 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()<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(' The interval is delayed ');
ylabel(' Delay in delivery ');
% Show Iteration Information
disp(['Iteraion ' num2str(it) ': Number of F1 Members = ' num2str(numel(PF))]);
end
```

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189.
190.
191.
192.
193.
194.
195.
196.
197.
```

edition ：2014a