wip
[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, 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<graph.size; i++)
26 {
27 int distance[graph.size];
28 graph.GetDistancesFrom(i, distance);
29 results.UpdateDistance(distance, graph.size);
30 }
31
32 /*int distance[graph.size];
33 float routesCount[graph.size];
34 int nRefresh = 1;
35
36 graph.GetDistancesFrom(0, distance);
37 double moy = 0;
38 for(int i=0; i<graph.size; i++)
39 moy += distance[i];
40 moy /= graph.size;
41 cout << "Avg distance : " << moy << endl; cout.flush();
42
43 for(int i = 0; i<100; i++)
44 {
45 for(int j=0; j<graph.size; j++)
46 {
47 graph.GetRoutesFrom(j, distance, routesCount);
48 unordered_set<int> alreadyConnected;
49
50 // erase some edge
51 for(int k=0; k<nRefresh; k++)
52 {
53 int minNode = -1;
54 int minimum = -1;
55 for(int index = 0; index < graph.generated[j].size(); index++)
56 if(minNode == -1 || routesCount[graph.generated[j][index]] < minimum)
57 {
58 minNode = graph.generated[j][index];
59 minimum = routesCount[minNode];
60 }
61
62 graph.RemoveEdge(j, minNode);
63 }
64
65 // Add new edges
66 alreadyConnected.insert(j);
67 for(int k : graph.adjacency[j])
68 alreadyConnected.insert(k);
69
70 for(int k=0; k<nRefresh; k++)
71 alreadyConnected.insert(graph.AddEdge(j, alreadyConnected));
72 }
73
74 graph.GetDistancesFrom(0, distance);
75 moy = 0;
76 for(int i=0; i<graph.size; i++)
77 moy += distance[i];
78 moy /= graph.size;
79 cout << "Avg distance : " << moy << endl;
80 }*/
81 }
82
83 results.Finalise();
84 return results;
85 }
86
87 int main(int argc, char** argv)
88 {
89 mt19937 rng(time(NULL));
90
91 FILE* output = fopen(outName, "wt");
92 int fno = fileno(output);
93 fprintf(output, "n,k,a,distance\n");
94
95 vector<future<string>> outputStrings;
96 for(int n=10; n<=100000; n*=2)
97 for(int k=5; k<=50; k+=5)
98 for(float a=1; a<=1; a+=0.05)
99 {
100 int seed = rng();
101 outputStrings.push_back(async(launch::async, [seed, n, k, a]()
102 {
103 Results results = Simulate(seed, n, k, 3*k, a, 1);
104 ostringstream out;
105 out << n << "," << k << "," << a << ","
106 << results.avgDistance
107 << endl;
108 return out.str();
109 }));
110 }
111
112 cout << "Launching finished" << endl;
113
114 int nTask = outputStrings.size();
115 for(int i=0; i<nTask; i++)
116 {
117 fprintf(output, outputStrings[i].get().c_str());
118 fflush(output);
119 fsync(fno);
120 cout << "\r" << i+1 << "/" << nTask;
121 cout.flush();
122 }
123
124
125 cout << endl;
126 fclose(output);
127 return 0;
128 }
129
130