chore: clean up outputs + add comments
This commit is contained in:
parent
5f56b578d2
commit
7bee845a97
1 changed files with 25 additions and 20 deletions
45
Probas.py
45
Probas.py
|
@ -11,11 +11,11 @@ def simulate_NFBP(N):
|
|||
"""
|
||||
Tries to simulate T_i, V_i and H_n for N items of random size.
|
||||
"""
|
||||
i = 0 # Nombre de boites
|
||||
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
|
||||
H = [] # Rang de la boite contenant le n-ieme paquet
|
||||
for n in range(N):
|
||||
size = random()
|
||||
if R[i] + size >= 1:
|
||||
|
@ -41,6 +41,7 @@ def simulate_NFBP(N):
|
|||
}
|
||||
|
||||
|
||||
# unused
|
||||
def stats_NFBP(R, N):
|
||||
"""
|
||||
Runs R runs of NFBP (for N items) and studies distribution, variance, mean...
|
||||
|
@ -65,14 +66,19 @@ def stats_NFBP_iter(R, N):
|
|||
Runs R runs of NFBP (for N items) 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 {} items".format(R, N))
|
||||
ISum = 0
|
||||
P=R*N # Total number of items
|
||||
print("## Running {} NFBP simulations with {} items".format(R, N))
|
||||
# number of bins
|
||||
ISum = 0
|
||||
IVarianceSum = 0
|
||||
HSum = [0 for _ in range(N)]
|
||||
# index of the bin containing the n-th item
|
||||
HSum = [0 for _ in range(N)]
|
||||
HSumVariance = [0 for _ in range(N)]
|
||||
# number of items in the i-th bin
|
||||
Sum_T=[0 for _ in range(N)]
|
||||
# size of the first item in the i-th bin
|
||||
Sum_V=[0 for _ in range(N)]
|
||||
|
||||
for i in range(R):
|
||||
sim = simulate_NFBP(N)
|
||||
ISum += sim["i"]
|
||||
|
@ -87,14 +93,13 @@ def stats_NFBP_iter(R, N):
|
|||
V.append(0)
|
||||
Sum_T=[x+y for x,y in zip(Sum_T,T)]
|
||||
Sum_V=[x+y for x,y in zip(Sum_V,V)]
|
||||
#we use round to approximate variations of continuous variable V
|
||||
# Sum_V= round(sim['V'],2))
|
||||
Sum_T=[x/R for x in Sum_T]
|
||||
Sum_V=[round(x/R,2) for x in Sum_V]
|
||||
print(Sum_V)
|
||||
#print(Sum_V)
|
||||
I = ISum/R
|
||||
IVariance = sqrt(IVarianceSum/(R-1) - I**2)
|
||||
print("Mean number of bins : {} (variance {})".format(I, IVariance),'\n')
|
||||
# TODO clarify line below
|
||||
print(" {} * {} iterations of T".format(R,N),'\n')
|
||||
|
||||
for n in range(N):
|
||||
|
@ -102,12 +107,12 @@ def stats_NFBP_iter(R, N):
|
|||
HVariance = sqrt(HSumVariance[n]/(R-1) - Hn**2) # Variance
|
||||
print("Index of bin containing the {}th item (H_{}) : {} (variance {})".format(n, n, Hn, HVariance))
|
||||
HSum=[x/R for x in HSum]
|
||||
print(HSum)
|
||||
# print(HSum)
|
||||
#Plotting
|
||||
fig = plt.figure()
|
||||
#T plot
|
||||
x = np.arange(N)
|
||||
print(x)
|
||||
# print(x)
|
||||
ax = fig.add_subplot(221)
|
||||
ax.bar(x,Sum_T, width=1,label='Empirical values', edgecolor="blue", linewidth=0.7,color='red')
|
||||
ax.set(xlim=(0, N), xticks=np.arange(0, N),ylim=(0,3), yticks=np.linspace(0, 3, 5))
|
||||
|
@ -142,12 +147,13 @@ def stats_NFBP_iter(R, N):
|
|||
def simulate_NFDBP(N):
|
||||
"""
|
||||
Tries to simulate T_i, V_i and H_n for N items of random size.
|
||||
Next Fit Dual Bin Packing : bins should overflow
|
||||
"""
|
||||
i = 0 # Nombre de boites
|
||||
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
|
||||
H = [] # Rang de la boite contenant le n-ieme paquet
|
||||
for n in range(N):
|
||||
size = random()
|
||||
R[i] += size
|
||||
|
@ -178,9 +184,9 @@ def stats_NFDBP(R, N,t_i):
|
|||
"""
|
||||
Runs R runs of NFDBP (for N items) and studies distribution, variance, mean...
|
||||
"""
|
||||
print("Running {} NFDBP simulations with {} items".format(R, N))
|
||||
P=N*R
|
||||
I = []
|
||||
print("## Running {} NFDBP simulations with {} items".format(R, N))
|
||||
P=N*R # Total number of items
|
||||
I = []
|
||||
H = [[] for _ in range(N)] # List of empty lists
|
||||
T=[]
|
||||
Tk=[[] for _ in range(N)]
|
||||
|
@ -214,14 +220,13 @@ def stats_NFDBP(R, N,t_i):
|
|||
T_maths.append(1/(factorial(u-1))-1/factorial(u))
|
||||
E=0
|
||||
sigma2=0
|
||||
print("hep")
|
||||
print(T_maths)
|
||||
# print(T_maths)
|
||||
for p in range(len(T_maths)):
|
||||
E=E+(p+1)*T_maths[p]
|
||||
sigma2=((T_maths[p]-E)**2)/(len(T_maths)-1)
|
||||
print("Mathematical values : Empiric mean T_{} : {} Variance {})".format(t_i, E, sqrt(sigma2)))
|
||||
T_maths=[x*100 for x in T_maths]
|
||||
#Plotting
|
||||
#Plotting
|
||||
fig = plt.figure()
|
||||
#T plot
|
||||
x = np.arange(N)
|
||||
|
@ -277,6 +282,6 @@ for j in range(sim["i"] + 1):
|
|||
sim["T"][j],
|
||||
sim["V"][j]))
|
||||
|
||||
print()
|
||||
stats_NFBP_iter(10**3, 10)
|
||||
print('\n\n')
|
||||
stats_NFDBP(10 ** 3, 10,1)
|
||||
|
|
Loading…
Reference in a new issue