杨利霞 发表于 2019-4-12 16:18

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

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

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

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

问题分析

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

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

问题一共有四种不同的情况:一道工序无故障,一道工序有故障,两道工序无故障,两道工序有故障。

一道工序无故障

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

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

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

以下给出第一种情况的模拟退火遗传算法算法以及对应的穷举最优证明 ↓↓↓
# -*- coding:UTF-8 -*-
"""
        作者:囚生CY
        平台:CSDN
        时间:2018/10/09
        转载请注明原作者
        创作不易,仅供分享
"""

import math
import random
import itertools

""" 选取一组数据 """
T = 580
d1 = 23
d2 = 41
d3 = 59
Te = 35
To = 30
Tc = 30

CNCT =                                                                                  # CNC上下料时间

N = 50
L = 17

varP = 0.1
croP = 0.6

croL = 4
e = 0.99

tm = [
        ,
        ,
        ,
        ,
        ,
        ,
        ,
        ,
]

def update_state(state,t):
        length = len(state)
        for i in range(length):
                if state < t:
                        state = 0
                else:
                        state -= t
        return state

def time_calc(seq):
        state =                                                                                   # 记录CNC状态
        isEmpty =                                                                                  # CNC是否为空?
        currP = 0
        total = 0
        length = len(seq)
        for No in seq:
                nextP = No
                t = tm
                total += t                                                                                                                 # rgv移动
                state = update_state(state,t)                                                                         # 更新state
                if state==0:                                                                                                 # 表明CNC等待
                        if isEmpty:                                                                                                 # 当前CNC空
                                t = CNCT
                                isEmpty = 0
                        else:
                                t = CNCT+Tc
                        total += t
                        state = update_state(state,t)
                        state = T
                else:                                                                                                                         # 当前CNC忙
                        total += state                                                                                         # 先等当前CNC结束
                        state = update_state(state,state)                                                 
                        t = CNCT+Tc
                        total += t
                        state = update_state(state,t)
                        state = T
                currP = No
        total += tm
        return total

def init_prob(sample):
        prob = []
        for seq in sample:
                prob.append(time_calc(seq))
        maxi = max(prob)
        prob = +1 for i in range(N)]
        temp = 0
        for p in prob:
                temp += p
        prob = /temp for i in range(N)]
        for i in range(1,len(prob)):
                prob += prob
        prob[-1] = 1                                                                                                                 # 精度有时候很出问题
        return prob

def minT_calc(sample):
        minT = time_calc(sample)
        index = 0
        for i in range(1,len(sample)):
                t = time_calc(sample)
                if t < minT:
                        index = i
                        minT = t
        return minT,index
       
def init():
        sample = []
        for i in range(N):
                sample.append([])
                for j in range(L):
                        sample[-1].append(random.randint(0,7))
        return sample

def select(sample,prob):                                                                                                 # 选择
        sampleEX = []
        for i in range(N):                                                                                                         # 取出N个样本
                rand = random.random()
                for j in range(len(prob)):
                        if rand<=prob:
                                sampleEX.append(sample)
                                break
        return sampleEX

def cross(sample,i):                                                                                                         # 交叉
        for i in range(len(sample)-1):
                for j in range(i,len(sample)):
                        rand = random.random()
                        if rand<=croP*(e**i):                                                                                 # 执行交叉
                                loc = random.randint(0,L-croL-1)
                                temp1 = sample
                                temp2 = sample
                                for k in range(loc,loc+croL):
                                        sample = temp2
                                        sample = temp1
        return sample
               
def variance(sample,i):                                                                                                         # 变异算子                                                                                 
        for i in range(len(sample)):
                rand = random.random()
                if rand<varP*(e**i):
                        rand1 = random.randint(0,L-1)
                        rand2 = random.randint(0,L-1)
                        temp = sample
                        sample = sample
                        sample = temp
        return sample
       
def main():
        sample = init()
        mini,index = minT_calc(sample)
        best = sample[:]
        print(best)
        for i in range(10000):
                print(i,'\t',minT_calc(sample),end="\t")
                prob = init_prob(sample)
                sample = select(sample,prob)
                sample = cross(sample,i)
                sample = variance(sample,i)
                mi,index = minT_calc(sample)
                if mi>mini and random.random()<e**i:                                                         # 精英保留策略
                        rand = random.randint(0,N-1)
                        sample = best[:]
                mini,index = minT_calc(sample)
                best = sample[:]
                print(best)
        print(sample)

if __name__ == "__main__":
        main1()
        """ 穷举搜索验证 """
        a = list(itertools.permutations(,7))
        ts = []
        first =
        for i in a:
                temp = first+list(i)
                temp.append(0)
                t = time_calc(temp)
                ts.append(t)
        print(min(ts))       
        print(time_calc())
       

一道工序有故障

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

两道工序无故障 & 两道工序有故障

这两个部分都是我来处理的,因为使用的方法大致相同,就并在一起说了。

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

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

2、加工过程可能仍然是一个循环,但是这个循环将可能会非常的庞大以至于不可能直观的看出来。

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

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

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

# -*- coding:UTF-8 -*-
"""
        作者:囚生CY
        平台:CSDN
        时间:2018/10/09
        转载请注明原作者
        创作不易,仅供分享
"""
import random

# 第1组
"""
d1 = 20
d2 = 33
d3 = 46
T1 = 400
T2 = 378
To = 28
Te = 31
Tc = 25
"""

# 第2组
"""
d1 = 23
d2 = 41
d3 = 59
T1 = 280
T2 = 500
To = 30
Te = 35
Tc = 30
"""

# 第3组
d1 = 18
d2 = 32
d3 = 46
T1 = 455
T2 = 182
To = 27
Te = 32
Tc = 25

cncT =
tm = [
        ,
        ,
        ,
        ,
        ,
        ,
        ,
        ,
]
Type =                                                                                                  # CNC刀具分类

N = 64
L = 100
varP = 0.1
croP = 0.6
croL = 2
e = 0.99

def init_first_round():                                                                                                         # 第一圈初始化(默认把所有第一道CNC按顺序加满再回到当前位置全部加满)
        state =                                                                                   # 记录CNC状态(还剩多少秒结束,0表示空闲)
        isEmpty =                                                                                  # CNC是否为空
        rgv = 0                                                                                                                                 # rgv状态(0表示空车,1表示载着半成品)
        currP = 0
        total = 0
        seq = []
        flag = False
        for i in range(len(Type)):
                if Type==0:
                        seq.append(i)
                        flag = True
        currP = seq
        seq.append(currP)
        rgv,currP,total = time_calc(seq,state,isEmpty,rgv,currP,total)
        return state,isEmpty,rgv,currP,total,seq

def update(state,t):
        for i in range(len(state)):
                if state < t:
                        state = 0
                else:
                        state -= t

def time_calc(seq,state,isEmpty,rgv,currP,total):                                                 # 事实上sequence可能是无效的,所以可能需要
        index = 0
        temp = 0
        while index<len(seq):
                """ 先移动到下一个位置 """
                nextP = seq
                t = tm
                total += t
                update(state,t)
                if Type==0:                                                                                                 # 如果下一个位置是第一道工作点
                        if rgv==1:                                                                                                         # 然而载着半成品
                                seq.pop(index)                                                                                         # 去掉这个元素并中止当次循环进入下一个循环
                                continue                               
                        if isEmpty:                                                                                         # 如果下一个位置是空的
                                t = cncT
                                total += t
                                update(state,t)
                                state = T1                                                                                 # 更新当前的CNC状态
                                isEmpty = 0                                                                                 # 就不空闲了
                        else:                                                                                                                 # 如果没有空闲
                                if state > 0:                                                                         # 如果还在工作就等待结束
                                        t = state
                                        total += t
                                        update(state,t)
                                t = cncT                                                                                         # 完成一次上下料
                                total += t
                                update(state,t)
                                state = T1
                                rgv = 1
                else:                                                                                                                         # 如果下一个位置是第二道工作点
                        if rgv==0:                                                                                                         # 如果是个空车
                                seq.pop(index)                                                                                         # 删除当前节点
                                continue
                        if isEmpty:                                                                                         # 如果下一个位置是空的
                                t = cncT
                                total += t
                                update(state,t)
                                state = T2
                                isEmpty = 0       
                        else:                                                                                                                 # 如果没有空闲
                                if state > 0:                                                                         # 如果还在工作就等待结束
                                        t = state
                                        total += t
                                        update(state,t)
                                t = cncT+Tc
                                total += t
                                update(state,t)
                                state = T2
                        rgv = 0
                currP = nextP
                temp = total
                index += 1       
        total += tm                                                                         # 最后归零
        return rgv,currP,total

def init_prob(sample,state,isEmpty,rgv,currP,total):                                         # 计算所有sample的
        prob = []
        for seq in sample:
                t = time_calc(seq,state[:],isEmpty[:],rgv,currP,total)[-1]
                prob.append(t)
        maxi = max(prob)
        prob = +1 for i in range(N)]
        temp = 0
        for p in prob:
                temp += p
        prob = /temp for i in range(N)]
        for i in range(1,len(prob)):
                prob += prob
        prob[-1] = 1                                                                                                                 # 精度有时候很出问题
        return prob

def minT_calc(sample,state,isEmpty,rgv,currP,total):
        minT = time_calc(sample,state[:],isEmpty[:],rgv,currP,total)[-1]
        index = 0
        for i in range(1,len(sample)):
                t = time_calc(sample,state[:],isEmpty[:],rgv,currP,total)[-1]
                if t < minT:
                        index = i
                        minT = t
        return minT,index
       
def init():                                                                                                                                 # 初始化种群(按照第二道工序,第一道工序,第二道工序,第一道工序顺序排列即可)
        sample = []
        refer0 = []
        refer1 = []
        for i in range(8):
                if Type==0:
                        refer0.append(i)
                else:
                        refer1.append(i)
        for i in range(N):
                sample.append([])
                for j in range(L):
                        if j%2==0:
                                sample[-1].append(refer1)
                        else:
                                sample[-1].append(refer0)
        return sample

def select(sample,prob):                                                                                                 # 选择算子
        sampleEX = []
        for i in range(N):                                                                                                         # 取出N个样本
                rand = random.random()
                for j in range(len(prob)):
                        if rand<=prob:
                                sampleEX.append(sample)
                                break
        return sampleEX

def cross(sample,i):                                                                                                         # 交叉算子
        for i in range(len(sample)-1):
                for j in range(i,len(sample)):
                        rand = random.random()
                        if rand<=croP*(e**i):                                                                                 # 执行交叉
                                loc = random.randint(0,L-croL-1)
                                temp1 = sample
                                temp2 = sample
                                for k in range(loc,loc+croL):
                                        sample = temp2
                                        sample = temp1
        return sample
               
def variance(sample,i):                                                                                                         # 变异算子                                                                                 
        for i in range(len(sample)):
                rand = random.random()
                if rand<varP*(e**i):
                        rand1 = random.randint(0,L-1)
                        randTemp = random.randint(0,int(L/2)-1)
                        rand2 = 2*randTemp if rand1%2==0 else 2*randTemp+1
                        temp = sample
                        sample = sample
                        sample = temp
        return sample

if __name__ == "__main__":
        state,isEmpty,rgv,currP,total,seq = init_first_round()
        print(state,isEmpty,rgv,currP,total)
        sample = init()
        mini,index = minT_calc(sample,state[:],isEmpty[:],rgv,currP,total)       
        best = sample[:]
        for i in range(100000):
                f = open("GA.txt","a")
                tmin = minT_calc(sample,state[:],isEmpty[:],rgv,currP,total)
                f.write("{}\t{}\n".format(i,tmin))
                print(i,"\t",tmin,end="\t")
                prob = init_prob(sample,state[:],isEmpty[:],rgv,currP,total)
                sample = select(sample,prob)
                sample = cross(sample,i)
                sample = variance(sample,i)
                mi,index = minT_calc(sample,state[:],isEmpty[:],rgv,currP,total)
                if mi>mini and random.random()<e**i:                                                         # 精英保留策略
                        rand = random.randint(0,N-1)
                        sample = best[:]
                mini,index = minT_calc(sample,state[:],isEmpty[:],rgv,currP,total)
                best = sample[:]
                print(best)
                f.close()
        print(sample)
遗传算法这条路被堵死后我一度陷入俗套,用最直接的贪心搞了一阵子,觉得用贪心算法(即考虑下一步的最优策略)实在是对不起这种比赛。然后我就变得——更贪心一点了。

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

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

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

以下是第三种情况的代码(第四种类似就不上传了)↓↓↓

#coding=gbk
import random
# -*- coding:UTF-8 -*-
"""
        作者:囚生CY
        平台:CSDN
        时间:2018/10/09
        转载请注明原作者
        创作不易,仅供分享
"""
from tranToXls import *

# 第1组
"""
d1 = 20
d2 = 33
d3 = 46
T1 = 400
T2 = 378
To = 28
Te = 31
Tc = 25
"""
# 第2组

d1 = 23
d2 = 41
d3 = 59
T1 = 280
T2 = 500
To = 30
Te = 35
Tc = 30


# 第3组

"""
d1 = 18
d2 = 32
d3 = 46
T1 = 455
T2 = 182
To = 27
Te = 32
Tc = 25
"""

cncT =
tm = [
        ,
        ,
        ,
        ,
        ,
        ,
        ,
        ,
]
Type =                                                                                                  # CNC刀具分类

A = []                                                                                                                                         # 储存第一道工序的CNC编号
B = []                                                                                                                                         # 储存第二道工序的CNC编号
for i in range(len(Type)):
        if Type:
                B.append(i)
        else:
                A.append(i)

def init_first_round():                                                                                                         # 第一圈初始化(默认把所有第一道CNC按顺序加满再回到当前位置全部加满)
        state =                                                                                   # 记录CNC状态(还剩多少秒结束,0表示空闲)
        isEmpty =                                                                                  # CNC是否为空
        log =                                                                                          # 记录每台CNC正在加工第几件物料
        count1 = 0
        rgv = 0                                                                                                                                 # rgv状态(0表示空车,1表示载着半成品)
        currP = 0
        total = 0
        seq = []
        flag = False
        for i in range(len(Type)):
                if Type==0:
                        seq.append(i)
                        flag = True
        currP = seq
        seq.append(currP)
        count1,rgv,currP,total = simulate(seq,state,isEmpty,log,count1,rgv,currP,total)
        return state,isEmpty,log,count1,rgv,currP,total,seq

def update(state,t):
        for i in range(len(state)):
                if state < t:
                        state = 0
                else:
                        state -= t

def simulate(seq,state,isEmpty,log,count1,rgv,currP,total,fpath="log.txt"):        # 给定了一个序列模拟它的过程以及返回结果(主要用于模拟并记录)
        index = 0
        temp = 0
        pro1 = {}                                                                                                                         # 第一道工序的上下料开始时间
        pro2 = {}                                                                                                                         # 第二道工序的上下料开始时间
        f = open(fpath,"a")
        while index<len(seq):
                print(isEmpty)
                nextP = seq
                t = tm
                total += t
                update(state,t)
                if Type==0:                                                                                                 # 如果下一个位置是第一道工作点
                        count1 += 1
                        if isEmpty:                                                                                         # 如果下一个位置是空的
                                f.write("第{}个物料的工序一上料开始时间为{}\tCNC编号为{}号\n".format(count1,total,nextP+1))
                                t = cncT
                                total += t
                                update(state,t)
                                state = T1                                                                                 # 更新当前的CNC状态
                                isEmpty = 0                                                                                 # 就不空闲了
                        else:                                                                                                                 # 如果没有空闲
                                if state > 0:                                                                         # 如果还在工作就等待结束
                                        t = state
                                        total += t
                                        update(state,t)
                                f.write("第{}个物料的工序一下料开始时间为{}\tCNC编号为{}号\n".format(log,total,nextP+1))
                                f.write("第{}个物料的工序一上料开始时间为{}\tCNC编号为{}号\n".format(count1,total,nextP+1))
                                t = cncT                                                                                         # 完成一次上下料
                                total += t
                                update(state,t)
                                state = T1
                                rgv = log
                        log = count1
                else:                                                                                                                         # 如果下一个位置是第二道工作点
                        if isEmpty:                                                                                         # 如果下一个位置是空的
                                f.write("第{}个物料的工序二上料开始时间为{}\tCNC编号为{}号\n".format(rgv,total,nextP+1))
                                t = cncT
                                total += t
                                update(state,t)
                                state = T2
                                isEmpty = 0       
                        else:                                                                                                                 # 如果没有空闲
                                f.write("第{}个物料的工序二下料开始时间为{}\tCNC编号为{}号\n".format(log,total,nextP+1))
                                f.write("第{}个物料的工序二上料开始时间为{}\tCNC编号为{}号\n".format(rgv,total,nextP+1))
                                if state > 0:                                                                         # 如果还在工作就等待结束
                                        t = state
                                        total += t
                                        update(state,t)
                                t = cncT+Tc
                                total += t
                                update(state,t)
                                state = T2
                        log = rgv
                        rgv = 0
                currP = nextP
                temp = total
                index += 1       
        f.close()
        total += tm                                                                         # 最后归到起始点
        return count1,rgv,currP,total

def time_calc(seq,state,isEmpty,rgv,currP,total):                                                 # 主要用于记录时间
        index = 0
        temp = 0
        while index<len(seq):
                nextP = seq
                t = tm
                total += t
                update(state,t)
                if Type==0:                                                                                                 # 如果下一个位置是第一道工作点
                        if rgv==1:                                                                                                         # 然而载着半成品
                                seq.pop(index)                                                                                         # 去掉这个元素并中止当次循环进入下一个循环
                                continue                               
                        if isEmpty:                                                                                         # 如果下一个位置是空的
                                t = cncT
                                total += t
                                update(state,t)
                                state = T1                                                                                 # 更新当前的CNC状态
                                isEmpty = 0                                                                                 # 就不空闲了
                        else:                                                                                                                 # 如果没有空闲
                                if state > 0:                                                                         # 如果还在工作就等待结束
                                        t = state
                                        total += t
                                        update(state,t)
                                t = cncT                                                                                         # 完成一次上下料
                                total += t
                                update(state,t)
                                state = T1
                                rgv = 1
                else:                                                                                                                         # 如果下一个位置是第二道工作点
                        if rgv==0:                                                                                                         # 如果是个空车
                                seq.pop(index)                                                                                         # 删除当前节点
                                continue
                        if isEmpty:                                                                                         # 如果下一个位置是空的
                                t = cncT
                                total += t
                                update(state,t)
                                state = T2
                                isEmpty = 0       
                        else:                                                                                                                 # 如果没有空闲
                                if state > 0:                                                                         # 如果还在工作就等待结束
                                        t = state
                                        total += t
                                        update(state,t)
                                t = cncT+Tc
                                total += t
                                update(state,t)
                                state = T2
                        rgv = 0
                currP = nextP
                temp = total
                index += 1       
        return rgv,currP,total

def forward1(state,isEmpty,currP):                                                                                 # 一步最优
        lists = []
        if currP in A:
                rgv = 1
                for e1 in B:
                        lists.append()
       
        else:
                rgv = 0
                for e1 in A:
                        lists.append()
       
        minV = 28800
        for i in range(len(lists)):
                t = time_calc(lists,state[:],isEmpty[:],rgv,currP,0)[-1]
                if t<minV:
                        minV = t
                        index = i
        return lists

def forward4(state,isEmpty,currP):                                                                                 # 四步最优
        lists = []
        """ 遍历所有的可能性 """
        if currP in A:                                                                                                                 # 如果当前在第二道工序CNC的位置
                rgv = 1
                for e1 in B:
                        for e2 in A:
                                for e3 in B:
                                        for e4 in A:
                                                lists.append()
        else:
                rgv = 0
                for e1 in A:
                        for e2 in B:
                                for e3 in A:
                                        for e4 in B:
                                                lists.append()
        minV = 28800
        for i in range(len(lists)):
                t = time_calc(lists,state[:],isEmpty[:],rgv,currP,0)[-1]
                if t<minV:
                        minV = t
                        index = i
        return lists                                                                                                 # 给定下一步的4步计算最优

def forward5(state,isEmpty,currP):                                                                                 # 五步最优
        lists = []
        """ 遍历所有的可能性 """
        if currP in A:                                                                                                                 # 如果当前在第二道工序CNC的位置
                rgv = 1
                for e1 in B:
                        for e2 in A:
                                for e3 in B:
                                        for e4 in A:
                                                for e5 in B:
                                                        lists.append()
        else:
                rgv = 0
                for e1 in A:
                        for e2 in B:
                                for e3 in A:
                                        for e4 in B:
                                                for e5 in A:
                                                        lists.append()
        minV = 28800
        for i in range(len(lists)):
                t = time_calc(lists,state[:],isEmpty[:],rgv,currP,0)[-1]
                if t<minV:
                        minV = t
                        index = i
        return lists                                                                                                 # 给定下一步的5步计算最优

def forward6(state,isEmpty,currP):                                                                                 # 六步最优
        lists = []
        """ 遍历所有的可能性 """
        if currP in A:                                                                                                                 # 如果当前在第二道工序CNC的位置
                rgv = 1
                for e1 in B:
                        for e2 in A:
                                for e3 in B:
                                        for e4 in A:
                                                for e5 in B:
                                                        for e6 in A:
                                                                lists.append()
        else:
                rgv = 0
                for e1 in A:
                        for e2 in B:
                                for e3 in A:
                                        for e4 in B:
                                                for e5 in A:
                                                        for e6 in B:
                                                                lists.append()
        minV = 28800
        for i in range(len(lists)):
                t = time_calc(lists,state[:],isEmpty[:],rgv,currP,0)[-1]
                if t<minV:
                        minV = t
                        index = i
        return lists                                                                                                 # 给定下一步的6步计算最优

def forward7(state,isEmpty,currP):                                                                                 # 七步最优
        lists = []
        """ 遍历所有的可能性 """
        if currP in A:                                                                                                                 # 如果当前在第二道工序CNC的位置
                rgv = 1
                for e1 in B:
                        for e2 in A:
                                for e3 in B:
                                        for e4 in A:
                                                for e5 in B:
                                                        for e6 in A:
                                                                for e7 in B:
                                                                        lists.append()
        else:
                rgv = 0
                for e1 in A:
                        for e2 in B:
                                for e3 in A:
                                        for e4 in B:
                                                for e5 in A:
                                                        for e6 in B:
                                                                for e7 in A:
                                                                        lists.append()
        minV = 28800
        for i in range(len(lists)):
                t = time_calc(lists,state[:],isEmpty[:],rgv,currP,0)[-1]
                if t<minV:
                        minV = t
                        index = i
        return lists                                                                                                 # 给定下一步的7步计算最优

def forward8(state,isEmpty,currP):                                                                                 # 八步最优
        lists = []
        """ 遍历所有的可能性 """
        if currP in A:                                                                                                                 # 如果当前在第二道工序CNC的位置
                rgv = 1
                for e1 in B:
                        for e2 in A:
                                for e3 in B:
                                        for e4 in A:
                                                for e5 in B:
                                                        for e6 in A:
                                                                for e7 in B:
                                                                        for e8 in A:
                                                                                lists.append()
        else:
                rgv = 0
                for e1 in A:
                        for e2 in B:
                                for e3 in A:
                                        for e4 in B:
                                                for e5 in A:
                                                        for e6 in B:
                                                                for e7 in A:
                                                                        for e8 in B:
                                                                                lists.append()
        minV = 28800
        for i in range(len(lists)):
                t = time_calc(lists,state[:],isEmpty[:],rgv,currP,0)[-1]
                if t<minV:
                        minV = t
                        index = i
        return lists                                                                                                 # 给定下一步的8步计算最优

def greedy(state,isEmpty,rgv,currP,total):                                                                 # 贪婪算法
        line = []
        count = 0
        while True:
                #nextP = forward4(state[:],isEmpty[:],currP)               
                nextP = forward5(state[:],isEmpty[:],currP)               
                line.append(nextP)
                rgv,currP,t = time_calc(,state,isEmpty,rgv,currP,0)
                total += t
                count += 1
                if total>=28800:
                        break
        return line

if __name__ == "__main__":
        state,isEmpty,log,count1,rgv,currP,total,seq = init_first_round()
        print(state,isEmpty,log,count1,rgv,currP,total,seq)
        line = greedy(state[:],isEmpty[:],rgv,currP,total)
        simulate(line,state,isEmpty,log,count1,rgv,currP,total)
       
        write_xlsx()
后记

这次博客有点赶,所以质量有点差,很多点没有具体说清楚。主要最近事情比较多。本来也没想写这篇博客,但是觉得人还是要善始善终,虽然没有人来阅读,但是学习的路上还是要多做小结,另外也是万一有需要的朋友也可以给一些参考。虽然我的水平很差劲,但是我希望能够通过交流学习提高更多人包括我自己的水平。不喜勿喷!
---------------------









页: [1]
查看完整版本: 【项目总结】2018年全国大学生数学建模大赛B题简要分析(附代码)