-
-
Save Sinabon2004/7c0ca8398449880a6693bea78fb97fb9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import math | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| # ========================== | |
| # ЭЛАСТИЧНАЯ МОДЕЛЬ (ЛР 3) | |
| # ========================== | |
| # Вместо stationaty_distribution(c, g, ro_1, ro_2) | |
| # теперь рассчитываем распределение для эластичного трафика p(n1, n2) | |
| def stationary_distribution_elastic(lam1, lam2, theta1, theta2, C, max_n=20): | |
| """Стационарное распределение p(n1, n2) для эластичной модели""" | |
| a1 = lam1 * theta1 / C | |
| a2 = lam2 * theta2 / C | |
| total = sum((a1 ** n1) / math.factorial(n1) * | |
| (a2 ** n2) / math.factorial(n2) | |
| for n1 in range(max_n) for n2 in range(max_n)) | |
| p00 = 1 / total | |
| # распределение состояний | |
| p = np.zeros((max_n, max_n)) | |
| for n1 in range(max_n): | |
| for n2 in range(max_n): | |
| p[n1, n2] = p00 * (a1 ** n1 / math.factorial(n1)) * (a2 ** n2 / math.factorial(n2)) | |
| return p | |
| # (формула 11.9) | |
| def average_numbers(lam1, lam2, theta1, theta2): | |
| N1 = (lam1 * theta1) / (lam1 * theta1 + lam2 * theta2) | |
| N2 = (lam2 * theta2) / (lam1 * theta1 + lam2 * theta2) | |
| return N1, N2 | |
| # (формула 11.10) | |
| def average_service_time(N, lam): | |
| return N / lam | |
| def metrics_elastic(lam1, lam2, theta1, theta2, C): | |
| p = stationary_distribution_elastic(lam1, lam2, theta1, theta2, C) | |
| N1, N2 = average_numbers(lam1, lam2, theta1, theta2) | |
| T1, T2 = average_service_time(N1, lam1), average_service_time(N2, lam2) | |
| return { | |
| "p": p, | |
| "N1": N1, | |
| "N2": N2, | |
| "T1": T1, | |
| "T2": T2, | |
| "E1": 0, | |
| "E2": 0, | |
| } | |
| # ========================== | |
| # ВИЗУАЛИЗАЦИЯ | |
| # ========================== | |
| # график времени обслуживания | |
| def plot_average_time_vs_lambda(lam_range, lam2, theta1, theta2, C): | |
| T1_list, T2_list = [], [] | |
| for lam1 in lam_range: | |
| res = metrics_elastic(lam1, lam2, theta1, theta2, C) | |
| T1_list.append(res["T1"]) | |
| T2_list.append(res["T2"]) | |
| plt.figure(figsize=(9, 6)) | |
| plt.plot(lam_range, T1_list, label="Среднее время обслуживания (тип 1)", linewidth=2, color="crimson") | |
| plt.plot(lam_range, T2_list, label="Среднее время обслуживания (тип 2)", linewidth=2, color="navy") | |
| plt.xlabel("Интенсивность λ₁", fontsize=13) | |
| plt.ylabel("Среднее время обслуживания", fontsize=13) | |
| plt.title("Эластичная модель: зависимость времени обслуживания от λ₁", fontsize=15) | |
| plt.grid(True, linestyle="--", alpha=0.7) | |
| plt.legend(fontsize=12) | |
| plt.tight_layout() | |
| plt.show() | |
| # график для среднего числа активных соединений | |
| def plot_average_number_vs_lambda(lam_range, lam2, theta1, theta2, C): | |
| N1_list, N2_list = [], [] | |
| for lam1 in lam_range: | |
| res = metrics_elastic(lam1, lam2, theta1, theta2, C) | |
| N1_list.append(res["N1"]) | |
| N2_list.append(res["N2"]) | |
| plt.figure(figsize=(9, 6)) | |
| plt.plot(lam_range, N1_list, label="Среднее число соединений (тип 1)", linewidth=2, color="green") | |
| plt.plot(lam_range, N2_list, label="Среднее число соединений (тип 2)", linewidth=2, color="orange") | |
| plt.xlabel("Интенсивность λ₁", fontsize=13) | |
| plt.ylabel("Среднее число соединений", fontsize=13) | |
| plt.title("Эластичная модель: среднее число соединений от λ₁", fontsize=15) | |
| plt.grid(True, linestyle="--", alpha=0.7) | |
| plt.legend(fontsize=12) | |
| plt.tight_layout() | |
| plt.show() | |
| # ========================== | |
| # ГЛАВНЫЙ БЛОК | |
| # ========================== | |
| if __name__ == "__main__": | |
| C = 100 | |
| lam1 = 30 | |
| lam2 = 20 | |
| theta1 = 2 | |
| theta2 = 3 | |
| result = metrics_elastic(lam1, lam2, theta1, theta2, C) | |
| print("\n--- РЕЗУЛЬТАТЫ ЭЛАСТИЧНОЙ МОДЕЛИ --- 📊") | |
| print(f"Среднее число соединений: N₁ = {result['N1']:.4f}, N₂ = {result['N2']:.4f}") | |
| print(f"Среднее время обслуживания: T₁ = {result['T1']:.4f}, T₂ = {result['T2']:.4f}") | |
| print(f"Вероятности блокировки: E₁ = {result['E1']}, E₂ = {result['E2']} (нет блокировок)") | |
| lam1_range = np.linspace(1, 60, 30) | |
| plot_average_time_vs_lambda(lam1_range, lam2, theta1, theta2, C) | |
| plot_average_number_vs_lambda(lam1_range, lam2, theta1, theta2, C) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment