Добавил:
KaFaka
t.me
Инфо для ГУАП студентов от меня: https://kafaka.notion.site/99e6d9b70ca74f7baef3daea17839e5a
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Zadaniye2.2_used
.pyimport random
import matplotlib.pyplot as plt
import numpy as np
def average_users_def(lam, num_slots):
'''
Calculate the average number of users in the system at each slot
'''
start_slot = [0] * num_slots # Initialize a list to store the number of users in each start slot
end_slot = [0] * num_slots # Initialize a list to store the number of users in each end slot
P = np.random.poisson(lam, num_slots) # Generate the Poisson distribution with mean lam for num_slots
for i in range(num_slots): # Loop through each slot
I = 0
if start_slot[i] != 0:
R = np.random.binomial(start_slot[i], 1 / start_slot[i], 1) # Generate a binomial distribution to simulate the leaving of users
I = int(R == 1) # Count the number of users that leave
end_slot[i] = start_slot[i] - I + P[i] # Calculate the number of users in the end slot
if i < num_slots - 1: # Set the end slot to be the start slot for the next slot
start_slot[i + 1] = end_slot[i]
return np.mean(end_slot)
def mean_time(average_users, lam):
'''
Calculate the mean time a user spends in the system
'''
return average_users / lam
if __name__ == "__main__":
num_slots = 10 ** 5
start = 0.05
step = 0.01
end = 0.5
lam_count = int((end - start) / step + 1)
lam = np.linspace(start, end, lam_count)
average_users = np.zeros(lam_count)
t_mean = np.zeros(lam_count)
for i, l in enumerate(lam):
average_users[i] = average_users_def(l, num_slots)
t_mean[i] = mean_time(average_users[i], l)
plt.figure()
plt.plot(lam, average_users, label='Среднее количество абонентов (N^)')
plt.plot(lam, t_mean, label='Среднее количество слотов (T^)', color='red')
plt.title('График зависимости среднего количества абонентов в системе (N^) \nи\n среднего времени нахождения абонента в системе (T^) от Lam')
plt.grid(color='gray')
plt.xlabel('Lam')
plt.ylabel('Values')
plt.ylim(-1, 21)
plt.xlim(0.05, 0.4)
plt.legend()
plt.show()
Соседние файлы в предмете Вычислительные системы, сети и телекоммуникации