我如何制作Simpy模拟来描述马尔可夫M/M/1过程



输出打印到达时间和服务时间我正试图使用simpy实现具有指数到达间和指数服务时间的M/M/1马尔可夫过程。代码运行良好,但我没有得到预期的结果。此外,到达时间中的列表项的数量小于代码运行后服务时间中的清单项的数量。

# make a markovian queue
# make a server as a resource
# make customers at random times
# record the customer arrival time
# customer gets the resource
# record when the customer got the resource
# serve the customers for a random time using resource
# save this random time as service time
# customer yields the resource and next is served
import statistics
import simpy
import random

arrival_time = []
service_time = []
mean_service = 2.0
mean_arrival = 1.0
num_servers = 1
class Markovian(object):
def __init__(self, env, num_servers):
self.env = env
self.servers  = simpy.Resource(env, num_servers)
#self.action = env.process(self.run())
def server(self,packet ):
#timeout after random service time
t = random.expovariate(1.0/mean_service)
#service_time.append(t)
yield self.env.timeout(t)

def getting_service(env, packet, markovian):
# new packet arrives in the system
arrival_time = env.now
with markovian.servers.request() as req:
yield req
yield env.process(markovian.server(packet))
service_time.append(env.now - arrival_time)

def run_markovian(env,num_servers):
markovian = Markovian(env,num_servers)
packet = 0
#generate new packets
while True:
t = random.expovariate(1.0/mean_arrival)
arrival_time.append(t)
yield env.timeout(t)
packet +=1
env.process(Markovian.getting_service(env,packet,markovian))
def get_average_service_time(service_time):
average_service_time = statistics.mean(service_time)
return average_service_time

def main():
random.seed(42)
env= simpy.Environment()
env.process(Markovian.run_markovian(env,num_servers))
env.run(until = 50)
print(Markovian.get_average_service_time(service_time))
print (arrival_time)
print (service_time)


if __name__ == "__main__":
main()

你好,您的代码中基本上有一个错误和两个对排队理论的误解:

Bug 1(服务器的定义在类中,这使得模型表现为M/M/inf而不是M/M/1

答:我把你的资源的定义放在类中,从现在起传递服务器而不是num_servers

误解1:按照你定义的时间:

平均服务=2.0平均到达时间=1.0

该系统将生成更多的数据包,并且能够提供服务。这就是为什么名单的大小如此不同。

答案:平均服务=1.0平均到达时间=2.0

误解2:

您在代码中所称的服务时间实际上是系统时间。

我还在你的代码中放了一些指纹,这样我们就可以看到它在做什么。欢迎评论。而且不需要图书馆统计学,所以我也评论了它。

我希望这个答案对你有用。

# make a markovian queue
# make a server as a resource
# make customers at random times
# record the customer arrival time
# customer gets the resource
# record when the customer got the resource
# serve the customers for a random time using resource
# save this random time as service time
# customer yields the resource and next is served
#import statistics
import simpy
import random

arrivals_time = []
service_time = []
waiting_time = []
mean_service = 1.0
mean_arrival = 2.0
num_servers = 1
class Markovian(object):
def __init__(self, env, servers):
self.env = env
#self.action = env.process(self.run())
#def server(self,packet ):
#timeout after random service time
#   t = random.expovariate(1.0/mean_service)
#service_time.append(t)
#  yield self.env.timeout(t)

def getting_service(env, packet, servers):
# new packet arrives in the system
begin_wait = env.now
req = servers.request()
yield req

begin_service = env.now
waiting_time.append(begin_service - begin_wait)
print('%.1f Begin Service of packet %d' % (begin_service, packet))

yield env.timeout(random.expovariate(1.0/mean_service))

service_time.append(env.now - begin_service)
yield servers.release(req)
print('%.1f End Service of packet %d' % (env.now, packet))

def run_markovian(env,servers):
markovian = Markovian(env,servers)
packet = 0
#generate new packets
while True:
t = random.expovariate(1.0/mean_arrival)
yield env.timeout(t)
arrivals_time.append(t)
packet +=1
print('%.1f Arrival of packet %d' % (env.now, packet))
env.process(Markovian.getting_service(env,packet,servers))

def get_average_service_time(service_time):
average_service_time = statistics.mean(service_time)
return average_service_time

def main():
random.seed(42)
env= simpy.Environment()
servers  = simpy.Resource(env, num_servers)
env.process(Markovian.run_markovian(env,servers))
env.run(until = 50)

print(Markovian.get_average_service_time(service_time))
print ("Time between consecutive arrivals n", arrivals_time)
print("Size: ", len(arrivals_time))
print ("Service Times n", service_time)
print("Size: ", len(service_time))
print ("Waiting Times n", service_time)
print (waiting_time)
print("Size: ",len(waiting_time))


if __name__ == "__main__":
main()

相关内容

  • 没有找到相关文章

最新更新