Remove trailing whitespaces
[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 <future>
4 #include <sstream>
5 #include <unistd.h>
6
7 const char* outName = "out.csv";
8
9 Results Simulate(int seed, int n, int k, int maxPeer, int maxDistanceFrom, float alivePercent, int runs)
10 {
11 Results results(maxPeer, 20);
12 mt19937 rng(seed);
13
14 for(int r=0; r<runs; r++)
15 {
16 Graph graph(n, k, maxPeer, rng);
17 graph.KillMachines(alivePercent);
18 results.AddAccessibilitySample(((double)graph.CountUnreachableFrom(0))/((double)n));
19 //int minCut = graph.GetMinCut();
20 //if(results.minKConnexity == -1 || results.minKConnexity > minCut)
21 //results.minKConnexity = minCut;
22 //results.UpdateArity(graph);
23
24 // Compute the shortest path
25 /*for(int i=0; i<min(graph.size, maxDistanceFrom); i++)
26 {
27 int distance[graph.size];
28 graph.GetDistancesFrom(i, distance);
29 results.UpdateDistance(distance, graph.size);
30 }*/
31 }
32
33 results.Finalise();
34 return results;
35 }
36
37 int main(int argc, char** argv)
38 {
39 mt19937 rng(time(NULL));
40
41 FILE* output = fopen(outName, "wt");
42 int fno = fileno(output);
43 fprintf(output, "n,k,a,maxPeer,avgDistance,disconnected,disconnectionProba,"
44 "maxDistance,maxArityDistrib,minCut,accessibility\n");
45
46 vector<future<string>> outputStrings;
47 for(int n=2000; n<=2000; n*=2)
48 for(int k=10; k<=10; k+=5)
49 for(float a=0.01; a<=1; a+=0.05)
50 {
51 int seed = rng();
52 outputStrings.push_back(async(launch::async, [seed, n, k, a]()
53 {
54 Results results = Simulate(seed, n, k, 3*k, 10000, a, 1);
55 ostringstream out;
56 out << n << "," << k << "," << a << "," << 3*k << ","
57 << results.avgDistance << ","
58 << results.disconnected << ","
59 << results.disconnectionProba << ","
60 << results.maxDistanceReached << ","
61 << results.arityDistrib[3*k] << ","
62 << results.minKConnexity << ","
63 << results.avgAccessibility
64 << endl;
65 return out.str();
66 }));
67 }
68
69 cout << "Launching finished" << endl;
70
71 int nTask = outputStrings.size();
72 for(int i=0; i<nTask; i++)
73 {
74 fprintf(output, outputStrings[i].get().c_str());
75 fflush(output);
76 fsync(fno);
77 cout << "\r" << i+1 << "/" << nTask;
78 cout.flush();
79 }
80
81
82 cout << endl;
83 fclose(output);
84 return 0;
85 }
86
87