Используя Simpy, я хочу смоделировать сетевую систему, которая имеет два разных режима. В определенный момент времени я хочу перевести систему из режима 1 в режим 2. Однако с помощью следующего кода после перехода с mode_1
на mode_2
процесс mode_1
все еще выполняется. Как мне остановить процесс mode_1
, когда процесс mode_2
запущен?
import random
import simpy
import pandas as pd
from datetime import datetime, timedelta
RANDOM_SEED = 42
MTTF_1 = 100000.0 # Mean time to failure in minutes
MTTF_2 = 1000000.0
RT_MEAN = 10.0 # Avg. recovering time in minutes
RT_SIGMA = 2.0 # Sigma of recovering time
RECOVERY_TIME_2 = 100.0
OPER_COST_PER_HOUR_1 = 1000 # Operation cost per hour
OPER_COST_PER_HOUR_2 = 3000
NUM_SYSTEMS = 1 # Number of subsuppliers included in the SLA contract
WEEKS = 200 # Simulation time in weeks
SIM_TIME = WEEKS * 7 * 24 * 60 # Simulation time in minutes
DOWNTIME_BUDGET = 800
PENALTY_PER_SECOND = 500
SHIFTING_TIME = 20 * 7 * 24 * 60
RECOVER_TIME = 60
def time_to_failure(mttf):
return random.expovariate(1.0/mttf)
def time_to_recover():
return RECOVER_TIME
def get_time(minutes):
week = int(minutes/(7*24*60))
day = int((minutes-week*7*24*60)/(24*60))
hour = int((minutes-week*7*24*60-day*24*60)/60)
minute = minutes-week*7*24*60-day*24*60-hour*60
return "%d:%d:%d:%d" % (week, day, hour, minute)
class System:
def __init__(self, env):
self.env = env
self.mode_1_proc = env.process(self.mode_1(env))
env.process(self.fail_in_mode_1())
self.failed = False
def mode_1(self, env):
while True:
mode_1 = SHIFTING_TIME
while mode_1:
try:
yield env.timeout(SHIFTING_TIME)
print('Start mode 2 at', get_time(env.now))
mode_2_proc = env.process(self.mode_2(env))
yield mode_2_proc
except simpy.Interrupt as i:
self.failed = True
print('fails in mode 1 at', get_time(env.now), i.cause)
yield self.env.timeout(time_to_recover())
print('recovers in mode 1 at', get_time(env.now))
self.failed = False
def mode_2(self, env):
print('Mode 2 started at', get_time(env.now))
yield env.timeout(time_to_failure(MTTF_2))
print('Mode 2 is failed at', get_time(env.now))
print('Mode 2 done at', env.now)
def fail_in_mode_1(self):
while True:
yield self.env.timeout(time_to_failure(MTTF_1))
if not self.failed:
self.mode_1_proc.interrupt()
env = simpy.Environment()
ev = System(env)
env.run(until=SIM_TIME)
Запустив код, я получаю результат как:
Start mode 2 at 20:0:0:0
Mode 2 started at 20:0:0:0
fails in mode 1 at 21:5:6:7 None
recovers in mode 1 at 21:5:7:7
fails in mode 1 at 35:6:6:30 None
recovers in mode 1 at 35:6:7:30
fails in mode 1 at 44:4:7:44 None
recovers in mode 1 at 44:4:8:44
fails in mode 1 at 47:1:5:14 None
recovers in mode 1 at 47:1:6:14
Start mode 2 at 67:1:6:14
Mode 2 started at 67:1:6:14
fails in mode 1 at 73:2:1:5 None
recovers in mode 1 at 73:2:2:5
fails in mode 1 at 85:1:8:21 None
recovers in mode 1 at 85:1:9:21
Mode 2 is failed at 96:2:1:15
Mode 2 done at 970635.0949625104
fails in mode 1 at 102:5:0:52 None
recovers in mode 1 at 102:5:1:52
fails in mode 1 at 107:1:20:24 None
recovers in mode 1 at 107:1:21:24
Start mode 2 at 127:1:21:24
Mode 2 started at 127:1:21:24
fails in mode 1 at 133:4:13:26 None
recovers in mode 1 at 133:4:14:26
fails in mode 1 at 146:1:9:43 None
recovers in mode 1 at 146:1:10:43
fails in mode 1 at 153:0:1:54 None
recovers in mode 1 at 153:0:2:54
Mode 2 is failed at 163:4:14:25
Mode 2 done at 1649665.6738734795
fails in mode 1 at 167:4:22:36 None
recovers in mode 1 at 167:4:23:36
fails in mode 1 at 179:0:1:29 None
recovers in mode 1 at 179:0:2:29
fails in mode 1 at 191:2:3:6 None
recovers in mode 1 at 191:2:4:6