如何正确使用python + =运算符?

时间:2017-02-16 16:09:54

标签: random operators simulation python-3.5 montecarlo

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

我对+ =运算符做错了什么,或者我做错了什么?

1 个答案:

答案 0 :(得分: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)将结果基于先前客户的等待时间而不是他们完成的时间。

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