英文:
use simpy.resource simulate logistics scheduling
问题
我想使用 simpy.Resource
来创建 4 辆车来运输货物。主进程每隔 10 分钟发送一辆车。所以它的工作方式是 1,2,3,4, 1,2,3,4。每辆车需要 70 分钟来交付。在第一辆车被派遣后,必须等待 70 分钟才能再次派遣。
我使用了这段代码,但它不起作用。
def main(self):
while True:
print(f"{self.env.now}")
car = Car(self.env, capacity=4)
yield from self.send(car)
def send(self, car):
while True:
yield env.timeout(10)
with car.request() as req:
yield req
print(f"{self.env.now} - car {car.count} - start")
print(car.users)
class Car(simpy.Resource):
def __init__(self, env, capacity=float('inf')):
super(Car, self).__init__(env, capacity)
def _do_get(self, event):
try:
self.users.remove(event.request)
yield env.timeout(70)
except ValueError:
pass
event.succeed()
def _do_put(self, event):
if len(self.users) < self.capacity:
self.users.append(event)
event.usage_since = self._env.now
event.succeed()
这是您提供的代码的翻译部分。
英文:
I want to use simpy.Resource
to create 4 vehicles to carry goods. The main process sends a car every 10 minutes. So it works like 1,2,3,4, 1,2,3,4. And each car takes 70 minutes to deliver. After the first vehicle is dispatched, it must wait for 70 minutes before being dispatched again.
I use this code, but it doesn't works.
def main(self):
while True:
print(f"{self.env.now}")
car = Car(self.env,capacity=4)
yield from slef.send(car)
def send(self, car):
while True:
yield env.timeout(10)
with car.request() as req:
yield req
print(f"{self.env.now} - car {car.count} -start")
print(car.users)
class Car(simpy.Resource):
def __init__(self, env, capacity=float('inf')) -> None:
super(Car, self).__init__(env, capacity)
def _do_get(self, event) -> None:
try:
self.users.remove(event.request) # type: ignore
yield env.time(70)
except ValueError:
pass
event.succeed()
def _do_put(self, event) -> None:
if len(self.users) < self.capacity:
self.users.append(event)
event.usage_since = self._env.now
event.succeed()
答案1
得分: 1
你只需要为每辆车创建一个流程来获取车辆、发送车辆和归还车辆。
import simpy
def main(env, car_pool):
for i in range(car_pool.capacity):
# 为池中的每辆车创建一个流程
env.process(send_car(env, car_pool))
def send_car(env, car_pool):
while True:
with car_pool.request() as req:
yield req
print(f'{env.now} 车辆已发出')
yield env.timeout(70)
print(f'{env.now} 车辆已归还')
env = simpy.Environment()
car_pool = simpy.Resource(env, 4)
main(env, car_pool)
env.run(500)
或者,如果您的目标是每隔10分钟请求发送一辆车,可以尝试以下代码:
import simpy
def main(env, car_pool):
for i in range(car_pool.capacity):
# 为池中的每辆车创建一个流程
env.process(send_car(env, car_pool))
def send_car(env, car_pool):
while True:
with car_pool.request() as req:
yield req
print(f'{env.now} 车辆已发出')
yield env.timeout(10) # 每隔10分钟发送一辆车
print(f'{env.now} 车辆已归还')
env = simpy.Environment()
car_pool = simpy.Resource(env, 4)
main(env, car_pool)
env.run(500)
这两段代码示例了如何使用SimPy库创建一个简单的资源池,以及如何为每辆车创建一个流程来处理发送和归还操作。根据您的需求,您可以选择其中一种代码示例。
英文:
You do not need to customize the resource pool
You just need a process per car to get car, send car, return car
""""
Quick car resouce example
Programmer: Michael R. Gibbs
"""
import simpy
def main(env, car_pool):
for i in range(car_pool.capacity):
# create one process per car in pool
env.process(send_car(env,car_pool))
def send_car(env, car_pool):
"""
get car from resouece pool
send car
return car to resouce pool
"""
while True:
with car_pool.request() as req:
yield req
print(f'{env.now} car dispatched')
yield env.timeout(70)
print(f'{env.now} car returned')
env = simpy.Environment()
car_pool = simpy.Resource(env, 4)
main(env, car_pool)
env.run(500)
or, if your goal is to request sending a car every 10 minutes, then try this
""""
Quick car resouce example
Programmer: Michael R. Gibbs
"""
import simpy
def main(env, car_pool):
for i in range(car_pool.capacity):
# create one process per car in pool
env.process(send_car(env,car_pool))
def send_car(env, car_pool):
"""
get car from resouece pool
send car
return car to resouce pool
"""
while True:
with car_pool.request() as req:
yield req
print(f'{env.now} car dispatched')
yield env.timeout(70)
print(f'{env.now} car returned')
env = simpy.Environment()
car_pool = simpy.Resource(env, 4)
main(env, car_pool)
env.run(500)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论