Commit 05ddb3f7 authored by Ulysse Beaugnon's avatar Ulysse Beaugnon

Added a tool to simulate on real data

I've akso changed the datastructure to bz able to see the graph evolve much faster
parent 0c4db51f
URGENT => Name ideas :
resnet ( for Resiliable NET )
resnet ( for Resiliable NET ) : i prefere resinet
rsnet ( Resiliable Scalable NET )
To be done :
......
#include "main.h"
Graph::Graph(int size, int k, int maxPeers, mt19937& generator, const Latency& latency) :
generator(generator), size(size), k(k), maxPeers(maxPeers), latency(latency),
distrib(uniform_int_distribution<int>(0, size-1))
{
adjacency = new unordered_set<int>[size];
generated = new unordered_set<int>[size];
for(int i=0; i<size; i++)
SaturateNode(i);
}
void Graph::SaturateNode(int node)
{
while(generated[node].size() < k && AddEdge(node)) { }
}
bool Graph::AddEdge(int from)
{
int to;
for(int i=0; i<50; i++)
{
to = distrib(generator);
if(latency.values[from][to] > 0
&& to != from
&& adjacency[from].count(to) == 0
&& adjacency[to].size() + generated[to].size() <= maxPeers + k)
{
generated[from].insert(to);
adjacency[from].insert(to);
adjacency[to].insert(from);
return true;
}
}
//cout << "Warning : a link could not be established from " << from << endl;
return false;
}
void Graph::RemoveEdge(int from, int to)
{
generated[from].erase(to);
adjacency[from].erase(to);
adjacency[to].erase(from);
}
void Graph::GetRoutesFrom(int from, int* nRoutes, int* prevs, int* distances)
{
// init vars
stack<int> order;
for(int i=0; i<size; i++)
{
distances[i] = -1;
nRoutes[i] = 1;
}
distances[from] = 0;
priority_queue<pair<int, int>> remainingNodes;
remainingNodes.push(pair<int, int>(-0, from));
// Get the order
while(!remainingNodes.empty())
{
pair<int, int> p = remainingNodes.top();
int node = p.second;
int d = -p.first;
remainingNodes.pop();
if(d == distances[node])
{
order.push(node);
for(int neighbor : adjacency[node])
{
int neighborDist = d + latency.values[neighbor][node];
if(distances[neighbor] == -1 || distances[neighbor] > neighborDist)
{
distances[neighbor] = neighborDist;
prevs[neighbor] = node;
remainingNodes.push(pair<int, int>(-neighborDist, neighbor));
}
}
}
}
// get the BC
while(!order.empty())
{
int node = order.top();
order.pop();
if(distances[node] != -1)
nRoutes[prevs[node]] += nRoutes[node];
}
}
void Graph::UpdateLowRoutes(double& avgDistance, double unreachable, double* arityDistrib)
{
routesResult results[size];
for(int i=0; i<size; i++)
{
// Compute the routes
int nRoutes[size], prevs[size], distances[size];
GetRoutesFrom(i, nRoutes, prevs, distances);
// Get the values
routesResult r;
r.toDelete = -1;
for(int j : generated[i])
if(r.toDelete == -1 || nRoutes[r.toDelete] > nRoutes[j])
r.toDelete = j;
r.arity = adjacency[i].size();
r.avgDistance = 0;
r.unreachable = 0;
for(int j=0; j<size; j++)
{
if(distances[i] >= 0)
r.avgDistance += distances[j];
else
r.unreachable++;
}
r.avgDistance /= (double)(size - r.unreachable);
results[i] = r;
}
avgDistance = 0;
double avgDistanceWeight = 0;
unreachable = 0;
for(int i=0; i<=maxPeers; i++)
arityDistrib[i] = 0;
for(int i = 0; i<size; i++)
{
routesResult r = results[i];
if(r.toDelete >= 0)
RemoveEdge(i, r.toDelete);
SaturateNode(i);
avgDistance += r.avgDistance*(size-r.unreachable);
avgDistanceWeight += size-r.unreachable;
unreachable += r.unreachable;
arityDistrib[adjacency[i].size()]++;
}
avgDistance /= avgDistanceWeight;
for(int i=0; i<=maxPeers; i++)
arityDistrib[i] /= size;
}
#include "main.h"
Latency::Latency(const char* filePath, int size) : size(size)
{
values = new int*[size];
for(int i=0; i<size; i++)
values[i] = new int[size];
FILE* file = NULL;
file = fopen(filePath, "r");
int a, b, latency;
while(!feof(file))
{
fscanf(file, "%d %d %d", &a, &b, &latency);
values[b][a] = latency;
values[a][b] = latency;
}
fclose(file);
}
void Latency::Rewrite(int n)
{
int nat[size];
int nextId = 0;
for(int i=0; i<size; i++)
{
int nReachable = 0;
for(int j=0; j<size; j++)
{
if(values[i][j] > 0)
nReachable++;
}
if(nReachable <= n)
nat[i] = -1;
else
{
nat[i] = nextId;
nextId++;
}
}
FILE* file = NULL;
file = fopen("latency/pw-1715/rewrite", "w");
for(int i=0; i<size-1; i++)
if(nat[i] != -1)
for(int j=1; j<size; j++)
if(nat[j] != -1)
fprintf(file, "%d %d %d\n", nat[i], nat[j], values[i][j]);
fclose(file);
}
Latency::~Latency()
{
for(int i=0; i<size; i++)
delete[] values[i];
delete[] values;
}
\ No newline at end of file
// To compile : g++ -std=c++0x results.cpp graph.cpp main.cpp -lpthread
#include "main.h"
void simulate(int size, int k, int maxPeer, int seed, const Latency& latency, const char* outName)
{
FILE* output = fopen(outName, "wt");
int fno = fileno(output);
fprintf(output, "round,avgdistance,unreachable,arity 0..30\n");
mt19937 rng(seed);
Graph graph(size, k, maxPeer, rng, latency);
cout << "\r" << 0 << "/" << 2000;
cout.flush();
for(int i=0; i<2000; i++)
{
double avgDistance, unreachable;
double arityDistrib[maxPeer+1];
graph.UpdateLowRoutes(avgDistance, unreachable, arityDistrib);
fprintf(output, "%d,%f,%f", i , avgDistance, unreachable);
for(int j=0; j<=maxPeer; j++)
fprintf(output, ",%f", arityDistrib[j]);
fprintf(output, "\n");
fflush(output);
fsync(fno);
cout << "\r" << i+1 << "/" << 2000;
cout.flush();
}
cout << endl;
fclose(output);
}
int main(int argc, char** argv)
{
mt19937 rng(time(NULL));
//Latency latency("latency/pw-1715/pw-1715-latencies", 1715);
//latency.Rewrite(20);
Latency latency("latency/pw-1715/rewrite", 1556);
vector<future<void>> threads;
for(int i=0; i<8; i++)
{
int seed = rng();
char* out = new char[20];
sprintf(out, "out_%d.csv", i);
threads.push_back(async(launch::async, [seed, out, &latency]()
{ simulate(1555, 10, 30, seed, latency, out); delete[] out; }));
}
for(int i=0; i<8; i++)
threads[i].get();
return 0;
}
\ No newline at end of file
#include <iostream>
#include <vector>
#include <random>
#include <queue>
#include <stack>
#include <unordered_set>
#include <future>
#include <sstream>
#include <unistd.h>
using namespace std;
class Latency
{
public:
Latency(const char* filePath, int size);
void Rewrite(int n);
~Latency();
int** values;
private:
int size;
};
class Graph
{
public:
Graph(int size, int k, int maxPeers, mt19937& generator, const Latency& latency);
~Graph() { delete[] adjacency; delete[] generated; };
void UpdateLowRoutes(double& avgDistance, double unreachable, double* arityDistrib);
private:
void SaturateNode(int node);
bool AddEdge(int from);
void RemoveEdge(int from, int to);
void GetRoutesFrom(int from, int* nRoutes, int* prevs, int* distances);
mt19937& generator;
uniform_int_distribution<int> distrib;
int maxPeers;
int k;
int size;
unordered_set<int>* adjacency;
unordered_set<int>* generated;
const Latency& latency;
};
struct routesResult
{
double avgDistance;
int arity;
int unreachable;
int toDelete;
};
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment