Python 如何修复我编写的用于查找被阻止呼叫数的计数器?

Python 如何修复我编写的用于查找被阻止呼叫数的计数器?,python,numpy,counter,Python,Numpy,Counter,我的代码的作用: import numpy as np import pandas as pd #Uniformly distributed start times startTimes = np.random.randint(0,60,100) startTimes.sort() #Average call time for 100 people callDuration = np.random.poisson(20, 100) channelCounter = 0; blockedC

我的代码的作用:

import numpy as np
import pandas as pd

#Uniformly distributed start times
startTimes = np.random.randint(0,60,100)
startTimes.sort()

#Average call time for 100 people 
callDuration = np.random.poisson(20, 100)

channelCounter = 0;
blockedCounter = 0;

endTimes = np.add(callDuration, startTimes)

numberChannels = 1

for x in range(0,60): 

    for y in range(0, startTimes.size):

        if startTimes[y] == x: 
            if channelCounter < numberChannels:
                channelCounter=channelCounter+1

            elif channelCounter == numberChannels: 

                blockedCounter = blockedCounter + 1

        if (endTimes[y] == x):
            if channelCounter >= 1:
                channelCounter=channelCounter-1
模拟在N通道网络上发生的100个语音呼叫,并计算60分钟内的服务等级(即阻塞呼叫的百分比)

我的代码:

import numpy as np
import pandas as pd

#Uniformly distributed start times
startTimes = np.random.randint(0,60,100)
startTimes.sort()

#Average call time for 100 people 
callDuration = np.random.poisson(20, 100)

channelCounter = 0;
blockedCounter = 0;

endTimes = np.add(callDuration, startTimes)

numberChannels = 1

for x in range(0,60): 

    for y in range(0, startTimes.size):

        if startTimes[y] == x: 
            if channelCounter < numberChannels:
                channelCounter=channelCounter+1

            elif channelCounter == numberChannels: 

                blockedCounter = blockedCounter + 1

        if (endTimes[y] == x):
            if channelCounter >= 1:
                channelCounter=channelCounter-1
将numpy导入为np
作为pd进口熊猫
#均匀分布的开始时间
startTimes=np.random.randint(0,60100)
startTimes.sort()
#100人的平均通话时间
callDuration=np.random.poisson(20100)
信道计数器=0;
阻塞计数器=0;
endTimes=np.add(callDuration,startTimes)
numberChannels=1
对于范围(0,60)内的x:
对于范围内的y(0,起始时间大小):
如果开始时间[y]==x:
如果通道计数器<数字通道:
信道计数器=信道计数器+1
elif channelCounter==numberChannels:
blockedCounter=blockedCounter+1
如果(结束时间[y]==x):
如果channelCounter>=1:
channelCounter=channelCounter-1
我的方法:

import numpy as np
import pandas as pd

#Uniformly distributed start times
startTimes = np.random.randint(0,60,100)
startTimes.sort()

#Average call time for 100 people 
callDuration = np.random.poisson(20, 100)

channelCounter = 0;
blockedCounter = 0;

endTimes = np.add(callDuration, startTimes)

numberChannels = 1

for x in range(0,60): 

    for y in range(0, startTimes.size):

        if startTimes[y] == x: 
            if channelCounter < numberChannels:
                channelCounter=channelCounter+1

            elif channelCounter == numberChannels: 

                blockedCounter = blockedCounter + 1

        if (endTimes[y] == x):
            if channelCounter >= 1:
                channelCounter=channelCounter-1
从0分钟到60分钟,我生成100个呼叫开始时间的统一分布

我生成了平均通话时间为20分钟的100个通话持续时间的随机泊松分布

我将通道计数器变量和阻塞计数器变量设置为0

最后,我创建另一个数组,该数组由调用开始时间+平均调用持续时间之和组成,以获得调用结束时间

阻塞计数器递增背后的伪代码逻辑如下:

if number of channels occupied < number channels available:
    put a call through 
else if number of channels occupied == number channels available ( ie full):
    call is dropped so counter incremements 

if a call that is ongoing finishes: 
    decrement number of channels occupied 
如果占用的通道数<可用通道数:
接通电话
否则,如果占用的通道数==可用通道数(即已满):
呼叫被丢弃,因此计数器递增
如果正在进行的通话结束:
减少占用的通道数
我的阻塞计数器没有像我预期的那样递增。我对出了什么问题有一个模糊的概念,但我不知道如何解决它。在输入当前值的情况下,我希望阻塞计数器的值大约为95。然而,我得到的是一个徘徊在70-75之间的值

如果有人发现我哪里出了问题,我将不胜感激


好的,这里的问题是,您需要取任何
endTime
值,这在给定的分钟内发生,作为释放频道的标志。而其中一些实际上被封锁了。这就是为什么你要夸大非阻塞呼叫的总数。您更希望的是计算非重叠间隔的数量(假设给定分钟内可能的最大重叠呼叫数等于
numberChannels
,并且呼叫的顺序优先级)

因此,假设您可以使用一个通道:

minute=0
非阻塞计数器=0

虽然(minuteGod,我花了6个小时试着调试这个。非常感谢你的帮助。我真的很感激:)我问这个问题很厚颜无耻,但你能告诉我如何使它在N个频道上工作吗?我正在努力实现允许重叠调用的功能。我只是想知道我是否能够让它处理N个通道而不必求助于多线程,因为我没有多线程处理的经验。Nvm,让它完全工作而不求助于线程:)你的例子帮了我很多。不用担心。我有一个想法,就是在for循环中(
for…While…
)迭代每个通道,删除计数的
开始时间和
结束时间,但没有接近实际编码。很高兴听到你的作品。