Advertisement
Infiniti_Inter

ИМС_3

Apr 18th, 2022
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.54 KB | None | 0 0
  1. from math import log
  2. from random import random
  3. import numpy as np
  4.  
  5.  
  6. class Demand:
  7.     def __init__(self, born, begin_service, death):
  8.         self.born_time = born
  9.         self.begin_service = begin_service
  10.         self.death_time = death
  11.        
  12.     def getinfo(self):
  13.         print(f'Время поступления в СМО: {self.born_time}')
  14.         print(f'Время поступления на прибор: {self.begin_service}')
  15.         print(f'Время выхода из СМО: {self.death_time}')
  16.            
  17.            
  18. # ввод параметров
  19. t_modeling = 100000 # время моделирования
  20. lambda_ = 10
  21. mu = 2
  22. k = 2
  23.  
  24.  
  25. t_act_source = 0 # момент генерации требования
  26. t_act_device1 = 0 # момент начала обслуживания требования 1
  27. t_act_device2 = 0 # момент начала обслуживания требования 2
  28. t_now = 0 # текущее время
  29. device1_free = True # индикатор занятости первого прибора
  30. device2_free = True # индикатор занятости второго прибора
  31. demands = [] # коллекция с прошедшими через систему требованиями
  32. N = 2 # число требований в системе
  33. cnt_refuse = 0 # число требований с отказом
  34. n = []
  35. t_born = [0] * k + [0] # моменты поступления требований
  36. t_service1 = [0, 0] # поступил / обслужился
  37. t_service2 = [0, 0]
  38. cnt_getting_demands = 0
  39.  
  40.  
  41. Q = [0] * k
  42. q1 = []
  43. q2 = []
  44.  
  45.  
  46. rnd = random()
  47.  
  48.  
  49. # начальные условия
  50. t_act_source = 0
  51. t_act_device1 = t_modeling + 0.000001
  52. t_act_device2 = t_modeling + 0.000001
  53.  
  54.  
  55.  
  56. # симуляция
  57. while t_now < t_modeling:
  58.    
  59.     indicator = False
  60.     # генерация требования
  61.     if (t_act_source == t_now):
  62.         #print(f"Момент формирования требования: {t_now}")
  63.         indicator = True
  64.         N += 1
  65.         if t_born[0] == 0:
  66.             t_born[0] = t_now                                
  67.         elif t_born[1] == 0:
  68.             t_born[1] = t_now
  69.         else:
  70.             t_born[2] = t_now
  71.         cnt_getting_demands += 1
  72.         # print(t_born)
  73.         t_act_source = t_now - log(rnd) / lambda_
  74.          
  75.          
  76.     # начало обслуживания требования прибором 1
  77.     if (device1_free and N > 0):
  78.         #print(f"Момент начала обслуживания прибором 1: {t_now}")
  79.         indicator = True
  80.         device1_free = False
  81.         t_act_device1 = t_now - log(rnd) / mu
  82.         t_service1[0] = t_now
  83.         # начало обслуживания требования прибором 2
  84.     elif (device2_free and N > 1):
  85.         #print(f"Момент начала обслуживания прибором 2: {t_now}")
  86.         indicator = True
  87.         device2_free = False
  88.         t_act_device2 = t_now - log(rnd) / mu
  89.         t_service2[0] = t_now
  90.         # отказ
  91.     elif (Q[0] <= Q[1] and Q[0] < 2):
  92.         q1.append(t_born[2])
  93.         Q[0] += 1
  94.     elif (Q[1] < 2):
  95.         q2.append(t_born[2])
  96.         Q[1] += 1
  97.     elif (N > 6):
  98.          N -= 1
  99.          t_born[2] = 0
  100.          cnt_refuse += 1
  101.          
  102.     # завершение обслуживания требования прибором 1
  103.     if (t_act_device1 == t_now):
  104.         #print(f"Момент завершения обслуживания: {t_now}")
  105.         t_service1[1] = t_now
  106.         indicator = True
  107.         device1_free = True
  108.         t_act_device1 = t_modeling + 0.0000001
  109.        # print(f"Момент поступления: {t_service1[0]}\n\ Момент выхода из СМО: {t_now}")
  110.         N -= 1
  111.         demands.append(Demand(t_born[0], t_service1[0], t_service1[1]))
  112.         if (Q[0] > 0):
  113.             Q[0]-=1
  114.             t_born[0] = q1.pop(0)
  115.             t_act_device1 = t_now - log(rnd) / mu
  116.             t_service1[0] = t_now
  117.         else:
  118.             t_born[0] = 0
  119.            
  120.        
  121.     # завершение обслуживания требования прибором 2
  122.     if (t_act_device2 == t_now):
  123.         #print(f"Момент завершения обслуживания: {t_now}")
  124.         t_service2[1] = t_now
  125.         indicator = True
  126.         device2_free = True
  127.         t_act_device2 = t_modeling + 0.0000001
  128.         #print(f"Момент поступления: {t_service2[0]}\n\ Момент выхода из СМО: {t_now}")
  129.         N -= 1
  130.         demands.append(Demand(t_born[1], t_service2[0], t_service2[1]))
  131.         if (Q[1] > 0):
  132.             Q[1]-=1
  133.             t_born[1] = q2.pop(0)
  134.             t_act_device2 = t_now - log(rnd) / mu
  135.             t_service2[0] = t_now
  136.         else:
  137.             t_born[1] = 0
  138.     # переход к следующему моменту
  139.     if not indicator:
  140.         n.append(N)
  141.         t_now = min(t_act_device1, t_act_device2, t_act_source)
  142.    
  143.     # анализ данных
  144. u = []
  145. for item in demands:
  146.     u.append(item.death_time - item.born_time)
  147. n_ = np.mean(n)
  148. u_ = np.mean(u)
  149. p_ref = cnt_refuse / cnt_getting_demands
  150. print(f'Число обслуженных требований: {len(demands)}')
  151. print(f'М.о. длительности пребывания требований в системе u_ = {u_}')
  152. print(f'М.о. числа требований в системе n_ = {n_}')
  153. print(f'Вероятность отказа = {p_ref}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement