上一章在NEH的帮助下我们找到了一个非常好的解决方案。
1 启发式算法的统一执行
现在,我们要把上一章提到的解决方法归纳为一个类别ConstructiveHeuristic。
这个类的作用在于,将来能够方便地调用各个方法。
我们的目的是,通过这个类,我们可以输入一组订单数据和想要使用的启发式算法,最后直接输出启发式算法得出的结果。
下面是一段代码,编译器提示类型错误,试着找出哪里出了问题:
class ConstructiveHeuristic:
def Run(inputData, solutionMethod):
if solutionMethod == 'FCFS':
solution = FirstComeFirstServe(inputData.InputJobs)
elif solutionMethod == 'SPT':
solution = ShortestProcessingTime(inputData.InputJobs)
elif solutionMethod == 'LPT':
solution = LongestProcessingTime(inputData.InputJobs)
elif solutionMethod == 'ROS':
solution = ROS(inputData.InputJobs)
elif solutionMethod == 'NEH':
solution = NEH(inputData.InputJobs)
else:
print('Unknown constructive solution method!')
return solution
print(ConstructiveHeuristic().Run(data, 'FCFS'))
我们在类中定义Run函数的时候,只给了两个参数作为函数的参数表。最后一行调用的时候也是传进去了 两个参数,但是为什么会提示错误呢?
原因在于调用Run函数的方式。
self,表示创建的类实例本身,方法内部,就可以把各种属性绑定到self,因为self就指向创建的实例本身。在创建实例的时候,就不能传入空的参数了,必须传入与方法匹配的参数,但self不需要传,Python解释器会自己把实例变量传进去。
因此,最后一行调用Run的时候,实际上是用ConstructiveHeuristic类的对象来调用的,虽然没写self,但是self也已经被传进去了。这就造成我们往只能接受两个参数的函数中传递了3个参数。
修改如下:
class ConstructiveHeuristic:
def Run(self, inputData, solutionMethod):
if solutionMethod == 'FCFS':
solution = FirstComeFirstServe(inputData.InputJobs)
elif solutionMethod == 'SPT':
solution = ShortestProcessingTime(inputData.InputJobs)
elif solutionMethod == 'LPT':
solution = LongestProcessingTime(inputData.InputJobs)
elif solutionMethod == 'ROS':
solution = ROS(inputData.InputJobs, 100, 2022)
elif solutionMethod == 'NEH':
solution = NEH(inputData.InputJobs)
else:
print('Unknown constructive solution method!')
return solution
print(ConstructiveHeuristic().Run(data, 'FCFS'))
print(ConstructiveHeuristic().Run(data, 'SPT'))
print(ConstructiveHeuristic().Run(data, 'LPT'))
print(ConstructiveHeuristic().Run(data, 'ROS'))
print(ConstructiveHeuristic().Run(data, 'NEH'))
注意:这里我也同时修改了ROS的调用。在调用任何函数的时候一定要注意函数的参数表*!!!!
2 交货延迟计算
到目前为止,我们对解决方案的尝试主要集中在通过尽可能多地利用产能来尽可能高效地制造。然而,交货的可靠性也是工业生产的一个重要标准,这样客户就能始终按时收到产品。因此,在下文中,总迟到率Total Tardiness将被视为一个目标函数。
a) 计算总延迟和总加权延迟
以前的启发式方法NEH、SPT和FCFS的总迟到率是多少?在计算之前,我们需要在EvaluationLogic类中写两个方法,分别计算总延迟和总加权延迟。
首先思考一下,一个订单在什么情况下算迟到?一个订单Job对象,拥有EndTimes属性和DueDate属性,那么如果该订单在当前的安排下,结束时间大于期限,就可以说这个订单延期了,延期的时长是二者的差。因此我们需要首先判断这个订单是否延期,如果延期,再计算时长。加权延迟相当于延期的时长乘以罚款费率。
def CalculateTardiness(self, currentSolution):
totalTardiness = 0
for key in currentSolution.OutputJobs: # 直接对字典进行遍历,遍历的是字典的key
if(currentSolution.OutputJobs[key].EndTimes[-1] > currentSolution.OutputJobs[key].DueDate):
currentSolution.OutputJobs[key].Tardiness = currentSolution.OutputJobs[key].EndTimes[-1] - currentSolution.OutputJobs[key].DueDate
totalTardiness += currentSolution.OutputJobs[key].EndTimes[-1] - currentSolution.OutputJobs[key].DueDate
currentSolution.TotalTardiness = totalTardiness
setattr(EvaluationLogic, 'CalculateTardiness', CalculateTardiness)
def CalculateWeightedTardiness(self, currentSolution):
totalWeightedTardiness = 0
for key in currentSolution.OutputJobs:
if(currentSolution.OutputJobs[key].EndTimes[-1] > currentSolution.OutputJobs[key].DueDate):
currentSolution.OutputJobs[key].Tardiness = currentSolution.OutputJobs[key].EndTimes[-1] - currentSolution.OutputJobs[key].DueDate
totalWeightedTardiness += (currentSolution.OutputJobs[key].EndTimes[-1] - currentSolution.OutputJobs[key].DueDate) * currentSolution.OutputJobs[key].TardCost
currentSolution.TotalWeightedTardiness = totalWeightedTardiness
setattr(EvaluationLogic, 'CalculateWeightedTardiness', CalculateWeightedTardiness)
# 下面进行计算,记住,上面两个函数都被写在EvaluationLogic中,要想调用这两个函数必须先将EvaluationLogic实例化
EvaluationLogic().CalculateTardiness(NEHSloution)
print(f'NEH rule leads to total delays of: {NEHSloution.TotalTardiness}')
EvaluationLogic().CalculateTardiness(FCFSSolution)
print(f'FCFS rule leads to total delays of: {FCFSSolution.TotalTardiness}')
EvaluationLogic().CalculateTardiness(SPTSloution)
print(f'SPT rule leads to total delays of: {SPTSloution.TotalTardiness}')
EvaluationLogic().CalculateWeightedTardiness(NEHSloution)
print(f'NEH rule leads to total weighted delays of: {NEHSloution.TotalWeightedTardiness}')
EvaluationLogic().CalculateWeightedTardiness(FCFSSolution)
print(f'FCFS rule leads to total weighted delays of: {FCFSSolution.TotalWeightedTardiness}')
EvaluationLogic().CalculateWeightedTardiness(SPTSloution)
print(f'SPT rule leads to total weighted delays of: {SPTSloution.TotalWeightedTardiness}')
*注意:这里代码并不复杂,但是要搞清楚涉及到的所有类的属性
b) 针对订单延期的优化:EDD
由于之前的解决方法还没有关注到订单延期问题,我们姑且认为这方面还有改进的可能。因此,我们要使用决策规则最早到期日Earliest Due Date来生成一个解决方案(为此写一个适当的函数)。
看看此时的总延期时长有多高?
EDD的算法比较简单,只涉及一个按照截止日期排序的问题,这里不展开了。先看一段错误代码:
def EarliestDueDate(jobList):
tmpPermutation = sorted(range(len(jobList)), key = lambda x: jobList[x].DueDate)
tmpSolution = Solution(jobList, tmpPermutation)
# EvaluationLogic().DefineStartEnd(tmpSolution)
EvaluationLogic().CalculateTardiness(tmpSolution)
return (tmpSolution)
EDDSol = EarliestDueDate(data.InputJobs)
print(f'EDD rule leads to total weighted delays of: {EDDSol.TotalTardiness}')
*注意:这里会输出0,也就是我们定义CalculateTardiness时给出的初始值。因此实际上这段代码并没有执行for循环中的比较和赋值。原因在于,我注释掉了DefineStartEnd这一行。这一行的目的在于计算每个订单的开始时间和结束时间并输出总加工时长。这里我们不需要知道总时长,所以我在第一次写的时候就省略了这一句。回头看一下CalculateTardiness的代码,我们在其中为了计算总延迟,使用了EndTimes属性,而这个属性必须经过DefineStartEnd才能被赋值,否则EndTimes的值始终为-1,也就不会执行循环体了
正确答案如下:
from InputData import *
def EarliestDueDate(jobList):
tmpPermutation = sorted(range(len(jobList)), key = lambda x: jobList[x].DueDate)
tmpSolution = Solution(jobList, tmpPermutation)
EvaluationLogic().DefineStartEnd(tmpSolution)
EvaluationLogic().CalculateTardiness(tmpSolution)
return tmpSolution
EDDSol = EarliestDueDate(data.InputJobs)
print(f'EDD rule leads to total weighted delays of: {EDDSol.TotalTardiness}')
补充内容 sort和sorted的区别
sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。
list 的 sort 方法返回的是对已经存在的列表进行操作,无返回值,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。
sorted(iterable, cmp=None, key=None, reverse=False)
参数说明:
- iterable -- 可迭代对象。
- cmp -- 比较的函数,这个具有两个参数,参数的值都是从可迭代对象中取出,此函数必须遵守的规则为,大于则返回1,小于则返回-1,等于则返回0。
- key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
reverse -- 排序规则,reverse = True 降序 , reverse = False 升序(默认)。