问题1,基于订单之间的相似度,对所有订单排序,然后在满足N<200的情况下取批次,最终取出的批次有85批。
代码如下:
import pandas as pd
import numpy as np
data=pd.read_csv('D:\Desktop\A题 附件/附件1:订单信息.csv')
Matrix=np.zeros((923,1941),dtype='int') # 每一行代表一个订单,
from tqdm import tqdm
for i in tqdm(range(0,20340)):
for j in range(1,924):
if data.iloc[i,0]=='D'+'%04d'%j :
x=data.iloc[i,1]
x=x[1:]
x=int(x)
Matrix[j-1,x-1]=1
np.savetxt('./Matrix.txt',Matrix,delimiter=' ',fmt='%d')
matrix=np.loadtxt('./Matrix.txt')
list=[]
U=np.zeros((923,923))
for i in tqdm(range(0,923)):
for j in range(0,923):
x=np.sum(abs(matrix[j]-matrix[i]))
list.append(x)
sorted_id = sorted(range(len(list)), key=lambda k: list[k])
U[i]=np.array(sorted_id)
list=[]
U=U+1
np.savetxt('./Distance.txt',U,delimiter=' ',fmt='%d')
Matrix=np.loadtxt('./Distance.txt')
list=[1]
k=0
z=1
y=np.zeros((1,1941))
for i in tqdm(range(1000000)):
x= Matrix[k][z]
if x not in list :
list.append(int(x)) #list直接储存订单
z=1
else:
z=z+1
if z==923:
break
continue
k=int(x-1)
np.savetxt('./list.txt',list,delimiter=' ',fmt='%d')
问题2按照蒙特卡洛随机的思想,对每批次内的货品随机排列,并计算距离的最小值,以此作为排列的最优值。
问题3按照指派问题的解决思想,对批次内的订单按距离生成效率矩阵,然后用linear_sum_assignment()函数求解,并分配订单。
问题2和问题3的代码如下:直接一步到位,将结果填入CSV中。
import csv
import random
import copy
import os
import numpy as np
from scipy import optimize
import numpy
import pandas as pd
from tqdm import tqdm
data=pd.read_csv('D:\Desktop\A题 附件/附件1:订单信息.csv')
# print(data)
data=np.array(data)
num_order=[]
for i in range(len(data)):
if data[i][0] not in num_order:
num_order.append(data[i][0])
# print(len(num_order))
# print(num_order)
list_all = []
for i in num_order:
list_r = []
for j in range(len(data)):
if str(i) == str(data[j][0]):
list_r.append(data[j][1])
list_all.append(list_r)
data1=np.loadtxt('./list.txt')
data1=data1
listp=[]
list2=[]
list1=[]
for i in range(len(data1)):
for j in list_all[int(data1[i])]:
if j not in list2:
list2.append(j)
if len(list2)<=200:
list1.append(int(data1[i]))
else:
listp.append(list1)
list1=[]
list2=[]
for j in list_all[int(data1[i])]:
if j not in list2:
list2.append(j)
list1.append(int(data1[i]))
if i == len(data1)-1:
list1 = []
list1.append(int(data1[i]))
listp.append(list1)
list_aall= [] # 结果2所有的记录,即将这个数据直接写入结果2中
list_3_aall=[] # 结果3所有的记录,即将这个结果直接写入结果3中
for m in tqdm(range(len(listp))):
if len(listp[m])==1:
number=listp[m][0]
list_p = []
for j in list_all[int(number)]:
if j not in list_p:
list_p.append(j)
# print(len(list_p), list_p)
sorted(list_p)
# print(list_p)
list_num = []
for i in range(len(list_p)):
list_num.append(i + 1)
# 直接写入结果2
list_last_all = []
for i in range(len(list_p)):
list_last = []
list_last.append(list_p[i])
list_last.append(m + 1)
list_last.append(i + 1)
list_last_all.append(list_last)
for i in list_last_all:
list_aall.append(i)
# 直接写入结果3
list_3=[]
list_3_all=[]
list_3.append('D%04d'%(number+1))
list_3.append(m+1)
list_3.append(1)
list_3.append(1)
list_3_all.append(list_3)
for i in range(len(list_3_all)):
list_3_aall.append(list_3_all[i])
path = './result2.csv'
f = open(r'{}'.format(path), 'w', encoding='gbk', newline='')
r1 = csv.writer(f)
header = ['ItemNo', 'GroupNo', 'ShelfNo']
r1.writerow(header)
r1.writerows(list_aall)
f.close()
path = './result3.csv'
f = open(r'{}'.format(path), 'w', encoding='gbk', newline='')
r1 = csv.writer(f)
header = ['OrderNo', 'GroupNo', 'WorkerNo', 'TaskNo']
r1.writerow(header)
r1.writerows(list_3_aall)
f.close()
else:
list_p=[]
for i in listp[m]:
for j in list_all[int(i)]:
if j not in list_p:
list_p.append(j)
# print(len(list_p),list_p)
sorted(list_p)
# print(list_p)
list_num=[]
for i in range(len(list_p)):
list_num.append(i+1)
vecdict=dict(zip(list_p,list_num))
# print(vecdict)
num_all=[]
for i in listp[m]:
num_r=[]
for j in list_all[int(i)]:
num_r.append(vecdict[j])
num_all.append(num_r)
# print(num_all)
x=[i+1 for i in range(len(list_num))]
# print(x)
x_all=[]
minx_all=[]
dic_all=[]
for i in range(200*200):
np.random.shuffle(x)
x_all.append(x)
dic=[]
for p in range(len(num_all)):
listx = []
for j in num_all[p]:
listx.append(x.index(j))
dicm=max(listx)-min(listx)
dic.append(dicm)
dic_all.append(dic)
# print(dic)
minx=sum(dic)
# print(minx)
minx_all.append(minx)
# print(min(minx_all))
# print(x_all[minx_all.index(min(minx_all))])
mindic=min(minx_all)
minindex=minx_all.index(mindic)
vecdict=dict((val,key) for key,val in vecdict.items())
list_name=[]
for i in range(len(x_all[minindex])):
list_name.append(vecdict[x_all[minindex][i]])
# print(list_name)
path='./result2.csv'
f=open(r'{}'.format(path),'w',encoding='gbk',newline='')
r1=csv.writer(f)
header=['ItemNo','GroupNo','ShelfNo']
list_last_all=[]
for i in range(len(list_name)):
list_last = []
list_last.append(list_name[i])
list_last.append(m+1)
list_last.append(i+1)
list_last_all.append(list_last)
for i in list_last_all:
list_aall.append(i)
r1.writerow(header)
r1.writerows(list_aall)
f.close()
# 完成一个批次内的订单指派
# 首先生成指派矩阵
# 分拣员人数
n=5
num=len(listp[m])
# print('oring len:',num)
num1=0
add_num=0
if num%n!=0:
add_num=int(num/n)+1
add_num=int(add_num*n)
else:add_num=int(num)
# print(add_num)
if num<=add_num:
num1=add_num
# print(num1,add_num)
x=copy.deepcopy(dic_all[minindex])
# print(x)
# print('dic long', len(x))
if num
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)