SimPy Resource of 3 where each has different characteristics

情到浓时终转凉″ 提交于 2019-12-05 05:36:57

问题


I am trying to simulate a situation where we have 5 machines that occur in a 1 -> 3 -> 1 situation. i.e the 3 in the middle operate in parallel to reduce the effective time they take.

I can easily simulate this by create a SimPy resource with a value of three like this:

simpy.Resource(env, capacity=3)

However in my situation each of the three resources operates slightly differently and sometimes I want to be able to use any of them (when I'm operating) or book a specific one (when i want to clean). Basically the three machines slowly foul up at different rates and operate slower, I want to be able to simulate these and also enable a clean to occur when one gets too dirty.

I have tried a few ways of simulating this but have come up with problems and issues every time.

The first was when it booked the resource it also booked one of the 3 machines (A,B,C) globals flags and a flag itself to tell it which machine it was using. This works but it's not clean and makes it really difficult to understand what is occurring with huge if statements everywhere.

The second was to model it as three separate resources and then try to wait and request one of the 3 machines with something like:

reqA = A.res.request()
reqB = B.res.request()
reqC = C.res.request()

unitnumber = yield reqA | reqB | reqC
yield env.process(batch_op(env, name, machineA, machineB, machineC, unitnumber))

But this doesn't work and I can't work out the best way to look at yielding one of a choice.

What would be the best way to simulate this scenario. For completeness here is what im looking for:

  1. Request any of 3 machines
  2. Request a specific machine
  3. Have each machine track it's history
  4. Have each machines characteristics be different. i.e on fouls up faster but works faster initially
  5. Detect and schedule a clean based on the performance or indicator

This is what I have so far on my latest version of trying to model each as seperate resources

class Machine(object):

    def __init__(self, env, cycletime, cleantime, k1foul, k2foul):
        self.env = env
        self.res = simpy.Resource(env, 1)

        self.cycletime = cycletime
        self.cleantime = cleantime
        self.k1foul = k1foul
        self.k2foul = k2foul
        self.batchessinceclean = 0

    def operate(self):
        self.cycletime = self.cycletime + self.k2foul * np.log(self.k1foul * self.batchessinceclean + 1)
        self.batchessinceclean += 1
        yield self.env.timeout(self.cycletime) 

    def clean(self):
        print('%s begin cleaning at %s' % (self.env.now))
        self.batchessinceclean = 0
        yield env.timeout(self.cleantime)
        print('%s finished cleaning at %s' % (self.env.now))

回答1:


You should try (Filter)Store:

import simpy


def user(machine):
    m = yield machine.get()
    print(m)
    yield machine.put(m)

    m = yield machine.get(lambda m: m['id'] == 1)
    print(m)
    yield machine.put(m)

    m = yield machine.get(lambda m: m['health'] > 98)
    print(m)
    yield machine.put(m)


env = simpy.Environment()
machine = simpy.FilterStore(env, 3)
machine.put({'id': 0, 'health': 100})
machine.put({'id': 1, 'health': 95})
machine.put({'id': 2, 'health': 97.2})

env.process(user(machine))

env.run()


来源:https://stackoverflow.com/questions/24867265/simpy-resource-of-3-where-each-has-different-characteristics

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!