Compare commits
6 commit
8284d7bf03
...
7e0c5a84bb
| 作者 | SHA1 | 提交日期 | |
|---|---|---|---|
|
|
7e0c5a84bb | ||
|
|
cf7a4cf7a6 | ||
|
|
0cdc13b869 | ||
|
|
7bee845a97 | ||
|
|
5f56b578d2 | ||
|
|
d8b470c9d4 |
共有 1 個文件被更改,包括 76 次插入 和 69 次删除
145
Probas.py
145
Probas.py
|
|
@ -9,13 +9,13 @@ import matplotlib.pyplot as pt
|
||||||
|
|
||||||
def simulate_NFBP(N):
|
def simulate_NFBP(N):
|
||||||
"""
|
"""
|
||||||
Tries to simulate T_i, V_i and H_n for N packages of random size.
|
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
|
R = [0] # Remplissage de la i-eme boite
|
||||||
T = [0] # Nombre de paquets 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
|
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):
|
for n in range(N):
|
||||||
size = random()
|
size = random()
|
||||||
if R[i] + size >= 1:
|
if R[i] + size >= 1:
|
||||||
|
|
@ -41,11 +41,12 @@ def simulate_NFBP(N):
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# unused
|
||||||
def stats_NFBP(R, N):
|
def stats_NFBP(R, N):
|
||||||
"""
|
"""
|
||||||
Runs R runs of NFBP (for N packages) and studies distribution, variance, mean...
|
Runs R runs of NFBP (for N items) and studies distribution, variance, mean...
|
||||||
"""
|
"""
|
||||||
print("Running {} NFBP simulations with {} packages".format(R, N))
|
print("Running {} NFBP simulations with {} items".format(R, N))
|
||||||
I = []
|
I = []
|
||||||
H = [[] for _ in range(N)] # List of empty lists
|
H = [[] for _ in range(N)] # List of empty lists
|
||||||
|
|
||||||
|
|
@ -55,24 +56,29 @@ def stats_NFBP(R, N):
|
||||||
for n in range(N):
|
for n in range(N):
|
||||||
H[n].append(sim["H"][n])
|
H[n].append(sim["H"][n])
|
||||||
|
|
||||||
print("Mean number of boxes : {} (variance {})".format(mean(I), variance(I)))
|
print("Mean number of bins : {} (variance {})".format(mean(I), variance(I)))
|
||||||
|
|
||||||
for n in range(N):
|
for n in range(N):
|
||||||
print("Mean H_{} : {} (variance {})".format(n, mean(H[n]), variance(H[n])))
|
print("Mean H_{} : {} (variance {})".format(n, mean(H[n]), variance(H[n])))
|
||||||
|
|
||||||
def stats_NFBP_iter(R, N):
|
def stats_NFBP_iter(R, N):
|
||||||
"""
|
"""
|
||||||
Runs R runs of NFBP (for N packages) and studies distribution, variance, mean...
|
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.
|
Calculates stats during runtime instead of after to avoid excessive memory usage.
|
||||||
"""
|
"""
|
||||||
P=R*N
|
P=R*N # Total number of items
|
||||||
print("Running {} NFBP simulations with {} packages".format(R, N))
|
print("## Running {} NFBP simulations with {} items".format(R, N))
|
||||||
ISum = 0
|
# number of bins
|
||||||
|
ISum = 0
|
||||||
IVarianceSum = 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)]
|
HSumVariance = [0 for _ in range(N)]
|
||||||
|
# number of items in the i-th bin
|
||||||
Sum_T=[0 for _ in range(N)]
|
Sum_T=[0 for _ in range(N)]
|
||||||
|
# size of the first item in the i-th bin
|
||||||
Sum_V=[0 for _ in range(N)]
|
Sum_V=[0 for _ in range(N)]
|
||||||
|
|
||||||
for i in range(R):
|
for i in range(R):
|
||||||
sim = simulate_NFBP(N)
|
sim = simulate_NFBP(N)
|
||||||
ISum += sim["i"]
|
ISum += sim["i"]
|
||||||
|
|
@ -82,55 +88,56 @@ def stats_NFBP_iter(R, N):
|
||||||
HSumVariance[n] += sim["H"][n]**2
|
HSumVariance[n] += sim["H"][n]**2
|
||||||
T=sim['T']
|
T=sim['T']
|
||||||
V=sim['V']
|
V=sim['V']
|
||||||
for i in range(N):
|
# ensure that T, V have the same length as Sum_T, Sum_V
|
||||||
|
for i in range(N - sim['i']):
|
||||||
T.append(0)
|
T.append(0)
|
||||||
V.append(0)
|
V.append(0)
|
||||||
Sum_T=[x+y for x,y in zip(Sum_T,T)]
|
Sum_T=[x+y for x,y in zip(Sum_T,T)]
|
||||||
Sum_V=[x+y for x,y in zip(Sum_V,V)]
|
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_T=[x/R for x in Sum_T]
|
||||||
Sum_V=[round(x/R,2) for x in Sum_V]
|
Sum_V=[round(x/R,2) for x in Sum_V]
|
||||||
print(Sum_V)
|
#print(Sum_V)
|
||||||
I = ISum/R
|
I = ISum/R
|
||||||
IVariance = sqrt(IVarianceSum/(R-1) - I**2)
|
IVariance = sqrt(IVarianceSum/(R-1) - I**2)
|
||||||
print("Mean number of boxes : {} (variance {})".format(I, IVariance),'\n')
|
print("Mean number of bins : {} (variance {})".format(I, IVariance),'\n')
|
||||||
|
# TODO clarify line below
|
||||||
print(" {} * {} iterations of T".format(R,N),'\n')
|
print(" {} * {} iterations of T".format(R,N),'\n')
|
||||||
|
|
||||||
for n in range(N):
|
for n in range(min(N, 10)):
|
||||||
Hn = HSum[n]/R # moyenne
|
Hn = HSum[n]/R # moyenne
|
||||||
HVariance = sqrt(HSumVariance[n]/(R-1) - Hn**2) # Variance
|
HVariance = sqrt(HSumVariance[n]/(R-1) - Hn**2) # Variance
|
||||||
print("Index of box containing the {}th package (H_{}) : {} (variance {})".format(n, n, Hn, HVariance))
|
print("Index of bin containing the {}th item (H_{}) : {} (variance {})".format(n, n, Hn, HVariance))
|
||||||
HSum=[x/R for x in HSum]
|
HSum=[x/R for x in HSum]
|
||||||
print(HSum)
|
# print(HSum)
|
||||||
#Plotting
|
#Plotting
|
||||||
fig = plt.figure()
|
fig = plt.figure()
|
||||||
#T plot
|
#T plot
|
||||||
x = np.arange(N)
|
x = np.arange(N)
|
||||||
print(x)
|
# print(x)
|
||||||
ax = fig.add_subplot(221)
|
ax = fig.add_subplot(221)
|
||||||
ax.bar(x,Sum_T, width=1,label='Empirical values', edgecolor="blue", linewidth=0.7,color='red')
|
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))
|
ax.set(xlim=(0, N), xticks=np.arange(0, N),ylim=(0,3), yticks=np.linspace(0, 3, 5))
|
||||||
ax.set_ylabel('Items')
|
ax.set_ylabel('Items')
|
||||||
ax.set_xlabel('Boxes (1-{})'.format(N))
|
ax.set_xlabel('Bins (1-{})'.format(N))
|
||||||
ax.set_title('T histogram for {} packages (Number of packages in each box)'.format(P))
|
ax.set_title('T histogram for {} items (Number of items in each bin)'.format(P))
|
||||||
ax.legend(loc='upper left',title='Legend')
|
ax.legend(loc='upper left',title='Legend')
|
||||||
#V plot
|
#V plot
|
||||||
bx = fig.add_subplot(222)
|
bx = fig.add_subplot(222)
|
||||||
bx.bar(x,Sum_V, width=1,label='Empirical values', edgecolor="blue", linewidth=0.7,color='orange')
|
bx.bar(x,Sum_V, width=1,label='Empirical values', edgecolor="blue", linewidth=0.7,color='orange')
|
||||||
bx.set(xlim=(0, N), xticks=np.arange(0, N),ylim=(0, 1), yticks=np.linspace(0, 1, 10))
|
bx.set(xlim=(0, N), xticks=np.arange(0, N),ylim=(0, 1), yticks=np.linspace(0, 1, 10))
|
||||||
bx.set_ylabel('First item size')
|
bx.set_ylabel('First item size')
|
||||||
bx.set_xlabel('Boxes (1-{})'.format(N))
|
bx.set_xlabel('Bins (1-{})'.format(N))
|
||||||
bx.set_title('V histogram for {} packages (first package size of each box)'.format(P))
|
bx.set_title('V histogram for {} items (first item size of each bin)'.format(P))
|
||||||
bx.legend(loc='upper left',title='Legend')
|
bx.legend(loc='upper left',title='Legend')
|
||||||
#H plot
|
#H plot
|
||||||
#We will simulate this part for a asymptotic study
|
#We will simulate this part for a asymptotic study
|
||||||
cx = fig.add_subplot(223)
|
cx = fig.add_subplot(223)
|
||||||
cx.bar(x,HSum, width=1,label='Empirical values', edgecolor="blue", linewidth=0.7,color='green')
|
cx.bar(x,HSum, width=1,label='Empirical values', edgecolor="blue", linewidth=0.7,color='green')
|
||||||
cx.set(xlim=(0, N), xticks=np.arange(0, N),ylim=(0, 10), yticks=np.linspace(0, N, 5))
|
cx.set(xlim=(0, N), xticks=np.arange(0, N),ylim=(0, 10), yticks=np.linspace(0, N, 5))
|
||||||
cx.set_ylabel('Box ranking of n-item')
|
cx.set_ylabel('Bin ranking of n-item')
|
||||||
cx.set_xlabel('n-item (1-{})'.format(N))
|
cx.set_xlabel('n-item (1-{})'.format(N))
|
||||||
cx.set_title('H histogram for {} packages'.format(P))
|
cx.set_title('H histogram for {} items'.format(P))
|
||||||
xb=linspace(0,N,10)
|
xb=linspace(0,N,10)
|
||||||
yb=Hn*xb/10
|
yb=Hn*xb/10
|
||||||
wb=HVariance*xb/10
|
wb=HVariance*xb/10
|
||||||
|
|
@ -141,29 +148,28 @@ def stats_NFBP_iter(R, N):
|
||||||
|
|
||||||
def simulate_NFDBP(N):
|
def simulate_NFDBP(N):
|
||||||
"""
|
"""
|
||||||
Tries to simulate T_i, V_i and H_n for N packages of random size.
|
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
|
R = [0] # Remplissage de la i-eme boite
|
||||||
T = [0] # Nombre de paquets 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
|
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):
|
for n in range(N):
|
||||||
size = random()
|
size = random()
|
||||||
R[i] += size
|
if R[i] >= 1:
|
||||||
T[i] += 1
|
|
||||||
if R[i] + size >= 1:
|
|
||||||
# Il y n'y a plus de la place dans la boite pour le paquet.
|
# Il y n'y a plus de la place dans la boite pour le paquet.
|
||||||
# On passe à la boite suivante (qu'on initialise)
|
# On passe à la boite suivante (qu'on initialise).
|
||||||
i += 1
|
i += 1
|
||||||
R.append(0)
|
R.append(0)
|
||||||
T.append(0)
|
T.append(0)
|
||||||
V.append(0)
|
|
||||||
|
|
||||||
if V[i] == 0:
|
|
||||||
# C'est le premier paquet de la boite
|
# C'est le premier paquet de la boite
|
||||||
V[i] = size
|
V.append(size)
|
||||||
H.append(i)
|
H.append(i)
|
||||||
|
R[i] += size
|
||||||
|
T[i] += 1
|
||||||
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"i": i,
|
"i": i,
|
||||||
|
|
@ -176,11 +182,11 @@ def simulate_NFDBP(N):
|
||||||
|
|
||||||
def stats_NFDBP(R, N,t_i):
|
def stats_NFDBP(R, N,t_i):
|
||||||
"""
|
"""
|
||||||
Runs R runs of NFDBP (for N packages) and studies distribution, variance, mean...
|
Runs R runs of NFDBP (for N items) and studies distribution, variance, mean...
|
||||||
"""
|
"""
|
||||||
print("Running {} NFDBP simulations with {} packages".format(R, N))
|
print("## Running {} NFDBP simulations with {} items".format(R, N))
|
||||||
P=N*R
|
P=N*R # Total number of items
|
||||||
I = []
|
I = []
|
||||||
H = [[] for _ in range(N)] # List of empty lists
|
H = [[] for _ in range(N)] # List of empty lists
|
||||||
T=[]
|
T=[]
|
||||||
Tk=[[] for _ in range(N)]
|
Tk=[[] for _ in range(N)]
|
||||||
|
|
@ -203,7 +209,7 @@ def stats_NFDBP(R, N,t_i):
|
||||||
Sum_T=[x/R for x in Sum_T] #Experimental [Ti=k]
|
Sum_T=[x/R for x in Sum_T] #Experimental [Ti=k]
|
||||||
Sum_T=[x*100/(sum(Sum_T)) for x in Sum_T] #Pourcentage de la repartition des items
|
Sum_T=[x*100/(sum(Sum_T)) for x in Sum_T] #Pourcentage de la repartition des items
|
||||||
|
|
||||||
print("Mean number of boxes : {} (variance {})".format(mean(I), variance(I)))
|
print("Mean number of bins : {} (variance {})".format(mean(I), variance(I)))
|
||||||
|
|
||||||
for n in range(N):
|
for n in range(N):
|
||||||
print("Mean H_{} : {} (variance {})".format(n, mean(H[n]), variance(H[n])))
|
print("Mean H_{} : {} (variance {})".format(n, mean(H[n]), variance(H[n])))
|
||||||
|
|
@ -214,14 +220,13 @@ def stats_NFDBP(R, N,t_i):
|
||||||
T_maths.append(1/(factorial(u-1))-1/factorial(u))
|
T_maths.append(1/(factorial(u-1))-1/factorial(u))
|
||||||
E=0
|
E=0
|
||||||
sigma2=0
|
sigma2=0
|
||||||
print("hep")
|
# print(T_maths)
|
||||||
print(T_maths)
|
|
||||||
for p in range(len(T_maths)):
|
for p in range(len(T_maths)):
|
||||||
E=E+(p+1)*T_maths[p]
|
E=E+(p+1)*T_maths[p]
|
||||||
sigma2=((T_maths[p]-E)**2)/(len(T_maths)-1)
|
sigma2=((T_maths[p]-E)**2)/(len(T_maths)-1)
|
||||||
print("Mathematical values : Empiric mean T_{} : {} Variance {})".format(t_i, E, sqrt(sigma2)))
|
print("Mathematical values : Empiric mean T_{} : {} Variance {})".format(t_i, E, sqrt(sigma2)))
|
||||||
T_maths=[x*100 for x in T_maths]
|
T_maths=[x*100 for x in T_maths]
|
||||||
#Plotting
|
#Plotting
|
||||||
fig = plt.figure()
|
fig = plt.figure()
|
||||||
#T plot
|
#T plot
|
||||||
x = np.arange(N)
|
x = np.arange(N)
|
||||||
|
|
@ -231,8 +236,8 @@ def stats_NFDBP(R, N,t_i):
|
||||||
ax.bar(x,Sum_T, width=1,label='Empirical values', edgecolor="blue", linewidth=0.7,color='red')
|
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,20), yticks=np.linspace(0, 20, 2))
|
ax.set(xlim=(0, N), xticks=np.arange(0, N),ylim=(0,20), yticks=np.linspace(0, 20, 2))
|
||||||
ax.set_ylabel('Items(n) in %')
|
ax.set_ylabel('Items(n) in %')
|
||||||
ax.set_xlabel('Boxes (1-{})'.format(N))
|
ax.set_xlabel('Bins (1-{})'.format(N))
|
||||||
ax.set_title('Items percentage for each box and {} packages (Number of packages in each box)'.format(P))
|
ax.set_title('Items percentage for each bin and {} items (Number of items in each bin)'.format(P))
|
||||||
ax.legend(loc='upper left',title='Legend')
|
ax.legend(loc='upper left',title='Legend')
|
||||||
|
|
||||||
#Mathematical P(Ti=k) plot. It shows the Ti(t_i) law with the probability of each number of items.
|
#Mathematical P(Ti=k) plot. It shows the Ti(t_i) law with the probability of each number of items.
|
||||||
|
|
@ -241,8 +246,8 @@ def stats_NFDBP(R, N,t_i):
|
||||||
bx.hist(Tk[t_i],bins=10, width=1,label='Empirical values', edgecolor="blue", linewidth=0.7,color='red')
|
bx.hist(Tk[t_i],bins=10, width=1,label='Empirical values', edgecolor="blue", linewidth=0.7,color='red')
|
||||||
bx.set(xlim=(0, N), xticks=np.arange(0, N),ylim=(0,len(Tk[t_i])), yticks=np.linspace(0, 1, 1))
|
bx.set(xlim=(0, N), xticks=np.arange(0, N),ylim=(0,len(Tk[t_i])), yticks=np.linspace(0, 1, 1))
|
||||||
bx.set_ylabel('P(T{}=i)'.format(t_i))
|
bx.set_ylabel('P(T{}=i)'.format(t_i))
|
||||||
bx.set_xlabel('Boxes i=(1-{}) in %'.format(N))
|
bx.set_xlabel('Bins i=(1-{}) in %'.format(N))
|
||||||
bx.set_title('T{} histogram for {} packages (Number of packages in each box)'.format(t_i,P))
|
bx.set_title('T{} histogram for {} items (Number of items in each bin)'.format(t_i,P))
|
||||||
bx.legend(loc='upper left',title='Legend')
|
bx.legend(loc='upper left',title='Legend')
|
||||||
|
|
||||||
#Loi mathematique
|
#Loi mathematique
|
||||||
|
|
@ -251,32 +256,34 @@ def stats_NFDBP(R, N,t_i):
|
||||||
cx.bar(x,T_maths, width=1,label='Theoretical values', edgecolor="blue", linewidth=0.7,color='red')
|
cx.bar(x,T_maths, width=1,label='Theoretical values', edgecolor="blue", linewidth=0.7,color='red')
|
||||||
cx.set(xlim=(0, N), xticks=np.arange(0, N),ylim=(0,100), yticks=np.linspace(0, 100, 10))
|
cx.set(xlim=(0, N), xticks=np.arange(0, N),ylim=(0,100), yticks=np.linspace(0, 100, 10))
|
||||||
cx.set_ylabel('P(T{}=i)'.format(t_i))
|
cx.set_ylabel('P(T{}=i)'.format(t_i))
|
||||||
cx.set_xlabel('Boxes i=(1-{})'.format(N))
|
cx.set_xlabel('Bins i=(1-{})'.format(N))
|
||||||
cx.set_title('Theoretical T{} values in %'.format(t_i))
|
cx.set_title('Theoretical T{} values in %'.format(t_i))
|
||||||
cx.legend(loc='upper left',title='Legend')
|
cx.legend(loc='upper left',title='Legend')
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
N = 10 ** 1
|
# unused
|
||||||
sim = simulate_NFBP(N)
|
def basic_demo():
|
||||||
|
N = 10 ** 1
|
||||||
|
sim = simulate_NFBP(N)
|
||||||
|
|
||||||
print("Simulation NFBP pour {} packaets. Contenu des boites :".format(N))
|
print("Simulation NFBP pour {} packaets. Contenu des boites :".format(N))
|
||||||
for j in range(sim["i"] + 1):
|
for j in range(sim["i"] + 1):
|
||||||
remplissage = floor(sim["R"][j] * 100)
|
remplissage = floor(sim["R"][j] * 100)
|
||||||
print("Boite {} : Rempli à {} % avec {} paquets. Taille du premier paquet : {}".format(j, remplissage, sim["T"][j],
|
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]))
|
sim["V"][j]))
|
||||||
|
|
||||||
print()
|
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]))
|
||||||
|
|
||||||
stats_NFBP_iter(10**3, 10)
|
stats_NFBP_iter(10**3, 10)
|
||||||
|
print('\n\n')
|
||||||
stats_NFDBP(10 ** 3, 10,1)
|
stats_NFDBP(10 ** 3, 10,1)
|
||||||
|
|
|
||||||
載入中…
Reference in a new issue