Adding T,V,H graphs for NFBP algorithm
This commit is contained in:
parent
e9dbc054fe
commit
ebf87d231c
1 changed files with 67 additions and 22 deletions
89
Probas.py
Normal file → Executable file
89
Probas.py
Normal file → Executable file
|
@ -1,11 +1,14 @@
|
|||
#!/usr/bin/python3
|
||||
from random import random
|
||||
from math import floor, sqrt
|
||||
from statistics import mean, variance
|
||||
# from matplotlib import pyplot
|
||||
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 boxes of random size.
|
||||
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
|
||||
|
@ -44,7 +47,7 @@ def stats_NFBP(R, N):
|
|||
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"])
|
||||
|
@ -61,12 +64,15 @@ 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"]
|
||||
|
@ -74,11 +80,49 @@ def stats_NFBP_iter(R, N):
|
|||
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))
|
||||
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)
|
||||
|
@ -86,7 +130,7 @@ def stats_NFBP_iter(R, N):
|
|||
|
||||
def simulate_NFDBP(N):
|
||||
"""
|
||||
Tries to simulate T_i, V_i and H_n for N boxes of random size.
|
||||
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
|
||||
|
@ -127,22 +171,24 @@ def stats_NFDBP(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+=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(mean(I))+1):
|
||||
print(Tmean[7])
|
||||
# print("Mean T_{} : {} (variance {})".format(k, mean(Tmean[k]), variance(Tmean[k])))
|
||||
for k in range(int(sim["i"])):
|
||||
print("Mean T_{} : {} (variance {})".format(k, mean(Tmean), variance(Tmean)))
|
||||
|
||||
N = 10 ** 1
|
||||
sim = simulate_NFBP(N)
|
||||
|
@ -154,7 +200,7 @@ for j in range(sim["i"] + 1):
|
|||
sim["V"][j]))
|
||||
|
||||
print()
|
||||
stats_NFBP(10 ** 4, 10)
|
||||
stats_NFBP(10 ** 3, 10)
|
||||
|
||||
N = 10 ** 1
|
||||
sim = simulate_NFDBP(N)
|
||||
|
@ -166,12 +212,11 @@ for j in range(sim["i"] + 1):
|
|||
sim["V"][j]))
|
||||
|
||||
print()
|
||||
stats_NFDBP(10 ** 4, 10)
|
||||
stats_NFBP_iter(10**6, 10)
|
||||
|
||||
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')
|
||||
|
||||
#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()
|
||||
|
|
Loading…
Reference in a new issue