同步来自不同进程的SimPy资源请求

时间:2019-09-16 12:12:37

标签: simpy

我是SimPy建模的入门者,所以我的问题的答案可能就在我的鼻子底下,但我看不到它。但是,这里有一个问题:我想用三个机床( deckel_maho,makino,huron )对车间进行建模。工作订单(类订单的实例)到达车间,要求以不同的(动态)顺序在每个机床中进行处理,即不同的网络工作订单具有不同的处理顺序(从外部数据文件中读取) )。另外,这是我的问题:在一个处理中,某些机床需要两个(或多个)相同类型的工作指令。如何在SimPy中的一个操作中实现请求和共享相同资源的不同进程的这种同步?我猜想 class Order run_process 方法中必须包含一些内容,但是我可以看到... 这是我的模型代码的一部分:

import random
import simpy

RANDOM_SEED = 42
T_INTER = 7       # Create an order every ~7 minutes
SIM_TIME = 20     # Simulation time in minutes

class Workshop(object):

    def __init__(self, env):
        self.env = env
        self.deckel_maho = simpy.Resource(env, 1)
        self.makino = simpy.Resource(env, 1)
        self.huron = simpy.Resource(env, 1)

        self.deckel_maho_queue = []
        self.makino_queue = []
        self.huron_queue = []

        self.latest_deckel_maho_order = ''
        self.latest_makino_order = ''
        self.latest_huron_order = ''

    def deckel_maho_process(self, order_type, cycle_time, setup_time):
        setup_required = False
        if order_type != self.latest_deckel_maho_order:
            setup_required = True
        if setup_required:
            yield self.env.timeout(setup_time)
            print('DeckelMaho exits setup for %s at %.2f.' % (order_type, self.env.now))
            self.latest_deckel_maho_order = order_type

        yield self.env.timeout(cycle_time)
        print('%s exits DeckelMaho at %.2f.' % (order_type, self.env.now))

    def makino_process(self, order_type, cycle_time, setup_time):
        setup_required = False
        if order_type != self.latest_makino_order:
            setup_required = True
        if setup_required:
            yield self.env.timeout(setup_time)
            print('Makino exits setup for %s at %.2f.' % (order_type, self.env.now))
            self.latest_makino_order = order_type

        yield self.env.timeout(cycle_time)
        print('%s exits Makino at %.2f.' % (order_type, self.env.now))

    def huron_process(self, order_type, cycle_time, setup_time):
        setup_required = False
        if order_type != self.latest_huron_order:
            setup_required = True
        if setup_required:
            yield self.env.timeout(setup_time)
            print('Huron exits setup for %s at %.2f.' % (order_type, self.env.now))
            self.latest_huron_order = order_type

        yield self.env.timeout(cycle_time)
        print('%s exits Huron at %.2f.' % (order_type, self.env.now))

class Order(object):
    def __init__(self, env, order_type, order_no, ws):
        self.env = env
        self.order_type = order_type
        self.order_no = order_no
        self.ws = ws

        self.order_id = str(self.order_type) + str(self.order_no)
        self.end_time = 0.0

        dirPath = os.path.abspath('C:/Per Aage/SimPy/My Test Models')
        filePath = os.path.join(dirPath, 'ProductHolons.txt')
        productHolonFile = open(filePath, 'r')
        productHolons = productHolonFile.readlines()
        productHolonFile.close()
        self.holonList = []
        self.holonList = self.build_product_holon_structure(productHolons, self.holonList)

        self.process = env.process(self.run_process())

    def run_process(self):
        op_no = '0102'
        opID, altResList = self.get_operation(self.order_type, op_no, self.holonList)
        op_data = altResList[0]
        cycle_time = float(op_data[1])
        setup_time = float(op_data[2])
        op_batch = int(op_data[3])

        res_queues = self.get_res_queues(altResList)
        best_queue = self.evaluate_res_queues(res_queues)
        print('Best Q for ', self.order_id, ': ', best_queue[0])

        machine = best_queue[0]

        self.enter_in_queue(machine, cycle_time, setup_time, res_queues, env)

        if machine == 'DeckelMaho':
            res = self.ws.deckel_maho
            print('%s arrives at DeckelMaho at %.2f.' % (self.order_id, env.now))
            with res.request() as request:
                yield request

                print('%s enters DeckelMaho at %.2f.' % (self.order_id, env.now))
                yield env.process(self.ws.deckel_maho_process(self.order_type, cycle_time, setup_time))

                print('%s exits DeckelMaho at %.2f.' % (self.order_id, env.now))
                self.ws.deckel_maho_queue.pop(0)
                #print('DeckelMaho queue at ', env.now, ': ', self.ws.deckel_maho_queue)
        elif machine == 'Makino':
            res = self.ws.makino
            print('%s arrives at Makino at %.2f.' % (self.order_id, env.now))
            with res.request() as request:
                yield request

                print('%s enters Makino at %.2f.' % (self.order_id, env.now))
                yield env.process(self.ws.makino_process(self.order_type, cycle_time, setup_time))

                print('%s exits Makino at %.2f.' % (self.order_id, env.now))
                self.ws.makino_queue.pop(0)
                #print('Makino queue at ', env.now, ': ', self.ws.makino_queue)
        elif machine == 'Huron':
            res = self.ws.huron
            print('%s arrives at Huron at %.2f.' % (self.order_id, env.now))
            with res.request() as request:
                yield request

                print('%s enters Huron at %.2f.' % (self.order_id, env.now))
                yield env.process(self.ws.huron_process(self.order_type, cycle_time, setup_time))

                print('%s exits Huron at %.2f.' % (self.order_id, env.now))
                self.ws.huron_queue.pop(0)
                #print('Huron queue at ', env.now, ': ', self.ws.huron_queue)

        op_no = '0202'
        opID, altResList = self.get_operation(self.order_type, op_no, self.holonList)
        op_data = altResList[0]
        cycle_time = float(op_data[1])
        setup_time = float(op_data[2])

        res_queues = self.get_res_queues(altResList)
        best_queue = self.evaluate_res_queues(res_queues)
        print('Best Q for ', self.order_id, ': ', best_queue[0])

        machine = best_queue[0]

        self.enter_in_queue(machine, cycle_time, setup_time, res_queues, env)

        if machine == 'DeckelMaho':
            res = self.ws.deckel_maho
            print('%s arrives at DeckelMaho at %.2f.' % (self.order_id, env.now))
            with res.request() as request:
                yield request

                print('%s enters DeckelMaho at %.2f.' % (self.order_id, env.now))
                yield env.process(self.ws.deckel_maho_process(self.order_type, cycle_time, setup_time))

                print('%s exits DeckelMaho at %.2f.' % (self.order_id, env.now))
                self.ws.deckel_maho_queue.pop(0)
                #print('DeckelMaho queue at ', env.now, ': ', self.ws.deckel_maho_queue)
        elif machine == 'Makino':
            res = self.ws.makino
            print('%s arrives at Makino at %.2f.' % (self.order_id, env.now))
            with res.request() as request:
                yield request

                print('%s enters Makino at %.2f.' % (self.order_id, env.now))
                yield env.process(self.ws.makino_process(self.order_type, cycle_time, setup_time))

                print('%s exits Makino at %.2f.' % (self.order_id, env.now))
                self.ws.makino_queue.pop(0)
                #print('Makino queue at ', env.now, ': ', self.ws.makino_queue)
        elif machine == 'Huron':
            res = self.ws.huron
            print('%s arrives at Huron at %.2f.' % (self.order_id, env.now))
            with res.request() as request:
                yield request

                print('%s enters Huron at %.2f.' % (self.order_id, env.now))
                yield env.process(self.ws.huron_process(self.order_type, cycle_time, setup_time))

                print('%s exits Huron at %.2f.' % (self.order_id, env.now))
                self.ws.huron_queue.pop(0)
                #print('Huron queue at ', env.now, ': ', self.ws.huron_queue)
            # And so on....

def setup(env, t_inter):
    # Create the workshop
    workshop = Workshop(env)

    order_sequence = ['EM2040RX', 'HISASRX', 'ES10', 'EM2040RX', 'HISASRX', 'ES10']

    # Create orders
    j = 0
    for i in range(len(order_sequence)):
        order_type = order_sequence[i]
        j += 1
        if j < 10:
            order_no = '000' + str(j)
        elif j < 100:
            order_no = '00' + str(j)
        order = Order(env, order_type, order_no, workshop)

    while True:
        yield env.timeout(random.randint(t_inter - 2, t_inter + 2))
        idx = i % (len(order_sequence) - 1)
        order_type = order_sequence[idx]
        i += 1
        j += 1
        if j < 10:
            order_no = '000' + str(j)
        elif j < 100:
            order_no = '00' + str(j)
        order = Order(env, order_type, order_no, workshop)  

# Setup and start the simulation
print('Workshop')
random.seed(RANDOM_SEED)

env = simpy.Environment()
env.process(setup(env, T_INTER))

env.run(until=SIM_TIME)```

0 个答案:

没有答案