python仿真Guitar factory

python仿真Guitar factory_第1张图片

  • 有两个容器:Wood和Electronic,这些容器数量为N,即各有N个木材和电子配件可以用来加工
  • 从Wood中取出一个木材转化成一个Body并存储在Body storage中,从Wood中取出一个木材转化成2个Neck并存储在Neck storage中
  • 取出一个Body和一个Neck分别彩绘,存放在Body storage 2和Neck storage 2中
  • 取出一个Body,一个Neck和一个电子配件组装成guitar,存放在Dispatch容器中
  • 制作完N个guitars后,某人会取走所有的吉他
  • Wood和Electronic低于一定水平后,T天后,supplier到达工厂补充原材料
import random

# 时间
hours = 8
days = 23
total_time = hours * days

# wood
wood_capacity = 500
init_wood = 200

# electronic
electronic_capacity = 100
initial_electronic = 60

# painter
body_pre_paint_capacity = 60
body_post_paint_capacity = 120
neck_pre_paint_capacity = 60
neck_post_paint_capacity = 120

# dispatch
dispatch_capacity = 500

# 数据分布
num_body = 2  
mean_body = 1  
std_body = 0.1 

num_neck = 1
mean_neck = 1
std_neck = 0.2


num_paint = 3
mean_paint = 3
std_paint = 0.3

 
num_ensam = 2
mean_ensam = 1
std_ensam = 0.2
wood_critical_level = ((8/mean_body) * num_body + (8/mean_neck) * num_neck) * 3
electronic_critical_level = ((8/mean_ensam) * num_ensam) * 2
class Guitar_Factory:
    def __init__(self, env):
        self.wood = simpy.Container(env, capacity=wood_capacity, init=init_wood)
        self.wood_control = env.process(self.wood_stock_level(env))
        
        self.electronic = simpy.Container(env, capacity=electronic_capacity, init=initial_electronic)
        self.electronic_control = env.process(self.electronic_stock_level(env))
        
        self.body_pre_paint = simpy.Container(env, capacity=body_pre_paint_capacity, init=0)
        self.body_post_paint = simpy.Container(env, capacity=body_post_paint_capacity, init=0)
        self.neck_pre_paint = simpy.Container(env, capacity=neck_pre_paint_capacity, init=0)
        self.neck_post_paint = simpy.Container(env, capacity=neck_post_paint_capacity, init=0)
    
        self.dispatch = simpy.Container(env, capacity=dispatch_capacity, init=0)
        self.dispatch_control = env.process(self.dispatch_control(env))
        
    def wood_stock_level(self, env):
        while True:
            if self.wood.level <= wood_critical_level:
                print(f'wood stock bellow critical level with {self.wood.level} at day {env.now/8} hour {env.now%8}')
                print('call wood supplier')
                print('------------------')
                yield env.timeout(16)
                print(f'wood arrive at day {env.now/8} hour {env.now%8}')
                yield self.wood.put(300)
                print(f'new wood stock {self.wood.level}')
                print('------------------')
                yield env.timeout(8)
            else:
                yield env.timeout(1)
    
    def electronic_stock_level(self, env):
        while True:
            if self.electronic.level <= electronic_critical_level:
                print(f'electronic stock bellow critical level with {self.electronic.level} at day {env.now/8} hour {env.now%8}')
                print('call electronic supplier')
                print('------------------')
                yield env.timeout(9)
                print(f'electronic arrive at day {env.now/8} hour {env.now%8}')
                yield self.electronic.put(30)
                print(f'new electronic stock {self.electronic.level}')
                print('------------------')
                yield env.timeout(8)
            else:
                yield env.timeout(1)
                
    def dispatch_control(self, env):
        while True:
            global guitars_made
            yield env.timeout(0)
            while True:
                if self.dispatch.level >= 50:
                    print(f'dispatch store {self.dispatch.level} , pick guitars at day {env.now/8} hour {env.now%8}')
                    print('-------------------')
                    yield env.timeout(4)
                    print(f'pick {self.dispatch.level} at day {env.now/8} hour {env.now%8}')
                    guitars_made += self.dispatch.level
                    yield self.dispatch.get(self.dispatch.level)
                    print('-------------')
                    yield env.timeout(8)
                else:
                    yield env.timeout(1)
def body_maker(env, guitar_factory):
    while True:
        yield guitar_factory.wood.get(1)  
        body_time = random.gauss(mean_body, std_body)
        yield env.timeout(body_time)  
        yield guitar_factory.body_pre_paint.put(1)  

def neck_maker(env, guitar_factory):
    while True:
        yield guitar_factory.wood.get(1)
        neck_time = random.gauss(mean_neck, std_neck)
        yield env.timeout(neck_time)
        yield guitar_factory.neck_pre_paint.put(2)  
        
def painter(env, guitar_factory):
    while True:
        yield guitar_factory.body_pre_paint.get(5)
        yield guitar_factory.neck_pre_paint.get(5)
        paint_time = random.gauss(mean_paint, std_paint)
        yield env.timeout(paint_time)
        yield guitar_factory.body_post_paint.put(5)
        yield guitar_factory.neck_post_paint.put(5)
        
def assembler(env, guitar_factory):
    while True:
        yield guitar_factory.body_post_paint.get(1)
        yield guitar_factory.neck_post_paint.get(1)
        yield guitar_factory.electronic.get(1)
        assembling_time = max(random.gauss(mean_ensam, std_ensam), 1)
        yield env.timeout(assembling_time)
        yield guitar_factory.dispatch.put(1)
def body_maker_gen(env, guitar_factory):
    for i in range(num_body):
        env.process(body_maker(env, guitar_factory))
        yield env.timeout(0)
        
def neck_maker_gen(env, guitar_factory):
    for i in range(num_neck):
        env.process(neck_maker(env, guitar_factory))
        yield env.timeout(0)
        
def painter_maker_gen(env, guitar_factory):
    for i in range(num_paint):
        env.process(painter(env, guitar_factory))
        yield env.timeout(0)
        
def assembler_maker_gen(env, guitar_factory):
    for i in range(num_ensam):
        env.process(assembler(env, guitar_factory))
        yield env.timeout(0)
env = simpy.Environment()
guitar_factory = Guitar_Factory(env)

_ = env.process(body_maker_gen(env, guitar_factory))
_ = env.process(neck_maker_gen(env, guitar_factory))
_ = env.process(painter_maker_gen(env, guitar_factory))
_ = env.process(assembler_maker_gen(env, guitar_factory))

env.run(until=total_time)

print('Pre paint has {0} bodies and {1} necks ready to be painted'.format(
    guitar_factory.body_pre_paint.level, guitar_factory.neck_pre_paint.level))
print('Post paint has {0} bodies and {1} necks ready to be assembled'.format(
    guitar_factory.body_post_paint.level, guitar_factory.neck_post_paint.level))
print(f'Dispatch has %d guitars ready to go!' % guitar_factory.dispatch.level)
print(f'----------------------------------')
print('total guitars made: {0}'.format(guitars_made + guitar_factory.dispatch.level))
print(f'----------------------------------')
print(f'SIMULATION COMPLETED')

输出

electronic stock bellow critical level with 31 at day 2.75 hour 6
call electronic supplier
------------------
electronic arrive at day 3.875 hour 7
new electronic stock 44
------------------
dispatch store 51 , pick guitars at day 4.375 hour 3
-------------------
electronic stock bellow critical level with 29 at day 4.875 hour 7
call electronic supplier
------------------
pick 59 at day 4.875 hour 7
-------------
wood stock bellow critical level with 72 at day 5.25 hour 2
call wood supplier
------------------
electronic arrive at day 6.0 hour 0
new electronic stock 43
------------------
electronic stock bellow critical level with 29 at day 7.0 hour 0
call electronic supplier
------------------
wood arrive at day 7.25 hour 2
new wood stock 326
------------------
electronic arrive at day 8.125 hour 1
new electronic stock 42
------------------
dispatch store 51 , pick guitars at day 8.375 hour 3
-------------------
pick 58 at day 8.875 hour 7
-------------
electronic stock bellow critical level with 28 at day 9.125 hour 1
call electronic supplier
------------------
electronic arrive at day 10.25 hour 2
new electronic stock 42
------------------
electronic stock bellow critical level with 28 at day 11.25 hour 2
call electronic supplier
------------------
electronic arrive at day 12.375 hour 3
dispatch store 50 , pick guitars at day 12.375 hour 3
-------------------
new electronic stock 41
------------------
pick 58 at day 12.875 hour 7
-------------
electronic stock bellow critical level with 26 at day 13.375 hour 3
call electronic supplier
------------------
electronic arrive at day 14.5 hour 4
new electronic stock 40
------------------
electronic stock bellow critical level with 25 at day 15.5 hour 4
call electronic supplier
------------------
dispatch store 50 , pick guitars at day 16.25 hour 2
-------------------
electronic arrive at day 16.625 hour 5
new electronic stock 38
------------------
pick 57 at day 16.75 hour 6
-------------
electronic stock bellow critical level with 24 at day 17.625 hour 5
call electronic supplier
------------------
wood stock bellow critical level with 71 at day 17.625 hour 5
call wood supplier
------------------
electronic arrive at day 18.75 hour 6
new electronic stock 37
------------------
wood arrive at day 19.625 hour 5
new wood stock 324
------------------
electronic stock bellow critical level with 22 at day 19.75 hour 6
call electronic supplier
------------------
dispatch store 51 , pick guitars at day 20.25 hour 2
-------------------
pick 59 at day 20.75 hour 6
-------------
electronic arrive at day 20.875 hour 7
new electronic stock 35
------------------
electronic stock bellow critical level with 19 at day 21.875 hour 7
call electronic supplier
------------------
Pre paint has 4 bodies and 5 necks ready to be painted
Post paint has 33 bodies and 33 necks ready to be assembled
Dispatch has 34 guitars ready to go!
----------------------------------
total guitars made: 1480
----------------------------------
SIMULATION COMPLETED

你可能感兴趣的