Compare commits

..

71 commits

Author SHA1 Message Date
Paul ALNET
5246b1c56b tex: final commit I suppose (I wish) 2023-06-05 00:08:48 +02:00
Paul ALNET
1f279bf2b9 merge 2023-06-04 23:28:52 +02:00
Paul ALNET
6ceddb79f2 tex: add more NFBP graphs 2023-06-04 23:26:48 +02:00
Clément Lacau
839b6f79ec jjj 2023-06-04 23:12:47 +02:00
Paul ALNET
2dc8ab1b36 tex: add NFBP graphs 2023-06-04 23:09:26 +02:00
Paul ALNET
1b48dacbed tex: set defense date 2023-06-04 22:25:40 +02:00
Paul ALNET
bbe25c3d4c tex: NFDBP add expected value T_i 2023-06-04 22:16:30 +02:00
Paul ALNET
ff1b121be9 tex: finalize NFDBP demo 2023-06-04 21:20:38 +02:00
Paul ALNET
07b4bec23e tex: NFDBP demo ! 2023-06-04 19:37:02 +02:00
Paul ALNET
4a4531a413 tex: NFBP stats 2023-06-04 18:39:18 +02:00
Paul ALNET
3a50f1d83d chore: des prints partout 2023-06-04 15:50:31 +02:00
Paul ALNET
7c1e115951 Merge branch 'latex' 2023-06-04 13:40:26 +02:00
Paul ALNET
1c6db889a6 tex: minor word corrections 2023-06-04 13:39:59 +02:00
Paul ALNET
8921d8c5df tex: add NFDP math spec 2023-06-04 12:57:42 +02:00
Paul ALNET
bcfbec57ba tex: add NFB(D)P drawings 2023-06-04 12:55:15 +02:00
Paul ALNET
dcda7297e0 tex: NFBP distribution boilerplate 2023-06-04 11:41:47 +02:00
Paul ALNET
7dc2616096 chore: remove unused duplicate pyplot import 2023-06-04 11:20:44 +02:00
Paul ALNET
06b9500efc tex: NFBP impl detail 2023-06-04 11:18:36 +02:00
Paul ALNET
bfbad23aed tex: NFBP math impl 2023-06-04 11:12:28 +02:00
Paul ALNET
ac6c443beb tex: detail stat analysis complexity 2023-06-04 11:02:26 +02:00
Paul ALNET
064eeabad5 tex: list variables 2023-06-04 10:42:04 +02:00
Paul ALNET
d03273baf1 feat: easter egg 2023-06-04 08:56:49 +02:00
Paul ALNET
df98bff285 tex: comment probabilistic annex 2023-06-04 08:54:56 +02:00
Paul ALNET
ad8d300348 Merge branch 'master' into latex 2023-06-04 08:50:31 +02:00
Paul ALNET
8fa29790f8 fix: remove accents in python script 2023-06-04 08:50:00 +02:00
Paul ALNET
de854e9fe6 tex: add code annex 2023-06-04 08:45:51 +02:00
Paul ALNET
836ffd538e fix: missing comma in perf annex 2023-06-04 08:43:26 +02:00
Paul ALNET
67bc7efd5c chore: add todos 2023-06-04 08:39:42 +02:00
Paul ALNET
6cd6df4b89 fix: better legend placement on graphs 2023-06-04 08:39:29 +02:00
Paul ALNET
316c910c3a chore: lint 2023-06-04 08:27:24 +02:00
Paul ALNET
6bb38429d1 fix: V_i in NFDBP algo 2023-06-04 08:17:35 +02:00
Paul ALNET
7e0c5a84bb fix: correct NFDBP algo 2023-06-04 08:12:30 +02:00
Paul ALNET
cf7a4cf7a6 chore: move legacy output to unused function 2023-06-04 08:12:10 +02:00
Paul ALNET
0cdc13b869 chore: clean up "zero padding" 2023-06-04 08:11:26 +02:00
Paul ALNET
7bee845a97 chore: clean up outputs + add comments 2023-06-04 08:10:42 +02:00
Paul ALNET
5f56b578d2 fix: rename packages to items 2023-06-04 07:14:37 +02:00
Paul ALNET
d8b470c9d4 fix: rename boxes to bins 2023-06-04 07:14:30 +02:00
Paul ALNET
95cf9001ad tex: some experimental proofing 2023-06-04 07:05:23 +02:00
Paul ALNET
0cb1203a30 tex: write partial demo with k1 and k2 2023-06-04 06:11:28 +02:00
Clément Lacau
8284d7bf03 Added many thing, check the code Paul 2023-06-04 00:28:24 +02:00
Clément Lacau
7804bbfc43 Beautiful graphs 2023-06-03 22:57:02 +02:00
Clément Lacau
ce5b4e560c aled paul 2023-06-03 17:27:38 +02:00
Paul ALNET
87a8bbbf0f tex: add performance analysis annex 2023-06-03 17:25:08 +02:00
Paul ALNET
00cc5befc7 chore: remove invisible ~ 2023-06-03 17:11:15 +02:00
Paul ALNET
c536e0b28b tex: performance analysis 2023-06-03 16:13:19 +02:00
Paul ALNET
184f4ff491 feat: add performance analysis code 2023-06-03 15:45:37 +02:00
Paul ALNET
78617e6130 tex: add listlistings for code colouring 2023-06-03 15:45:16 +02:00
Paul ALNET
c0abf64ee0 tex: move and write performance part 2023-06-03 15:45:04 +02:00
Paul ALNET
4c74dd7877 tex: NFBP 2023-06-03 14:22:45 +02:00
Paul ALNET
29851204fe tex: add networking part 2023-06-03 14:00:00 +02:00
Paul ALNET
33b994e4e5 tex: add content for part 1 2023-06-03 13:23:53 +02:00
Paul ALNET
ec2ac3144a chore(tex): remove annexes & remerciements 2023-06-03 11:59:46 +02:00
Paul ALNET
9fa6d45000 tex: add missing charte graphique 2023-06-03 11:20:17 +02:00
Paul ALNET
9bc4e67c68 tex(bib): add bin packing video 2023-06-03 10:35:46 +02:00
Paul ALNET
41908fb402 tex :add bibliography 2023-06-03 10:26:50 +02:00
Paul ALNET
5a206bd5d7 fix(tex): typo in intro 2023-06-03 10:07:35 +02:00
Clément Lacau
ebf87d231c Adding T,V,H graphs for NFBP algorithm 2023-06-03 01:01:13 +02:00
Paul ALNET
0bb99a16d0 tex: add intro paragraph 2023-06-02 16:40:56 +02:00
Paul ALNET
d65684d743 tex: add intro 2023-06-02 16:20:15 +02:00
Paul ALNET
709b29662b chore(tex): remove lorem ipsum 2023-06-02 15:24:28 +02:00
Paul ALNET
4d8823fc1f fix: babel to english + bugfix 2023-05-24 11:39:45 +02:00
Paul ALNET
6a4f298f28 doc(readme): explain report compilation 2023-05-24 07:14:25 +02:00
Paul ALNET
a696d77f7b feat: import latex template & change vars 2023-05-24 07:11:48 +02:00
Paul ALNET
8f3dce3490 feat: add gitignore 2023-05-24 06:24:43 +02:00
Clément Lacau
e9dbc054fe v_w_space 2023-05-22 16:49:32 +02:00
Clément Lacau
8bfeac55f4 v3 2023-05-22 16:48:06 +02:00
Clément Lacau
572bb721df V2 2023-05-22 16:43:21 +02:00
Clément Lacau
959bb80ae8 TODO List 2023-05-22 16:39:03 +02:00
Paul ALNET
3055d7ef9f feat: detailed readme 2023-05-22 14:57:26 +02:00
d6dfa090f6 Transférer les fichiers vers '' 2023-03-15 13:31:32 +01:00
75d0c36d8d Merge branch 'iterative' of lacau/Projet_Boites into master 2023-03-13 10:27:03 +01:00
27 changed files with 1792 additions and 75 deletions

21
.gitignore vendored Normal file
View file

@ -0,0 +1,21 @@
*.aux
*.log
*.swp
*.out
*.pdf
*.toc
*.maf
*.mtc
*.mtc0
*.stc
*.stc0
*.stc1
*.stc2
*.stc3
*.bbl
*.blg
*.stc*
*.bcf
*.run.xml

350
Boxes.ipynb Normal file

File diff suppressed because one or more lines are too long

400
Probas.py Normal file → Executable file
View file

@ -1,11 +1,15 @@
#!/usr/bin/python3
from random import random
from math import floor, sqrt
from math import floor, sqrt, factorial,exp
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 items of random size.
"""
i = 0 # Nombre de boites
R = [0] # Remplissage de la i-eme boite
@ -16,7 +20,7 @@ def simulate_NFBP(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)
# On passe a la boite suivante (qu'on initialise)
i += 1
R.append(0)
T.append(0)
@ -28,20 +32,15 @@ def simulate_NFBP(N):
V[i] = size
H.append(i)
return {
"i": i,
"R": R,
"T": T,
"V": V,
"H": H
}
return {"i": i, "R": R, "T": T, "V": V, "H": H}
# unused
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 = []
H = [[] for _ in range(N)] # List of empty lists
@ -51,42 +50,161 @@ def stats_NFBP(R, N):
for n in range(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):
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...
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.
"""
print("Running {} NFBP simulations with {} packages".format(R, N))
Hmean=0
Var=[]
H=[]
Exp=0
P = R * N # Total number of items
print("## Running {} NFBP simulations with {} items".format(R, N))
# number of bins
ISum = 0
IVarianceSum = 0
# 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)]
TSumVariance = [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"]
IVarianceSum += sim["i"]**2
IVarianceSum += sim["i"] ** 2
for n in range(N):
HSum[n] += sim["H"][n]
HSumVariance[n] += sim["H"][n]**2
HSumVariance[n] += sim["H"][n] ** 2
T = sim["T"]
V = sim["V"]
# ensure that T, V have the same length as Sum_T, Sum_V
for i in range(N - sim["i"]):
T.append(0)
V.append(0)
Sum_T = [x + y for x, y in zip(Sum_T, T)]
TSumVariance = [x + y**2 for x, y in zip(TSumVariance, T)]
Sum_V = [x + y for x, y in zip(Sum_V, V)]
I = ISum/R
IVariance = sqrt(IVarianceSum/(R-1) - I**2)
Sum_T = [x / R for x in Sum_T]
print(min(Sum_T[0:20]))
print(mean(Sum_T[0:35]))
print(Sum_T[0])
TVariance = sqrt(TSumVariance[0] / (R - 1) - Sum_T[0]**2) # Variance
print(TVariance)
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))
Sum_V = [round(x / R, 2) for x in 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):
Hn = HSum[n] / R # moyenne
HVariance = sqrt(HSumVariance[n] / (R - 1) - Hn**2) # Variance
Var.append(HVariance)
H.append(Hn)
print(
"Index of bin containing the {}th item (H_{}) : {} (variance {})".format(
n, n, Hn, HVariance
)
)
print(HSum)
print(len(HSum))
for x in range(len(HSum)):
Hmean+=HSum[x]
Hmean=Hmean/P
print("Hmean is : {}".format(Hmean))
Exp=np.exp(1)
HSum = [x / R for x in HSum]
HSumVariance = [x / R for x in HSumVariance]
print(HSumVariance)
# Plotting
fig = plt.figure()
# T plot
x = np.arange(N)
# 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,N/10), ylim=(0, 3), yticks=np.linspace(0, 3, 4)
)
ax.set_ylabel("Items")
ax.set_xlabel("Bins (1-{})".format(N))
ax.set_title("T histogram for {} items (Number of items in each bin)".format(P))
ax.legend(loc="upper left", title="Legend")
# V plot
bx = fig.add_subplot(222)
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,N/10), ylim=(0, 1), yticks=np.linspace(0, 1, 10)
)
bx.set_ylabel("First item size")
bx.set_xlabel("Bins (1-{})".format(N))
bx.set_title("V histogram for {} items (first item size of each bin)".format(P))
bx.legend(loc="upper left", title="Legend")
# H plot
# We will simulate this part for a asymptotic study
cx = fig.add_subplot(223)
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,N/10), ylim=(0, 10), yticks=np.linspace(0, N, 5)
)
cx.set_ylabel("Bin ranking of n-item")
cx.set_xlabel("n-item (1-{})".format(N))
cx.set_title("H histogram for {} items".format(P))
xb = linspace(0, N, 10)
xc=linspace(0,N,50)
yb = [Hmean for n in range(N)]
db =(( HSum[30] - HSum[1])/30)*xc
wb =(( HSumVariance[30] - HSumVariance[1])/30)*xc
cx.plot(xc, yb, label="Experimental Hn_Mean", color="brown")
cx.plot(xc, H, label="Experimental E(Hn)", color="red")
cx.plot(xc, Var, label="Experimental V(Hn)", color="purple")
cx.legend(loc="upper left", title="Legend")
plt.show()
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 items of random size.
Next Fit Dual Bin Packing : bins should overflow
"""
i = 0 # Nombre de boites
R = [0] # Remplissage de la i-eme boite
@ -95,83 +213,215 @@ def simulate_NFDBP(N):
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:
if R[i] >= 1:
# 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 a 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)
R[i] += size
T[i] += 1
return {
"i": i,
"R": R,
"T": T,
"V": V,
"H": H
}
return {"i": i, "R": R, "T": T, "V": V, "H": H}
def stats_NFDBP(R, N):
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))
# TODO comment this function
T1=[]
P = N * R # Total number of items
I = []
H = [[] for _ in range(N)] # List of empty lists
Tmean=[]
T = []
Tk = [[] for _ in range(N)]
Ti = []
T_maths = []
# First iteration to use zip after
sim = simulate_NFDBP(N)
Sum_T = [0 for _ in range(N)]
for i in range(R):
sim = simulate_NFDBP(N)
I.append(sim["i"])
for k in range(N):
T.append(0)
T = sim["T"]
T1.append(sim["T"][0])
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)))
Tk[n].append(sim["T"][n])
Ti.append(sim["T"])
Sum_T = [x + y for x, y in zip(Sum_T, T)]
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
T1=[x/100 for x in T1]
print("Mean number of bins : {} (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])))
# TODO variance for T_k doesn't see right
print("Mean T_{} : {} (variance {})".format(k, mean(Sum_T), variance(Sum_T)))
# Loi math
for u in range(N):
u = u + 2
T_maths.append(1 / (factorial(u - 1)) - 1 / factorial(u))
E = 0
sigma2 = 0
# print(T_maths)
T_maths = [x * 100 for x in 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
fig = plt.figure()
# T plot
x = np.arange(N)
print(x)
print(Sum_T)
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, 20), yticks=np.linspace(0, 20, 2)
)
ax.set_ylabel("Items(n) in %")
ax.set_xlabel("Bins (1-{})".format(N))
ax.set_title(
"Items percentage for each bin and {} items (Number of items in each bin)".format(
P
)
)
ax.legend(loc="upper right", title="Legend")
N = 10 ** 1
sim = simulate_NFBP(N)
# TODO fix the graph below
# Mathematical P(Ti=k) plot. It shows the Ti(t_i) law with the probability of each number of items.
print(len(Tk[t_i]))
bx = fig.add_subplot(222)
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_ylabel("P(T{}=i)".format(t_i))
bx.set_xlabel("Bins i=(1-{}) in %".format(N))
bx.set_title(
"T{} histogram for {} items (Number of items in each bin)".format(t_i, P)
)
bx.legend(loc="upper right", title="Legend")
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]))
# Loi mathematique
print("ici")
print(T_maths)
cx = fig.add_subplot(223)
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_ylabel("P(T{}=i)".format(t_i))
cx.set_xlabel("Bins i=(1-{})".format(N))
cx.set_title("Theoretical T{} values in %".format(t_i))
cx.legend(loc="upper right", title="Legend")
dx = fig.add_subplot(224)
dx.hist(
T1,
bins=10,
width=1,
label="Empirical values",
edgecolor="blue",
linewidth=0.7,
color="black",
)
dx.set(
xlim=(0, 10),
xticks=np.arange(0, 10,1),
ylim=(0, 100),
yticks=np.linspace(0, 100, 10),
)
dx.set_ylabel("Number of items in T1 for {} iterations")
dx.set_xlabel("{} iterations for T{}".format(R,1))
dx.set_title(
"T{} items repartition {} items (Number of items in each bin)".format(1, P)
)
dx.legend(loc="upper right", title="Legend")
print()
stats_NFBP(10 ** 4, 10)
plt.show()
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)
# unused
def basic_demo():
N = 10**1
sim = simulate_NFBP(N)
#
# 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')
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 a {} % 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 a {} % avec {} paquets. Taille du premier paquet : {}".format(
j, remplissage, sim["T"][j], sim["V"][j]
)
)
stats_NFBP_iter(10**5, 50)
print("\n\n")
stats_NFDBP(10**3, 10, 1)
print("Don't run code you don't understand or trust without a sandbox")

View file

@ -1,2 +1,38 @@
# Projet_Boites
TODO LIST:
Rapport: 15 pages max
Explication du projet et des 2 modeles
Exemples d'applications(cf Internet ou description du cours)
Analyse des liens entre experiences et lois
Programme
Pour les differentes variable, faire plusieurs essais avec des experiences variables
-NFBP : Lois mathematiques + representations pour T, V et H
Pour Hn comparaison historigramme et densite de la loi normale pour plusieurs valeurs de n
-NFDBP: Terminer programme sur T et recreer les lois maths (mettre le calcul pour obtenir explicitement la loi des Ti. Comparer simulations et theorie
Oral: 15 min de présentation - 5 min de questions
-Reflechir aux questions:
trouver un argument solide sur pk on a choisi ce projet
## Compiling report
Install the necessary tools with `sudo apt-get install texlive-full`.
Navigate to `latex` directory and then run `pdflatex main.tex`. The output pdf
should be at `main.pdf`. You may want to run `biber main` first, to compile the
bibliography.

View file

@ -0,0 +1,41 @@
# importing the memory tracking module
import tracemalloc
from random import random
from math import floor, sqrt
#from statistics import mean, variance
from time import perf_counter
# starting the monitoring
tracemalloc.start()
start_time = perf_counter()
# store memory consumption before
current_before, peak_before = tracemalloc.get_traced_memory()
N = 10**6
Tot = 0
Tot2 = 0
for _ in range(N):
item = random()
Tot += item
Tot2 += item ** 2
mean = Tot / N
variance = Tot2 / (N-1) - mean**2
# store memory after
current_after, peak_after = tracemalloc.get_traced_memory()
end_time = perf_counter()
print("mean :", mean)
print("variance :", variance)
# displaying the memory usage
print("Used memory before : {} B (current), {} B (peak)".format(current_before,peak_before))
print("Used memory after : {} B (current), {} B (peak)".format(current_after,peak_after))
print("Used memory : {} B".format(peak_after - current_before))
print("Time : {} ms".format((end_time - start_time) * 1000))
# stopping the library
tracemalloc.stop()

View file

@ -0,0 +1,36 @@
# importing the memory tracking module
import tracemalloc
from random import random
from math import floor, sqrt
from statistics import mean, variance
from time import perf_counter
# starting the monitoring
tracemalloc.start()
start_time = perf_counter()
# store memory consumption before
current_before, peak_before = tracemalloc.get_traced_memory()
N = 10**6
values = [random() for _ in range(N)]
mean = mean(values)
variance = variance(values)
# store memory after
current_after, peak_after = tracemalloc.get_traced_memory()
end_time = perf_counter()
print("mean :", mean)
print("variance :", variance)
# displaying the memory usage
print("Used memory before : {} B (current), {} B (peak)".format(current_before,peak_before))
print("Used memory after : {} B (current), {} B (peak)".format(current_after,peak_after))
print("Used memory : {} B".format(peak_after - current_before))
print("Time : {} ms".format((end_time - start_time) * 1000))
# stopping the library
tracemalloc.stop()

View file

@ -0,0 +1,42 @@
\newcommand\tab[1][0.6cm]{\hspace*{#1}} %Create and define tab
\definecolor{lightgray}{gray}{0.85}
\definecolor{lightgrey}{gray}{0.85}
\definecolor{vlg}{gray}{0.85}
%Patch pour utiliser des équations dans les titres sans que hypperref nous insulte.
% Définition cyclique, compile pas. Mais c'est l"idée
%\renewcommand{\chapter}[1]{\chapter{\texorpdfstring{#1}}}
%\renewcommand{\section}[1]{\section{\texorpdfstring{#1}}}
%\renewcommand{\subsection}[1]{\subsection{\texorpdfstring{#1}}}
%\renewcommand{\subsubsection}[1]{\subsubsection{\texorpdfstring{#1}}}
%Chapter No Numbering but appears in TOC
\newcommand{\chapternn}[1]{\chapter*{#1}\addcontentsline{toc}{chapter}{#1}}
\newcommand{\sectionnn}[1]{\phantomsection\section*{#1}\addcontentsline{toc}{section}{#1}}
% phantomsection is necessary for links in TOC to function. It places the anchor
\newcommand{\subsectionnn}[1]{\subsection*{#1}\addcontentsline{toc}{subsection}{#1}}
\newcommand{\subsubsectionnn}[1]{\subsubsection*{#1}\addcontentsline{toc}{subsubsection}{#1}}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash\hspace{0pt}}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash\hspace{0pt}}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash\hspace{0pt}}p{#1}}
\renewcommand\thesection{\arabic{section}}
\renewcommand\thesubsection{\thesection.\arabic{subsection}}
%------- Do not append new commands after :
\hypersetup{
colorlinks=false, % colorise les liens
linkbordercolor={1 1 1},
breaklinks=true, % permet le retour à la ligne dans les liens trop longs
urlcolor=blue, % couleur des hyperliens
linkcolor=black, % couleur des liens internes
citecolor=black, % couleur des références
pdftitle={}, % informations apparaissant dans
pdfauthor={}, % les informations du document
pdfsubject={} % sous Acrobat.
}

View file

@ -0,0 +1,13 @@
% \tikzset{every picture/.style={execute at begin picture={
% \shorthandoff{:;!?};}
% }}
\tikzset{
boxnode/.style={ % requires library shapes.misc
draw,
rectangle,
text centered,
align=center,
fill=gray!5!white
},
}

View file

@ -0,0 +1,57 @@
For simplicity, we only include the script for the improved algorithm. For the
intuitive algorithm, simply replace the algorithm. The imports, timing and memory
usage tracking code are nearly identical.
\begin{lstlisting}[language=python]
#!/usr/bin/python3
import tracemalloc
from random import random
from math import floor, sqrt
#from statistics import mean, variance
from time import perf_counter
# starting the memory monitoring
tracemalloc.start()
start_time = perf_counter()
# store memory consumption before
current_before, peak_before = tracemalloc.get_traced_memory()
# algorithm (part to replace)
N = 10**6
Tot = 0
Tot2 = 0
for _ in range(N):
item = random()
Tot += item
Tot2 += item ** 2
mean = Tot / N
variance = Tot2 / (N-1) - mean**2
# store memory after
current_after, peak_after = tracemalloc.get_traced_memory()
end_time = perf_counter()
print("mean :", mean)
print("variance :", variance)
# displaying the memory usage
print("Used memory before : {} B (current), {} B (peak)".format(current_before,peak_before))
print("Used memory after : {} B (current), {} B (peak)".format(current_after,peak_after))
print("Used memory : {} B".format(peak_after - current_before))
print("Time : {} ms".format((end_time - start_time) * 1000))
tracemalloc.stop()
\end{lstlisting}
Example output:
\begin{lstlisting}[language=python]
mean : 0.5002592040785124
variance : 0.0833757719902084
Used memory before : 0 B (current), 0 B (peak)
Used memory after : 1308 B (current), 1336 B (peak)
Used memory : 1336 B
Time : 535.1873079998768 ms
\end{lstlisting}

View file

@ -0,0 +1,5 @@
Script should have been provided with report. % TODO
\lstinputlisting[language=Python]{../Probas.py}
% TODO include output example

18
latex/bibliography.bib Normal file
View file

@ -0,0 +1,18 @@
@book{hofri:1987,
author = {Hofri, M.},
title = {Probabilistic Analysis of Algorithms: On Computing Methodologies for
Computer Algorithms Performance Evaluation},
year = {1987},
isbn = {0387965785},
publisher = {Springer-Verlag},
address = {Berlin, Heidelberg},
}
@misc{bin-packing-approximation:2022,
author = {{Computational Thinking}},
title = {Bin Packing Approximation},
year = {2022},
howpublished = {YouTube video},
url = {https://www.youtube.com/watch?v=R76aAh_li50},
}

3
latex/clean.sh Normal file
View file

@ -0,0 +1,3 @@
rm main.stc* main.toc main.bcf *.aux *.out main.mtc*
rm main.maf main.run.xml
rm main.log

589
latex/content.tex Normal file
View file

@ -0,0 +1,589 @@
\sectionnn{Introduction}
Bin packing is the process of packing a set of items of different sizes into
containers of a fixed capacity in a way that minimizes the number of containers
used. This has applications in many fields, such as logistics, where we want to
optimize the storage and transport of items in boxes, containers, trucks, etc.
Building mathematical models for bin packing is useful in understanding the
problem and in designing better algorithms, depending on the use case. An
algorithm optimized for packing cubes into boxes will not perform as well as
another algorithm for packing long items into trucks. Studying the mathematics
behind algorithms provides us with a better understanding of what works best.
When operating at scale, every small detail can have a huge impact on overall
efficiency and cost. Therefore, carefully developing algorithms based on solid
mathematical models is crucial. As we have seen in our Automatics class, a
small logic breach can be an issue in the long run in systems that are supposed
to run autonomously. This situation can be avoided by using mathematical models
during the design process wich will lead to better choices welding economic and
relibility concerns.
We will conduct a probabilistic analysis of multiple algorithms and compare
results to theoretical values. We will also consider the algoriths complexity
and performance, both in resource consumption and in box usage.
\clearpage
\section{Bin packing use cases}
Before studying the mathematics behind bin packing algorithms, we will have a
look at the motivations behind this project.
Bin packing has applications in many fields and allows to automize and optimize
complex systems. We will illustrate with examples focusing on two use cases:
logistics and computer science. We will consider examples of multiple dimensions
to show the versatility of bin packing algorithms.
\paragraph{} In the modern day, an effective supply chain relies on an automated production
thanks to sensors and actuators installed along conveyor belts. It is often
required to implement a packing procedure. All of this is controlled by a
computer system running continuously.
\subsection{3D : Containers}
Storing items in containers can be a prime application of bin packing. These
tree-dimensional objects of standardized size are used to transport goods.
While the dimensions of the containers are predictable, those of the transported
items are not. Storage is furthermore complicated by the fact that there can be
a void between items, allowing to move around. Multiple types of items can also
be stored in the same container.
There are many ways to optimize the storage of items in containers. For
example, by ensuring items are of an optimal standardized size or by storing a
specific item in each container, both eliminating the randomness in item size.
In these settings, it is easy to fill a container by assimilating them to
rectangular blocks. However, when items come in pseudo-random dimensions, it is
intuitive to start filling the container with larger items and then filling the
remaining gaps with smaller items. As containers must be closed, in the event
of an overflow, the remaining items must be stored in another container.
\subsection{2D : Cutting stock problem}
In industries such as woodworking bin packing algorithms are utilized to
minimize material waste when cutting large planks into smaller pieces of
desired sizes. Many tools use this two-dimensional cut process. For example, at
the Fabric'INSA Fablab, the milling machine, laser cutter and many more are
used to cut large planks of wood into smaller pieces for student projects. In
this scenario, we try to organize the desired cuts in a way that minimizes the
unusable excess wood.
\begin{figure}[ht]
\centering
\includegraphics[width=0.65\linewidth]{graphics/fraiseuse.jpg}
\caption[]{Milling machine at the Fabric'INSA Fablab \footnotemark}
\label{fig:fraiseuse}
\end{figure}
\footnotetext{Photo courtesy of Inés Bafaluy}
Managing the placement of items of complex shapes can be optimized by using
by various algorithms minimizing the waste of material.
\subsection{1D : Networking}
When managing network traffic at scale, efficiently routing packets is
necessary to avoid congestion, which leads to lower bandwidth and higher
latency. Say you're a internet service provider and your users are watching
videos on popular streaming platforms. You want to ensure that the traffic is
balanced between the different routes to minimize throttling and energy
consumption.
\paragraph{} We can consider the different routes as bins and the users'
bandwidth as the items. If a bin overflows, we can redirect the traffic to
another route. Using less bins means less energy consumption and decreased
operating costs. This is a good example of bin packing in a dynamic
environment, where the items are constantly changing. Humans are not involved
in the process, as it is fast-paced and requires a high level of automation.
\vspace{0.4cm}
\paragraph{} We have seen multiple examples of how bin packing algorithms can
be used in various technical fields. In these examples, a choice was made,
evaluating the process effectiveness and reliability, based on a probabilistic
analysis allowing the adaptation of the algorithm to the use case. We will now
conduct our own analysis and study various algorithms and their probabilistic
advantages, focusing on one-dimensional bin packing, where we try to store
items of different heights in a linear bin.
\section{Next Fit Bin Packing algorithm (NFBP)}
Our goal is to study the number of bins $ H_n $ required to store $ n $ items
for each algorithm. We first consider the Next Fit Bin Packing algorithm, where
we store each item in the current bin if it fits, otherwise we open a new bin.
\begin{figure}[h]
\centering
\begin{tikzpicture}[scale=0.8]
% Bins
\draw[thick] (0,0) rectangle (2,6);
\draw[thick] (3,0) rectangle (5,6);
\draw[thick] (6,0) rectangle (8,6);
% Items
\draw[fill=red] (0.5,0.5) rectangle (1.5,3.25);
\draw[fill=blue] (0.5,3.5) rectangle (1.5,5.5);
\draw[fill=green] (3.5,0.5) rectangle (4.5,1.5);
\draw[fill=orange] (3.5,1.75) rectangle (4.5,3.75);
\draw[fill=purple] (6.5,0.5) rectangle (7.5,2.75);
\draw[fill=yellow] (6.5,3) rectangle (7.5,4);
% arrow
\draw[->, thick] (8.6,3.5) -- (7.0,3.5);
\draw[->, thick] (8.6,1.725) -- (7.0,1.725);
% Labels
\node at (1,-0.75) {Bin 0};
\node at (4,-0.75) {Bin 1};
\node at (7,-0.75) {Bin 2};
\node at (10.0,3.5) {Yellow item};
\node at (10.0,1.725) {Purple item};
\end{tikzpicture}
\label{fig:nfbp}
\caption{Next Fit Bin Packing example}
\end{figure}
\paragraph{} The example in figure \ref{fig:nfbp} shows the limitations of the
NFBP algorithm. The yellow item is stored in bin 2, while it could fit in bin
1, because the purple item is considered first and is too large to fit.
\paragraph{} Each bin will have a fixed capacity of $ 1 $ and items
will be of random sizes between $ 0 $ and $ 1 $.
\subsection{Variables used in models}
We use the following variables in our algorithms and models :
\begin{itemize}
\item $ U_n $ : the size of the $ n $-th item. $ (U_n)_{n \in \mathbb{N^*}} $
denotes the mathematical sequence of random variables of uniform
distribution on $ [0, 1] $ representing the items' sizes.
\item $ T_i $ : the number of items in the $ i $-th bin.
\item $ V_i $ : the size of the first item in the $ i $-th bin.
\item $ H_n $ : the number of bins required to store $ n $ items.
\end{itemize}
Mathematically, the NFBP algorithm imposes the following constraint on the first box :
\begin{align*}
T_1 = k \iff & U_1 + U_2 + \ldots + U_{k} < 1 \\
\text{ and } & U_1 + U_2 + \ldots + U_{k+1} \geq 1 \qquad \text{ with } k \geq 1 \\
\end{align*}
\subsection{Implementation and results}
We implemented the NFBP algorithm in Python \footnotemark, for its ease of use
and broad recommendation. We used the \texttt{random} library to generate
random numbers between $ 0 $ and $ 1 $ and \texttt{matplotlib} to plot the
results in the form of histograms.
\footnotetext{The code is available in Annex \ref{annex:probabilistic}}
We will try to approximate $ \mathbb{E}[R] $ and $ \mathbb{E}[V] $ with $
\overline{X_N} $ using $ {S_n}^2 $. This operation will be done for both $ R =
2 $ and $ R = 10^6 $ simulations.
\[
\overline{X_N} = \frac{1}{N} \sum_{i=1}^{N} X_i
\]
As the variance value is unknown, we will use $ {S_n}^2 $ to estimate the
variance and further determine the Confidence Interval (95 \% certainty).
\begin{align*}
{S_N}^2 & = \frac{1}{N-1} \sum_{i=1}^{N} (X_i - \overline{X_N})^2 \\
IC_{95\%}(m) & = \left[ \overline{X_N} \pm \frac{S_N}{\sqrt{N}} \cdot t_{1 - \frac{\alpha}{2}, N-1} \right] \\
\end{align*}
\paragraph{2 simulations} We first ran $ R = 2 $ simulations to observe the
behavior of the algorithm and the low precision of the results.
\begin{figure}[h]
\centering
\includegraphics[width=0.8\textwidth]{graphics/graphic-NFBP-Ti-2-sim}
\caption{Histogram of $ T_i $ for $ R = 2 $ simulations and $ N = 50 $ items (number of items per bin)}
\label{fig:graphic-NFBP-Ti-2-sim}
\end{figure}
On this graph (figure \ref{fig:graphic-NFBP-Ti-2-sim}), we can see each value
of $ T_i $. Our calculations have yielded that $ \overline{T_1} = 1.0 $ and $
{S_N}^2 = 2.7 $. Our Student coefficient is $ t_{0.95, 2} = 4.303 $.
We can now calculate the Confidence Interval for $ T_1 $ for $ R = 2 $ simulations :
\begin{align*}
IC_{95\%}(T_1) & = \left[ 1.0 \pm 1.96 \frac{\sqrt{2.7}}{\sqrt{2}} \cdot 4.303 \right] \\
& = \left[ 1 \pm 9.8 \right] \\
\end{align*}
We can see that the Confidence Interval is very large, which is due to the low
number of simulations. Looking at figure \ref{fig:graphic-NFBP-Ti-2-sim}, we
easily notice the high variance.
\begin{figure}[h]
\centering
\includegraphics[width=0.8\textwidth]{graphics/graphic-NFBP-Vi-2-sim}
\caption{Histogram of $ V_i $ for $ R = 2 $ simulations and $ N = 50 $ items (size of the first item in a bin)}
\label{fig:graphic-NFBP-Vi-2-sim}
\end{figure}
On the graph of $ V_i $ (figure \ref{fig:graphic-NFBP-Vi-2-sim}), we can see
that the sizes are scattered pseudo-randomly between $ 0 $ and $ 1 $, which is
unsuprising given the low number of simulations. The process determinig the statistics
is the same as for $ T_i $, yielding $ \overline{V_1} = 0.897 $, $ {S_N}^2 =
0.2 $ and $ IC_{95\%}(V_1) = \left[ 0.897 \pm 1.3 \right] $. In this particular run,
the two values for $ V_1 $ are high (being bouded between $ 0 $ and $ 1 $).
\paragraph{100 000 simulations} In order to ensure better precision, we then
ran $ R = 10^5 $ simulations with $ N = 50 $ different items each.
\begin{figure}[h]
\centering
\includegraphics[width=0.8\textwidth]{graphics/graphic-NFBP-Ti-105-sim}
\caption{Histogram of $ T_i $ for $ R = 10^5 $ simulations and $ N = 50 $ items (number of items per bin)}
\label{fig:graphic-NFBP-Ti-105-sim}
\end{figure}
On this graph (figure \ref{fig:graphic-NFBP-Ti-2-sim}), we can see each value
of $ T_i $. Our calculations have yielded that $ \overline{T_1} = 1.72 $ and $
{S_N}^2 = 0.88 $. Our Student coefficient is $ t_{0.95, 2} = 2 $.
We can now calculate the Confidence Interval for $ T_1 $ for $ R = 10^5 $ simulations :
\begin{align*}
IC_{95\%}(T_1) & = \left[ 1.72 \pm 1.96 \frac{\sqrt{0.88}}{\sqrt{10^5}} \cdot 2 \right] \\
& = \left[ 172 \pm 0.012 \right] \\
\end{align*}
We can see that the Confidence Interval is very small, thanks to the large number of iterations.
This results in a steady curve in figure \ref{fig:graphic-NFBP-Ti-105-sim}.
\begin{figure}[h]
\centering
\includegraphics[width=0.8\textwidth]{graphics/graphic-NFBP-Vi-105-sim}
\caption{Histogram of $ V_i $ for $ R = 10^5 $ simulations and $ N = 50 $ items (size of the first item in a bin)}
\label{fig:graphic-NFBP-Vi-105-sim}
\end{figure}
\begin{figure}[h]
\centering
\includegraphics[width=0.8\textwidth]{graphics/graphic-NFBP-Hn-105-sim}
\caption{Histogram of $ H_n $ for $ R = 10^5 $ simulations and $ N = 50 $ items (number of bins required to store $n$ items)}
\label{fig:graphic-NFBP-Hn-105-sim}
\end{figure}
\paragraph{Asymptotic behavior of $ H_n $} Finally, we analyzed how many bins
were needed to store $ n $ items. We used the numbers from the $ R = 10^5 $ simulations.
We can see in figure \ref{fig:graphic-NFBP-Hn-105-sim} that $ H_n $ is
asymptotically linear. The expected value and the variance are also displayed.
The variance also increases linearly.
\paragraph{} The Next Fit Bin Packing algorithm is a very simple algorithm
with predictable results. It is very fast, but it is not optimal.
\section{Next Fit Dual Bin Packing algorithm (NFDBP)}
Next Fit Dual Bin Packing is a variation of NFBP in which we allow the bins to
overflow. A bin must be fully filled, unless it is the last bin.
\begin{figure}[h]
\centering
\begin{tikzpicture}[scale=0.8]
% Bins
\draw[thick] (0,0) rectangle (2,6);
\draw[thick] (3,0) rectangle (5,6);
% Transparent Tops
\fill[white,opacity=1.0] (0,5.9) rectangle (2,6.5);
\fill[white,opacity=1.0] (3,5.9) rectangle (5,6.5);
% Items
\draw[fill=red] (0.5,0.5) rectangle (1.5,3.25);
\draw[fill=blue] (0.5,3.5) rectangle (1.5,5.5);
\draw[fill=green] (0.5,5.75) rectangle (1.5,6.75);
\draw[fill=orange] (3.5,0.5) rectangle (4.5,2.5);
\draw[fill=purple] (3.5,2.75) rectangle (4.5,5.0);
\draw[fill=yellow] (3.5,5.25) rectangle (4.5,6.25);
% Labels
\node at (1,-0.75) {Bin 0};
\node at (4,-0.75) {Bin 1};
\end{tikzpicture}
\caption{Next Fit Dual Bin Packing example}
\label{fig:nfdbp}
\end{figure}
\paragraph{} The example in figure \ref{fig:nfdbp} shows how NFDBP utilizes
less bins than NFBP, due to less stringent constraints. The top of the bin is
effectively removed, allowing for an extra item to be stored in the bin. We can
easily see how with NFDBP each bin can at least contain two items.
\paragraph{} The variables used are the same as for NFBP. Mathematically, the
new constraints on the first bin can be expressed as follows :
\begin{align*}
T_1 = k \iff & U_1 + U_2 + \ldots + U_{k-1} < 1 \\
\text{ and } & U_1 + U_2 + \ldots + U_{k} \geq 1 \qquad \text{ with } k \geq 2 \\
\end{align*}
\subsection{Building a mathematical model}
In this section we will try to determine the probabilistic law followed by $ T_i $.
Let $ k \geq 2 $. Let $ (U_n)_{n \in \mathbb{N}^*} $ be a sequence of
independent random variables with uniform distribution on $ [0, 1] $, representing
the size of the $ n $-th item.
Let $ i \in \mathbb{N} $. $ T_i $ denotes the number of items in the $ i $-th
bin. We have that
\begin{equation*}
T_i = k \iff U_1 + U_2 + \ldots + U_{k-1} < 1 \text{ and } U_1 + U_2 + \ldots + U_{k} \geq 1
\end{equation*}
Let $ A_k = \{ U_1 + U_2 + \ldots + U_{k} < 1 \}$. Hence,
\begin{align*}
\label{eq:prob}
P(T_i = k)
& = P(A_{k-1} \cap A_k^c) \\
& = P(A_{k-1}) - P(A_k) \qquad \text{ (as $ A_k \subset A_{k-1} $)} \\
\end{align*}
We will try to show that $ \forall k \geq 1 $, $ P(A_k) = \frac{1}{k!} $. To do
so, we will use induction to prove the following proposition \eqref{eq:induction},
$ \forall k \geq 1 $:
\begin{equation}
\label{eq:induction}
\tag{$ \mathcal{H}_k $}
P(U_1 + U_2 + \ldots + U_{k} < a) = \frac{a^k}{k!} \qquad \forall a \in [0, 1],
\end{equation}
Let us denote $ S_k = U_1 + U_2 + \ldots + U_{k} \qquad \forall k \geq 1 $.
\paragraph{Base case} $ k = 1 $ : $ P(U_1 < a) = a = \frac{a^1}{1!}$, proving $ (\mathcal{H}_1) $.
\paragraph{Induction step} Let $ k \geq 2 $. We assume $ (\mathcal{H}_{k-1}) $ is
true. We will show that $ (\mathcal{H}_{k}) $ is true.
\begin{align*}
P(S_k < a) & = P(S_{k-1} + U_k < a) \\
& = \iint_{\cal{D}} f_{S_{k-1}, U_k}(x, y) dxdy \\
\text{Where } \mathcal{D} & = \{ (x, y) \in [0, 1]^2 \mid x + y < a \} \\
& = \{ (x, y) \in [0, 1]^2 \mid 0 < x < a \text{ and } 0 < y < a - x \} \\
P(S_k < a) & = \iint_{\cal{D}} f_{S_{k-1}}(x) \cdot f_{U_k}(y) dxdy \qquad
\text{because $ S_{k-1} $ and $ U_k $ are independent} \\
& = \int_{0}^{a} f_{S_{k-1}}(x) \cdot \left( \int_{0}^{a-x} f_{U_k}(y) dy \right) dx \\
\end{align*}
$ (\mathcal{H}_{k-1}) $ gives us that $ \forall x \in [0, 1] $,
$ F_{S_{k-1}}(x) = P(S_{k-1} < x) = \frac{x^{k-1}}{(k-1)!} $.
By differentiating, we get that $ \forall x \in [0, 1] $,
\[
f_{S_{k-1}}(x) = F'_{S_{k-1}}(x) = \frac{x^{k-2}}{(k-2)!}
\]
Furthermore, $ U_{k-1} $ is uniformly distributed on $ [0, 1] $, so
$ f_{U_{k-1}}(y) = 1 $. We can then integrate by parts :
\begin{align*}
P(S_k < a)
& = \int_{0}^{a} f_{S_{k-1}}(x) \cdot \left( \int_{0}^{a-x} 1 dy \right) dx \\
& = \int_{0}^{a} f_{S_{k-1}}(x) \cdot (a - x) dx \\
& = a \int_{0}^{a} f_{S_{k-1}}(x) dx - \int_{0}^{a} x f_{S_{k-1}}(x) dx \\
& = a \int_0^a F'_{S_{k-1}}(x) dx - \left[ x F_{S_{k-1}}(x) \right]_0^a
+ \int_{0}^{a} F_{S_{k-1}}(x) dx \qquad \text{(IPP : }x, F_{S_{k-1}} \in C^1([0,1]) \\
& = a \left[ F_{S_{k-1}}(x) \right]_0^a - \left[ x F_{S_{k-1}}(x) \right]_0^a
+ \int_{0}^{a} \frac{x^{k-1}}{(k-1)!} dx \\
& = \left[ \frac{x^k}{k!} \right]_0^a \\
& = \frac{a^k}{k!} \\
\end{align*}
\paragraph{Conclusion} We have shown that $ (\mathcal{H}_{k}) $ is true, so by induction, $ \forall k \geq 1 $,
$ \forall a \in [0, 1] $, $ P(U_1 + U_2 + \ldots + U_{k} < a) = \frac{a^k}{k!} $. Take
$ a = 1 $ to get
\[ P(U_1 + U_2 + \ldots + U_{k} < 1) = \frac{1}{k!} \]
Finally, plugging this into \eqref{eq:prob} gives us
\[
P(T_i = k) = P(A_{k-1}) - P(A_{k}) = \frac{1}{(k-1)!} - \frac{1}{k!} \qquad \forall k \geq 2
\]
\subsection{Empirical results}
We ran $ R = 10^3 $ simulations for $ N = 10 $ items. The empirical results are
similar to the mathematical model.
\begin{figure}[h]
\centering
\includegraphics[width=1.0\textwidth]{graphics/graphic-NFDBP-T1-103-sim}
\caption{Therotical and empiric histograms of $ T_1 $ for $ R = 10^3 $ simulations and $ N = 10 $ items (number of itens in the first bin)}
\label{fig:graphic-NFDBP-T1-103-sim}
\end{figure}
\subsection{Expected value of $ T_i $}
We now compute the expected value $ \mu $ and variance $ \sigma^2 $ of $ T_i $.
\begin{align*}
\mu = E(T_i) & = \sum_{k=2}^{\infty} k \cdot P(T_i = k) \\
& = \sum_{k=2}^{\infty} (\frac{k}{(k-1)!} - \frac{1}{(k-1)!}) \\
& = \sum_{k=2}^{\infty} \frac{k-1}{(k-1)!} \\
& = \sum_{k=0}^{\infty} \frac{1}{k!} \\
& = e \\
\end{align*}
\begin{align*}
E({T_i}^2) & = \sum_{k=2}^{\infty} k^2 \cdot P(T_i = k) \\
& = \sum_{k=2}^{\infty} (\frac{k^2}{(k-1)!} - \frac{k}{(k-1)!}) \\
& = \sum_{k=2}^{\infty} \frac{(k-1)k}{(k-1)!} \\
& = \sum_{k=2}^{\infty} \frac{k}{(k-2)!} \\
& = \sum_{k=0}^{\infty} \frac{k+2}{k!} \\
& = \sum_{k=0}^{\infty} (\frac{1}{(k-1)!} + \frac{2}{(k)!}) \\
& = \sum_{k=0}^{\infty} \frac{1}{(k)!} - 1 + 2e \\
& = 3e - 1
\end{align*}
\begin{align*}
\sigma^2 = E({T_i}^2) - E(T_i)^2 = 3e - 1 - e^2
\end{align*}
$ H_n $ is asymptotically normal, following a $ \mathcal{N}(\frac{N}{\mu}, \frac{N \sigma^2}{\mu^3}) $
\section{Complexity and implementation optimization}
Both the NFBP and NFDBP algorithms have a linear complexity $ O(n) $, as we
only need to iterate over the items once. While the algorithms themselves are
linear, calculating the statistics may not not be. In this section, we will
discuss how to optimize the implementation of the statistical analysis.
\subsection{Performance optimization}
When implementing the statistical analysis, the intuitive way to do it is to
run $ R $ simulations, store the results, then conduct the analysis. However,
when running a large number of simulations, this can be very memory
consuming. We can optimize the process by computing the statistics on the fly,
by using sum formulae. This uses nearly constant memory, as we only need to
store the current sum and the current sum of squares for different variables.
While the mean can easily be calculated by summing then dividing, the empirical
variance can be calculated using the following formula:
\begin{align*}
{S_N}^2 & = \frac{1}{N-1} \sum_{i=1}^{N} (X_i - \overline{X})^2 \\
& = \frac{1}{N-1} \sum_{i=1}^{N} X_i^2 - \frac{N}{N-1} \overline{X}^2
\end{align*}
The sum $ \frac{1}{N-1} \sum_{i=1}^{N} X_i^2 $ can be calculated iteratively
after each simulation.
\subsection{Effective resource consumption}
We set out to study the resource consumption of the algorithms. We implemented
the above formulae to calculate the mean and variance of $ N = 10^6 $ random
numbers. We wrote the following algorithms \footnotemark :
\footnotetext{The full code used to measure performance can be found in Annex \ref{annex:performance}.}
\paragraph{Intuitive algorithm} Store values first, calculate later
\begin{lstlisting}[language=python]
N = 10**6
values = [random() for _ in range(N)]
mean = mean(values)
variance = variance(values)
\end{lstlisting}
Execution time : $ 4.8 $ seconds
Memory usage : $ 32 $ MB
\paragraph{Improved algorithm} Continuous calculation
\begin{lstlisting}[language=python]
N = 10**6
Tot = 0
Tot2 = 0
for _ in range(N):
item = random()
Tot += item
Tot2 += item ** 2
mean = Tot / N
variance = Tot2 / (N-1) - mean**2
\end{lstlisting}
Execution time : $ 530 $ milliseconds
Memory usage : $ 1.3 $ kB
\paragraph{Analysis} Memory usage is, as expected, much lower when calculating
the statistics on the fly. Furthermore, something we hadn't anticipated is the
execution time. The improved algorithm is nearly 10 times faster than the
intuitive one. This can be explained by the time taken to allocate memory and
then calculate the statistics (which iterates multiple times over the array).
\footnotemark
\footnotetext{Performance was measured on a single computer and will vary
between devices. Execution time and memory usage do not include the import of
libraries.}
\subsection{Optimal algorithm}
As we have seen, NFDBP algorithm is much better than NFBP algorithm. All the
variables excluding V are showing this. More specifically, the most relevant
variable is Hn which is growing slightly slower in the NFDBP algorithm than in
the NFBP algorithm.
Another algorithm that we did not explore in this project is the SUBP (Skim Up
Bin Packing) algorithm. It works in the same way as the NFDBP algorithm.
However, when an item exceeds the box size, it is removed from the current bin
and placed into the next bin. This algorithm that we could not exploit is much
more efficient than both of the previous algorithms. His main issue is that it
takes a lot of storage and requires higher capacities.
We redirect you towards this video which demonstrates why another algorithm is
actually the most efficient that we can imagine. In this video we see that the
mostoptimized of alrogithm is another version of NFBP where we sort the items
in a decreasing order before sending them into the different bins.
\clearpage
\sectionnn{Conclusion}
In this project, we explored many bin packing algorithms in 1 dimension. We
discovered how some bin packing algorithms can be really simple to implement
but also a strong data consumer as the NFBP algorithm.
By modifying the conditions of bin packing we can upgrade our performances. For
example, the NFDBP doest not permit to close the boxes (which depend of the
context of this implementation). The performance analysis conclusions are the
consequences of a precise statistical and probabilistic study that we have leaded
on this project.
To go further, we could now think about the best applications of different
algorithms in real contexts, thanks to simulations.
\nocite{bin-packing-approximation:2022}
\nocite{hofri:1987}

Binary file not shown.

30
latex/cover/cover_in.tex Normal file
View file

@ -0,0 +1,30 @@
\input{main_variables}
\pagenumbering{gobble}
% Couverture
\thispagestyle{empty}
\definecolor{insa_blue}{RGB}{52,83,111}
\definecolor{insa_red}{RGB}{230,39,20}
\noindent\begin{tikzpicture}[remember picture, overlay, shift={(current page.south west)}]
\draw[draw=none, fill=insa_red] (12.32,1.24) -- (12.32,4.94) -- (19.7,4.94) -- (19.7,1.24) -- cycle;
\node[anchor=north west, align=left, color=white, text width=12cm] at (2.3,23.5) {\varmaintitle};
\node[anchor=north west, align=left, color=white, text width=8cm] at (2.3,22.5) {\varmainsubtitle};
% \node[anchor=north west] at (2.3,21.5) {\includegraphics[scale=1.00]{\varlogo}};
\node[anchor=north west, align=left, color=white, text width=12cm] at (2.3,16) {\varcovertext};
%\node[anchor=center, align=center, text width=12cm] at (10,6.7) {\color{black} \varcovertext}; %-3, 4.1
\node[anchor=north west, align=left, text width=12cm] at (1.2,4.7) {\varinsaaddress};
% \node[anchor=north west, align=left, color=white, text width=12cm] at (12.5,4.7) {\varcompanyaddress};
\node[anchor=north west, align=left, text width=12cm] at (0.5,4) {\includepdf[pages={1}]{cover/charte_graphique}};
\end{tikzpicture}
\newpage
% % Page de garde
% \thispagestyle{empty}
% \noindent\begin{tikzpicture}[remember picture, overlay, shift={(current page.south west)}]
% \node[anchor=north west, align=left, text width=10cm] at (2.3,23.5) {\color{black} \varmaintitle};
% \node[anchor=north west, align=left, text width=8cm] at (2.3,22.5) {\varmainsubtitle};
% \node[anchor=north west, align=left, text width=12cm] at (2.3,16) {\color{black} \varcovertext};
% \node[anchor=north west, align=left, text width=12cm] at (1.2,4.7) {\varinsaaddress};
% \node[anchor=north west, align=left, text width=12cm] at (12.5,4.7) {\color{black} \varcompanyaddress};
% \end{tikzpicture}
% \newpage

21
latex/cover/cover_out.tex Normal file
View file

@ -0,0 +1,21 @@
\newpage
\pagenumbering{gobble}
\thispagestyle{empty}
\definecolor{insa_blue}{RGB}{52,83,111}
\definecolor{insa_red}{RGB}{226,50,46}
\noindent\begin{tikzpicture}[remember picture, overlay, shift={(current page.south west)}]
% \draw[draw=none, path fading=east, left color=insa_blue, right color=insa_blue!25!white] (21,7.3) -- (4.3,16.05) -- (21,24.8) -- cycle;
% \draw[draw=none, path fading=east, left color=insa_blue, right color=insa_blue!60!white] (0,16.2) -- (7,19.75) -- (0,23.1) -- cycle;
% \draw[draw=none, path fading=east, left color=insa_blue, right color=insa_blue!25!white] (0,17.3) -- (13.7,24.5) -- (2.3,29.7) -- (0,29.7) -- cycle;
%
%
% \draw[draw=none, fill=insa_red] (6.2,0) -- (6.2,1.2) -- (11.6,0) -- cycle;
%
% \draw[draw=none, fill=white] (0,0) -- (0,10) -- (10,10) -- (10,0) -- cycle;
% \node[anchor=south west, align=left] at (1.25,3.6) {\textbf{INSA Toulouse}};
% \node[anchor=south west, align=left] at (1.25,2.20) {135, Avenue de Rangueil \\ 31077 Toulouse Cedex 4 - France \\ \href{http://www.insa-toulouse.fr}{www.insa-toulouse.fr}};
%
% \node[anchor=south west] at (11.1,2.2) {\includegraphics[height=1cm, keepaspectratio]{cover/meta/univ.png}};
%\node[anchor=south west] at (13.4,2.2) {\includegraphics[height=1cm, keepaspectratio]{cover/meta/ministere.png}};
% \node[anchor=north west, align=left, text width=12cm] at (0.5,4) {\includepdf[pages={2}]{cover/charte_graphique}};
\end{tikzpicture}

View file

@ -0,0 +1,3 @@
\AtBeginDocument{\input{cover/cover_in.tex}\pagenumbering{arabic}}
%\AtEndDocument{\input{cover/cover_out.tex}}

Binary file not shown.

After

Width:  |  Height:  |  Size: 151 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

139
latex/main.tex Normal file
View file

@ -0,0 +1,139 @@
\documentclass[a4paper,11pt,twoside]{article}
%\pdfminorversion=7 % To use charte graphique (pdf 1.7)
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lscape}
\usepackage{boldline,multirow,tabularx,colortbl,diagbox,makecell,fancybox}
\usepackage{amsfonts,amssymb,amsmath,mathrsfs,array}
\usepackage{pgf,tikz,xcolor}
\usetikzlibrary{calc,positioning,shapes.geometric,shapes.symbols,shapes.misc, fit, shapes, arrows, arrows.meta,fadings,through}
\usepackage[top=2cm, bottom=2cm, left=4cm, right=4cm]{geometry}
\usepackage{hyperref}
\usepackage{titlesec}
\usepackage{eurosym}
\usepackage[english]{babel}
\usepackage{eso-pic} % for background on cover
\usepackage{listings}
\usepackage{tikz}
% Define colors for code
\definecolor{codegreen}{rgb}{0,0.4,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{mystyle}{
backgroundcolor=\color{backcolour},
commentstyle=\color{codegreen},
keywordstyle=\color{magenta},
numberstyle=\tiny\color{codegray},
stringstyle=\color{codepurple},
basicstyle=\ttfamily\small,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2
}
\lstset{style=mystyle}
% table des annexes
\usepackage{minitoc}
\usepackage{pdfpages}
\usepackage[style=iso-alphabetic]{biblatex}
\addbibresource{bibliography.bib}
\input{advanced.params/tikz.conf}
\input{advanced.params/misc.commands}
\input{cover/covermain.tex}
\date{\today}
\begin{document}
\dosecttoc{} % generate TOC
% % Remerciements
% \thispagestyle{empty} % removes page number
% \subsection*{Remerciements}
% \input{remerciements}
% \clearpage
% TABLE DES MATIÈRES
\thispagestyle{empty} % removes page number
\setcounter{secnumdepth}{3}
\tableofcontents
\clearpage
% Contenu
\setcounter{page}{1}
\include{content}
% BIBLIOGRAPHIE
\addcontentsline{toc}{section}{Bibliography}
\printbibliography[title={Bibliography}]
% TABLE DES ANNEXES
\clearpage
\appendix
% \thispagestyle{empty} % removes page number
\sectionnn{Table des Annexes}
% Désactivation de la table des matières
\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}
% Personnalisation de la table des annexes
\renewcommand{\stctitle}{} % Titre (issue with previous subsection showing up)
\renewcommand\thesubsection{A\arabic{subsection}} % Numérotation
\renewcommand{\stcSSfont}{} % Police normale, pas en gras
\mtcsetrules{secttoc}{off} % Désactivation des lignes en haut et en bas de la table
% Affichage de la table des annexes
\secttoc
% ANNEXES
\clearpage
\pagenumbering{Roman}
\subsection{Performance analysis script}
\label{annex:performance}
\input{annex-performance}
\clearpage
\subsection{Probabilistic analysis script}
\label{annex:probabilistic}
\input{annex-probabilistic}
\clearpage
% \includepdf[pages={1}, scale=0.96,
% pagecommand=\subsection{Questionnaire 1 : Sensibilisation à lHygiène et à la Sécurité}]
% {questionnaires}
% \clearpage
%
%
% \includepdf[pages={2}, scale=0.96,
% pagecommand=\subsection{Questionnaire 2 : Sensibilisation à linnovation en entreprise}]
% {questionnaires}
% \clearpage
%
\includepdf[pages={2}, scale=1]
{cover/charte_graphique}
\mtcsetrules{secttoc}{off}
\end{document}

34
latex/main_variables.tex Normal file
View file

@ -0,0 +1,34 @@
\def\varmaintitle{
\textbf{\Huge{One-dimensional box fitting}}
}
\def\varmainsubtitle{
\LARGE{A statistical analysis of different algorithms}
}
% \def\varlogo{logo.png} %ou 5 ou 13
\def\varcovertext{
\textbf{Clément LACAU}
\textbf{Paul ALNET}
2MIC B - Year 59
\vspace{0.33cm}
%\begin{center}
% -\hspace{0.25cm}Version du \today\hspace{0.25cm}-
%\end{center}
Defense on June 7th 2023
}
\def\varinsaaddress{
\textbf{INSA Toulouse}
135, Avenue de Rangueil
31077 Toulouse Cedex 4 - France
\href{https://www.insa-toulouse.fr}{www.insa-toulouse.fr}
}

29
latex/remerciements.tex Normal file
View file

@ -0,0 +1,29 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Tortor pretium
viverra suspendisse potenti nullam ac tortor vitae. Porttitor massa id
neque aliquam vestibulum morbi blandit. Quis imperdiet massa tincidunt
nunc pulvinar sapien et ligula. Tincidunt lobortis feugiat vivamus
at. Amet justo donec enim diam. Ut tortor pretium viverra suspendisse
potenti nullam ac tortor vitae. Consectetur a erat nam at lectus urna
duis convallis convallis. Viverra nam libero justo laoreet sit amet
cursus. Non enim praesent elementum facilisis leo. Sit amet mauris
commodo quis. Lectus mauris ultrices eros in cursus turpis. Cursus euismod
quis viverra nibh cras pulvinar mattis. Duis at consectetur lorem donec
massa sapien faucibus. In hac habitasse platea dictumst quisque sagittis
purus. Ut aliquam purus sit amet. Eget egestas purus viverra accumsan
in nisl. Egestas dui id ornare arcu odio ut sem. Nunc mi ipsum faucibus
vitae. Vel pretium lectus quam id leo in vitaLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Tortor pretium
viverra suspendisse potenti nullam ac tortor vitae. Porttitor massa id
neque aliquam vestibulum morbi blandit. Quis imperdiet massa tincidunt
nunc pulvinar sapien et ligula. Tincidunt lobortis feugiat vivamus
at. Amet justo donec enim diam. Ut tortor pretium viverra suspendisse
potenti nullam ac tortor vitae. Consectetur a erat nam at lectus urna
duis convallis convallis. Viverra nam libero justo laoreet sit amet
cursus. Non enim praesent elementum facilisis leo. Sit amet mauris
commodo quis. Lectus mauris ultrices eros in cursus turpis. Cursus euismod
quis viverra nibh cras pulvinar mattis. Duis at consectetur lorem donec
massa sapien faucibus. In hac habitasse platea dictumst quisque sagittis
purus. Ut aliquam purus sit amet. Eget egestas purus viverra accumsan
in nisl. Egestas dui id ornare arcu odio ut sem. Nunc mi ipsum faucibus
vitae. Vel pretium lectus quam id leo in vitae