问题描述
我正在尝试模拟双端排队系统,其中:
-
一家商店的容量= 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')
如果最终效果相同,那么当消费者在商店里闲逛时,是否将其放在手里对您来说是否重要?