使用Simpy资源模拟物流调度。

huangapple go评论64阅读模式
英文:

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&quot;{self.env.now}&quot;)
        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&quot;{self.env.now} - car {car.count} -start&quot;)
            print(car.users)

class Car(simpy.Resource):
    def __init__(self, env, capacity=float(&#39;inf&#39;)) -&gt; None:
        super(Car, self).__init__(env, capacity)

    def _do_get(self, event) -&gt; None:
        try:
            self.users.remove(event.request)  # type: ignore
            yield env.time(70)
        except ValueError:
            pass
        event.succeed()
    def _do_put(self, event) -&gt; None:
        if len(self.users) &lt; 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

&quot;&quot;&quot;&quot;
Quick car resouce example

Programmer: Michael R. Gibbs

&quot;&quot;&quot;

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):
    &quot;&quot;&quot;
    get car from resouece pool
    send car
    return car to resouce pool
    &quot;&quot;&quot;
    while True:
        with car_pool.request() as req:
            yield req
            print(f&#39;{env.now} car dispatched&#39;)
            yield env.timeout(70)
        print(f&#39;{env.now} car returned&#39;)

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

&quot;&quot;&quot;&quot;
Quick car resouce example

Programmer: Michael R. Gibbs

&quot;&quot;&quot;

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):
    &quot;&quot;&quot;
    get car from resouece pool
    send car
    return car to resouce pool
    &quot;&quot;&quot;
    while True:
        with car_pool.request() as req:
            yield req
            print(f&#39;{env.now} car dispatched&#39;)
            yield env.timeout(70)
        print(f&#39;{env.now} car returned&#39;)

env = simpy.Environment()

car_pool = simpy.Resource(env, 4)
main(env, car_pool)

env.run(500)

huangapple
  • 本文由 发表于 2023年2月24日 15:23:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定