main.cpp 3.85 KB
Newer Older
1
// To compile : g++ -std=c++0x results.cpp graph.cpp main.cpp -lpthread 
2
#include "main.h"
3 4
#include <future>
#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 15 16

    for(int r=0; r<runs; r++)
    {
        Graph graph(n, k, maxPeer, rng);
17 18
        //graph.KillMachines(alivePercent);
        //results.AddAccessibilitySample(((double)graph.CountUnreachableFrom(0))/((double)n));
19 20 21 22
        //int minCut = graph.GetMinCut();
        //if(results.minKConnexity == -1 || results.minKConnexity > minCut)
        //results.minKConnexity = minCut;
        //results.UpdateArity(graph);
23 24

        // Compute the shortest path
25
        for(int i=0; i<graph.size; i++)
26 27 28
        {
            int distance[graph.size];
            graph.GetDistancesFrom(i, distance);
29
            results.UpdateDistance(distance, graph.size);
30
        }
31

Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
32
        /*int distance[graph.size];
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
        float routesCount[graph.size];
        int nRefresh = 1;

        graph.GetDistancesFrom(0, distance);
        double moy = 0;
        for(int i=0; i<graph.size; i++)
            moy += distance[i];
        moy /= graph.size;
        cout << "Avg distance : " << moy << endl; cout.flush();

        for(int i = 0; i<100; i++)
        {
            for(int j=0; j<graph.size; j++)
            {
                graph.GetRoutesFrom(j, distance, routesCount);
                unordered_set<int> alreadyConnected;

                // erase some edge
                for(int k=0; k<nRefresh; k++)
                {
                    int minNode = -1;
                    int minimum = -1;
                    for(int index = 0; index < graph.generated[j].size(); index++)
                        if(minNode == -1 || routesCount[graph.generated[j][index]] < minimum)
                        {
                            minNode = graph.generated[j][index];
                            minimum = routesCount[minNode];
                        }

                    graph.RemoveEdge(j, minNode);
                }

                // Add new edges
                alreadyConnected.insert(j);
                for(int k : graph.adjacency[j])
                    alreadyConnected.insert(k);

                for(int k=0; k<nRefresh; k++)
                    alreadyConnected.insert(graph.AddEdge(j, alreadyConnected));
            }

            graph.GetDistancesFrom(0, distance);
            moy = 0;
            for(int i=0; i<graph.size; i++)
                moy += distance[i];
            moy /= graph.size;
            cout << "Avg distance : " << moy << endl;
Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
80
        }*/
81 82
    }

83
    results.Finalise();
84 85 86 87 88 89 90
    return results;
}

int main(int argc, char** argv)
{
    mt19937 rng(time(NULL));

91 92
    FILE* output = fopen(outName, "wt");
    int fno = fileno(output);
93
    fprintf(output, "n,k,a,distance\n");
94 95

    vector<future<string>> outputStrings;
96 97 98
    for(int n=10; n<=100000; n*=2)
        for(int k=5; k<=50; k+=5)
            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;
Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
105
                        out << n << "," << k << "," << a << ","
106
                            << results.avgDistance
107 108 109 110
                            << endl;
                        return out.str();
                    }));
            }
111

112 113 114 115 116
    cout << "Launching finished" << endl;

    int nTask = outputStrings.size();
    for(int i=0; i<nTask; i++)
    {
117 118 119
        fprintf(output, outputStrings[i].get().c_str());
        fflush(output);
        fsync(fno);
120
        cout << "\r" << i+1 << "/" << nTask;
121 122 123
        cout.flush();
    }

124

125
    cout << endl;
126
    fclose(output);
127 128 129 130
    return 0;
}