接前一文章
部分关键代码及方法:
程序界面代码:
win=Tk()
win.config(bg='#F2F2D7')
win.geometry('550x440')
win.title('最优排班计算器')frame=Frame(win,width=500,height=60,bg='#F2F2D7')
frame.grid(row=0,column=0,columnspan=5)
l1=Label(frame,text='选择文件:',font=('bold',14),fg='#f00',bg='#F2F1D7')
l1.place(x=20,y=25)
txt=StringVar()
entry=Entry(frame,width=40,textvariable=txt,relief='groove')
entry.place(x=120,y=20,width=220,height=30)
btn1=Button(frame,text='浏览',fg='#f00',bg='#F8FFE8',font=14,relief='groove')
btn1.config(command=select_btn)
btn1.place(x=350,y=20,width=60,height=30)frame2=frame=Frame(win,width=500,height=60,bg='#F2F2D7',relief='groove')
frame2.grid(row=1,column=0,columnspan=5)btn_jisuan = Button(frame2,text='开始计算',command=jisuan,font=14,bg='#00FFFF')
btn_jisuan.grid(row=5,column=3,ipadx=10)
win.mainloop()
文件选取方法:
def select_btn():global afiletype=[('excel文件','*.xlsx'),('jpg文件','*.jpg')]a=askopenfilename(title='选择文件',filetypes=filetype)txt.set(a)
调用计算方法:
def jisuan():sel=[]sel_ban=[]for i in range(len(ban_list)):if check[i].get()==1:sel.append(i)sel_ban.append(ban_hour[i])h_need=hc_math.hour_need(entry.get())print('需要人数:'+str(h_need))print('开始计算')min_hc=int(hc_math.max_need(sel_ban,h_need))showinfo('计算结果','单天需要的最少人数为:'+str(min_hc))
汇总计算方法:
def max_need(banci,hour_need):#每个班次的最大人数ban_type_max=hc_random.ban_max(hour_need,banci)#每个班次最大人数下,所需人力max_hc=sum(hc_random.ban_max(hour_need,banci))print(ban_type_max)print(max_hc) #总人数#辅助数据,获取最大需求人数和最小需求人数hour_sel=[max(hour_need),min(hour_need)]#随机抽取处理,减少数据处理量ran=hc_random.ran_choice(ban_type_max,int(max_hc)*10)ran_yes=hc_random.sele(ran, hour_need, banci)ran_hc=max_hcprint('随机计算')for i in range(len(ran_yes)):ran_hc=min(ran_hc,sum(ran_yes[i]))#随机策略下最少需求人数print(ran_hc)hour_sel.append(ran_hc)#穷举所有可能性hc_list=hc_random.all_(ban_type_max,hour_sel)print(len(hc_list))time.sleep(3)#验证可能性hc_list_yes=hc_random.sele(hc_list,hour_need,banci)for i in range(len(hc_list_yes)):ran_hc=min(ran_hc,sum(hc_list_yes[i]))print(ran_hc)hc_list_yes_min=[]for i in range(len(hc_list_yes)):if sum(hc_list_yes[i])==ran_hc:hc_list_yes_min.append(hc_list_yes[i])print(hc_list_yes_min)return ran_hc
计算结果演示:
(部分中间计算过程在后台进行打印输出)
后台打印的一些运行记录,最后一行是具体方案