这是一个广为所知的问题
超市里咋样排队才是最快的呢?
语言表述以后再加,现在先上代码
import time
import random
import matplotlib.pyplot as plt
#state--0/1/2/3/4/5/6
class customer:
def __init__(self,name,state=None,pay=False):
self.name = name
self.state = state
self.pay = pay
class cashier_desk:
def __init__(self,number,wait=0,allwait=0):
self.number = number
self.wait = wait
self.allwait = allwait
def line_as_random(customers,cashier_desks):
for i in customers:
#找位置
#付钱
c = random.choice(cashier_desks)
i.state = c
c.wait += 1
for i in cashier_desks:
for ii in range(i.wait):
i.allwait += random.randint(10,60)
return max([i.allwait for i in cashier_desks])
def line_as_indicator(customers,cashier_desks):
for i in range(1000):
for ii in cashier_desks:
if ii.allwait == 0:
try:
d = customers.pop()
d.state = ii
ii.allwait += random.randint(10,60)
except:
pass
for ii in cashier_desks:
ii.allwait -= 1
if len(customers) == 0:
start = i
break
return start
customers = [customer(i,0) for i in ["Bob","harry","jame","potter","lili","ron","hemine","weislei","tom","jerry","lijia","mei","sun","tang"
,"chandler","li","qiu","sitel","heige","denboliduo","maige","xinpusen","batter","sumei","kivi"]]
cashier_desks = [cashier_desk(str(i)) for i in range(1,4)]
time = []
sum = 0
for i in range(40000):
s = line_as_random(customers,cashier_desks)
time.append(s)
sum += s
customers = [customer(i,0) for i in ["Bob","harry","jame","potter","lili","ron","hemine","weislei","tom","jerry","lijia","mei","sun","tang"
,"chandler","li","qiu","sitel","heige","denboliduo","maige","xinpusen","batter","sumei","kivi"]]
cashier_desks = [cashier_desk(str(i)) for i in range(1,4)]
time1 = []
sum1 = 0
for i in range(40000):
s = line_as_indicator(customers,cashier_desks)
time1.append(s)
sum1 += s
customers = [customer(i,0) for i in ["Bob","harry","jame","potter","lili","ron","hemine","weislei","tom","jerry","lijia","mei","sun","tang"
,"chandler","li","qiu","sitel","heige","denboliduo","maige","xinpusen","batter","sumei","kivi"]]
cashier_desks = [cashier_desk(str(i)) for i in range(1,4)]
plt.plot(range(1,1+len(time)),time,c="red")
plt.plot(range(1,1+len(time1)),time1,c="green")
plt.title("total random average "+str(sum/len(time))+"line as lead average"+str(sum1/len(time1)))
plt.savefig("compare.jpg")
效果图
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)