模拟时存储正确的值时出现问题

2024-05-31 23:31:56 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我根据泊松分布模拟不同的随机电话。我每分钟都在检查是否有电话,并把它储存在字典里

这是我的calls字典的一个示例:

{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, ...... 

我想做的是,如果那一分钟有电话(value>;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)

有人能帮我看看我做错了什么吗

谢谢


Tags: ofinselffortimeavailableappointmentslot
1条回答
网友
1楼 · 发布于 2024-05-31 23:31:56

首先,这似乎只是从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

相关问题 更多 >