【问题标题】:How to use the python += operator correctly?如何正确使用 python += 运算符?
【发布时间】:2017-02-16 16:09:53
【问题描述】:

我正在尝试使用 += 运算符,但我一直得到不正确的结果。我们在一家沙龙有一位美发师,为她的顾客提供服务。每天,她有 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)

我的 += 没有做我想做的事,首先我希望第一个客户的等待时间始终为 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

我在使用 += 运算符时做错了什么,或者我做错了什么?

【问题讨论】:

    标签: random operators simulation python-3.5 montecarlo


    【解决方案1】:

    您将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) 的基本逻辑缺陷,包括等待时间中当前客户的 customer_service,以及 b) 基于等待时间的结果之前的客户,而不是他们完成的时间。

    可能还有其他缺陷,但我停止了检查,因为这些都是阻碍因素,并且您没有提供实际的驱动程序代码来运行您的模型。

    【讨论】:

    • 嗨,Pjs,我只是想借此机会非常感谢您抽出时间来解决我的问题。我非常感谢您的投入和时间,对此我确实非常感谢。我只是通过你的信息来调整我的程序。
    • 如何删除 i 并循环遍历列表 Customer_Numbers = [1, 2, 3, 4, 5] # 列表中的每个项目代表 i,我一直在尝试做这个练习一个多小时了
    • 这个想法是您不需要为所有客户的所有值维护一个数组的存储。例如,一旦您找到客户iarrival_time,您就不再需要任何先前客户的arrival_times。因此,您可以将值计算为 arrival_time += interarrival_time 而不使用索引。您仍然需要遍历客户集,但不需要同时为所有客户分配存储空间。
    猜你喜欢
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 2018-06-15
    • 1970-01-01
    • 2011-10-03
    相关资源
    最近更新 更多