Commit 577bc667 authored by pmla's avatar pmla

renamed some variables

parent e16b8cd3
Pipeline #24536482 failed with stage
......@@ -83,6 +83,8 @@ void GrainSegmentationEngine::perform()
for(FloatType& angle : _results->neighborDisorientationAngles()->floatRange())
angle *= FloatType(180) / FLOATTYPE_PI;
//if(!mergeOrphanAtoms()) return;
// Return the results to the pipeline system.
setResult(std::move(_results));
}
......
......@@ -42,85 +42,71 @@
class Graph {
public:
int nb_nodes;
double total_weight;
int sum_nodes_w;
std::vector<unsigned long long> degrees;
int num_nodes;
std::vector<int> node_size;
std::vector<int> degrees;
std::vector<int> edges;
std::vector<double> weights;
std::vector<int> nodes_w;
Graph(int nbc, double _total_weight) {
total_weight = _total_weight;
sum_nodes_w = 0;
Graph(int _num_nodes, std::vector<int>& comm_size) {
num_nodes = _num_nodes;
degrees.resize(_num_nodes);
nb_nodes = nbc;
degrees.resize(nbc);
nodes_w.resize(nbc);
for (auto c: comm_size)
node_size.push_back(c);
};
Graph(size_t num_vertices, std::vector<size_t>::iterator adjdegrees, size_t num_edges, std::vector<size_t>::iterator adjlist, std::vector<double>::iterator adjweight, double _total_weight)
Graph(size_t num_vertices, std::vector<size_t>::iterator adjdegrees, size_t num_edges, std::vector<size_t>::iterator adjlist, std::vector<double>::iterator adjweight)
{
nb_nodes = num_vertices;
total_weight = _total_weight;
num_nodes = num_vertices;
for (int i = 0; i < num_edges; i++) {
for (int i=0;i<num_edges;i++) {
edges.push_back(adjlist[i]);
weights.push_back(adjweight[i]);
}
unsigned long long tot = 0ULL;
for (int i = 0; i < nb_nodes; i++) {
tot += (unsigned long long)adjdegrees[i];
size_t tot = 0;
for (int i=0;i<num_nodes;i++) {
tot += adjdegrees[i];
degrees.push_back(tot);
}
nodes_w.assign(nb_nodes, 1);
sum_nodes_w = nb_nodes;
}
// assign a weight to a node (needed after the first level)
void assign_weight(int node, int weight) {
sum_nodes_w -= nodes_w[node];
nodes_w[node] = weight;
sum_nodes_w += weight;
node_size.assign(num_nodes, 1);
}
int nb_neighbors(int node) {
assert(node >= 0 && node < nb_nodes);
int num_neighbors(int node) {
assert(node >= 0 && node < num_nodes);
if (node == 0)
return degrees[0];
else
return (int)(degrees[node] - degrees[node - 1]);
return degrees[node] - degrees[node - 1];
}
double nb_selfloops(int node) {
assert(node >= 0 && node < nb_nodes);
double num_selfloops(int node) {
assert(node >= 0 && node < num_nodes);
std::pair<std::vector<int>::iterator, std::vector<double>::iterator> p = neighbors(node);
for (int i = 0; i < nb_neighbors(node); i++)
for (int i = 0; i < num_neighbors(node); i++)
if (*(p.first + i) == node)
return (double)*(p.second + i);
return 0.0L;
return 0;
}
double weighted_degree(int node) {
assert(node >= 0 && node < nb_nodes);
assert(node >= 0 && node < num_nodes);
std::pair<std::vector<int>::iterator, std::vector<double>::iterator> p = neighbors(node);
double res = 0.0L;
for (int i = 0; i < nb_neighbors(node); i++) {
for (int i = 0; i < num_neighbors(node); i++) {
res += (double)*(p.second + i);
}
return res;
}
std::pair<std::vector<int>::iterator, std::vector<double>::iterator> neighbors(int node) {
assert(node >= 0 && node < nb_nodes);
assert(node >= 0 && node < num_nodes);
if (node == 0)
return make_pair(edges.begin(), weights.begin());
......@@ -129,69 +115,56 @@ public:
}
};
// a new pass is computed if the last one has generated an increase
// better than eps_impr
// if 0.0L even a minor increase is enough to go for one more pass
const double eps_impr = 1E-6;
static void remove(Graph* g, std::vector<int>& n2c, std::vector<double>& in, std::vector<double>& tot, int node, int comm, double dnodecomm) {
assert(node >= 0 && node < size);
in[comm] -= 2 * dnodecomm + g->nb_selfloops(node);
in[comm] -= 2 * dnodecomm + g->num_selfloops(node);
tot[comm] -= g->weighted_degree(node);
n2c[node] = -1;
}
static void insert(Graph* g, std::vector<int>& n2c, std::vector<double>& in, std::vector<double>& tot, int node, int comm, double dnodecomm) {
assert(node >= 0 && node < size);
assert(node>=0 && node<size);
in[comm] += 2 * dnodecomm + g->nb_selfloops(node);
in[comm] += 2 * dnodecomm + g->num_selfloops(node);
tot[comm] += g->weighted_degree(node);
n2c[node] = comm;
}
static double gain(Graph* g, std::vector<double>& in, std::vector<double>& tot, int node, int comm, double dnc, double degc) {
static double gain(double total_weight, std::vector<double>& tot, int node, int comm, double dnc, double degc) {
assert(node>=0 && node<size);
assert(node >= 0 && node < size);
double totc = tot[comm];
double m2 = g->total_weight;
return (dnc - totc * degc / m2);
return dnc - totc * degc / total_weight;
}
static double quality(Graph* g, std::vector<double>& in, std::vector<double>& tot) {
static double quality(double total_weight, Graph* g, std::vector<double>& in, std::vector<double>& tot) {
double q = 0;
double m2 = g->total_weight;
size_t size = g->nb_nodes;
for (int i=0;i<size;i++)
for (int i=0;i<g->num_nodes;i++)
if (tot[i] > 0)
q += in[i] - (tot[i] * tot[i]) / m2;
q /= m2;
return q;
q += in[i] - (tot[i] * tot[i]) / total_weight;
return q / total_weight;
}
static void neigh_comm(Graph* g, int node, int& neigh_last, std::vector<int>& n2c, std::vector<double>& neigh_weight, std::vector<int>& neigh_pos) {
for (int i = 0; i < neigh_last; i++)
for (int i=0;i<neigh_last;i++)
neigh_weight[neigh_pos[i]] = -1;
neigh_last = 0;
std::pair<std::vector<int>::iterator, std::vector<double>::iterator> p = g->neighbors(node);
int deg = g->nb_neighbors(node);
int deg = g->num_neighbors(node);
neigh_pos[0] = n2c[node];
neigh_weight[neigh_pos[0]] = 0;
neigh_last = 1;
for (int i = 0; i < deg; i++) {
for (int i=0;i<deg;i++) {
int neigh = *(p.first + i);
int neigh_comm = n2c[neigh];
double neigh_w = (g->weights.size() == 0) ? 1 : *(p.second + i);
......@@ -224,14 +197,14 @@ static std::vector<int> shuffled_order(std::mt19937_64& mt_rand, int n)
static Graph one_level(std::mt19937_64& mt_rand, Graph* g, std::vector< int > &prev_n2c, double total_weight, bool& improvement) {
size_t num_nodes = g->nb_nodes;
size_t num_nodes = g->num_nodes;
std::vector<int> n2c(num_nodes); // community to which each node belongs
std::vector<double> in(num_nodes);
std::vector<double> tot(num_nodes);
for (int i=0;i<num_nodes;i++) {
n2c[i] = i;
in[i] = g->nb_selfloops(i);
in[i] = g->num_selfloops(i);
tot[i] = g->weighted_degree(i);
}
......@@ -241,7 +214,7 @@ static Graph one_level(std::mt19937_64& mt_rand, Graph* g, std::vector< int > &p
improvement = false;
int nb_moves = 0;
double new_qual = quality(g, in, tot);
double new_qual = quality(total_weight, g, in, tot);
double cur_qual = new_qual;
std::vector<int> random_order = shuffled_order(mt_rand, num_nodes);
......@@ -272,7 +245,7 @@ static Graph one_level(std::mt19937_64& mt_rand, Graph* g, std::vector< int > &p
double best_nblinks = 0;
double best_increase = 0;
for (int i = 0; i < neigh_last; i++) {
double increase = gain(g, in, tot, node, neigh_pos[i], neigh_weight[neigh_pos[i]], w_degree);
double increase = gain(total_weight, tot, node, neigh_pos[i], neigh_weight[neigh_pos[i]], w_degree);
if (increase > best_increase) {
best_comm = neigh_pos[i];
best_nblinks = neigh_weight[neigh_pos[i]];
......@@ -286,11 +259,11 @@ static Graph one_level(std::mt19937_64& mt_rand, Graph* g, std::vector< int > &p
nb_moves++;
}
new_qual = quality(g, in, tot);
new_qual = quality(total_weight, g, in, tot);
if (nb_moves > 0)
improvement = true;
} while (nb_moves > 0 && new_qual - cur_qual > eps_impr);
} while (nb_moves > 0 && new_qual - cur_qual > 1E-6);
//
//relabelling
......@@ -314,26 +287,25 @@ static Graph one_level(std::mt19937_64& mt_rand, Graph* g, std::vector< int > &p
// Compute communities
std::vector< std::vector< int > > comm_nodes(num_reduced_nodes);
std::vector<int> comm_weight(num_reduced_nodes, 0);
std::vector<int> comm_size(num_reduced_nodes, 0);
for (int i=0;i<num_nodes;i++) {
comm_nodes[renumber[n2c[i]]].push_back(i);
comm_weight[renumber[n2c[i]]] += g->nodes_w[i];
comm_size[renumber[n2c[i]]] += g->node_size[i];
}
// Compute weighted graph
Graph g2(num_reduced_nodes, total_weight);
Graph g2(num_reduced_nodes, comm_size);
for (int comm = 0; comm < num_reduced_nodes; comm++) {
std::map<int, double> m;
int csize = comm_nodes[comm].size();
g2.assign_weight(comm, comm_weight[comm]);
for (int node = 0; node < csize; node++) {
std::pair<std::vector<int>::iterator, std::vector<double>::iterator> p = g->neighbors(comm_nodes[comm][node]);
int deg = g->nb_neighbors(comm_nodes[comm][node]);
int deg = g->num_neighbors(comm_nodes[comm][node]);
for (int i = 0; i < deg; i++) {
int neigh = *(p.first + i);
int neigh_comm = renumber[n2c[neigh]];
......@@ -363,15 +335,15 @@ int assign_communities_halfedge(size_t num_vertices, std::vector<size_t>::iterat
//thread-safe random number generation
std::mt19937_64 mt_rand(0);
Graph g(num_vertices, adjdegrees, num_edges, adjlist, adjweight, total_weight);
Graph g(num_vertices, adjdegrees, num_edges, adjlist, adjweight);
for (int i=0;i<num_vertices;i++)
n2c[i] = i;
int numc = g.nb_nodes;
int numc = g.num_nodes;
bool improvement = true;
do {
g = one_level(mt_rand, &g, n2c, total_weight, improvement);
numc = g.nb_nodes;
numc = g.num_nodes;
} while (improvement);
......
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