【发布时间】: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