222 lines
7.5 KiB
Python
Executable file
222 lines
7.5 KiB
Python
Executable file
#!/usr/bin/python3
|
|
from random import random
|
|
from math import floor, sqrt
|
|
from statistics import mean, variance
|
|
from matplotlib import pyplot as plt
|
|
from pylab import *
|
|
import numpy as np
|
|
|
|
def simulate_NFBP(N):
|
|
"""
|
|
Tries to simulate T_i, V_i and H_n for N packages 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.
|
|
"""
|
|
P=R*N
|
|
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)]
|
|
Sum_T=[]
|
|
Sum_V=[]
|
|
Sum_H=[]
|
|
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
|
|
Sum_T=Sum_T+sim['T']
|
|
Sum_H=Sum_H+sim['H']
|
|
for k in range(sim['i']):
|
|
#we use round to approximate variations of continuous variable V
|
|
Sum_V.append(round(sim['V'][k],2))
|
|
I = ISum/R
|
|
IVariance = sqrt(IVarianceSum/(R-1) - I**2)
|
|
print("Mean number of boxes : {} (variance {})".format(I, IVariance),'\n')
|
|
print(" {} * {} iterations of T".format(R,N),'\n')
|
|
|
|
#Plotting
|
|
#fig = plt.figure()
|
|
#ax = fig.add_subplot(111)
|
|
#matplotlib.stairs(Sum_T,bins=[0,1,2,3,4])
|
|
#ax.hist(Sum_T, bins=8, edgecolor='k', density=True, label='Valeurs empiriques')
|
|
#ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
|
|
#ylim=(0, 500), yticks=np.linspace(0, 56, 9))
|
|
#ax.legend()
|
|
#plt.show()
|
|
#plt.style.use('_mpl-gallery')
|
|
#make data
|
|
#plot:
|
|
#fig = plt.subplots()
|
|
fig = plt.figure()
|
|
#T plot
|
|
ax = fig.add_subplot(221)
|
|
ax.hist(Sum_T, bins=6, linewidth=0.5, edgecolor="white", label='Empirical values')
|
|
ax.set(xlim=(0, 6), xticks=np.arange(0, 6),ylim=(0, 6000), yticks=np.linspace(0, 6000, 9))
|
|
ax.set_title('T histogram for {} packages (Number of packages in each box)'.format(P))
|
|
ax.legend()
|
|
#V plot
|
|
bx = fig.add_subplot(222)
|
|
bx.hist(Sum_V, bins=10, linewidth=0.5, edgecolor="white", label='Empirical values')
|
|
bx.set(xlim=(0, 1), xticks=np.arange(0, 1),ylim=(0, 1000), yticks=np.linspace(0, 1000, 9))
|
|
bx.set_title('V histogram for {} packages (first package size of each box)'.format(P))
|
|
bx.legend()
|
|
#H plot
|
|
cx = fig.add_subplot(223)
|
|
cx.hist(Sum_H, bins=10, linewidth=0.5, edgecolor="white", label='Empirical values')
|
|
cx.set(xlim=(0, 10), xticks=np.arange(0, 10),ylim=(0, 2000), yticks=np.linspace(0, 2000, 9))
|
|
cx.set_title('H histogram for {} packages'.format(P))
|
|
cx.legend()
|
|
plt.show()
|
|
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 packages 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=[]
|
|
T=[]
|
|
for i in range(R):
|
|
sim = simulate_NFDBP(N)
|
|
I.append(sim["i"])
|
|
for n in range(N):
|
|
H[n].append(sim["H"][n])
|
|
T=sim["T"]
|
|
for k in range(sim["i"]):
|
|
# for o in range(sim["i"]):
|
|
#Tmean+=sim["T"]
|
|
Tmean.append(T[k])
|
|
|
|
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(sim["i"])):
|
|
print("Mean T_{} : {} (variance {})".format(k, mean(Tmean), variance(Tmean)))
|
|
|
|
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 ** 3, 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_NFBP_iter(10**3, 10)
|
|
stats_NFDBP(10 ** 3, 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')
|
|
#show()
|