如何解决Simpy:如何使用存储资源在非零时间内执行“获取”请求?
我正在尝试模拟双端排队系统,其中:
-
一家商店的容量= 1(一次只能包含1件商品)
-
工厂每0.37s生产一次物品。
-
顾客每隔1秒就会到达商店,得到一件商品然后离开。一旦一件物品被拿走,商店立即将下一件可用的物品出售。
我尝试的代码如下。
但是,我真正想要的是,当客户到达商店并看到有可用的商品正在销售时,他/她将把商品持有非零时间,例如, t 在将其从商店中取出之前,strong>秒(t呈指数分布)。在此期间,客户及其持有的商品仍在商店中(这意味着队列中的下一个客户和下一个商品需要等待,直到当前客户实际完成购买并离开系统为止)。有什么办法吗?
import simpy
class Generate():
def gen_product(env,resource):
for i in range(10000):
product = Producer(name = "product%01d" % (i))
yield env.timeout(0.37)
print(env.Now,product.name,'is produced')
env.process(product.produce_action(env,resource))
def gen_customer(env,resource):
for i in range(10000):
customer = Consumer(name = "customer%01d" % (i))
yield env.timeout(1)
print(env.Now,customer.name,'arrives')
env.process(customer.consumer_action(env,resource))
class Producer():
def __init__(self,name):
self.name = name
def produce_action(self,env,resource):
yield resource.put(item = self.name)
print(env.Now,self.name,'arrives at store')
class Consumer():
def __init__(self,name):
self.name = name
def consumer_action(self,resource):
#print(env.Now,'requesting resource at',env.Now)
item = yield resource.get()
print(env.Now,'got',item,' and leaves')
env = simpy.Environment()
resource = simpy.Store(env,capacity=1)
env.process(Generate.gen_product(env,resource))
env.process(Generate.gen_customer(env,resource))
env.run(until=5)
0.37 product0 is produced
0.37 product0 arrives at store
0.74 product1 is produced
1 customer0 arrives
1 customer0 got product0 and leaves
1 product1 arrives at store
1.10 product2 is produced
1.48 product3 is produced
1.85 product4 is produced
2 customer1 arrives
2 customer1 got product1 and leaves
2 product2 arrives at store
2.22 product5 is produced
2.59 product6 is produced
2.96 product7 is produced
3 customer2 arrives
3 customer2 got product2 and leaves
3 product3 arrives at store
解决方法
您能在夺取资源之前屈服吗?也就是说,像这样修改您的代码:
def consumer_action(self,env,resource):
#print(env.now,self.name,'requesting resource at',env.now)
yield env.timeout(0.7)
item = yield resource.get()
print(env.now,'got',item,' and leaves')
如果最终效果相同,那么当消费者在商店里闲逛时,是否将其放在手里对您来说是否重要?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。