如何正确使用Python =操作员



我正在尝试使用 =运算符,但我的结果不正确。我们在沙龙中有一个梳妆台,为客户提供服务。在每天,她有6个可以预约的插槽,每次约会之间的间隔平均分布。如果她设法预约一个插槽的约会,我们用变量1表示,如果她找不到该插槽的客户,那么我们用变量0表示。

Appointments_Booked = [1, 0, 1, 1, 1]  # Where 1 indicates an appointment booked and 0 no appointment booked.
def service_time():
    service = random.normalvariate(5, 1)  # The time the hair dresser takes to service her customers follows a normal distribution, the hair dresser takes around 5 minutes on average to service each customer
    return service
def wait_time():
    waiting_time_last_customer = 0  # The waiting time of the first customer is zero because there is no customer booked before him or her
    interval_time_between_slots = 5  # This is how much time we have between each appointment
    y = 0
    for x in Appointments_Booked:
        if x == 1:  # If we have a customer booked for a slot
            customer_service = service_time()  #How long we will take to service a customer
            waiting_time_present_customer = max((waiting_time_last_customer + customer_service) - interval_time_between_slots, 0)  # This is the formula to compute the waiting time of the current customer. It essentially says that the waiting time of the current customer is simply the interval time (space) between appointments minus how much the previous customer had to wait for service and then get serviced.
            y += waiting_time_present_customer  # THIS IS WHERE I AM ENCOUNTERING PROBLEMS 
            print('waiting time =', y)
            print('service time =', customer_service)
        elif x == 0:
             customer_service = 0
             waiting_time_last_customer = 0
             y += waiting_time_present_customer
             print('waiting time =', y)
             print('service time =', customer_service)

my =没有做我想做的事情,首先,我希望第一个客户的等待时间始终为0,因为这个客户不仅仅因为他/她没有其他客户。其次,例如其他客户的结果也不同,例如,我的输出是:

waiting time = 1.449555339084272  #This does not make any sense because the first customer is supposed to have zero waiting time because they are first in line
service time = 4.400365861292478
waiting time = 0
service time = 0   # refA
waiting time = 0   # refA
service time = 4.42621491273674
waiting time = 1.0771427601173116  # The waiting time of this customer is supposed to also be zero because the service time (#refA) + waiting time(#refA) of the previous customer is zero.
service time = 6.077142760117312
waiting time = 1.0771427601173116  # The waiting time of this customer is also wrong because its supposed to be 2.154. The waiting time (1.077) + the service time (6.077) of the previous customer is 7.154 minus the interval 5 gives 2.154
service time = 4.166720282779419

我在 =操作员上做错了什么,或者我在做其他错误?

您将customer_service添加到等待时间。标准的单服务器队列模型说:

arrival_time(i) = arrival_time(i-1) + interarrival_time  # often exponential
begin_service_time(i) = max(arrival_time(i), end_service_time(i-1))
end_service_time(i) = begin_service_time(i) + customer_service(i)

其中 i是客户编号。通过适当的初始化,您可以删除i并仅循环,因为更新仅取决于先前的值。

您选择将其离散到时插槽,但它不会改变a)a)的基本逻辑缺陷,包括当前客户的 customer_service,而b)将结果基于先前的等待时间客户而不是他们完成的时间。

可能还有其他缺陷,但是我停止检查了这些缺陷,鉴于这些是示威者,您没有提供实际的驱动程序代码来运行模型。

最新更新