Projet_Boites/Probas.py
2023-03-13 10:25:04 +01:00

177 lines
5.6 KiB
Python

from random import random
from math import floor, sqrt
from statistics import mean, variance
# from matplotlib import pyplot
def simulate_NFBP(N):
"""
Tries to simulate T_i, V_i and H_n for N boxes of random size.
"""
i = 0 # Nombre de boites
R = [0] # Remplissage de la i-eme boite
T = [0] # Nombre de paquets de la i-eme boite
V = [0] # Taille du premier paquet de la i-eme boite
H = [] # Rang de la boite contenant le n-ieme paquet
for n in range(N):
size = random()
if R[i] + size >= 1:
# Il y n'y a plus de la place dans la boite pour le paquet.
# On passe à la boite suivante (qu'on initialise)
i += 1
R.append(0)
T.append(0)
V.append(0)
R[i] += size
T[i] += 1
if V[i] == 0:
# C'est le premier paquet de la boite
V[i] = size
H.append(i)
return {
"i": i,
"R": R,
"T": T,
"V": V,
"H": H
}
def stats_NFBP(R, N):
"""
Runs R runs of NFBP (for N packages) and studies distribution, variance, mean...
"""
print("Running {} NFBP simulations with {} packages".format(R, N))
I = []
H = [[] for _ in range(N)] # List of empty lists
for i in range(R):
sim = simulate_NFBP(N)
I.append(sim["i"])
for n in range(N):
H[n].append(sim["H"][n])
print("Mean number of boxes : {} (variance {})".format(mean(I), variance(I)))
for n in range(N):
print("Mean H_{} : {} (variance {})".format(n, mean(H[n]), variance(H[n])))
def stats_NFBP_iter(R, N):
"""
Runs R runs of NFBP (for N packages) and studies distribution, variance, mean...
Calculates stats during runtime instead of after to avoid excessive memory usage.
"""
print("Running {} NFBP simulations with {} packages".format(R, N))
ISum = 0
IVarianceSum = 0
HSum = [0 for _ in range(N)]
HSumVariance = [0 for _ in range(N)]
for i in range(R):
sim = simulate_NFBP(N)
ISum += sim["i"]
IVarianceSum += sim["i"]**2
for n in range(N):
HSum[n] += sim["H"][n]
HSumVariance[n] += sim["H"][n]**2
I = ISum/R
IVariance = sqrt(IVarianceSum/(R-1) - I**2)
print("Mean number of boxes : {} (variance {})".format(I, IVariance))
for n in range(n):
Hn = HSum[n]/R
HVariance = sqrt(HSumVariance[n]/(R-1) - Hn**2)
print("Index of box containing the {}th package (H_{}) : {} (variance {})".format(n, n, Hn, HVariance))
def simulate_NFDBP(N):
"""
Tries to simulate T_i, V_i and H_n for N boxes of random size.
"""
i = 0 # Nombre de boites
R = [0] # Remplissage de la i-eme boite
T = [0] # Nombre de paquets de la i-eme boite
V = [0] # Taille du premier paquet de la i-eme boite
H = [] # Rang de la boite contenant le n-ieme paquet
for n in range(N):
size = random()
R[i] += size
T[i] += 1
if R[i] + size >= 1:
# Il y n'y a plus de la place dans la boite pour le paquet.
# On passe à la boite suivante (qu'on initialise)
i += 1
R.append(0)
T.append(0)
V.append(0)
if V[i] == 0:
# C'est le premier paquet de la boite
V[i] = size
H.append(i)
return {
"i": i,
"R": R,
"T": T,
"V": V,
"H": H
}
def stats_NFDBP(R, N):
"""
Runs R runs of NFDBP (for N packages) and studies distribution, variance, mean...
"""
print("Running {} NFDBP simulations with {} packages".format(R, N))
I = []
H = [[] for _ in range(N)] # List of empty lists
Tmean=[]
for i in range(R):
sim = simulate_NFDBP(N)
I.append(sim["i"])
for n in range(N):
H[n].append(sim["H"][n])
for k in range(sim["i"]):
# for o in range(sim["i"]):
Tmean+=sim["T"]
print("Mean number of boxes : {} (variance {})".format(mean(I), variance(I)))
for n in range(N):
print("Mean H_{} : {} (variance {})".format(n, mean(H[n]), variance(H[n])))
for k in range(int(mean(I))+1):
print(Tmean[7])
# print("Mean T_{} : {} (variance {})".format(k, mean(Tmean[k]), variance(Tmean[k])))
N = 10 ** 1
sim = simulate_NFBP(N)
print("Simulation NFBP pour {} packaets. Contenu des boites :".format(N))
for j in range(sim["i"] + 1):
remplissage = floor(sim["R"][j] * 100)
print("Boite {} : Rempli à {} % avec {} paquets. Taille du premier paquet : {}".format(j, remplissage, sim["T"][j],
sim["V"][j]))
print()
stats_NFBP(10 ** 4, 10)
N = 10 ** 1
sim = simulate_NFDBP(N)
print("Simulation NFDBP pour {} packaets. Contenu des boites :".format(N))
for j in range(sim["i"] + 1):
remplissage = floor(sim["R"][j] * 100)
print("Boite {} : Rempli à {} % avec {} paquets. Taille du premier paquet : {}".format(j, remplissage,
sim["T"][j],
sim["V"][j]))
print()
stats_NFDBP(10 ** 4, 10)
stats_NFBP_iter(10**6, 10)
#
# pyplot.plot([1, 2, 4, 4, 2, 1], color = 'red', linestyle = 'dashed', linewidth = 2,
# markerfacecolor = 'blue', markersize = 5)
# pyplot.ylim(0, 5)
# pyplot.title('Un exemple')