优化后的版本,增加了必要注释,修改了绘图逻辑
This commit is contained in:
parent
b91c034162
commit
a1244920f7
|
|
@ -32,7 +32,7 @@ class Decode:
|
|||
Site = 0
|
||||
# 按照基因的MS部分按工件序号划分
|
||||
for S_i in self.J.values():
|
||||
Ms_decompose.append(MS[Site:Site + S_i])
|
||||
Ms_decompose.append(MS[Site:Site + S_i]) #对MS按照每道工序数进行切分
|
||||
Site += S_i
|
||||
for i in range(len(Ms_decompose)): # len(Ms_decompose)表示工件数
|
||||
JM_i = []
|
||||
|
|
|
|||
|
|
@ -22,12 +22,12 @@ class Encode:
|
|||
self.RS_num = int(0.2 * Pop_size) # 随机选择初始化
|
||||
self.Len_Chromo = 0
|
||||
for i in J.values():
|
||||
self.Len_Chromo += i
|
||||
self.Len_Chromo += i # 遍历字典J的所有值并求和,即工序数之和
|
||||
|
||||
# 生成工序准备的部分
|
||||
def OS_List(self):
|
||||
OS_list = []
|
||||
for k, v in self.J.items():
|
||||
for k, v in self.J.items(): # 遍历字典J的所有键值对,初始化工序矩阵
|
||||
OS_add = [k - 1 for j in range(v)]
|
||||
OS_list.extend(OS_add)
|
||||
return OS_list
|
||||
|
|
@ -36,7 +36,7 @@ class Encode:
|
|||
def CHS_Matrix(self, C_num):
|
||||
return np.zeros([C_num, self.Len_Chromo], dtype=int)
|
||||
|
||||
# 定位每个工件的每道工序的位置
|
||||
# 定位每个工件的每道工序的位置,Job第几个工件,Operation第几道工序
|
||||
def Site(self, Job, Operation):
|
||||
O_num = 0
|
||||
for i in range(len(self.J)):
|
||||
|
|
@ -84,7 +84,7 @@ class Encode:
|
|||
OS_list = self.OS_List()
|
||||
OS = self.CHS_Matrix(self.LS_num)
|
||||
for i in range(self.LS_num):
|
||||
random.shuffle(OS_list) # 生成工序排序部分
|
||||
random.shuffle(OS_list) # (随机打乱)生成工序排序部分
|
||||
OS[i] = np.array(OS_list) # 随机打乱后将其赋值给OS的某一行(因为有一个种群,第i则是赋值在OS的第i行,以此生成完整的OS)
|
||||
GJ_List = [i_1 for i_1 in range(self.J_num)] # 生成工件集
|
||||
for g in GJ_List: # 选择第一个工件(注意:不用随机打乱了)
|
||||
|
|
|
|||
8
GA.py
8
GA.py
|
|
@ -9,18 +9,18 @@ from Instance import *
|
|||
|
||||
class GA():
|
||||
def __init__(self):
|
||||
self.Pop_size = 400 # 种群数量
|
||||
self.Pop_size = 300 # 种群数量
|
||||
self.Pc = 0.8 # 交叉概率
|
||||
self.Pm = 0.3 # 变异概率
|
||||
self.Pv = 0.5 # 选择何种方式进行交叉的概率阈值
|
||||
self.Pw = 0.95 # 选择何种方式进行变异的概率阈值
|
||||
self.Max_Itertions = 100 # 最大迭代次数
|
||||
self.Max_Itertions = 20 # 最大迭代次数
|
||||
|
||||
# 适应度
|
||||
def fitness(self, CHS, J, Processing_time, M_num, Len):
|
||||
Fit = []
|
||||
for i in range(len(CHS)):
|
||||
d = Decode(J, Processing_time, M_num)
|
||||
d = Decode(J, Processing_time, M_num) #实例化一个解码器,传入问题参数
|
||||
Fit.append(d.decode(CHS[i], Len))
|
||||
return Fit
|
||||
|
||||
|
|
@ -34,7 +34,7 @@ class GA():
|
|||
"""
|
||||
T_r = [j for j in range(T0)]
|
||||
r = random.randint(1, 10) # 在区间[1,T0]内产生一个整数r
|
||||
random.shuffle(T_r)
|
||||
random.shuffle(T_r) #直接打乱T_r,原地修改
|
||||
R = T_r[0:r] # 按照随机数r产生r个互不相等的整数
|
||||
OS_1 = CHS1[O_num:2 * T0]
|
||||
OS_2 = CHS2[O_num:2 * T0]
|
||||
|
|
|
|||
62
Instance.py
62
Instance.py
|
|
@ -5,7 +5,7 @@ M_num:加工机器数
|
|||
O_num:加工工序数
|
||||
J_num:工件个数
|
||||
"""
|
||||
|
||||
"""
|
||||
CKS201 = [[10, 9, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999],
|
||||
[9999, 9999, 14, 16, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999],
|
||||
[9999, 9999, 9999, 9999, 15, 25, 21, 9999, 9999, 9999, 9999, 9999],
|
||||
|
|
@ -24,3 +24,63 @@ J = {i: (5 if i < 4 else 8) for i in range(1, 8)}
|
|||
M_num = 12
|
||||
O_num = 3 * 5 + 4 * 8
|
||||
J_num = 7
|
||||
"""
|
||||
CKS201 = [
|
||||
[5, 9999, 7, 9999, 9999, 9999], # 工序1: M1=5, M3=7
|
||||
[9999, 6, 9999, 8, 9999, 9999], # 工序2: M2=6, M4=8
|
||||
[10, 9999, 9999, 9999, 12, 9999], # 工序3: M1=10, M5=12
|
||||
[9999, 9999, 15, 9999, 9999, 18], # 工序4: M3=15, M6=18
|
||||
[9999, 9, 9999, 11, 9999, 9999], # 工序5: M2=9, M4=11
|
||||
[14, 9999, 9999, 9999, 9999, 16] # 工序6: M1=14, M6=16
|
||||
]
|
||||
|
||||
CKS301 =[
|
||||
[9999, 4, 9999, 6, 9999, 9999], # 工序1: M2=4, M4=6
|
||||
[9999, 9999, 5, 9999, 7, 9999], # 工序2: M3=5, M5=7
|
||||
[9999, 8, 9999, 9999, 9999, 10], # 工序3: M2=8, M6=10
|
||||
[12, 9999, 9999, 14, 9999, 9999], # 工序4: M1=12, M4=14
|
||||
[9999, 9999, 16, 9999, 18, 9999], # 工序5: M3=16, M5=18
|
||||
[9999, 20, 9999, 9999, 9999, 22] # 工序6: M2=20, M6=22
|
||||
]
|
||||
|
||||
CKS401 =[
|
||||
[3, 9999, 9999, 9999, 5, 9999], # 工序1: M1=3, M5=5
|
||||
[9999, 7, 9999, 9999, 9999, 9], # 工序2: M2=7, M6=9
|
||||
[9999, 9999, 11, 13, 9999, 9999], # 工序3: M3=11, M4=13
|
||||
[15, 9999, 9999, 9999, 17, 9999], # 工序4: M1=15, M5=17
|
||||
[9999, 19, 9999, 9999, 9999, 21], # 工序5: M2=19, M6=21
|
||||
[9999, 9999, 23, 25, 9999, 9999] # 工序6: M3=23, M4=25
|
||||
]
|
||||
|
||||
CKS501 =[
|
||||
[9999, 9999, 9999, 6, 9999, 8], # 工序1: M4=6, M6=8
|
||||
[10, 9999, 12, 9999, 9999, 9999], # 工序2: M1=10, M3=12
|
||||
[9999, 14, 9999, 9999, 16, 9999], # 工序3: M2=14, M5=16
|
||||
[9999, 9999, 9999, 18, 9999, 20], # 工序4: M4=18, M6=20
|
||||
[22, 9999, 24, 9999, 9999, 9999], # 工序5: M1=22, M3=24
|
||||
[9999, 26, 9999, 9999, 28, 9999] # 工序6: M2=26, M5=28
|
||||
]
|
||||
|
||||
CKS601 =[
|
||||
[9999, 9999, 5, 9999, 9999, 7], # 工序1: M3=5, M6=7
|
||||
[9, 9999, 9999, 11, 9999, 9999], # 工序2: M1=9, M4=11
|
||||
[9999, 13, 9999, 9999, 15, 9999], # 工序3: M2=13, M5=15
|
||||
[9999, 9999, 17, 9999, 9999, 19], # 工序4: M3=17, M6=19
|
||||
[21, 9999, 9999, 23, 9999, 9999], # 工序5: M1=21, M4=23
|
||||
[9999, 25, 9999, 9999, 27, 9999] # 工序6: M2=25, M5=27
|
||||
]
|
||||
|
||||
CKS701 =[
|
||||
[4, 6, 9999, 9999, 9999, 9999], # 工序1: M1=4, M2=6
|
||||
[9999, 9999, 8, 10, 9999, 9999], # 工序2: M3=8, M4=10
|
||||
[9999, 9999, 9999, 9999, 12, 14], # 工序3: M5=12, M6=14
|
||||
[16, 18, 9999, 9999, 9999, 9999], # 工序4: M1=16, M2=18
|
||||
[9999, 9999, 20, 22, 9999, 9999], # 工序5: M3=20, M4=22
|
||||
[9999, 9999, 9999, 9999, 24, 26] # 工序6: M5=24, M6=26
|
||||
]
|
||||
|
||||
Processing_time = [CKS201] + [CKS301] + [CKS401]+ [CKS501]+ [CKS601]+ [CKS701]
|
||||
J = {i: 6 for i in range(1, 7)}
|
||||
M_num = 6
|
||||
O_num = 6 * 6
|
||||
J_num = 6
|
||||
36
main.py
36
main.py
|
|
@ -1,5 +1,4 @@
|
|||
import random
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
|
|
@ -13,7 +12,7 @@ from Instance import *
|
|||
def Gantt(Machines):
|
||||
M = ['red', 'blue', 'yellow', 'orange', 'green', 'palegoldenrod', 'purple', 'pink', 'Thistle', 'Magenta',
|
||||
'SlateBlue', 'RoyalBlue', 'Cyan', 'Aqua', 'floralwhite', 'ghostwhite', 'goldenrod', 'mediumslateblue',
|
||||
'navajowhite', 'navy', 'sandybrown', 'moccasin']
|
||||
'navajowhite', 'nawy', 'sandybrown', 'moccasin']
|
||||
for i in range(len(Machines)):
|
||||
Machine = Machines[i]
|
||||
Start_time = Machine.O_start
|
||||
|
|
@ -49,13 +48,8 @@ if __name__ == '__main__':
|
|||
if best_fitness < Optimal_fit:
|
||||
Optimal_fit = best_fitness
|
||||
Optimal_CHS = Best
|
||||
Best_fit.append(Optimal_fit)
|
||||
print('best_fitness', best_fitness)
|
||||
d = Decode(J, Processing_time, M_num)
|
||||
Fit.append(d.decode(Optimal_CHS, O_num))
|
||||
Gantt(d.Machines)
|
||||
else:
|
||||
Best_fit.append(Optimal_fit)
|
||||
print('iter_{}: new best_fitness = {}'.format(i, best_fitness))
|
||||
Best_fit.append(Optimal_fit) # 始终记录当前最优适应度
|
||||
for j in range(len(C)):
|
||||
Cafter = []
|
||||
if random.random() < g.Pc:
|
||||
|
|
@ -76,10 +70,26 @@ if __name__ == '__main__':
|
|||
if Cafter != []:
|
||||
Fit = g.fitness(Cafter, J, Processing_time, M_num, O_num)
|
||||
C[j] = Cafter[Fit.index(min(Fit))]
|
||||
x = np.linspace(0, 50, g.Max_Itertions)
|
||||
|
||||
# 迭代结束后输出最终结果
|
||||
print("\n=== 优化结果 ===")
|
||||
print("最优适应度 (最少完成时间):", Optimal_fit)
|
||||
|
||||
# 解码最优解并绘制甘特图
|
||||
d = Decode(J, Processing_time, M_num)
|
||||
final_fitness = d.decode(Optimal_CHS, O_num)
|
||||
print("解码验证适应度:", final_fitness)
|
||||
|
||||
# 绘制最优甘特图
|
||||
Gantt(d.Machines)
|
||||
|
||||
# 绘制收敛曲线
|
||||
x = np.linspace(0, g.Max_Itertions, g.Max_Itertions)
|
||||
plt.figure()
|
||||
plt.plot(x, Best_fit, '-k')
|
||||
plt.title('the maximum completion time of each iteration')
|
||||
plt.title('The maximum completion time of each iteration')
|
||||
plt.ylabel('Cmax')
|
||||
plt.xlabel('Test Num')
|
||||
plt.xlabel('Iteration')
|
||||
plt.grid(True)
|
||||
plt.savefig('最大完成时间的优化过程.png')
|
||||
plt.show()
|
||||
plt.show()
|
||||
BIN
优化后排程方案的甘特图.png
BIN
优化后排程方案的甘特图.png
Binary file not shown.
|
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 19 KiB |
Loading…
Reference in New Issue