【转】2018年全国大学生数学建模大赛B题简要分析(附代码)

  今天早上跟学姐室友去复旦把论文答辩作掉了,虽然整个项目基本上是我承担了主要的思路与代码部分,可是今天答辩我跟室友居然连一句有用的话都没说出来,全场都靠学姐开场流畅地引入,临场随机应变,从而得以与答辩教授欢聚喜散。主要缘由是教授居然准确地问到了我代码里一个细节却至关致命的问题(是一个随机初始化问题,我下面代码部分会详细提到),正好学姐室友都不是特别熟悉个人随机初始化方法,我又不能当场跟他们两个解释这个随机初始化的问题。我差点当场就要以“这样随机初始化可以减小代码量”这种蹩脚的理由跟教授争辩了。好在姜仍是老的辣,辩论队队长出身的学姐一顿 Speech Art 操做成功忽悠掉了两位教授,最终两位答辩教授仍是承认了咱们的模拟仿真方法[捂脸]。过后细想之后我成功也好,失败也罢,恐怕也是成也言语,败也言语。也许我确实可以成为一个有能力的人,可是说话艺术确实是一门很大的学问。不过看我运气一直这么差,大几率仍是凡人一个落入俗套吧[摊手]。算法

  言归正传,本文主要介绍咱们小组解决2018年全国大学生B题的思路分析,不表明标准答案。固然我仍是有自知之明,自己水平不是很高,再加上三天时间限制,本身作出来的模型以及算法确定是比较差的。这里仅仅从我我的的思考角度出发写一些参考思路做为分享讨论,但愿各位读者朋友轻喷。app

问题分析

  今年的B题确实与往年有很大的不一样。往年的数学建模问题每每具备比较好的开放性,问题解决存在较大的建模空间。今年的B题的题干自己就几乎是一个明确的模型(8台CNC+1台RGV+CNC定址),加上第二道任务要求咱们根据给定三组数据完成八小时内的RGV详细调度方案,并写入四张Excel表格,给人的感受就是要求咱们去完成一道填空题,而后附带写一篇论文[尴尬]。dom

  为了方便各位读者对赛题的阅读,这里给出连接:https://download.csdn.net/download/cy19980216/10708725布局

  问题一共有四种不一样的状况:一道工序无端障,一道工序有故障,两道工序无端障,两道工序有故障。学习

一道工序无端障

  第一种状况是最简单的,直观上直接不停地1234567812345678……按顺序上料差很少就是最优了。但严谨地来讲,虽然题目中给的三组数据确实都是用这种最幼稚的策略可以达到最优,可是若是对于通常的状况而言,好比最极端的状况下,RGV移动时间无穷大,那RGV显然就只会不停地在121212121212……这样原地上下料了。spa

  然而咱们发现不管参数怎么变化,最终RGV给CNC上下料的过程始终是一个周期性过程。固然这个彷佛很“显然”的事实倒是至关难以经过数学严格证实的(参数已知的状况下通常比较容易证实,可是全部的参数都是未知的状况下是很难严格说明的)。我赛后也仔细的思考过,可是也没有得出很漂亮的证实。我最终仅仅是针对给定的三组数据使用了遗传算法对RGV前17次上下料(17次是考虑从初始状态开始循环两圈的最短路径)的最优路径进行了搜索,而且利用穷举证实了这是前17步最优的上下料次序。以后基本上就是不断地循环。.net

  这里的模拟退火遗传算法比较鸡肋,因此我不详细说明,在第三种状况我会详细说明模拟退火遗传算法的原理。code

  如下给出第一种状况的模拟退火遗传算法算法以及对应的穷举最优证实 ↓↓↓orm

 1 # -*- coding:UTF-8 -*-
 2 """
 3  做者:囚生CY  4  平台:CSDN  5  时间:2018/10/09  6  转载请注明原做者  7  创做不易,仅供分享  8 """
 9  
 10 import math  11 import random  12 import itertools  13  
 14 """ 选取一组数据 """
 15 T = 580
 16 d1 = 23
 17 d2 = 41
 18 d3 = 59
 19 Te = 35
 20 To = 30
 21 Tc = 30
 22  
 23 CNCT = [To,Te,To,Te,To,Te,To,Te]                                         # CNC上下料时间
 24  
 25 N = 50
 26 L = 17
 27  
 28 varP = 0.1
 29 croP = 0.6
 30  
 31 croL = 4
 32 e = 0.99
 33  
 34 tm = [  35  [0,0,d1,d1,d2,d2,d3,d3],  36  [0,0,d1,d1,d2,d2,d3,d3],  37  [d1,d1,0,0,d1,d1,d2,d2],  38  [d1,d1,0,0,d1,d1,d2,d2],  39  [d2,d2,d1,d1,0,0,d1,d1],  40  [d2,d2,d1,d1,0,0,d1,d1],  41  [d3,d3,d2,d2,d1,d1,0,0],  42  [d3,d3,d2,d2,d1,d1,0,0],  43 ]  44  
 45 def update_state(state,t):  46     length = len(state)  47     for i in range(length):  48         if state[i] < t:  49             state[i] = 0  50         else:  51             state[i] -= t  52     return state  53  
 54 def time_calc(seq):  55     state = [0 for i in range(8)]                                           # 记录CNC状态
 56     isEmpty = [1 for i in range(8)]                                         # CNC是否为空?
 57     currP = 0  58     total = 0  59     length = len(seq)  60     for No in seq:  61         nextP = No  62         t = tm[currP][nextP]  63         total += t                                                         # rgv移动
 64         state = update_state(state,t)                                     # 更新state
 65         if state[No]==0:                                                 # 代表CNC等待
 66             if isEmpty[No]:                                                 # 当前CNC空
 67                 t = CNCT[No]  68                 isEmpty[No] = 0  69             else:  70                 t = CNCT[No]+Tc  71             total += t  72             state = update_state(state,t)  73             state[No] = T  74         else:                                                             # 当前CNC忙
 75             total += state[No]                                             # 先等当前CNC结束
 76             state = update_state(state,state[No])  77             t = CNCT[No]+Tc  78             total += t  79             state = update_state(state,t)  80             state[No] = T  81         currP = No  82     total += tm[currP][0]  83     return total  84  
 85 def init_prob(sample):  86     prob = []  87     for seq in sample:  88  prob.append(time_calc(seq))  89     maxi = max(prob)  90     prob = [maxi-prob[i]+1 for i in range(N)]  91     temp = 0  92     for p in prob:  93         temp += p  94     prob = [prob[i]/temp for i in range(N)]  95     for i in range(1,len(prob)):  96         prob[i] += prob[i-1]  97     prob[-1] = 1                                                         # 精度有时候很出问题
 98     return prob  99  
100 def minT_calc(sample): 101     minT = time_calc(sample[0]) 102     index = 0 103     for i in range(1,len(sample)): 104         t = time_calc(sample[i]) 105         if t < minT: 106             index = i 107             minT = t 108     return minT,index 109     
110 def init(): 111     sample = [] 112     for i in range(N): 113  sample.append([]) 114         for j in range(L): 115             sample[-1].append(random.randint(0,7)) 116     return sample 117  
118 def select(sample,prob):                                                 # 选择
119     sampleEX = [] 120     for i in range(N):                                                     # 取出N个样本
121         rand = random.random() 122         for j in range(len(prob)): 123             if rand<=prob[j]: 124  sampleEX.append(sample[j]) 125                 break
126     return sampleEX 127  
128 def cross(sample,i):                                                     # 交叉
129     for i in range(len(sample)-1): 130         for j in range(i,len(sample)): 131             rand = random.random() 132             if rand<=croP*(e**i):                                         # 执行交叉
133                 loc = random.randint(0,L-croL-1) 134                 temp1 = sample[i][loc:loc+croL] 135                 temp2 = sample[j][loc:loc+croL] 136                 for k in range(loc,loc+croL): 137                     sample[i][k] = temp2[k-loc] 138                     sample[j][k] = temp1[k-loc] 139     return sample 140         
141 def variance(sample,i):                                                     # 变异算子 
142     for i in range(len(sample)): 143         rand = random.random() 144         if rand<varP*(e**i): 145             rand1 = random.randint(0,L-1) 146             rand2 = random.randint(0,L-1) 147             temp = sample[i][rand1] 148             sample[i][rand1] = sample[i][rand2] 149             sample[i][rand2] = temp 150     return sample 151     
152 def main(): 153     sample = init() 154     mini,index = minT_calc(sample) 155     best = sample[index][:] 156     print(best) 157     for i in range(10000): 158         print(i,'\t',minT_calc(sample),end="\t") 159         prob = init_prob(sample) 160         sample = select(sample,prob) 161         sample = cross(sample,i) 162         sample = variance(sample,i) 163         mi,index = minT_calc(sample) 164         if mi>mini and random.random()<e**i:                             # 精英保留策略
165             rand = random.randint(0,N-1) 166             sample[rand] = best[:] 167         mini,index = minT_calc(sample) 168         best = sample[index][:] 169         print(best) 170     print(sample) 171  
172 if __name__ == "__main__": 173  main1() 174     """ 穷举搜索验证 """
175     a = list(itertools.permutations([1,2,3,4,5,6,7],7)) 176     ts = [] 177     first = [0,1,2,3,4,5,6,7,0] 178     for i in a: 179         temp = first+list(i) 180  temp.append(0) 181         t = time_calc(temp) 182  ts.append(t) 183     print(min(ts)) 184     print(time_calc([0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,0]))

一道工序有故障

这部分是学姐作的,学姐用了偏数学的思考方式,仍然从循环的角度去考虑,主要考虑故障发生是否会影响当前循环,是否须要创建新的循环。所以就没有写代码处理问题了。具体的思路我确实不是很能讲清楚。可是这里面有一个很是大的问题,就是若是出现多台CNC同时发生故障怎么办。关于多台机器同时发生故障的几率,咱们经过估算认为以给定的三组数据8小时内会出现这种特殊状况的可能性大约为30%。这个问题是我没法很好严格处理的(固然若是用贪心算法也就没这么多事了)。blog

两道工序无端障 & 两道工序有故障
这两个部分都是我来处理的,由于使用的方法大体相同,就并在一块儿说了。

两道工序与一道工序最大的区别在于三点:

一、开始要处理CNC任务分配:分配给第一道工序几台CNC,分配给第二道工序几台CNC?具体怎么布局?

二、加工过程可能仍然是一个循环,可是这个循环将可能会很是的庞大以致于不可能直观的看出来。

三、两道工序的分配已是一个严格的NP难问题了(即理论上没法在多项式时间内求得最优解)。

第一点个人想法很单纯——穷举,没错,就是穷举,除了显然不合适的分配方案外,其余方案都试一遍(虽然真的很蠢,可是我真的想不出到底能怎么办了)

第二点由于不存在循环则使用遗传算法须要设定一个至关长的染色体长度(咱们设定的染色体是RGV为各台CNC上下料的次序,若是要考虑全过程的模拟退火遗传算法,则染色体长度大约在300~400左右)。事实上我也尝试了这个方法,结果从我写完这个算法我开始跑,一直跑到比赛结束算法依旧没有收敛[捂脸]。这里给出代码仅供参考(各位朋友要是有好意见也能够提出) ↓↓↓

 1 # -*- coding:UTF-8 -*-
 2 """
 3  做者:囚生CY  4  平台:CSDN  5  时间:2018/10/09  6  转载请注明原做者  7  创做不易,仅供分享  8 """
 9 import random  10  
 11 # 第1组
 12 """
 13 d1 = 20  14 d2 = 33  15 d3 = 46  16 T1 = 400  17 T2 = 378  18 To = 28  19 Te = 31  20 Tc = 25  21 """
 22  
 23 # 第2组
 24 """
 25 d1 = 23  26 d2 = 41  27 d3 = 59  28 T1 = 280  29 T2 = 500  30 To = 30  31 Te = 35  32 Tc = 30  33 """
 34  
 35 # 第3组
 36 d1 = 18
 37 d2 = 32
 38 d3 = 46
 39 T1 = 455
 40 T2 = 182
 41 To = 27
 42 Te = 32
 43 Tc = 25
 44  
 45 cncT = [To,Te,To,Te,To,Te,To,Te]  46 tm = [  47  [0,0,d1,d1,d2,d2,d3,d3],  48  [0,0,d1,d1,d2,d2,d3,d3],  49  [d1,d1,0,0,d1,d1,d2,d2],  50  [d1,d1,0,0,d1,d1,d2,d2],  51  [d2,d2,d1,d1,0,0,d1,d1],  52  [d2,d2,d1,d1,0,0,d1,d1],  53  [d3,d3,d2,d2,d1,d1,0,0],  54  [d3,d3,d2,d2,d1,d1,0,0],  55 ]  56 Type = [1,0,1,0,1,0,0,0]                                                 # CNC刀具分类
 57  
 58 N = 64
 59 L = 100
 60 varP = 0.1
 61 croP = 0.6
 62 croL = 2
 63 e = 0.99
 64  
 65 def init_first_round():                                                     # 第一圈初始化(默认把全部第一道CNC按顺序加满再回到当前位置所有加满)
 66     state = [0 for i in range(8)]                                           # 记录CNC状态(还剩多少秒结束,0表示空闲)
 67     isEmpty = [1 for i in range(8)]                                         # CNC是否为空
 68     rgv = 0                                                                 # rgv状态(0表示空车,1表示载着半成品)
 69     currP = 0  70     total = 0  71     seq = []  72     flag = False  73     for i in range(len(Type)):  74         if Type[i]==0:  75  seq.append(i)  76             flag = True  77     currP = seq[0]  78  seq.append(currP)  79     rgv,currP,total = time_calc(seq,state,isEmpty,rgv,currP,total)  80     return state,isEmpty,rgv,currP,total,seq  81  
 82 def update(state,t):  83     for i in range(len(state)):  84         if state[i] < t:  85             state[i] = 0  86         else:  87             state[i] -= t  88  
 89 def time_calc(seq,state,isEmpty,rgv,currP,total):                         # 事实上sequence多是无效的,因此可能须要
 90     index = 0  91     temp = 0  92     while index<len(seq):  93         """ 先移动到下一个位置 """
 94         nextP = seq[index]  95         t = tm[currP][nextP]  96         total += t  97  update(state,t)  98         if Type[nextP]==0:                                                 # 若是下一个位置是第一道工做点
 99             if rgv==1:                                                     # 然而载着半成品
100                 seq.pop(index)                                             # 去掉这个元素并停止当次循环进入下一个循环
101                 continue                
102             if isEmpty[nextP]:                                             # 若是下一个位置是空的
103                 t = cncT[nextP] 104                 total += t 105  update(state,t) 106                 state[nextP] = T1                                         # 更新当前的CNC状态
107                 isEmpty[nextP] = 0                                         # 就不空闲了
108             else:                                                         # 若是没有空闲
109                 if state[nextP] > 0:                                     # 若是还在工做就等待结束
110                     t = state[nextP] 111                     total += t 112  update(state,t) 113                 t = cncT[nextP]                                             # 完成一次上下料
114                 total += t 115  update(state,t) 116                 state[nextP] = T1 117                 rgv = 1
118         else:                                                             # 若是下一个位置是第二道工做点
119             if rgv==0:                                                     # 若是是个空车
120                 seq.pop(index)                                             # 删除当前节点
121                 continue
122             if isEmpty[nextP]:                                             # 若是下一个位置是空的
123                 t = cncT[nextP] 124                 total += t 125  update(state,t) 126                 state[nextP] = T2 127                 isEmpty[nextP] = 0 128             else:                                                         # 若是没有空闲
129                 if state[nextP] > 0:                                     # 若是还在工做就等待结束
130                     t = state[nextP] 131                     total += t 132  update(state,t) 133                 t = cncT[nextP]+Tc 134                 total += t 135  update(state,t) 136                 state[nextP] = T2 137             rgv = 0 138         currP = nextP 139         temp = total 140         index += 1    
141     total += tm[currP][Type.index(0)]                                     # 最后归零
142     return rgv,currP,total 143  
144 def init_prob(sample,state,isEmpty,rgv,currP,total):                     # 计算全部sample的
145     prob = [] 146     for seq in sample: 147         t = time_calc(seq,state[:],isEmpty[:],rgv,currP,total)[-1] 148  prob.append(t) 149     maxi = max(prob) 150     prob = [maxi-prob[i]+1 for i in range(N)] 151     temp = 0 152     for p in prob: 153         temp += p 154     prob = [prob[i]/temp for i in range(N)] 155     for i in range(1,len(prob)): 156         prob[i] += prob[i-1] 157     prob[-1] = 1                                                         # 精度有时候很出问题
158     return prob 159  
160 def minT_calc(sample,state,isEmpty,rgv,currP,total): 161     minT = time_calc(sample[0],state[:],isEmpty[:],rgv,currP,total)[-1] 162     index = 0 163     for i in range(1,len(sample)): 164         t = time_calc(sample[i],state[:],isEmpty[:],rgv,currP,total)[-1] 165         if t < minT: 166             index = i 167             minT = t 168     return minT,index 169     
170 def init():                                                                 # 初始化种群(按照第二道工序,第一道工序,第二道工序,第一道工序顺序排列便可)
171     sample = [] 172     refer0 = [] 173     refer1 = [] 174     for i in range(8): 175         if Type[i]==0: 176  refer0.append(i) 177         else: 178  refer1.append(i) 179     for i in range(N): 180  sample.append([]) 181         for j in range(L): 182             if j%2==0: 183                 sample[-1].append(refer1[random.randint(0,len(refer1)-1)]) 184             else: 185                 sample[-1].append(refer0[random.randint(0,len(refer0)-1)]) 186     return sample 187  
188 def select(sample,prob):                                                 # 选择算子
189     sampleEX = [] 190     for i in range(N):                                                     # 取出N个样本
191         rand = random.random() 192         for j in range(len(prob)): 193             if rand<=prob[j]: 194  sampleEX.append(sample[j]) 195                 break
196     return sampleEX 197  
198 def cross(sample,i):                                                     # 交叉算子
199     for i in range(len(sample)-1): 200         for j in range(i,len(sample)): 201             rand = random.random() 202             if rand<=croP*(e**i):                                         # 执行交叉
203                 loc = random.randint(0,L-croL-1) 204                 temp1 = sample[i][loc:loc+croL] 205                 temp2 = sample[j][loc:loc+croL] 206                 for k in range(loc,loc+croL): 207                     sample[i][k] = temp2[k-loc] 208                     sample[j][k] = temp1[k-loc] 209     return sample 210         
211 def variance(sample,i):                                                     # 变异算子 
212     for i in range(len(sample)): 213         rand = random.random() 214         if rand<varP*(e**i): 215             rand1 = random.randint(0,L-1) 216             randTemp = random.randint(0,int(L/2)-1) 217             rand2 = 2*randTemp if rand1%2==0 else 2*randTemp+1
218             temp = sample[i][rand1] 219             sample[i][rand1] = sample[i][rand2] 220             sample[i][rand2] = temp 221     return sample 222  
223 if __name__ == "__main__": 224     state,isEmpty,rgv,currP,total,seq = init_first_round() 225     print(state,isEmpty,rgv,currP,total) 226     sample = init() 227     mini,index = minT_calc(sample,state[:],isEmpty[:],rgv,currP,total) 228     best = sample[index][:] 229     for i in range(100000): 230         f = open("GA.txt","a") 231         tmin = minT_calc(sample,state[:],isEmpty[:],rgv,currP,total)[0] 232         f.write("{}\t{}\n".format(i,tmin)) 233         print(i,"\t",tmin,end="\t") 234         prob = init_prob(sample,state[:],isEmpty[:],rgv,currP,total) 235         sample = select(sample,prob) 236         sample = cross(sample,i) 237         sample = variance(sample,i) 238         mi,index = minT_calc(sample,state[:],isEmpty[:],rgv,currP,total) 239         if mi>mini and random.random()<e**i:                             # 精英保留策略
240             rand = random.randint(0,N-1) 241             sample[rand] = best[:] 242         mini,index = minT_calc(sample,state[:],isEmpty[:],rgv,currP,total) 243         best = sample[index][:] 244         print(best) 245  f.close() 246     print(sample)

遗传算法这条路被堵死后我一度陷入俗套,用最直接的贪心搞了一阵子,以为用贪心算法(即考虑下一步的最优策略)实在是对不起这种比赛。而后我就变得——更贪心一点了。

我试图去寻找接下来K步最优的策略,而后走一步。K=1时算法退化为贪心算法,最终咱们设置为K=4(当K>=8时算法速度已经至关缓慢,而4~7的结果大体相同,且K=4的速度基本能够作到2秒内获得结果)。

值得注意的是我假设RGV在两道工序下只能由第一道工序的CNC到第二道工序的CNC(忽略清洗时间状况下),而后回到第一道工序的CNC,这样往复移动(这里我不说明为何必定要这样,可是我认为确实应该是这样)。在这个规律的引导下我大大减缩了代码量以及计算复杂度。

而后到第四种状况咱们已经没有多余时间了,只能延续使用状况三的算法,进行了随机模拟的修改,完成了第四种状况的填表。

如下是第三种状况的代码(第四种相似就不上传了)↓↓↓

 1 #coding=gbk
 2 import random  3 # -*- coding:UTF-8 -*-
 4 """
 5  做者:囚生CY  6  平台:CSDN  7  时间:2018/10/09  8  转载请注明原做者  9  创做不易,仅供分享  10 """
 11 from tranToXls import *
 12  
 13 # 第1组
 14 """
 15 d1 = 20  16 d2 = 33  17 d3 = 46  18 T1 = 400  19 T2 = 378  20 To = 28  21 Te = 31  22 Tc = 25  23 """
 24 # 第2组
 25  
 26 d1 = 23
 27 d2 = 41
 28 d3 = 59
 29 T1 = 280
 30 T2 = 500
 31 To = 30
 32 Te = 35
 33 Tc = 30
 34  
 35  
 36 # 第3组
 37  
 38 """
 39 d1 = 18  40 d2 = 32  41 d3 = 46  42 T1 = 455  43 T2 = 182  44 To = 27  45 Te = 32  46 Tc = 25  47 """
 48  
 49 cncT = [To,Te,To,Te,To,Te,To,Te]  50 tm = [  51  [0,0,d1,d1,d2,d2,d3,d3],  52  [0,0,d1,d1,d2,d2,d3,d3],  53  [d1,d1,0,0,d1,d1,d2,d2],  54  [d1,d1,0,0,d1,d1,d2,d2],  55  [d2,d2,d1,d1,0,0,d1,d1],  56  [d2,d2,d1,d1,0,0,d1,d1],  57  [d3,d3,d2,d2,d1,d1,0,0],  58  [d3,d3,d2,d2,d1,d1,0,0],  59 ]  60 Type = [0,1,0,1,1,1,0,1]                                                 # CNC刀具分类
 61  
 62 A = []                                                                     # 储存第一道工序的CNC编号
 63 B = []                                                                     # 储存第二道工序的CNC编号
 64 for i in range(len(Type)):  65     if Type[i]:  66  B.append(i)  67     else:  68  A.append(i)  69  
 70 def init_first_round():                                                     # 第一圈初始化(默认把全部第一道CNC按顺序加满再回到当前位置所有加满)
 71     state = [0 for i in range(8)]                                           # 记录CNC状态(还剩多少秒结束,0表示空闲)
 72     isEmpty = [1 for i in range(8)]                                         # CNC是否为空
 73     log = [0 for i in range(8)]                                             # 记录每台CNC正在加工第几件物料
 74     count1 = 0  75     rgv = 0                                                                 # rgv状态(0表示空车,1表示载着半成品)
 76     currP = 0  77     total = 0  78     seq = []  79     flag = False  80     for i in range(len(Type)):  81         if Type[i]==0:  82  seq.append(i)  83             flag = True  84     currP = seq[0]  85  seq.append(currP)  86     count1,rgv,currP,total = simulate(seq,state,isEmpty,log,count1,rgv,currP,total)  87     return state,isEmpty,log,count1,rgv,currP,total,seq  88  
 89 def update(state,t):  90     for i in range(len(state)):  91         if state[i] < t:  92             state[i] = 0  93         else:  94             state[i] -= t  95  
 96 def simulate(seq,state,isEmpty,log,count1,rgv,currP,total,fpath="log.txt"):    # 给定了一个序列模拟它的过程以及返回结果(主要用于模拟并记录)
 97     index = 0  98     temp = 0  99     pro1 = {}                                                             # 第一道工序的上下料开始时间
100     pro2 = {}                                                             # 第二道工序的上下料开始时间
101     f = open(fpath,"a") 102     while index<len(seq): 103         print(isEmpty) 104         nextP = seq[index] 105         t = tm[currP][nextP] 106         total += t 107  update(state,t) 108         if Type[nextP]==0:                                                 # 若是下一个位置是第一道工做点
109             count1 += 1
110             if isEmpty[nextP]:                                             # 若是下一个位置是空的
111                 f.write("第{}个物料的工序一上料开始时间为{}\tCNC编号为{}号\n".format(count1,total,nextP+1)) 112                 t = cncT[nextP] 113                 total += t 114  update(state,t) 115                 state[nextP] = T1                                         # 更新当前的CNC状态
116                 isEmpty[nextP] = 0                                         # 就不空闲了
117             else:                                                         # 若是没有空闲
118                 if state[nextP] > 0:                                     # 若是还在工做就等待结束
119                     t = state[nextP] 120                     total += t 121  update(state,t) 122                 f.write("第{}个物料的工序一下料开始时间为{}\tCNC编号为{}号\n".format(log[nextP],total,nextP+1)) 123                 f.write("第{}个物料的工序一上料开始时间为{}\tCNC编号为{}号\n".format(count1,total,nextP+1)) 124                 t = cncT[nextP]                                             # 完成一次上下料
125                 total += t 126  update(state,t) 127                 state[nextP] = T1 128                 rgv = log[nextP] 129             log[nextP] = count1 130         else:                                                             # 若是下一个位置是第二道工做点
131             if isEmpty[nextP]:                                             # 若是下一个位置是空的
132                 f.write("第{}个物料的工序二上料开始时间为{}\tCNC编号为{}号\n".format(rgv,total,nextP+1)) 133                 t = cncT[nextP] 134                 total += t 135  update(state,t) 136                 state[nextP] = T2 137                 isEmpty[nextP] = 0 138             else:                                                         # 若是没有空闲
139                 f.write("第{}个物料的工序二下料开始时间为{}\tCNC编号为{}号\n".format(log[nextP],total,nextP+1)) 140                 f.write("第{}个物料的工序二上料开始时间为{}\tCNC编号为{}号\n".format(rgv,total,nextP+1)) 141                 if state[nextP] > 0:                                     # 若是还在工做就等待结束
142                     t = state[nextP] 143                     total += t 144  update(state,t) 145                 t = cncT[nextP]+Tc 146                 total += t 147  update(state,t) 148                 state[nextP] = T2 149             log[nextP] = rgv 150             rgv = 0 151         currP = nextP 152         temp = total 153         index += 1    
154  f.close() 155     total += tm[currP][Type.index(0)]                                     # 最后归到起始点
156     return count1,rgv,currP,total 157  
158 def time_calc(seq,state,isEmpty,rgv,currP,total):                         # 主要用于记录时间
159     index = 0 160     temp = 0 161     while index<len(seq): 162         nextP = seq[index] 163         t = tm[currP][nextP] 164         total += t 165  update(state,t) 166         if Type[nextP]==0:                                                 # 若是下一个位置是第一道工做点
167             if rgv==1:                                                     # 然而载着半成品
168                 seq.pop(index)                                             # 去掉这个元素并停止当次循环进入下一个循环
169                 continue                
170             if isEmpty[nextP]:                                             # 若是下一个位置是空的
171                 t = cncT[nextP] 172                 total += t 173  update(state,t) 174                 state[nextP] = T1                                         # 更新当前的CNC状态
175                 isEmpty[nextP] = 0                                         # 就不空闲了
176             else:                                                         # 若是没有空闲
177                 if state[nextP] > 0:                                     # 若是还在工做就等待结束
178                     t = state[nextP] 179                     total += t 180  update(state,t) 181                 t = cncT[nextP]                                             # 完成一次上下料
182                 total += t 183  update(state,t) 184                 state[nextP] = T1 185                 rgv = 1
186         else:                                                             # 若是下一个位置是第二道工做点
187             if rgv==0:                                                     # 若是是个空车
188                 seq.pop(index)                                             # 删除当前节点
189                 continue
190             if isEmpty[nextP]:                                             # 若是下一个位置是空的
191                 t = cncT[nextP] 192                 total += t 193  update(state,t) 194                 state[nextP] = T2 195                 isEmpty[nextP] = 0 196             else:                                                         # 若是没有空闲
197                 if state[nextP] > 0:                                     # 若是还在工做就等待结束
198                     t = state[nextP] 199                     total += t 200  update(state,t) 201                 t = cncT[nextP]+Tc 202                 total += t 203  update(state,t) 204                 state[nextP] = T2 205             rgv = 0 206         currP = nextP 207         temp = total 208         index += 1    
209     return rgv,currP,total 210  
211 def forward1(state,isEmpty,currP):                                         # 一步最优
212     lists = [] 213     if currP in A: 214         rgv = 1
215         for e1 in B: 216  lists.append([e1]) 217     
218     else: 219         rgv = 0 220         for e1 in A: 221  lists.append([e1]) 222     
223     minV = 28800
224     for i in range(len(lists)): 225         t = time_calc(lists[i],state[:],isEmpty[:],rgv,currP,0)[-1] 226         if t<minV: 227             minV = t 228             index = i 229     return lists[index][0] 230  
231 def forward4(state,isEmpty,currP):                                         # 四步最优
232     lists = [] 233     """ 遍历全部的可能性 """
234     if currP in A:                                                         # 若是当前在第二道工序CNC的位置
235         rgv = 1
236         for e1 in B: 237             for e2 in A: 238                 for e3 in B: 239                     for e4 in A: 240  lists.append([e1,e2,e3,e4]) 241     else: 242         rgv = 0 243         for e1 in A: 244             for e2 in B: 245                 for e3 in A: 246                     for e4 in B: 247  lists.append([e1,e2,e3,e4]) 248     minV = 28800
249     for i in range(len(lists)): 250         t = time_calc(lists[i],state[:],isEmpty[:],rgv,currP,0)[-1] 251         if t<minV: 252             minV = t 253             index = i 254     return lists[index][0]                                                 # 给定下一步的4步计算最优
255  
256 def forward5(state,isEmpty,currP):                                         # 五步最优
257     lists = [] 258     """ 遍历全部的可能性 """
259     if currP in A:                                                         # 若是当前在第二道工序CNC的位置
260         rgv = 1
261         for e1 in B: 262             for e2 in A: 263                 for e3 in B: 264                     for e4 in A: 265                         for e5 in B: 266  lists.append([e1,e2,e3,e4,e5]) 267     else: 268         rgv = 0 269         for e1 in A: 270             for e2 in B: 271                 for e3 in A: 272                     for e4 in B: 273                         for e5 in A: 274  lists.append([e1,e2,e3,e4,e5]) 275     minV = 28800
276     for i in range(len(lists)): 277         t = time_calc(lists[i],state[:],isEmpty[:],rgv,currP,0)[-1] 278         if t<minV: 279             minV = t 280             index = i 281     return lists[index][0]                                                 # 给定下一步的5步计算最优
282  
283 def forward6(state,isEmpty,currP):                                         # 六步最优
284     lists = [] 285     """ 遍历全部的可能性 """
286     if currP in A:                                                         # 若是当前在第二道工序CNC的位置
287         rgv = 1
288         for e1 in B: 289             for e2 in A: 290                 for e3 in B: 291                     for e4 in A: 292                         for e5 in B: 293                             for e6 in A: 294  lists.append([e1,e2,e3,e4,e5,e6]) 295     else: 296         rgv = 0 297         for e1 in A: 298             for e2 in B: 299                 for e3 in A: 300                     for e4 in B: 301                         for e5 in A: 302                             for e6 in B: 303  lists.append([e1,e2,e3,e4,e5,e6]) 304     minV = 28800
305     for i in range(len(lists)): 306         t = time_calc(lists[i],state[:],isEmpty[:],rgv,currP,0)[-1] 307         if t<minV: 308             minV = t 309             index = i 310     return lists[index][0]                                                 # 给定下一步的6步计算最优
311  
312 def forward7(state,isEmpty,currP):                                         # 七步最优
313     lists = [] 314     """ 遍历全部的可能性 """
315     if currP in A:                                                         # 若是当前在第二道工序CNC的位置
316         rgv = 1
317         for e1 in B: 318             for e2 in A: 319                 for e3 in B: 320                     for e4 in A: 321                         for e5 in B: 322                             for e6 in A: 323                                 for e7 in B: 324  lists.append([e1,e2,e3,e4,e5,e6,e7]) 325     else: 326         rgv = 0 327         for e1 in A: 328             for e2 in B: 329                 for e3 in A: 330                     for e4 in B: 331                         for e5 in A: 332                             for e6 in B: 333                                 for e7 in A: 334  lists.append([e1,e2,e3,e4,e5,e6,e7]) 335     minV = 28800
336     for i in range(len(lists)): 337         t = time_calc(lists[i],state[:],isEmpty[:],rgv,currP,0)[-1] 338         if t<minV: 339             minV = t 340             index = i 341     return lists[index][0]                                                 # 给定下一步的7步计算最优
342  
343 def forward8(state,isEmpty,currP):                                         # 八步最优
344     lists = [] 345     """ 遍历全部的可能性 """
346     if currP in A:                                                         # 若是当前在第二道工序CNC的位置
347         rgv = 1
348         for e1 in B: 349             for e2 in A: 350                 for e3 in B: 351                     for e4 in A: 352                         for e5 in B: 353                             for e6 in A: 354                                 for e7 in B: 355                                     for e8 in A: 356  lists.append([e1,e2,e3,e4,e5,e6,e7,e8]) 357     else: 358         rgv = 0 359         for e1 in A: 360             for e2 in B: 361                 for e3 in A: 362                     for e4 in B: 363                         for e5 in A: 364                             for e6 in B: 365                                 for e7 in A: 366                                     for e8 in B: 367  lists.append([e1,e2,e3,e4,e5,e6,e7,e8]) 368     minV = 28800
369     for i in range(len(lists)): 370         t = time_calc(lists[i],state[:],isEmpty[:],rgv,currP,0)[-1] 371         if t<minV: 372             minV = t 373             index = i 374     return lists[index][0]                                                 # 给定下一步的8步计算最优
375  
376 def greedy(state,isEmpty,rgv,currP,total):                                 # 贪婪算法
377     line = [] 378     count = 0 379     while True: 380         #nextP = forward4(state[:],isEmpty[:],currP) 
381         nextP = forward5(state[:],isEmpty[:],currP) 382  line.append(nextP) 383         rgv,currP,t = time_calc([nextP],state,isEmpty,rgv,currP,0) 384         total += t 385         count += 1
386         if total>=28800: 387             break
388     return line 389  
390 if __name__ == "__main__": 391     state,isEmpty,log,count1,rgv,currP,total,seq = init_first_round() 392     print(state,isEmpty,log,count1,rgv,currP,total,seq) 393     line = greedy(state[:],isEmpty[:],rgv,currP,total) 394  simulate(line,state,isEmpty,log,count1,rgv,currP,total) 395     
396     write_xlsx()

后记

此次博客有点赶,因此质量有点差,不少点没有具体说清楚。主要最近事情比较多。原本也没想写这篇博客,可是以为人仍是要有始有终,虽然没有人来阅读,可是学习的路上仍是要多作小结,另外也是万一有须要的朋友也能够给一些参考。虽然个人水平不好劲,可是我但愿可以经过交流学习提升更多人包括我本身的水平。不喜勿喷!

--------------------- 版权声明:本文为CSDN博主「囚生CY」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处连接及本声明。原文连接:https://blog.csdn.net/CY19980216/article/details/82893846

相关文章
相关标签/搜索