You can now simulate the failure of a given percentage of the nodes
[re6stnet.git] / simulation / main.cpp
1 // To compile : g++ -std=c++0x results.cpp graph.cpp main.cpp -lpthread
2 #include "main.h"
3 #include <fstream>
4 #include <future>
5 #include <sstream>
6
7 const char* outName = "out.csv";
8
9 Results Simulate(mt19937 rng, int n, int k, int maxPeer, int maxDistanceFrom, float alivePercent, int runs)
10 {
11 Results results(maxPeer, 20);
12
13 for(int r=0; r<runs; r++)
14 {
15 Graph graph(n, k, maxPeer, rng);
16 graph.KillMachines(alivePercent);
17 results.UpdateArity(graph);
18
19 // Compute the shortest path
20 for(int i=0; i<min(graph.size, maxDistanceFrom); i++)
21 {
22 int distance[graph.size];
23 graph.GetDistancesFrom(i, distance);
24 results.UpdateDistance(distance, graph.size);
25 }
26 }
27
28 results.Finalise();
29 return results;
30 }
31
32 int main(int argc, char** argv)
33 {
34 mt19937 rng(time(NULL));
35
36 fstream output(outName, fstream::out);
37 output << "n,k,maxPeer,avgDistance,disconnected,disconnectionProba,maxDistance,maxArityDistrib" << endl;
38
39
40 vector<future<string>> outputStrings;
41 for(int n=200; n<=200; n*=2)
42 for(int k=10; k<=10; k+=5)
43 for(float a=0.05; a<1; a+=0.05)
44 {
45 outputStrings.push_back(async(launch::async, [rng, n, k, a]()
46 {
47 Results results = Simulate(rng, n, k, 3*k, 10000, a, 100);
48 ostringstream out;
49 out << n << "," << k << "," << 3*k << "," << results.avgDistance << ","
50 << results.disconnected << "," << results.disconnectionProba << ","
51 << results.maxDistanceReached << "," << results.arityDistrib[3*k];
52
53 return out.str();
54 }));
55 }
56
57 cout << "Launching finished" << endl;
58
59 int nTask = outputStrings.size();
60 for(int i=0; i<nTask; i++)
61 {
62 output << outputStrings[i].get() << endl;
63 cout << "\r" << i+1 << "/" << nTask;
64 cout.flush();
65 }
66
67 cout << endl;
68 output.close();
69 return 0;
70 }
71
72