我有一个现实世界的问题。
这是一个收银员的排队系统。目标是能够在最短的时间内高效地为30位顾客提供服务。收银员1需要20秒扫描一件商品,收银员2需要30秒扫描一件商品,收银员3需要40秒扫描一件商品。
该算法的方法是,顾客购物1-5分钟(随机),购买1-10件商品,然后排队到3个给定的收银员中的任何一个,这些收银员似乎排队时间最短。
创建一个类对象队列,并使用此队列编写一个python代码,在不使用函数的情况下模拟算法:枚举和lambda,以及导入时间模块
到目前为止,我已经尝试了这个算法:
import random
class Customer:
def __init__(self, id, arrivalTime):
self.id = id
self.arrivalTime = arrivalTime
self.items = random.randit(1,10)
class Cashier:
def __init__(self, id, scanningTime):
self.id = id
self.scanningTime = scanningTime
self.currentCust = None
self.finish = 0
def serveCust(self, customer, currentTime):
self.currentCust = customer
self.finish = currentTime + customer.items * self.scanningTime
def is_available(self, currentTime):
return self.finish <= currentTime
def finish_serv(self):
customer = self.currentCust
self.currentCust = None
self.finish = 0
return customer
class BQ:
def __init__(self, numCashier, cashierScanTime):
self.customers = []
self.cashiers = []
for i in range(numCashier):
self.cashiers.append(Cashier(i, cashierScanTime[i]))
self.numCustServed = 0
self.totalWaitTime = 0
def addCust(self, currentTime):
customer = Customer(len(self.customers), currentTime)
self.customers.append(customer)
def serveCust(self, currentTime):
for cashier in self.cashiers:
if cashier.is_available(currentTime) and self.customers:
minItemsCust = self.customers[0]
for customer in self.customers:
if customer.items < minItemsCust.items:
minItemsCust = customer
self.customers.remove(minItemsCust)
self.totalWaitTime += currentTime - minItemsCust.arrivalTime
cashier.serveCust(minItemsCust, currentTime)
self.numCustServed += 1
def run(self, numCust):
currentTime = 0
for i in range(numCust):
self.addCust(currentTime)
currentTime += random.randit(60,300)
while self.customers:
self.serveCust(currentTime)
currentTime += 1
averageWaitTime = self.totalWaitTime / self.numCustServed
我通常只是不知道如何使它更有效,以及如何使它真正起作用
1条答案
按热度按时间tzcvj98z1#