most-queue 1.53__py3-none-any.whl → 2.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- most_queue/__init__.py +18 -0
- most_queue/constants.py +22 -0
- most_queue/io/plots.py +101 -0
- most_queue/io/tables.py +466 -0
- most_queue/{general → misc}/approx_cdf_make.py +11 -14
- most_queue/misc/calc_v_pls.py +343 -0
- most_queue/misc/compare_fj_calc.py +44 -37
- most_queue/misc/flow_sum.py +41 -26
- most_queue/misc/run_mgn_with_h2_delay_cold_warm.py +619 -0
- most_queue/misc/tests/test_fj_delta.py +83 -0
- most_queue/misc/tests/test_network_opt_approx.py +54 -0
- most_queue/misc/tests/test_network_opt_plus.py +63 -0
- most_queue/misc/vacations_paper_utils.py +124 -0
- most_queue/{rand_distribution.py → random/distributions.py} +268 -291
- most_queue/random/distributions.yaml +27 -0
- most_queue/{general/interfaces.py → random/utils/base.py} +5 -3
- most_queue/random/utils/create.py +71 -0
- most_queue/random/utils/fit.py +329 -0
- most_queue/random/utils/load.py +100 -0
- most_queue/{general/distribution_params.py → random/utils/params.py} +18 -0
- most_queue/sim/__init__.py +10 -0
- most_queue/sim/base.py +473 -0
- most_queue/sim/batch.py +99 -0
- most_queue/sim/{queueing_systems/finite_source.py → finite_source.py} +42 -91
- most_queue/sim/flow_sum.py +95 -138
- most_queue/sim/{queueing_systems/fork_join.py → fork_join.py} +122 -134
- most_queue/sim/{queueing_systems/fork_join_delta.py → fork_join_delta.py} +72 -65
- most_queue/sim/impatient.py +184 -0
- most_queue/sim/negative.py +402 -0
- most_queue/sim/networks/base_network_sim.py +111 -0
- most_queue/sim/networks/network.py +218 -0
- most_queue/sim/networks/priority_network.py +135 -85
- most_queue/sim/{queueing_systems/priority.py → priority.py} +261 -205
- most_queue/sim/utils/phase.py +2 -2
- most_queue/sim/utils/qs_queue.py +30 -5
- most_queue/sim/utils/servers.py +35 -39
- most_queue/sim/utils/stats_update.py +10 -4
- most_queue/sim/utils/tasks.py +51 -12
- most_queue/sim/vacations.py +339 -0
- most_queue/structs.py +170 -0
- most_queue/theory/__init__.py +54 -0
- most_queue/theory/base_queue.py +193 -0
- most_queue/theory/{queueing_systems → batch}/__init__.py +0 -0
- most_queue/theory/batch/mm1.py +160 -0
- most_queue/theory/calc_params.py +34 -0
- most_queue/theory/closed/engset.py +195 -0
- most_queue/theory/fifo/__init__.py +21 -0
- most_queue/theory/fifo/ek_d_n.py +201 -0
- most_queue/theory/fifo/gi_m_1.py +212 -0
- most_queue/theory/fifo/gi_m_n.py +258 -0
- most_queue/theory/{queueing_systems/fifo → fifo}/m_d_n.py +159 -120
- most_queue/theory/fifo/mg1.py +145 -0
- most_queue/theory/{queueing_systems/fifo → fifo}/mgn_takahasi.py +226 -85
- most_queue/theory/fifo/mmnr.py +172 -0
- most_queue/theory/{queueing_systems/fork_join → fork_join}/m_m_n.py +133 -39
- most_queue/theory/fork_join/split_join.py +135 -0
- most_queue/theory/impatience/mm1.py +172 -0
- most_queue/theory/negative/mg1_disasters.py +134 -0
- most_queue/theory/negative/mg1_rcs.py +117 -0
- most_queue/theory/negative/mgn_disaster.py +456 -0
- most_queue/theory/negative/mgn_rcs.py +321 -0
- most_queue/theory/negative/structs.py +1 -0
- most_queue/theory/networks/base_network_calc.py +131 -0
- most_queue/theory/networks/network_viewer.py +45 -41
- most_queue/theory/networks/open_network.py +98 -85
- most_queue/theory/networks/open_network_prty.py +202 -0
- most_queue/theory/networks/opt/transition.py +335 -0
- most_queue/theory/networks/opt/transition_approx.py +140 -0
- most_queue/theory/networks/opt/transition_plus.py +175 -0
- most_queue/theory/priority/__init__.py +13 -0
- most_queue/theory/priority/mgn_invar_approx.py +180 -0
- most_queue/theory/{queueing_systems/priority → priority}/non_preemptive/mg1.py +78 -18
- most_queue/theory/{queueing_systems/priority → priority}/preemptive/m_ph_n_busy_approx.py +203 -255
- most_queue/theory/priority/preemptive/mg1.py +197 -0
- most_queue/theory/{queueing_systems/priority/preemptive/mmn_3cls_busy_approx.py → priority/preemptive/mm2_3cls_busy_approx.py} +289 -172
- most_queue/theory/{queueing_systems/priority → priority}/preemptive/mmn_2cls_pr_busy_approx.py +234 -230
- most_queue/theory/utils/__init__.py +25 -0
- most_queue/theory/utils/binom_probs.py +11 -2
- most_queue/theory/utils/busy_periods.py +116 -32
- most_queue/theory/utils/conditional.py +69 -0
- most_queue/{general → theory/utils}/conv.py +23 -5
- most_queue/theory/utils/diff5dots.py +12 -9
- most_queue/theory/utils/flow_sum.py +38 -34
- most_queue/theory/utils/max_dist.py +196 -0
- most_queue/theory/utils/moments.py +42 -0
- most_queue/theory/utils/passage_time.py +241 -161
- most_queue/theory/utils/q_poisson_arrival_calc.py +22 -16
- most_queue/theory/utils/residual.py +23 -0
- most_queue/theory/utils/student_stat.py +133 -149
- most_queue/theory/utils/timing.py +81 -0
- most_queue/theory/utils/transforms.py +23 -3
- most_queue/theory/utils/utilization_approx.py +68 -0
- most_queue/theory/utils/validation.py +97 -0
- most_queue/theory/{queueing_systems/vacations → vacations}/m_h2_h2warm.py +273 -211
- most_queue/theory/vacations/mg1_warm_calc.py +122 -0
- most_queue/theory/{queueing_systems/vacations → vacations}/mgn_with_h2_delay_cold_warm.py +257 -166
- most_queue/theory/{queueing_systems/vacations → vacations}/mmn_with_h2_cold_and_h2_warmup.py +335 -267
- most_queue-2.6.dist-info/METADATA +247 -0
- most_queue-2.6.dist-info/RECORD +114 -0
- most_queue/general/tables.py +0 -236
- most_queue/general/vacations_paper_utils.py +0 -135
- most_queue/misc/test_weibull.py +0 -37
- most_queue/sim/queueing_systems/batch.py +0 -93
- most_queue/sim/queueing_systems/fifo.py +0 -538
- most_queue/sim/queueing_systems/impatient.py +0 -168
- most_queue/sim/utils/distribution_utils.py +0 -204
- most_queue/sim/utils/exceptions.py +0 -37
- most_queue/sim/utils/qs.py +0 -0
- most_queue/theory/queueing_systems/batch/mm1.py +0 -111
- most_queue/theory/queueing_systems/closed/engset.py +0 -141
- most_queue/theory/queueing_systems/fifo/ek_d_n.py +0 -160
- most_queue/theory/queueing_systems/fifo/gi_m_1.py +0 -133
- most_queue/theory/queueing_systems/fifo/gi_m_n.py +0 -190
- most_queue/theory/queueing_systems/fifo/mg1.py +0 -91
- most_queue/theory/queueing_systems/fifo/mmnr.py +0 -116
- most_queue/theory/queueing_systems/fork_join/split_join.py +0 -284
- most_queue/theory/queueing_systems/impatience/mm1.py +0 -117
- most_queue/theory/queueing_systems/priority/mgn_invar_approx.py +0 -129
- most_queue/theory/queueing_systems/priority/preemptive/__init__.py +0 -0
- most_queue/theory/queueing_systems/priority/preemptive/mg1.py +0 -205
- most_queue/theory/queueing_systems/vacations/__init__.py +0 -0
- most_queue/theory/queueing_systems/vacations/mg1_warm_calc.py +0 -53
- most_queue/theory/utils/weibull.py +0 -155
- most_queue-1.53.dist-info/METADATA +0 -154
- most_queue-1.53.dist-info/RECORD +0 -84
- /most_queue/{general → io}/__init__.py +0 -0
- /most_queue/{sim/queueing_systems → random}/__init__.py +0 -0
- /most_queue/theory/{queueing_systems/batch → closed}/__init__.py +0 -0
- /most_queue/theory/{queueing_systems/closed → fork_join}/__init__.py +0 -0
- /most_queue/theory/{queueing_systems/fifo → impatience}/__init__.py +0 -0
- /most_queue/theory/{queueing_systems/fork_join → networks/opt}/__init__.py +0 -0
- /most_queue/theory/{queueing_systems/impatience → priority/non_preemptive}/__init__.py +0 -0
- /most_queue/theory/{queueing_systems/priority → priority/preemptive}/__init__.py +0 -0
- /most_queue/theory/{queueing_systems/priority/non_preemptive → vacations}/__init__.py +0 -0
- {most_queue-1.53.dist-info → most_queue-2.6.dist-info}/WHEEL +0 -0
- {most_queue-1.53.dist-info → most_queue-2.6.dist-info}/licenses/LICENSE +0 -0
most_queue/__init__.py
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Most-Queue: A Python package for simulating and analyzing queueing systems.
|
|
3
|
+
|
|
4
|
+
This package provides both simulation and numerical calculation methods
|
|
5
|
+
for various types of queueing systems and networks.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
__version__ = "2.05"
|
|
9
|
+
|
|
10
|
+
# Import main simulation and theory classes for easy access
|
|
11
|
+
from most_queue.sim.base import QsSim
|
|
12
|
+
from most_queue.theory.base_queue import BaseQueue
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
"QsSim",
|
|
16
|
+
"BaseQueue",
|
|
17
|
+
"__version__",
|
|
18
|
+
]
|
most_queue/constants.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Constants used throughout the most_queue package.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
# Default number of states for simulation
|
|
6
|
+
DEFAULT_NUM_STATES: int = 100000
|
|
7
|
+
|
|
8
|
+
# Default number of jobs for simulation
|
|
9
|
+
DEFAULT_NUM_JOBS: int = 1000000
|
|
10
|
+
|
|
11
|
+
# Default number of probabilities to calculate
|
|
12
|
+
DEFAULT_P_NUM: int = 1000
|
|
13
|
+
|
|
14
|
+
# Numerical tolerances
|
|
15
|
+
DEFAULT_TOLERANCE: float = 1e-12
|
|
16
|
+
LSTSQ_RCOND: float = 1e-8 # rcond parameter for numpy.linalg.lstsq
|
|
17
|
+
|
|
18
|
+
# Maximum iterations for fitting algorithms
|
|
19
|
+
MAX_FIT_ITERATIONS: int = 10000
|
|
20
|
+
|
|
21
|
+
# Default number of dots for approximation
|
|
22
|
+
DEFAULT_DOTS_NUM: int = 10000
|
most_queue/io/plots.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Plotting utilities for simulation results.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from enum import Enum
|
|
6
|
+
|
|
7
|
+
import matplotlib.pyplot as plt
|
|
8
|
+
from matplotlib.axes import Axes
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class DependsType(Enum):
|
|
12
|
+
"""
|
|
13
|
+
Enum to specify the type of dependency for plotting.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
NONE = 0
|
|
17
|
+
UTILIZATION_FACTOR = 1
|
|
18
|
+
CHANNELS_NUMBER = 2
|
|
19
|
+
COEFFICIENT_OF_VARIATION = 3
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Plotter:
|
|
23
|
+
"""
|
|
24
|
+
A class for plotting simulation results.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
def __init__(
|
|
28
|
+
self,
|
|
29
|
+
xs: list[float],
|
|
30
|
+
sim_results: list[float],
|
|
31
|
+
calc_results: list[float],
|
|
32
|
+
depends_on: DependsType = DependsType.CHANNELS_NUMBER,
|
|
33
|
+
):
|
|
34
|
+
self.xs = xs
|
|
35
|
+
self.sim_results = sim_results
|
|
36
|
+
self.calc_results = calc_results
|
|
37
|
+
self.depends_on = depends_on
|
|
38
|
+
|
|
39
|
+
def _configure_plot(self, ax: Axes, ylabel: str):
|
|
40
|
+
"""
|
|
41
|
+
Helper method to configure common plot elements.
|
|
42
|
+
"""
|
|
43
|
+
ax.set_ylabel(ylabel)
|
|
44
|
+
if self.depends_on == DependsType.UTILIZATION_FACTOR:
|
|
45
|
+
ax.set_xlabel(r"$\rho$")
|
|
46
|
+
elif self.depends_on == DependsType.COEFFICIENT_OF_VARIATION:
|
|
47
|
+
ax.set_xlabel(r"$\nu$")
|
|
48
|
+
else:
|
|
49
|
+
ax.set_xlabel("n")
|
|
50
|
+
plt.xticks(range(1, len(self.xs) + 1))
|
|
51
|
+
|
|
52
|
+
def plot_errors(self, save_path: str = None):
|
|
53
|
+
"""
|
|
54
|
+
Plots the simulation and calculated moments for a given list of x values.
|
|
55
|
+
"""
|
|
56
|
+
_fig, ax = plt.subplots()
|
|
57
|
+
errors = [100 * (w_sim - w_tt) / w_tt for w_sim, w_tt in zip(self.sim_results, self.calc_results)]
|
|
58
|
+
|
|
59
|
+
ax.plot(self.xs, errors, color="black")
|
|
60
|
+
self._configure_plot(ax, r"$\varepsilon$, %")
|
|
61
|
+
|
|
62
|
+
if save_path is not None:
|
|
63
|
+
plt.savefig(save_path, dpi=300)
|
|
64
|
+
else:
|
|
65
|
+
plt.show()
|
|
66
|
+
|
|
67
|
+
plt.close(_fig)
|
|
68
|
+
|
|
69
|
+
def plot_waiting(self, save_path: str = None):
|
|
70
|
+
"""
|
|
71
|
+
Plots the simulation and calculated queue waiting times.
|
|
72
|
+
"""
|
|
73
|
+
_fig, ax = plt.subplots()
|
|
74
|
+
ax.plot(self.xs, self.sim_results, label="Sim", color="black", linestyle="--")
|
|
75
|
+
ax.plot(self.xs, self.calc_results, label="Calc", color="black")
|
|
76
|
+
ax.legend()
|
|
77
|
+
self._configure_plot(ax, r"$\omega_{1}$")
|
|
78
|
+
|
|
79
|
+
if save_path is not None:
|
|
80
|
+
plt.savefig(save_path, dpi=300)
|
|
81
|
+
else:
|
|
82
|
+
plt.show()
|
|
83
|
+
|
|
84
|
+
plt.close(_fig)
|
|
85
|
+
|
|
86
|
+
def plot_sojourn(self, save_path: str = None):
|
|
87
|
+
"""
|
|
88
|
+
Plots the simulation and calculated sojourn times.
|
|
89
|
+
"""
|
|
90
|
+
_fig, ax = plt.subplots()
|
|
91
|
+
ax.plot(self.xs, self.sim_results, label="Sim", color="black", linestyle="--")
|
|
92
|
+
ax.plot(self.xs, self.calc_results, label="Calc", color="black")
|
|
93
|
+
ax.legend()
|
|
94
|
+
self._configure_plot(ax, r"$\upsilon_{1}$")
|
|
95
|
+
|
|
96
|
+
if save_path is not None:
|
|
97
|
+
plt.savefig(save_path, dpi=300)
|
|
98
|
+
else:
|
|
99
|
+
plt.show()
|
|
100
|
+
|
|
101
|
+
plt.close(_fig)
|
most_queue/io/tables.py
ADDED
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Printing tables for simulation and calculation results.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import math
|
|
6
|
+
|
|
7
|
+
from colorama import Fore, Style, init
|
|
8
|
+
|
|
9
|
+
from most_queue.theory.utils.moments import convert_raw_to_central
|
|
10
|
+
|
|
11
|
+
init()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def print_raw_moments(
|
|
15
|
+
sim_moments: list[float] | float,
|
|
16
|
+
calc_moments: list[float] | float,
|
|
17
|
+
header=None,
|
|
18
|
+
sim_header="Sim",
|
|
19
|
+
calc_header="Num",
|
|
20
|
+
):
|
|
21
|
+
"""
|
|
22
|
+
Prints the moments of waiting or sojourn time in the system.
|
|
23
|
+
Args:
|
|
24
|
+
sim_moments (list): List of simulated moments.
|
|
25
|
+
calc_moments (list): List of calculated moments.
|
|
26
|
+
header (str): Header for the table.
|
|
27
|
+
sim_header (str): Header for simulated moments.
|
|
28
|
+
calc_header (str): Header for calculated moments.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
if not header is None:
|
|
32
|
+
print(Fore.CYAN + f"\n{header:^45}")
|
|
33
|
+
print("-" * 45)
|
|
34
|
+
|
|
35
|
+
num_col = "#"
|
|
36
|
+
print(f"{num_col:^15s}|{calc_header:^15s}|{sim_header:^15s}")
|
|
37
|
+
print("-" * 45)
|
|
38
|
+
if isinstance(sim_moments, list):
|
|
39
|
+
for j in range(min(len(sim_moments), len(calc_moments))):
|
|
40
|
+
calc_mom = calc_moments[j].real if isinstance(calc_moments[j], complex) else calc_moments[j]
|
|
41
|
+
sim_mom = sim_moments[j].real if isinstance(sim_moments[j], complex) else sim_moments[j]
|
|
42
|
+
print(
|
|
43
|
+
Fore.CYAN
|
|
44
|
+
+ f"{j + 1:^15d}|"
|
|
45
|
+
+ Fore.YELLOW
|
|
46
|
+
+ f"{calc_mom:^15.5g}"
|
|
47
|
+
+ Fore.CYAN
|
|
48
|
+
+ "|"
|
|
49
|
+
+ Fore.YELLOW
|
|
50
|
+
+ f"{sim_mom:^15.5g}"
|
|
51
|
+
)
|
|
52
|
+
else:
|
|
53
|
+
calc_mom = calc_moments.real if isinstance(calc_moments, complex) else calc_moments
|
|
54
|
+
sim_mom = sim_moments.real if isinstance(sim_moments, complex) else sim_moments
|
|
55
|
+
print(
|
|
56
|
+
Fore.CYAN
|
|
57
|
+
+ f"{1:^15d}|"
|
|
58
|
+
+ Fore.YELLOW
|
|
59
|
+
+ f"{calc_mom:^15.5g}"
|
|
60
|
+
+ Fore.CYAN
|
|
61
|
+
+ "|"
|
|
62
|
+
+ Fore.YELLOW
|
|
63
|
+
+ f"{sim_mom:^15.5g}"
|
|
64
|
+
)
|
|
65
|
+
print(Style.RESET_ALL)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def print_central_moments(
|
|
69
|
+
sim_moments: list[float] | float,
|
|
70
|
+
calc_moments: list[float] | float,
|
|
71
|
+
header=None,
|
|
72
|
+
sim_header="Sim",
|
|
73
|
+
calc_header="Num",
|
|
74
|
+
):
|
|
75
|
+
"""
|
|
76
|
+
Prints the moments of waiting or sojourn time in the system.
|
|
77
|
+
Args:
|
|
78
|
+
sim_moments (list): List of simulated moments.
|
|
79
|
+
calc_moments (list): List of calculated moments.
|
|
80
|
+
header (str): Header for the table.
|
|
81
|
+
sim_header (str): Header for simulated moments.
|
|
82
|
+
calc_header (str): Header for calculated moments.
|
|
83
|
+
"""
|
|
84
|
+
|
|
85
|
+
if not header is None:
|
|
86
|
+
print(Fore.CYAN + f"\n{header:^45}")
|
|
87
|
+
print("-" * 45)
|
|
88
|
+
|
|
89
|
+
num_col = "Moment"
|
|
90
|
+
central_moments_names = ["Mean", "Variance", "Skewness", "Kurtosis"]
|
|
91
|
+
|
|
92
|
+
print(f"{num_col:^15s}|{calc_header:^15s}|{sim_header:^15s}")
|
|
93
|
+
print("-" * 45)
|
|
94
|
+
if isinstance(sim_moments, list):
|
|
95
|
+
for j in range(min(len(sim_moments), len(calc_moments))):
|
|
96
|
+
calc_mom = calc_moments[j].real if isinstance(calc_moments[j], complex) else calc_moments[j]
|
|
97
|
+
sim_mom = sim_moments[j].real if isinstance(sim_moments[j], complex) else sim_moments[j]
|
|
98
|
+
name = central_moments_names[j]
|
|
99
|
+
print(
|
|
100
|
+
Fore.CYAN
|
|
101
|
+
+ f"{name:^15s}|"
|
|
102
|
+
+ Fore.YELLOW
|
|
103
|
+
+ f"{calc_mom:^15.5g}"
|
|
104
|
+
+ Fore.CYAN
|
|
105
|
+
+ "|"
|
|
106
|
+
+ Fore.YELLOW
|
|
107
|
+
+ f"{sim_mom:^15.5g}"
|
|
108
|
+
)
|
|
109
|
+
else:
|
|
110
|
+
calc_mom = calc_moments.real if isinstance(calc_moments, complex) else calc_moments
|
|
111
|
+
sim_mom = sim_moments.real if isinstance(sim_moments, complex) else sim_moments
|
|
112
|
+
name = central_moments_names[0]
|
|
113
|
+
print(
|
|
114
|
+
Fore.CYAN
|
|
115
|
+
+ f"{name:^15s}|"
|
|
116
|
+
+ Fore.YELLOW
|
|
117
|
+
+ f"{calc_mom:^15.5g}"
|
|
118
|
+
+ Fore.CYAN
|
|
119
|
+
+ "|"
|
|
120
|
+
+ Fore.YELLOW
|
|
121
|
+
+ f"{sim_mom:^15.5g}"
|
|
122
|
+
)
|
|
123
|
+
print(Style.RESET_ALL)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def print_waiting_moments(
|
|
127
|
+
sim_moments: list[float] | float,
|
|
128
|
+
calc_moments: list[float] | float,
|
|
129
|
+
sim_header="Sim",
|
|
130
|
+
calc_header="Num",
|
|
131
|
+
convert_to_central=True,
|
|
132
|
+
):
|
|
133
|
+
"""
|
|
134
|
+
Prints the moments of waiting or sojourn time in the system.
|
|
135
|
+
Args:
|
|
136
|
+
sim_moments (list): List of simulated moments.
|
|
137
|
+
calc_moments (list): List of calculated moments.
|
|
138
|
+
sim_header (str): Header for simulated moments.
|
|
139
|
+
calc_header (str): Header for calculated moments
|
|
140
|
+
"""
|
|
141
|
+
|
|
142
|
+
header = "Moments of waiting time in the system"
|
|
143
|
+
|
|
144
|
+
if convert_to_central:
|
|
145
|
+
calc_moments_central = convert_raw_to_central(calc_moments)
|
|
146
|
+
sim_moments_central = convert_raw_to_central(sim_moments)
|
|
147
|
+
|
|
148
|
+
print_central_moments(
|
|
149
|
+
sim_moments_central, calc_moments_central, sim_header=sim_header, calc_header=calc_header, header=header
|
|
150
|
+
)
|
|
151
|
+
else:
|
|
152
|
+
print_raw_moments(sim_moments, calc_moments, sim_header=sim_header, calc_header=calc_header, header=header)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def print_sojourn_moments(
|
|
156
|
+
sim_moments: list[float] | float,
|
|
157
|
+
calc_moments: list[float] | float,
|
|
158
|
+
sim_header="Sim",
|
|
159
|
+
calc_header="Num",
|
|
160
|
+
convert_to_central=True,
|
|
161
|
+
):
|
|
162
|
+
"""
|
|
163
|
+
Prints the moments of waiting or sojourn time in the system.
|
|
164
|
+
Args:
|
|
165
|
+
sim_moments (list): List of simulated moments.
|
|
166
|
+
calc_moments (list): List of calculated moments.
|
|
167
|
+
sim_header (str): Header for simulated moments.
|
|
168
|
+
calc_header (str): Header for calculated moments
|
|
169
|
+
"""
|
|
170
|
+
|
|
171
|
+
header = "Moments of sojourn time in the system"
|
|
172
|
+
|
|
173
|
+
if convert_to_central:
|
|
174
|
+
calc_moments_central = convert_raw_to_central(calc_moments)
|
|
175
|
+
sim_moments_central = convert_raw_to_central(sim_moments)
|
|
176
|
+
|
|
177
|
+
print_central_moments(
|
|
178
|
+
sim_moments_central, calc_moments_central, sim_header=sim_header, calc_header=calc_header, header=header
|
|
179
|
+
)
|
|
180
|
+
else:
|
|
181
|
+
print_raw_moments(sim_moments, calc_moments, sim_header=sim_header, calc_header=calc_header, header=header)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def print_with_two_numerical(
|
|
185
|
+
sim_moments: list[float] | float,
|
|
186
|
+
calc_moments1: list[float] | float,
|
|
187
|
+
calc_moments2: list[float] | float,
|
|
188
|
+
num1_header="Num1",
|
|
189
|
+
num2_header="Num2",
|
|
190
|
+
):
|
|
191
|
+
"""
|
|
192
|
+
Prints the moments.
|
|
193
|
+
Args:
|
|
194
|
+
sim_moments (list): List of simulated moments.
|
|
195
|
+
calc_moments1 (list): List of calculated moments for the first approximation.
|
|
196
|
+
calc_moments2 (list): List of calculated moments for the second approximation.
|
|
197
|
+
num1_header (str, optional): Header for the first numerical approximation.
|
|
198
|
+
num2_header (str, optional): Header for the second numerical approximation.
|
|
199
|
+
"""
|
|
200
|
+
|
|
201
|
+
num_col = "#"
|
|
202
|
+
print(f"{num_col:^15s}|{num1_header:^15s}|{num2_header:^15s}|{'Sim':^15s}")
|
|
203
|
+
print("-" * 60)
|
|
204
|
+
if isinstance(sim_moments, list):
|
|
205
|
+
for j in range(min(len(sim_moments), len(calc_moments1), len(calc_moments2))):
|
|
206
|
+
calc_mom1 = calc_moments1[j].real if isinstance(calc_moments1[j], complex) else calc_moments1[j]
|
|
207
|
+
calc_mom2 = calc_moments2[j].real if isinstance(calc_moments2[j], complex) else calc_moments2[j]
|
|
208
|
+
sim_mom = sim_moments[j].real if isinstance(sim_moments[j], complex) else sim_moments[j]
|
|
209
|
+
print(
|
|
210
|
+
Fore.CYAN
|
|
211
|
+
+ f"{j + 1:^15d}|"
|
|
212
|
+
+ Fore.YELLOW
|
|
213
|
+
+ f"{calc_mom1:^15.5g}"
|
|
214
|
+
+ Fore.CYAN
|
|
215
|
+
+ "|"
|
|
216
|
+
+ Fore.YELLOW
|
|
217
|
+
+ f"{calc_mom2:^15.5g}"
|
|
218
|
+
+ Fore.CYAN
|
|
219
|
+
+ "|"
|
|
220
|
+
+ Fore.YELLOW
|
|
221
|
+
+ f"{sim_mom:^15.5g}"
|
|
222
|
+
)
|
|
223
|
+
else:
|
|
224
|
+
calc_mom1 = calc_moments1.real if isinstance(calc_moments1, complex) else calc_moments1
|
|
225
|
+
calc_mom2 = calc_moments2.real if isinstance(calc_moments2, complex) else calc_moments2
|
|
226
|
+
|
|
227
|
+
sim_mom = sim_moments.real if isinstance(sim_moments, complex) else sim_moments
|
|
228
|
+
print(
|
|
229
|
+
Fore.CYAN
|
|
230
|
+
+ f"{1:^15d}|"
|
|
231
|
+
+ Fore.YELLOW
|
|
232
|
+
+ f"{calc_mom1:^15.5g}"
|
|
233
|
+
+ Fore.CYAN
|
|
234
|
+
+ "|"
|
|
235
|
+
+ Fore.YELLOW
|
|
236
|
+
+ f"{calc_mom2:^15.5g}"
|
|
237
|
+
+ Fore.CYAN
|
|
238
|
+
+ "|"
|
|
239
|
+
+ Fore.YELLOW
|
|
240
|
+
+ f"{sim_mom:^15.5g}"
|
|
241
|
+
)
|
|
242
|
+
print(Style.RESET_ALL)
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def print_waiting_moments_with_two_numerical(
|
|
246
|
+
sim_moments,
|
|
247
|
+
calc_moments1,
|
|
248
|
+
calc_moments2,
|
|
249
|
+
num1_header="Num1",
|
|
250
|
+
num2_header="Num2",
|
|
251
|
+
):
|
|
252
|
+
"""
|
|
253
|
+
Prints the moments of waiting in the system.
|
|
254
|
+
Args:
|
|
255
|
+
sim_moments (list): List of simulated moments.
|
|
256
|
+
calc_moments1 (list): List of calculated moments for the first approximation.
|
|
257
|
+
calc_moments2 (list): List of calculated moments for the second approximation.
|
|
258
|
+
num1_header (str, optional): Header for the first numerical approximation.
|
|
259
|
+
num2_header (str, optional): Header for the second numerical approximation.
|
|
260
|
+
|
|
261
|
+
"""
|
|
262
|
+
|
|
263
|
+
header = "Moments of waiting time in the system"
|
|
264
|
+
|
|
265
|
+
print(Fore.CYAN + f"\n{header:^45}")
|
|
266
|
+
print("-" * 60)
|
|
267
|
+
print_with_two_numerical(
|
|
268
|
+
sim_moments=sim_moments,
|
|
269
|
+
calc_moments1=calc_moments1,
|
|
270
|
+
calc_moments2=calc_moments2,
|
|
271
|
+
num1_header=num1_header,
|
|
272
|
+
num2_header=num2_header,
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def print_sojourn_moments_with_two_numerical(
|
|
277
|
+
sim_moments,
|
|
278
|
+
calc_moments1,
|
|
279
|
+
calc_moments2,
|
|
280
|
+
num1_header="Num1",
|
|
281
|
+
num2_header="Num2",
|
|
282
|
+
):
|
|
283
|
+
"""
|
|
284
|
+
Prints the nts of sojourn time in the system.
|
|
285
|
+
Args:
|
|
286
|
+
sim_moments (list): List of simulated moments.
|
|
287
|
+
calc_moments1 (list): List of calculated moments for the first approximation.
|
|
288
|
+
calc_moments2 (list): List of calculated moments for the second approximation.
|
|
289
|
+
is_w (bool, optional): If True, prints waiting time moments.
|
|
290
|
+
Otherwise, prints sojourn time moments. Defaults to True.
|
|
291
|
+
|
|
292
|
+
"""
|
|
293
|
+
|
|
294
|
+
header = "Moments of sojourn time in the system"
|
|
295
|
+
|
|
296
|
+
print(Fore.CYAN + f"\n{header:^45}")
|
|
297
|
+
print("-" * 60)
|
|
298
|
+
print_with_two_numerical(
|
|
299
|
+
sim_moments=sim_moments,
|
|
300
|
+
calc_moments1=calc_moments1,
|
|
301
|
+
calc_moments2=calc_moments2,
|
|
302
|
+
num1_header=num1_header,
|
|
303
|
+
num2_header=num2_header,
|
|
304
|
+
)
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def print_raw_moments_multiclass(sim_moments, calc_moments, convert_to_central=True):
|
|
308
|
+
"""
|
|
309
|
+
Print moments with classes
|
|
310
|
+
:param sim_moments: Simulated moments
|
|
311
|
+
:param calc_moments: Calculated moments
|
|
312
|
+
"""
|
|
313
|
+
|
|
314
|
+
if convert_to_central:
|
|
315
|
+
sim_moments = [convert_raw_to_central(mom) for mom in sim_moments]
|
|
316
|
+
calc_moments = [convert_raw_to_central(mom) for mom in calc_moments]
|
|
317
|
+
blank_col, header_col, cls_col = "", "Moment", "Cls"
|
|
318
|
+
else:
|
|
319
|
+
blank_col, header_col, cls_col = "", "Number of moment", "Cls"
|
|
320
|
+
|
|
321
|
+
central_moments_names = ["Mean", "Variance", "Skewness", "Kurtosis"]
|
|
322
|
+
|
|
323
|
+
k_num = len(sim_moments)
|
|
324
|
+
size = min(len(sim_moments[0]), len(calc_moments[0]))
|
|
325
|
+
|
|
326
|
+
num_col, sim_col = "Num", "Sim"
|
|
327
|
+
|
|
328
|
+
print("-" * 60)
|
|
329
|
+
print(f"{blank_col:^11}|{header_col:^47}|")
|
|
330
|
+
print(f"{cls_col:^11}| ", end="")
|
|
331
|
+
print("-" * 45 + " |")
|
|
332
|
+
|
|
333
|
+
print(" " * 11 + "|", end="")
|
|
334
|
+
for j in range(size):
|
|
335
|
+
if convert_to_central:
|
|
336
|
+
s = central_moments_names[j]
|
|
337
|
+
else:
|
|
338
|
+
s = str(j + 1)
|
|
339
|
+
print(f"{s:^15}|", end="")
|
|
340
|
+
print("")
|
|
341
|
+
print("-" * 60)
|
|
342
|
+
|
|
343
|
+
for i in range(k_num):
|
|
344
|
+
print(Fore.CYAN + " " * 5 + "|", end="")
|
|
345
|
+
print(Fore.CYAN + f"{sim_col:^5}|", end="")
|
|
346
|
+
for j in range(size):
|
|
347
|
+
print(Fore.YELLOW + f"{sim_moments[i][j]:^15.3g}" + Fore.CYAN + "|", end="")
|
|
348
|
+
print("")
|
|
349
|
+
print(Fore.CYAN + f"{str(i + 1):^5}" + "|" + "-" * 54)
|
|
350
|
+
|
|
351
|
+
print(Fore.CYAN + " " * 5 + "|", end="")
|
|
352
|
+
print(Fore.CYAN + f"{num_col:^5}|", end="")
|
|
353
|
+
for j in range(size):
|
|
354
|
+
print(Fore.YELLOW + f"{calc_moments[i][j]:^15.3g}" + Fore.CYAN + "|", end="")
|
|
355
|
+
print("")
|
|
356
|
+
print(Fore.CYAN + "-" * 60)
|
|
357
|
+
|
|
358
|
+
print("\n")
|
|
359
|
+
|
|
360
|
+
print(Style.RESET_ALL)
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def print_waiting_multiclass(sim_moments, calc_moments, convert_to_central=True):
|
|
364
|
+
"""
|
|
365
|
+
Print waiting moments with classes
|
|
366
|
+
:param sim_moments: Simulated moments
|
|
367
|
+
:param calc_moments: Calculated moments
|
|
368
|
+
"""
|
|
369
|
+
header = "Moments of waiting time in the system"
|
|
370
|
+
|
|
371
|
+
print(Fore.CYAN + f"{header:^60s}")
|
|
372
|
+
|
|
373
|
+
print_raw_moments_multiclass(sim_moments, calc_moments, convert_to_central)
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
def print_sojourn_multiclass(sim_moments, calc_moments, convert_to_central=True):
|
|
377
|
+
"""
|
|
378
|
+
Print sojourn moments with classes
|
|
379
|
+
:param sim_moments: Simulated moments
|
|
380
|
+
:param calc_moments: Calculated moments
|
|
381
|
+
"""
|
|
382
|
+
header = "Moments of sojourn time in the system"
|
|
383
|
+
|
|
384
|
+
print(Fore.CYAN + f"{header:^60s}")
|
|
385
|
+
|
|
386
|
+
print_raw_moments_multiclass(sim_moments, calc_moments, convert_to_central)
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
def probs_print(p_sim, p_num, size=10):
|
|
390
|
+
"""
|
|
391
|
+
Prints the probabilities of states.
|
|
392
|
+
:param p_sim: Simulated probabilities
|
|
393
|
+
:param p_num: Calculated probabilities
|
|
394
|
+
:param size: Number of states to print
|
|
395
|
+
:return: None
|
|
396
|
+
"""
|
|
397
|
+
header = "Probabilities of states"
|
|
398
|
+
print(Fore.CYAN + "-" * 36)
|
|
399
|
+
print(f"{header:^36s}")
|
|
400
|
+
print("-" * 36)
|
|
401
|
+
sharp_col, num_col, sim_col = "#", "Num", "Sim"
|
|
402
|
+
print(f"{sharp_col:^4s}|{num_col:^15s}|{sim_col:^15s}")
|
|
403
|
+
print("-" * 36)
|
|
404
|
+
size = min(len(p_sim), len(p_num), size)
|
|
405
|
+
for i in range(size):
|
|
406
|
+
print(
|
|
407
|
+
Fore.CYAN
|
|
408
|
+
+ f"{i:^4d}|"
|
|
409
|
+
+ Fore.YELLOW
|
|
410
|
+
+ f"{p_num[i]:^15.5g}"
|
|
411
|
+
+ Fore.CYAN
|
|
412
|
+
+ "|"
|
|
413
|
+
+ Fore.YELLOW
|
|
414
|
+
+ f"{p_sim[i]:^15.5g}"
|
|
415
|
+
)
|
|
416
|
+
print(Fore.CYAN + "-" * 36)
|
|
417
|
+
|
|
418
|
+
print(Style.RESET_ALL)
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
def probs_print_no_compare(probs, size=10, header="Probabilities of states"):
|
|
422
|
+
"""
|
|
423
|
+
Print table with probabilities
|
|
424
|
+
:param probs: Probabilities
|
|
425
|
+
:param size: Number of states to print
|
|
426
|
+
:return: None
|
|
427
|
+
"""
|
|
428
|
+
print(Fore.CYAN + "-" * 36)
|
|
429
|
+
print(f"{header:^36s}")
|
|
430
|
+
print("-" * 36)
|
|
431
|
+
sharp_col, prob_col = "#", "Probability"
|
|
432
|
+
print(f"{sharp_col:^4s}|{prob_col:^15s}")
|
|
433
|
+
print("-" * 36)
|
|
434
|
+
|
|
435
|
+
size = min(len(probs), size)
|
|
436
|
+
for i in range(size):
|
|
437
|
+
print(Fore.CYAN + f"{i:^4d}|" + Fore.YELLOW + f"{probs[i]:^15.5g}" + Fore.CYAN)
|
|
438
|
+
print(Fore.CYAN + "-" * 36)
|
|
439
|
+
|
|
440
|
+
print(Style.RESET_ALL)
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
def print_mrx(mrx, is_short=False):
|
|
444
|
+
"""
|
|
445
|
+
Print matrix mrx
|
|
446
|
+
"""
|
|
447
|
+
row = mrx.shape[0]
|
|
448
|
+
col = mrx.shape[1]
|
|
449
|
+
|
|
450
|
+
print(f"{Fore.GREEN}\n")
|
|
451
|
+
|
|
452
|
+
for i in range(row):
|
|
453
|
+
print("| ", end="")
|
|
454
|
+
|
|
455
|
+
for j in range(col):
|
|
456
|
+
if math.isclose(mrx[i, j].real, 0.0):
|
|
457
|
+
color = Fore.RED
|
|
458
|
+
else:
|
|
459
|
+
color = Fore.GREEN
|
|
460
|
+
if is_short or mrx[i, j].imag == 0.0:
|
|
461
|
+
print(f"{color}{mrx[i, j].real:^4.2f} | ", end="")
|
|
462
|
+
else:
|
|
463
|
+
print(f"{color}{mrx[i, j]:^5.3f} | ", end="")
|
|
464
|
+
print("\n" + "-------" * col if is_short or mrx[i, j].imag == 0.0 else "\n" + "---------------" * col)
|
|
465
|
+
|
|
466
|
+
print(f"{Style.RESET_ALL}")
|
|
@@ -4,21 +4,21 @@ import matplotlib.pyplot as plt
|
|
|
4
4
|
import numpy as np
|
|
5
5
|
from tqdm import tqdm
|
|
6
6
|
|
|
7
|
-
from most_queue.
|
|
7
|
+
from most_queue.random.distributions import GammaDistribution, H2Distribution
|
|
8
8
|
|
|
9
9
|
if __name__ == "__main__":
|
|
10
10
|
|
|
11
11
|
# Входные данные
|
|
12
12
|
|
|
13
|
-
b1 = 3
|
|
14
|
-
|
|
15
|
-
a_s = 4
|
|
13
|
+
b1 = 3 # мат. ожидание
|
|
14
|
+
cv = 1.2 # коэффициент вариации
|
|
15
|
+
a_s = 4 # ассиметрия
|
|
16
16
|
|
|
17
17
|
# задаем начальные моменты распределения
|
|
18
18
|
|
|
19
19
|
b = [0, 0, 0]
|
|
20
20
|
b[0] = b1
|
|
21
|
-
b[1] = pow(b[0], 2)*(1 + pow(
|
|
21
|
+
b[1] = pow(b[0], 2) * (1 + pow(cv, 2))
|
|
22
22
|
|
|
23
23
|
# дисперсия
|
|
24
24
|
D = b[1] - pow(b[0], 2)
|
|
@@ -27,7 +27,7 @@ if __name__ == "__main__":
|
|
|
27
27
|
sigma = math.sqrt(D)
|
|
28
28
|
|
|
29
29
|
# 3 начальный момент
|
|
30
|
-
b[2] = a_s*pow(sigma, 3) + 3*b[1]*b[0] - 2*pow(b[0], 2)
|
|
30
|
+
b[2] = a_s * pow(sigma, 3) + 3 * b[1] * b[0] - 2 * pow(b[0], 2)
|
|
31
31
|
|
|
32
32
|
# Гамма-аппроксимация
|
|
33
33
|
gamma_params = GammaDistribution.get_params(b)
|
|
@@ -44,7 +44,7 @@ if __name__ == "__main__":
|
|
|
44
44
|
val_gamma = GammaDistribution.generate_static(gamma_params)
|
|
45
45
|
val_h2 = H2Distribution.generate_static(h2_params)
|
|
46
46
|
for k in range(3):
|
|
47
|
-
b_approx_gamma[k] += pow(val_gamma, k+1)
|
|
47
|
+
b_approx_gamma[k] += pow(val_gamma, k + 1)
|
|
48
48
|
b_approx_h2[k] += pow(val_h2, k + 1)
|
|
49
49
|
|
|
50
50
|
for k in range(3):
|
|
@@ -52,22 +52,19 @@ if __name__ == "__main__":
|
|
|
52
52
|
b_approx_h2[k] /= n
|
|
53
53
|
|
|
54
54
|
print("{0:^40s}".format("Начальные моменты СВ"))
|
|
55
|
-
print("{0:^3s}|{1:^15s}|{2:^15s}|{3:^15s}".format(
|
|
56
|
-
"№", "Реал", "Gamma", "H2"))
|
|
55
|
+
print("{0:^3s}|{1:^15s}|{2:^15s}|{3:^15s}".format("№", "Реал", "Gamma", "H2"))
|
|
57
56
|
print("-" * 50)
|
|
58
57
|
for i in range(3):
|
|
59
|
-
print("{0:^4d}|{1:^15.3g}|{2:^15.3g}|{3:^15.3g}".format(
|
|
60
|
-
i+1, b[i], b_approx_gamma[i], b_approx_h2[i]))
|
|
58
|
+
print("{0:^4d}|{1:^15.3g}|{2:^15.3g}|{3:^15.3g}".format(i + 1, b[i], b_approx_gamma[i], b_approx_h2[i]))
|
|
61
59
|
|
|
62
60
|
dots_num = 10000
|
|
63
61
|
|
|
64
|
-
x = np.linspace(0, 1*sigma, dots_num)
|
|
62
|
+
x = np.linspace(0, 1 * sigma, dots_num)
|
|
65
63
|
y_gamma = []
|
|
66
64
|
y_h2 = []
|
|
67
65
|
|
|
68
66
|
for i in range(dots_num):
|
|
69
|
-
y_gamma.append(GammaDistribution.get_pdf(
|
|
70
|
-
gamma_params, x[i]))
|
|
67
|
+
y_gamma.append(GammaDistribution.get_pdf(gamma_params, x[i]))
|
|
71
68
|
y_h2.append(H2Distribution.get_pdf(h2_params, x[i]))
|
|
72
69
|
|
|
73
70
|
fig, ax = plt.subplots()
|