Simpy:在分配新资源时同时释放资源

Simpy: simultaneously release resource on allocation of new resource

我已经简化了我尝试建模的问题的一个版本,我正在使用 Simpy 来描述旅行者沿着路径的运动。

路径由 Node() 个对象的集合表示,其中每个节点包含一个 Simpy.Resource。每个节点都通过 connected_to 属性连接到路径中的下一个节点。在示例代码中,我创建了一个包含 10 个节点的列表,其中列表中的每个节点都连接到列表中的前一个节点。

当旅行者(由 Occupier() 对象表示)被实例化时,它会被分配一个节点的资源。然后旅行者沿着节点移动,只有在下一个节点可用时才迈出一步。我的目标是让旅行者同时被分配到它的目的地节点释放它之前所在的节点。

import simpy


class Node(object):
    def __init__(self, env):
        self.env = env
        self.resource = simpy.Resource(self.env)
        self.up_connection = None
        self.travel_delay = 5


class Occupier(object):
    def __init__(self, env):
        self.env = env
        self.location = None
        self.destination = None
        self.requests = []

    def travel(self, instantiation_loc):
        self.requests.append(instantiation_loc.resource.request())
        yield(self.requests[-1])

        self.location = instantiation_loc
        self.destination = instantiation_loc.up_connection
        yield self.env.timeout(self.location.travel_delay)
        node_occupancy(nodes)

        while self.destination.up_connection != None:
            self.requests.append(self.destination.resource.request())
            yield self.requests[-1]

            self.location.resource.release(self.requests[0])
            self.requests.pop(0)
            self.location = self.destination
            self.destination = self.location.up_connection

            yield self.env.timeout(self.location.travel_delay)
            node_occupancy(nodes)


def node_occupancy(nodes):
    print([node.resource.count for node in nodes])       


env = simpy.Environment()

nodes = [Node(env) for i in range(10)]
for i in range(len(nodes) - 1):
    nodes[i].up_connection = nodes[i + 1]

env.process(Occupier(env).travel(nodes[0]))

env.run()

如果我 运行 上面的代码与一位旅行者一起使用,它似乎可以正常工作,给出以下输出:

[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0]

但是,如果实例化第二个旅行者,您会发现在某些时间点,一个旅行者占用了两个资源,而实际上它应该只占用一个:

env.process(Occupier(env).travel(nodes[3]))
env.process(Occupier(env).travel(nodes[0]))

对应输出:

[1, 0, 0, 1, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 1, 1, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 1, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 1, 1, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 1, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 1, 1, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 1, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 1, 1, 0, 0]
[0, 0, 0, 0, 1, 0, 0, 1, 0, 0]
[0, 0, 0, 0, 1, 0, 0, 1, 1, 0]
[0, 0, 0, 0, 0, 1, 0, 0, 1, 0]
[0, 0, 0, 0, 0, 1, 0, 0, 1, 0]
[0, 0, 0, 0, 0, 0, 1, 0, 1, 0]
[0, 0, 0, 0, 0, 0, 0, 1, 1, 0]

对我的模拟来说重要的是,旅行者只占用一个资源,因为节点的属性会根据此频繁修改。

有什么方法可以防止旅行者从不占用多个资源的这种行为吗?即当旅行者被分配到一个新资源时,同时释放一个资源

事实上,您的模型 运行 正确。

尝试添加到函数node_ocupacy、当前执行时间和一些标记来标识当前模拟阶段:

def node_occupancy(nodes, node, case):
    print(env.now, node, case, [node.resource.count for node in nodes])  

此外,我做了一些更改只是为了看到更好的模拟日志:

def travel(self, instantiation_loc, loc):
    self.requests.append(instantiation_loc.resource.request())
    yield(self.requests[-1])

    self.location = instantiation_loc
    self.destination = instantiation_loc.up_connection
    yield self.env.timeout(self.location.travel_delay)
    node_occupancy(nodes, loc, 1)

    while self.destination.up_connection != None:
        self.requests.append(self.destination.resource.request())
        node_occupancy(nodes, loc, 2)

        yield self.requests[-1]
        node_occupancy(nodes, loc, 3)
        self.location.resource.release(self.requests[0])
        node_occupancy(nodes, loc, 4)
        self.requests.pop(0)
        self.location = self.destination
        self.destination = self.location.up_connection

        yield self.env.timeout(self.location.travel_delay)
        node_occupancy(nodes, loc, 5)

现在,运行 带有当前节点标记的模拟:

env.process(Occupier(env).travel(nodes[3], 3))
env.process(Occupier(env).travel(nodes[0], 0))

查看结果,你会发现事件(request/release)同时发生并且同时资源时间占用总是=0(即:阶段'3'和'4'之间的时间',对于同一个实体将始终为 0):

5 3 1 [1, 0, 0, 1, 0, 0, 0, 0, 0, 0]
5 3 2 [1, 0, 0, 1, 1, 0, 0, 0, 0, 0]
5 0 1 [1, 0, 0, 1, 1, 0, 0, 0, 0, 0]
5 0 2 [1, 1, 0, 1, 1, 0, 0, 0, 0, 0]
5 3 3 [1, 1, 0, 1, 1, 0, 0, 0, 0, 0]
5 3 4 [1, 1, 0, 0, 1, 0, 0, 0, 0, 0]
5 0 3 [1, 1, 0, 0, 1, 0, 0, 0, 0, 0]
5 0 4 [0, 1, 0, 0, 1, 0, 0, 0, 0, 0]