-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.cpp
83 lines (71 loc) · 1.75 KB
/
simulator.cpp
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
#include <iostream>
#include "simulator.h"
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
const int rounds = 10000;
const double pi = acos(-1.);
void simulation(int n, const float *angles, const float h) {
int freq[n];
memset(freq, 0, sizeof(freq));
for (int i = 0; i < rounds; ++ i) {
if (i % 1000 == 0) cout << i << endl;
//cout << simulator->simulate() << endl;
//simulator->reset();
Simulator *simulator = new Simulator(n, angles, h);
int ret = simulator->simulate();
if (ret < 0) cerr << "!" << endl;
freq[ret] ++;
delete simulator;
}
cout << "----------------shape---------------" << endl;
Simulator::printDiceShape(n, angles, h);
cout << "------------frequency---------------" << endl;
for (int i = 0; i < n; ++ i) {
cout << i << ' ' << freq[i] << endl;
}
vector<double> p;
for (int i = 0; i < (1 << n); ++ i) {
double t = 0;
for (int k = 0; k < n; ++ k) {
if (i & (1 << k))
t += ((double)freq[k]) / rounds;
}
p.push_back(t);
}
sort(p.begin(), p.end());
double gap = p[1] - p[0];
for (int i = 1; i < p.size() - 1; ++ i)
gap = max(gap, p[i + 1] - p[i]);
cout << "Estimated gap: " << gap << endl;
}
int main() {
srand(time(0));
const int n = 6;
int weight[n];
float angles[n];
float h = 0.3;
int tot = 0;
for (int i = 0; i < n - 1; ++ i)
tot += (1 << i) + (n - i);
for (int i = 0; i < n - 1; ++ i)
angles[i] = pi * 2.0 * ((1 << i) + (n - i)) / tot;
simulation(n, angles, h);
while (true) {
tot = 0;
for (int i = 0; i < n - 1; ++ i) {
cin >> weight[i];
tot += weight[i];
}
cin >> h;
for (int i = 0; i < n; ++ i) {
angles[i] = pi * 2.0 * weight[i] / tot;
}
simulation(n, angles, h);
}
return 0;
}