超市排队问题- Python(面向对象)

c9qzyr3d  于 2023-03-28  发布在  Python
关注(0)|答案(1)|浏览(150)

我有一个现实世界的问题。
这是一个收银员的排队系统。目标是能够在最短的时间内高效地为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

我通常只是不知道如何使它更有效,以及如何使它真正起作用

tzcvj98z

tzcvj98z1#

import random

class Queue:
    def __init__(self):
        self.queue1 = []
        self.queue2 = []
        self.queue3 = []
        self.time = 0

    def shortest_queue(self):
        if len(self.queue1) <= len(self.queue2) and len(self.queue1) <= len(self.queue3):
            return self.queue1
        elif len(self.queue2) <= len(self.queue1) and len(self.queue2) <= len(self.queue3):
            return self.queue2
        else:
            return self.queue3

    def add_customer(self, customer):
        shortest = self.shortest_queue()
        shortest.append(customer)

    def process_customers(self):
        while len(self.queue1) > 0 or len(self.queue2) > 0 or len(self.queue3) > 0:
            if len(self.queue1) > 0:
                self.queue1[0]["items"] -= 1
                if self.queue1[0]["items"] == 0:
                    self.queue1.pop(0)
            if len(self.queue2) > 0:
                self.queue2[0]["items"] -= 1
                if self.queue2[0]["items"] == 0:
                    self.queue2.pop(0)
            if len(self.queue3) > 0:
                self.queue3[0]["items"] -= 1
                if self.queue3[0]["items"] == 0:
                    self.queue3.pop(0)
            self.time += 1

    def run_simulation(self, num_customers):
        for i in range(num_customers):
            customer = {
                "shop_time": random.randint(1, 5),
                "items": random.randint(1, 10),
            }
            self.add_customer(customer)
            self.process_customers()
        return self.time

# Example usage:
q = Queue()
total_time = q.run_simulation(30)
print(f"Total time to serve 30 customers: {total_time} seconds")

相关问题