54#define localConstrMajorIterations 1000
75 float *dist_accumulator =
NULL;
76 float *tmp_coords =
NULL;
78 double *degrees =
NULL;
81 float *f_storage =
NULL;
82 float **coords =
NULL;
85 CMajEnvVPSC *cMajEnvHor =
NULL;
86 CMajEnvVPSC *cMajEnvVrt =
NULL;
96 double old_stress, new_stress = 0;
99 double nsizeScale = 0;
100 float maxEdgeLen = 0;
107 for (i = 0; i < n; i++) {
108 for (
size_t j = 1; j <
graph[i].nedges; j++) {
109 maxEdgeLen =
MAX(
graph[i].ewgts[j], maxEdgeLen);
127 fprintf(stderr,
"Calculating subset model");
133 "graph is disconnected. Hence, the circuit model\n");
135 "is undefined. Reverting to the shortest path model.\n");
139 fprintf(stderr,
"Calculating MDS model");
144 fprintf(stderr,
"Calculating shortest paths");
149 fprintf(stderr,
"Setting initial positions");
154 length = n + n * (n - 1) / 2;
155 for (i = 0; i < length; i++) {
156 if (Dij[i] > diameter) {
157 diameter = (int) Dij[i];
163 for (i = 0; i <
dim; i++) {
164 for (
int j = 0; j < n; j++) {
165 max = fmax(max, fabs(d_coords[i][j]));
168 for (i = 0; i <
dim; i++) {
169 for (
int j = 0; j < n; j++) {
170 d_coords[i][j] *= 10 / max;
178 for (i = 0; i <
dim; i++) {
183 y_0 = d_coords[1][0];
184 for (i = 0; i < n; i++) {
185 d_coords[1][i] -= y_0;
195 lap_length = n + n * (n - 1) / 2;
200 if (opt->clusters.nclusters > 0) {
201 int nn = n + opt->clusters.nclusters * 2;
202 int clap_length = nn + nn * (nn - 1) / 2;
203 float *clap =
gv_calloc(clap_length,
sizeof(
float));
207 for (i = 0; i < nn; i++) {
208 for (
int j = 0; j < nn - i; j++) {
209 if (i < n && j < n - i) {
213 if (j == 1 && i % 2 == 1) {
228 lap_length = clap_length;
234 for (i = 0; i < n - 1; i++) {
237 for (
int j = 1; j < n - i; j++, count++) {
240 degrees[i + j] -= val;
242 degrees[i] -= degree;
244 for (step = n, count = 0, i = 0; i < n; i++, count += step, step--) {
245 lap2[count] = (float) degrees[i];
250 for (i = 0; i <
dim; i++) {
251 coords[i] = f_storage + i * n;
252 for (
int j = 0; j < n; j++) {
253 coords[i][j] = j < orig_n ? (float)d_coords[i][j] : 0;
260 constant_term = (float) (n * (n - 1) / 2);
268 for (k = 1; k <
dim; k++) {
272 tmp_coords =
gv_calloc(n,
sizeof(
float));
273 dist_accumulator =
gv_calloc(n,
sizeof(
float));
275 old_stress = DBL_MAX;
277 if ((cMajEnvHor = initCMajVPSC(n, lap2,
graph, opt, 0)) ==
NULL) {
281 if ((cMajEnvVrt = initCMajVPSC(n, lap2,
graph, opt, opt->diredges)) ==
NULL) {
286 lap1 =
gv_calloc(lap_length,
sizeof(
float));
288 for (converged =
false, iterations = 0;
289 iterations < maxi && !converged; iterations++) {
294 for (count = 0, i = 0; i < n - 1; i++) {
302 for (k = 0; k <
dim; k++) {
312 for (
int j = 0; j <
len; j++) {
313 if (dist_accumulator[j] >= FLT_MAX || dist_accumulator[j] < 0) {
314 dist_accumulator[j] = 0;
320 for (
int j = 0; j <
len; j++, count++) {
321 val = lap1[count] *= dist_accumulator[j];
323 degrees[i + j + 1] -= val;
325 degrees[i] -= degree;
327 for (step = n, count = 0, i = 0; i < n; i++, count += step, step--) {
328 lap1[count] = (float) degrees[i];
332 for (k = 0; k <
dim; k++) {
342 for (k = 0; k <
dim; k++) {
346 new_stress += constant_term;
347 for (k = 0; k <
dim; k++) {
353 if (
Verbose && (iterations % 1 == 0)) {
354 fprintf(stderr,
"%.3f ", new_stress);
355 if (iterations % 10 == 0)
356 fprintf(stderr,
"\n");
358 converged = new_stress < old_stress
359 && fabs(new_stress - old_stress) / fabs(old_stress + 1e-10) <
365 old_stress = new_stress;
370 if ((iterations >= maxi - 1 || converged) && opt->noverlap == 1
371 && nsizeScale < 0.999) {
374 fprintf(stderr,
"nsizescale=%f,iterations=%d\n",
375 nsizeScale, iterations);
393 if (opt->noverlap == 1 && nsizeScale > 0.001) {
394 generateNonoverlapConstraints(cMajEnvHor, nsizeScale, coords,
399 if (cMajEnvHor->m > 0) {
400 constrained_majorization_vpsc(cMajEnvHor, b[0], coords[0],
401 localConstrMajorIterations);
412 if (opt->noverlap == 1 && nsizeScale > 0.001) {
413 generateNonoverlapConstraints(cMajEnvVrt, nsizeScale, coords,
416 if (cMajEnvVrt->m > 0) {
417 if (constrained_majorization_vpsc(cMajEnvVrt, b[1], coords[1],
418 localConstrMajorIterations) < 0) {
428 fprintf(stderr,
"\nfinal e = %f %d iterations %.2f sec\n",
431 deleteCMajEnvVPSC(cMajEnvHor);
432 deleteCMajEnvVPSC(cMajEnvVrt);
434 if (opt->noverlap == 2) {
436 removeoverlaps(orig_n, coords, opt);
440 if (coords !=
NULL) {
441 for (i = 0; i <
dim; i++) {
442 for (
int j = 0; j < orig_n; j++) {
443 d_coords[i][j] = coords[i][j];
455 free(dist_accumulator);
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
int conjugate_gradient_mkernel(float *A, float *x, float *b, int n, double tol, int max_iterations)
static double len(glCompPoint p)
void agwarningf(const char *fmt,...)
int agerr(agerrlevel_t level, const char *fmt,...)
Agraph_t * graph(char *name)
void invert_vec(int n, float *vec)
void invert_sqrt_vec(int n, float *vec)
void vectors_additionf(int n, float *vector1, float *vector2, float *result)
void set_vector_valf(int n, float val, float *result)
void orthog1(int n, double *vec)
void sqrt_vecf(int n, float *source, float *target)
void set_vector_val(int n, double val, double *result)
void right_mult_with_vector_ff(float *packed_matrix, int n, float *vector, float *result)
void vectors_mult_additionf(int n, float *vector1, float alpha, float *vector2)
void square_vec(int n, float *vec)
double vectors_inner_productf(int n, float *vector1, float *vector2)
float * compute_apsp_artificial_weights_packed(vtx_data *graph, int n)
float * circuitModel(vtx_data *graph, int nG)
int initLayout(int n, int dim, double **coords, node_t **nodes)
float * mdsModel(vtx_data *graph, int nG)
float * compute_apsp_packed(vtx_data *graph, int n)