如何从 Python SimPly 中获取一个输入获得多次输出间隔时间
How to get many times outputs interval time from geting one input in Python SimPy
我使用 Python Simpy 进行模拟。
我想从一个单位的原始 materials(factory.materials) 生成产品库存(factory.stock)。
在我得到一个单位的 raw material 之后,
我不想等待一次生成成品库存,
我想每次在一定的时间间隔生成多个库存,
例如我得到一个原始单位material,得到树时间产品库存,每次得到5个产品,间隔6个单位时间。
但是我的代码delay()是用来延迟时间的,但是不起作用。
如果我使用 yield 延迟,它会得到我的 raw material 很多次,而我只想得到一个单元的 raw material.
import simpy
class Factory():
def __init__(self, env):
self.materials = simpy.Container(env, capacity = 10000, init = 9000)
self.stock = simpy.FilterStore(env, capacity = 10000)
def create_stock(env, factory):
while True:
print('time:{0} materials:{1} stock:{2}'.format(env.now,factory.materials.level,len(factory.stock.items)))
yield factory.materials.get(1)
for i in range(5):
for j in range(2):
factory.stock.put({'order_id': 'order_id', 'id':j})
delay(2)
yield env.timeout(1)
def delay(how_long):
yield env.timeout(how_long)
env = simpy.Environment()
factory = Factory(env)
on_process = env.process(create_stock(env, factory))
print('start...')
env.run(until = 30)
print('end')
我不确定你想要实现什么,但是 delay
不起作用,因为 delay
内部的 yield
不会屈服于函数外部。函数 yield 与编写 yield env.timeout(x)
.
不同
将行 delay(2)
更改为 env.timeout(2)
将解决该问题
如果你真的想要一个函数来做,你可以做
def delay(how_long):
return env.timeout(how_long)
yield delay(2)
我想每2个单位时间循环5次获取一些产品库存,然后退出流程。
将延迟(2)更改为env.timeout(2),它不起作用。
import simpy
class Factory():
def __init__(self, env):
self.materials = simpy.Container(env, capacity = 10000, init = 9000)
self.stock = simpy.FilterStore(env, capacity = 10000)
def create_stock(env, factory):
while True:
print('time:{0} materials:{1} stock:{2}'.format(env.now,factory.materials.level,len(factory.stock.items)))
yield factory.materials.get(1)
for i in range(5):
for j in range(2):
factory.stock.put({'order_id': 'order_id', 'id':j})
env.timeout(2)
yield env.timeout(0)
env = simpy.Environment()
factory = Factory(env)
on_process = env.process(create_stock(env, factory))
print('start...')
env.run(until = 30)
print('end')
我用发电机的yield,它works.When得到一个单位材料,每3个单位时间放出2个库存。
import simpy
class Factory():
def __init__(self, env):
self.materials = simpy.Container(env, capacity = 10000, init = 9000)
self.stock = simpy.FilterStore(env, capacity = 10000)
def create_stock(env, factory):
while True:
#print('time:{0} materials:{1} stock:{2}'.format(env.now,factory.materials.level,len(factory.stock.items)))
yield factory.materials.get(1)
for i in range(5):
yield from get_stock()
def get_stock():
print('time:{0} materials:{1} stock:{2}'.format(env.now,factory.materials.level,len(factory.stock.items)))
for i in range(2):
factory.stock.put({'order_id': '', 'id':i})
yield env.timeout(3)
env = simpy.Environment()
factory = Factory(env)
on_process = env.process(create_stock(env, factory))
print('start...')
env.run(until = 200)
print('end')
你只需要为你的内部超时添加一个收益
试试这个
import simpy
class Factory():
def __init__(self, env):
self.materials = simpy.Container(env, capacity = 10000, init = 9000)
self.stock = simpy.FilterStore(env, capacity = 10000)
def create_stock(env, factory):
while True:
print('time:{0} materials:{1} stock:{2}'.format(env.now,factory.materials.level,len(factory.stock.items)))
yield factory.materials.get(1)
for i in range(5):
for j in range(2):
factory.stock.put({'order_id': 'order_id', 'id':j})
#env.timeout(2)
yield env.timeout(2)
print('time:{0} materials:{1} stock:{2}'.format(env.now,factory.materials.level,len(factory.stock.items)))
env = simpy.Environment()
factory = Factory(env)
on_process = env.process(create_stock(env, factory))
print('start...')
env.run(until = 30)
print('end')
我使用 Python Simpy 进行模拟。
我想从一个单位的原始 materials(factory.materials) 生成产品库存(factory.stock)。 在我得到一个单位的 raw material 之后, 我不想等待一次生成成品库存, 我想每次在一定的时间间隔生成多个库存, 例如我得到一个原始单位material,得到树时间产品库存,每次得到5个产品,间隔6个单位时间。
但是我的代码delay()是用来延迟时间的,但是不起作用。 如果我使用 yield 延迟,它会得到我的 raw material 很多次,而我只想得到一个单元的 raw material.
import simpy
class Factory():
def __init__(self, env):
self.materials = simpy.Container(env, capacity = 10000, init = 9000)
self.stock = simpy.FilterStore(env, capacity = 10000)
def create_stock(env, factory):
while True:
print('time:{0} materials:{1} stock:{2}'.format(env.now,factory.materials.level,len(factory.stock.items)))
yield factory.materials.get(1)
for i in range(5):
for j in range(2):
factory.stock.put({'order_id': 'order_id', 'id':j})
delay(2)
yield env.timeout(1)
def delay(how_long):
yield env.timeout(how_long)
env = simpy.Environment()
factory = Factory(env)
on_process = env.process(create_stock(env, factory))
print('start...')
env.run(until = 30)
print('end')
我不确定你想要实现什么,但是 delay
不起作用,因为 delay
内部的 yield
不会屈服于函数外部。函数 yield 与编写 yield env.timeout(x)
.
将行 delay(2)
更改为 env.timeout(2)
将解决该问题
如果你真的想要一个函数来做,你可以做
def delay(how_long):
return env.timeout(how_long)
yield delay(2)
我想每2个单位时间循环5次获取一些产品库存,然后退出流程。 将延迟(2)更改为env.timeout(2),它不起作用。
import simpy
class Factory():
def __init__(self, env):
self.materials = simpy.Container(env, capacity = 10000, init = 9000)
self.stock = simpy.FilterStore(env, capacity = 10000)
def create_stock(env, factory):
while True:
print('time:{0} materials:{1} stock:{2}'.format(env.now,factory.materials.level,len(factory.stock.items)))
yield factory.materials.get(1)
for i in range(5):
for j in range(2):
factory.stock.put({'order_id': 'order_id', 'id':j})
env.timeout(2)
yield env.timeout(0)
env = simpy.Environment()
factory = Factory(env)
on_process = env.process(create_stock(env, factory))
print('start...')
env.run(until = 30)
print('end')
我用发电机的yield,它works.When得到一个单位材料,每3个单位时间放出2个库存。
import simpy
class Factory():
def __init__(self, env):
self.materials = simpy.Container(env, capacity = 10000, init = 9000)
self.stock = simpy.FilterStore(env, capacity = 10000)
def create_stock(env, factory):
while True:
#print('time:{0} materials:{1} stock:{2}'.format(env.now,factory.materials.level,len(factory.stock.items)))
yield factory.materials.get(1)
for i in range(5):
yield from get_stock()
def get_stock():
print('time:{0} materials:{1} stock:{2}'.format(env.now,factory.materials.level,len(factory.stock.items)))
for i in range(2):
factory.stock.put({'order_id': '', 'id':i})
yield env.timeout(3)
env = simpy.Environment()
factory = Factory(env)
on_process = env.process(create_stock(env, factory))
print('start...')
env.run(until = 200)
print('end')
你只需要为你的内部超时添加一个收益 试试这个
import simpy
class Factory():
def __init__(self, env):
self.materials = simpy.Container(env, capacity = 10000, init = 9000)
self.stock = simpy.FilterStore(env, capacity = 10000)
def create_stock(env, factory):
while True:
print('time:{0} materials:{1} stock:{2}'.format(env.now,factory.materials.level,len(factory.stock.items)))
yield factory.materials.get(1)
for i in range(5):
for j in range(2):
factory.stock.put({'order_id': 'order_id', 'id':j})
#env.timeout(2)
yield env.timeout(2)
print('time:{0} materials:{1} stock:{2}'.format(env.now,factory.materials.level,len(factory.stock.items)))
env = simpy.Environment()
factory = Factory(env)
on_process = env.process(create_stock(env, factory))
print('start...')
env.run(until = 30)
print('end')