所以我正在根据泊松分布模拟不同的随机电话。我会每分钟检查是否有电话,并将其存储在字典中。
这是我的通话字典外观示例:
{0: 0, 1: 0, 2: 1, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 2,
10: 0, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0, 16: 0, 17: 0, ......
我想做的是,是否有呼叫该分钟(值> 0)以将时间存储在类实例中的方法。每个字典键都对应一分钟。
当前,这是我正在使用的代码:
import numpy as np
import datetime as dt
# initial day and time of simulation
numberOfSimulationDays = 28
currentDay = 1
currentTime = 0
# 2. Timeslots & availability
hours = []
free = []
start_time = 0
end_time = 480
time = start_time
end = end_time
while time <= end:
hours.append(time)
free.append(True)
time += 15
class Slot:
def __init__(self, slot, available):
self.slot = slot
self.available = available
timeSlots = {}
rangeHours = np.size(hours)
for i in range(rangeHours):
timeSlots[i] = Slot(hours[i],free[i])
# 2. Simualte calls
patient = {}
class Patient:
def __init__(self, calltime, slot):
self.calltime = calltime
self.slot = slot
self.arrivalTime = slot + np.random.normal(loc=0, scale=2.5,size=None)
self.AppointmentWaitingTime = self.arrivalTime - calltime
class Schedule:
@staticmethod
def get_number_of_calls_in_minute():
number_of_calls_in_minute =
np.random.poisson(lam=28.345 / (9 * 24), size=None)
return number_of_calls_in_minute
class SimulationEveryMinute:
while currentDay <= numberOfSimulationDays:
calls = {}
currentTime = 0
endOfDayTime = 540
while currentTime < endOfDayTime:
for i in range(endOfDayTime):
calls[i] = Schedule.get_number_of_calls_in_minute()
for call in calls:
if call == 1:
for slot_index, Slot in timeSlots.items():
if timeSlots[slot_index].available == True:
patient[slot_index] =
Patient(currentTime, Slot.slot)
timeSlots[slot_index].available = False
currentTime += 1
currentDay = currentDay + 1
print(SimulationEveryMinute.calls)
print("Number of calls:", sum(SimulationEveryMinute.calls.values()))
print("Number of scheduled patients:", len(patient))
for i in patient:
print("Calltime:", patient[i].calltime, "Slottime:",
patient[i].slot, "Appointment waiting time:",
patient[i].AppointmentWaitingTime)
问题是它只会将currentTime从1增加到32(我有32个时隙),而不是存储拨打电话的时间。
输出示例:
('Calltime:', 0, 'Slottime:', 0, 'Appointment waiting time:', 0.4138422146210218)
('Calltime:', 1, 'Slottime:', 15, 'Appointment waiting time:', 11.162701908696011)
('Calltime:', 2, 'Slottime:', 30, 'Appointment waiting time:', 27.40165239831842)
('Calltime:', 3, 'Slottime:', 45, 'Appointment waiting time:', 41.696420163160745)
相反,它应该给出
('Calltime:', 2, 'Slottime:', 0, 'Appointment waiting time:', 0.4138422146210218)
('Calltime:', 9, 'Slottime:', 15, 'Appointment waiting time:', 11.162701908696011)
('Calltime:', 9, 'Slottime:', 30, 'Appointment waiting time:', 27.40165239831842)
有人可以帮我看看我在做什么错吗?
谢谢
答案 0 :(得分:0)
首先,看来这只是从Patient
对象而不是插槽号打印呼叫时间的问题:
print("Calltime:", ceil(patient[i].arrivalTime),
"Slottime:",patient[i].slot, "Appointment waiting time:",
patient[i].AppointmentWaitingTime)
第二,我不了解您使用的字典。如果只是在给定的分钟内拨打电话,则使用列表。列表索引是分钟数:
[0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 0, ...]
您可以使用更好的逻辑应用程序和偶尔的enumerate
调用来简化几个循环-这是一个遍历索引和值的迭代器,例如
for slot_num, freq in enumerate(calls):
我建议您稍微简化一下编码。对于初学者,请使用增量编程:编写一段代码,并确保结果完全符合您的要求。在调试完该代码之前,不要编写更多代码。
第二,不要不要使用类,直到您知道需要其他功能。发布的代码使用一些类,这些类不会向其中的简单代码添加任何内容。
您的呼叫生成代码每天运行一次,覆盖原始的呼叫频率。
currentTime = 0
while currentTime < endOfDayTime:
# This generates a call frequency for every minute of the day.
for i in range(endOfDayTime):
calls[i] = Schedule.get_number_of_calls_in_minute()
for call in calls:
if call == 1: # Should this be call >= 1 ??
...
currentTime += 1