A C++ simulation of the balls and bins model, with multiple ball allocation strategies. With support for automated experiments and analysis via a Python notebook.
Clone the repository:
git clone https://github.com/PauJimeno/RA_Assignment-2.git
cd RA_Assignment-2Compile the C++ code using g++:
g++ -o simulation simulation.cppNo external libraries have been used.
The simulation supports several parameters. Use --help or -h to view usage:
./simulation --help| Flag | Type | Description | Default |
|---|---|---|---|
| -m / --bins | <int> |
Number of bins | 100 |
| -n / --balls | <int> |
Number of balls | 100 |
| -d / --sample | <double> |
Number of bins for d-choice criteria (1<=d) | 1 |
| -k / --queries | <int> |
Number of binary queries to use in b-batched setting | 0 |
| -b / --batches | <int> |
Number of batches (b>0) for the b-batched setting | 1 |
- Heavily loaded d-choice scheme with
$d=3$
./simulation -m 100 -n 10000 -d 3 - Heavily loaded
$1+\beta$ -choice scheme with$\beta=0.7$
Here the
./simulation -m 100 -n 10000 -d 1.73 . Heavily loaded b-batch scheme with
./simulation -m 100 -n 10000 -d 2 -k 2 -b 8The simulation prints the most loaded bin
99;101;100;99;98;101;100;100;101;101
max(Xi) = 101
average/bin = 100
Gn = 1
A Python notebook (Balls_and_Bins.ipynb) is provided to average multiple simulations, compute variance, analyze results and generate plots.
Requires numpy and matplotlib.
3.1 Example Usage for an icreasing amount of balls following a logaritmic step (with 20 steps), 10 averaged simulations ($T=10$ ), different amounts of bins.
exec_name = "simulation"
bins = [10, 30, 50, 70, 100, 1000]
d_choice = [1, 2, 3]
n_s = 10
step = 20
log=True
for m in bins:
results_matrix = []
labels = []
for d in d_choice:
Gn_arr, _, n_values = run_experiment(exec_name, m, m, d, m*m, step, n_s, log=log)
results_matrix.append(Gn_arr)
labels.append(f"d={d}")
title = f"d-choice comparison with $n\in[m..m^2$], m={m}"
plot_multiple(n_values, results_matrix, m, labels, title, log=log, xticks=(m<=100))The run_experiment function:
- Runs the C++ simulation in a subprocess.
- Collects results over
$T$ runs of the experiment.
More details can be found in the notebook.
The notebook produces plots such as:

Pau Jimeno Román
Thomas Aubertier