隨機程序模擬(二)

程式碼

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# -*- coding: utf-8 -*-

import simpy
import numpy


# 機具
class Machine(object):
    # 建構機具物件
    def __init__(self, sim_environment, 
                 machine_definition, item_definition):
        
        self.sim_environment = sim_environment
        self.machine_definition = machine_definition
        self.item_definition = item_definition
        
        # 將機具視為有限資源
        resource_capacity = len(machine_definition.keys())
        self.machine_resource = simpy.Resource(sim_environment, 
                                               capacity=resource_capacity)
        
    # 機具資源
    def getResource(self):
        return self.machine_resource

    # 隨機指派機具
    def findMachine(self):
        name_machine = 'M-%02d' % numpy.random.randint(
            len(self.machine_definition.keys()))
        _machine = self.machine_definition[name_machine]
            
        return name_machine
            
    # 當機具收到待修物件時
    def service(self, name_machine, name_item):
        # 進場時間
        time_start = self.sim_environment.now
        
        machine = self.machine_definition[name_machine]
        item = self.item_definition[name_item]
        
        machine['busy'] = True

        # 機具處理時間 = 機具單位處理時間 * 物件損壞程度
        working_time = machine['time'] * item['damage']
        yield self.sim_environment.timeout(working_time)

        # 機具處理後紀錄
        machine['busy'] = False
        
        machine['history'].append({
            'item': name_item, 
            'start': time_start, 
            'end': self.sim_environment.now })


# 物件
class Item(object):
    def __init__(self, sim_environment, 
                 machine_definition, item_definition, 
                 machine):
        
        self.sim_environment = sim_environment
        self.machine_definition = machine_definition
        self.item_definition = item_definition
        self.machine = machine

    # 當物件需要待修時
    def service(self, name_item):
        item = self.item_definition[name_item]

        # 物件請求機具資源成功
        with self.machine.getResource().request() as request:
            yield request

            # 物件開始處理時間
            time_start = self.sim_environment.now
            
            # 隨機指派機具服務
            name_machine = self.machine.findMachine()
            if name_machine is None:
                # 機具沒空處理物件
                item['history'].append({
                    'machine': '',
                    'start': self.sim_environment.now, 
                    'end': self.sim_environment.now })
            else:
                # 觸發機具服務
                yield self.sim_environment.process(
                    self.machine.service(name_machine, name_item))
        
                # 物件處理結束時間
                item['broken'] = False
                
                item['history'].append({
                    'machine': name_machine,
                    'start': time_start, 
                    'end': self.sim_environment.now })


# 模擬
class Simulation(object):
    # 建構物件
    def __init__(self, simulation_cycle, 
                 num_machine=4, num_item=16, 
                 event_interval=8):
        
        self.simulation_cycle = simulation_cycle
        self.event_interval = event_interval
        
        # 依據模擬週期,重設亂數因子
        numpy.random.seed(numpy.random.randint(simulation_cycle) + 1)
    
        # 隨機產生機具參數,至少有 4 ~ 7 部可供服務
        self.machine_definition = {}
        for i in range(0, numpy.random.randint(num_machine) + num_machine):
            self.machine_definition['M-%02d' % i] = {
                'time': 10 * numpy.random.rand() + 0.1,
                'busy': False,
                'history': []
                }
        
        # 隨機產生物件參數,至少有 16 ~ 35 部可供服務
        self.item_definition = {}
        for i in range(0, numpy.random.randint(num_item) + num_item):
            self.item_definition['I-%02d' % i] = {
                'damage': numpy.random.rand() + 0.1,
                'broken': False,
                'history': []
                }

        # 模擬環境
        self.sim_environment = simpy.Environment()

        # 初始機具
        self.machine = Machine(self.sim_environment, 
                               self.machine_definition, self.item_definition)
        # 初始物件
        self.item = Item(self.sim_environment, 
                         self.machine_definition, self.item_definition, 
                         self.machine)

    # 待修物件
    def __run__(self):
        while True:
            # 隨機產生待修物件,至少要等候 8 ~ 15 個
            _event_interval = numpy.random.randint(self.event_interval) + self.event_interval
            
            # 物件等候處理所需之間隔時間
            yield self.sim_environment.timeout(_event_interval)
            
            name_item = 'I-%02d' % numpy.random.randint(len(self.item_definition.keys()))
            item = self.item_definition[name_item]
            item['broken'] = True
            
            # 觸發機具處理新物件
            self.sim_environment.process(self.item.service(name_item))
        
    # 模擬進入點
    def service(self):
        # 設置模擬程序
        self.sim_environment.process(self.__run__())
        
        # 啟動模擬
        self.sim_environment.run(until=self.simulation_cycle)

        # 輸出結果
        self.result()

    # 輸出結果
    def result(self):
        # 輸出機具服務
        with open('M.dat', 'w') as fM:
            for machine in self.machine_definition:
                _machine = self.machine_definition[machine]
                
                _history = _machine['history']        
                for _work in _history:
                    fM.write(
                        '%s\t%6.2f\t%s\t%6.2f\t%6.2f\t%6.2f\n' % (
                            machine, _machine['time'], _work['item'], 
                            _work['start'], _work['end'],
                            _work['end'] - _work['start'])
                         )
            
        # 輸出物件維修
        with open('I.dat', 'w') as fI:
            for item in self.item_definition:
                _item = self.item_definition[item]
                
                _history = _item['history']
                for _work in _history:
                    fI.write(
                        '%s\t%6.2f\t%s\t%6.2f\t%6.2f\t%6.2f\n' % (
                            item, _item['damage'], _work['machine'], 
                            _work['start'], _work['end'], 
                            _work['end'] - _work['start'])
                         )


# 主程式
if __name__ == '__main__':
    # m 個物件指派 n 台機具維修
    # 物件隨機等候發生故障
    # 機具隨機被指派維修物件
    
    simulation = Simulation(
        simulation_cycle=500, 
        num_machine=4, num_item=16, 
        event_interval=8)
    
    simulation.service()
    

機具模擬紀錄

M-04	  6.65	I-18	207.00	212.07	  5.07
M-04	  6.65	I-18	232.00	237.07	  5.07
M-04	  6.65	I-06	253.00	259.54	  6.54
M-04	  6.65	I-22	268.00	269.01	  1.01
M-04	  6.65	I-16	354.00	358.29	  4.29
M-05	  3.32	I-18	 11.00	 13.53	  2.53
M-05	  3.32	I-16	 50.00	 52.14	  2.14
M-05	  3.32	I-07	104.00	106.29	  2.29
M-05	  3.32	I-19	123.00	126.47	  3.47
M-05	  3.32	I-03	294.00	294.66	  0.66
M-05	  3.32	I-00	329.00	329.39	  0.39
M-05	  3.32	I-21	363.00	366.36	  3.36
M-05	  3.32	I-18	387.00	389.53	  2.53
M-02	  9.36	I-13	 36.00	 39.35	  3.35
M-02	  9.36	I-17	 72.00	 74.32	  2.32
M-02	  9.36	I-03	 92.00	 93.86	  1.86
M-02	  9.36	I-19	151.00	160.80	  9.80
M-02	  9.36	I-03	170.00	171.86	  1.86
M-02	  9.36	I-24	398.00	399.78	  1.78
M-03	  5.22	I-24	114.00	114.99	  0.99
M-03	  5.22	I-04	138.00	142.94	  4.94
M-03	  5.22	I-20	244.00	247.70	  3.70
M-03	  5.22	I-12	314.00	315.55	  1.55
M-03	  5.22	I-04	406.00	410.94	  4.94
M-03	  5.22	I-24	429.00	429.99	  0.99
M-03	  5.22	I-25	465.00	470.17	  5.17
M-03	  5.22	I-20	495.00	498.70	  3.70
M-00	  8.58	I-23	 21.00	 29.94	  8.94
M-00	  8.58	I-22	 64.00	 65.31	  1.31
M-00	  8.58	I-10	306.00	308.42	  2.42
M-00	  8.58	I-13	342.00	345.07	  3.07
M-00	  8.58	I-01	377.00	382.51	  5.51
M-00	  8.58	I-05	480.00	487.98	  7.98
M-01	  8.38	I-20	 80.00	 85.94	  5.94
M-01	  8.38	I-00	160.00	160.98	  0.98
M-01	  8.38	I-02	178.00	179.06	  1.06
M-01	  8.38	I-20	186.00	191.94	  5.94
M-01	  8.38	I-15	196.00	200.17	  4.17
M-01	  8.38	I-17	219.00	221.08	  2.08
M-01	  8.38	I-18	283.00	289.39	  6.39
M-01	  8.38	I-21	414.00	422.49	  8.49
M-01	  8.38	I-02	438.00	439.06	  1.06
M-01	  8.38	I-09	450.00	455.79	  5.79

機具模擬紀錄

I-02	  0.13	M-01	178.00	179.06	  1.06
I-02	  0.13	M-01	438.00	439.06	  1.06
I-03	  0.20	M-02	 92.00	 93.86	  1.86
I-03	  0.20	M-02	170.00	171.86	  1.86
I-03	  0.20	M-05	294.00	294.66	  0.66
I-00	  0.12	M-01	160.00	160.98	  0.98
I-00	  0.12	M-05	329.00	329.39	  0.39
I-01	  0.64	M-00	377.00	382.51	  5.51
I-06	  0.98	M-04	253.00	259.54	  6.54
I-07	  0.69	M-05	104.00	106.29	  2.29
I-04	  0.95	M-03	138.00	142.94	  4.94
I-04	  0.95	M-03	406.00	410.94	  4.94
I-05	  0.93	M-00	480.00	487.98	  7.98
I-09	  0.69	M-01	450.00	455.79	  5.79
I-20	  0.71	M-01	 80.00	 85.94	  5.94
I-20	  0.71	M-01	186.00	191.94	  5.94
I-20	  0.71	M-03	244.00	247.70	  3.70
I-20	  0.71	M-03	495.00	498.70	  3.70
I-21	  1.01	M-05	363.00	366.36	  3.36
I-21	  1.01	M-01	414.00	422.49	  8.49
I-22	  0.15	M-00	 64.00	 65.31	  1.31
I-22	  0.15	M-04	268.00	269.01	  1.01
I-23	  1.04	M-00	 21.00	 29.94	  8.94
I-25	  0.99	M-03	465.00	470.17	  5.17
I-19	  1.05	M-05	123.00	126.47	  3.47
I-19	  1.05	M-02	151.00	160.80	  9.80
I-18	  0.76	M-05	 11.00	 13.53	  2.53
I-18	  0.76	M-04	207.00	212.07	  5.07
I-18	  0.76	M-04	232.00	237.07	  5.07
I-18	  0.76	M-01	283.00	289.39	  6.39
I-18	  0.76	M-05	387.00	389.53	  2.53
I-15	  0.50	M-01	196.00	200.17	  4.17
I-17	  0.25	M-02	 72.00	 74.32	  2.32
I-17	  0.25	M-01	219.00	221.08	  2.08
I-16	  0.65	M-05	 50.00	 52.14	  2.14
I-16	  0.65	M-04	354.00	358.29	  4.29
I-10	  0.28	M-00	306.00	308.42	  2.42
I-13	  0.36	M-02	 36.00	 39.35	  3.35
I-13	  0.36	M-00	342.00	345.07	  3.07
I-12	  0.30	M-03	314.00	315.55	  1.55
I-24	  0.19	M-03	114.00	114.99	  0.99
I-24	  0.19	M-02	398.00	399.78	  1.78
I-24	  0.19	M-03	429.00	429.99	  0.99