-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.cpp
More file actions
189 lines (162 loc) · 5.84 KB
/
simulation.cpp
File metadata and controls
189 lines (162 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <sys/time.h>
/* Text color indicators */
#define COL_NULL "\33[0m"
#define COL_BOLD "\33[0;1m"
#define COL_YEL "\33[0;93m"
using namespace std;
bool check_params(int argc, char* argv[], int &n, int &m, int &d, int &d_rand, int &k, int &b);
vector<int> simulate(int &n, int &m, int &d, int &d_rand, int &k, int &b);
int main(int argc, char* argv[]) {
/*
Set values for each parameter. If not specified in argv, use a default value.
Launch the simulation.
*/
struct timeval time;
gettimeofday(&time,NULL);
srand((time.tv_sec * 1000) + (time.tv_usec / 1000));
// Default values
int n = 100; //Balls
int m = 100; //Bins
int d = 1; //Number of choice
int d_rand = 0.0; //Fractional part of d *1000
int k = 0; // Number of queries in the b-batched setting
int b = 1; // Number of batches
vector<int> result;
if (check_params(argc, argv, n, m, d, d_rand, k, b)) {
if (d > m || (d == m && d_rand != 0)) { //More choice than bins
cout << "Invalid value : d must be >= m (" << d+((float)d_rand/1000) << " < " << m << ")" << endl;
return 0;
}
cout << "------------------------------------------------------" << endl;
result = simulate(n, m, d, d_rand, k, b);
cout << "------------------------------------------------------" << endl;
}
return 0;
}
void print_flag_line(const string& flag, const string& type, const string& description, const string& def) {
/*
Define the print format displayed by the --help flag.
*/
cout << COL_YEL << left << setw(18) << flag << COL_NULL
<< setw(10) << type
<< setw(55) << description
<< def << "\n";
}
bool check_params(int argc, char* argv[], int &n, int &m, int &d, int &d_rand, int &k, int &b) {
/*
Check flag values from user call.
*/
bool valid = true;
string arg;
for (int i = 1; i < argc; i++) {
arg = argv[i];
if ((arg == "-m" || arg == "--bins") && i+1 < argc) {
m = stoi(argv[i+1]);
i++;
}
else if ((arg == "-n" || arg == "--balls") && i+1 < argc) {
n = stoi(argv[i+1]);
i++;
}
else if ((arg == "-d" || arg == "--sample") && i+1 < argc) {
d = stoi(argv[i+1]);
d_rand = 1000*(stod(argv[i+1]) - d);
i++;
}
else if ((arg == "-k" || arg == "--queries") && i+1 < argc) {
k = stoi(argv[i+1]);
i++;
}
else if ((arg == "-b" || arg == "--batches") && i+1 < argc) {
b = stoi(argv[i+1]);
i++;
}
else {
cout << "Unknown parameter \"" << arg << "\"" << endl;
valid = false;
}
}
if (!valid) {
cout << COL_BOLD << left << setw(18) << "FLAG" << setw(10) << "TYPE" << setw(55) << "DESCRIPTION" << "DEFAULT" << COL_NULL << "\n";
print_flag_line("-m/--bins", "<int>", "Number of bins", "100");
print_flag_line("-n/--balls", "<int>", "Number of balls", "100");
print_flag_line("-d/--sample", "<double>", "Number of bins for d-choice criteria (1<=d)", "1");
print_flag_line("-k/--queries", "<int>", "Number of binary queries to use in b-batched setting", "0");
print_flag_line("-b/--batches", "<int>", "Number of batches (b>0) for the b-batched setting", "1");
}
return valid;
}
int least_balls(vector<int> &bins, int &n, int &m, int &d){
int index = 0;
int min_index = 0;
int min = n;
for (int i = 0; i<d; i++){
index = rand() % m; // random value between 0 and m
if(bins[index] < min){
min_index = index;
min = bins[index];
}
}
return min_index;
}
int k_selection(const vector<int> &bins, int m, int d, int k) {
// Pick d candidate bins at random
vector<int> bin_subset;
for (int i = 0; i < d; ++i) {
bin_subset.push_back(rand() % m);
}
// Since bins is already sorted, we can just compute percentile indices
double percentile = 0.5; // start at median
for (int step = 0; step < k && bin_subset.size() > 1; ++step) {
int threshold_idx = static_cast<int>(percentile * m);
threshold_idx = max(0, min(m - 1, threshold_idx));
int threshold = bins[threshold_idx];
vector<int> best_bins, worst_bins;
for (int idx : bin_subset) {
if (bins[idx] <= threshold) best_bins.push_back(idx);
else worst_bins.push_back(idx);
}
if (!best_bins.empty()) {
bin_subset = best_bins;
percentile = max(0.0, percentile / 2.0); // zoom toward lighter half
} else {
bin_subset = worst_bins;
percentile = min(1.0, percentile + (1.0 - percentile) / 2.0); // zoom toward heavier half
}
}
return bin_subset[rand() % bin_subset.size()];
}
vector<int> simulate(int &n, int &m, int &d, int &d_rand, int &k, int &b){
vector bins(m, 0);
int index, max = 0, n_balls = 0;
int d_;
for (int i = 0; i < b; i++) {
if (k != 0) sort(bins.begin(), bins.end());
vector<int> snapshot = bins;
n_balls = (n / b) + ((n % b) > i);
for (int j = 0; j < n_balls; j++) {
if (k != 0)
index = k_selection(snapshot, m, d, k);
else {
d_ = d;
if ((rand() % 1000) < d_rand) d_ += 1;
index = least_balls(bins, n, m, d_);
}
bins[index]++;
if (bins[index] > max) max = bins[index];
}
}
for (int x : bins) cout << x << ";";
cout << endl;
cout << "max(Xi) = " << max << endl;
cout << "average/bin = " << ((float)n / m) << endl;
cout << "Gn = " << (float)max - ((float)n / m) << endl;
return bins;
}