50 #include <csolve_VPSC.h>
55 #define localConstrMajorIterations 1000
77 float *dist_accumulator =
NULL;
78 float *tmp_coords =
NULL;
80 double *degrees =
NULL;
83 float *f_storage =
NULL;
84 float **coords =
NULL;
88 CMajEnvVPSC *cMajEnvHor =
NULL;
89 CMajEnvVPSC *cMajEnvVrt =
NULL;
99 double old_stress, new_stress = 0;
102 double nsizeScale = 0;
103 float maxEdgeLen = 0;
110 for (i = 0; i < n; i++) {
111 for (j = 1; j < graph[i].
nedges; j++) {
112 maxEdgeLen =
MAX(graph[i].ewgts[j], maxEdgeLen);
130 fprintf(stderr,
"Calculating subset model");
136 "graph is disconnected. Hence, the circuit model\n");
138 "is undefined. Reverting to the shortest path model.\n");
142 fprintf(stderr,
"Calculating MDS model");
147 fprintf(stderr,
"Calculating shortest paths");
152 fprintf(stderr,
"Setting initial positions");
157 length = n + n * (n - 1) / 2;
158 for (i = 0; i < length; i++) {
159 if (Dij[i] > diameter) {
160 diameter = (
int) Dij[i];
166 for (i = 0; i < dim; i++) {
167 for (j = 0; j < n; j++) {
168 if (fabs(d_coords[i][j]) >
max) {
169 max = fabs(d_coords[i][j]);
173 for (i = 0; i < dim; i++) {
174 for (j = 0; j < n; j++) {
175 d_coords[i][j] *= 10 /
max;
183 for (i = 0; i < dim; i++) {
188 y_0 = d_coords[1][0];
189 for (i = 0; i < n; i++) {
190 d_coords[1][i] -= y_0;
200 lap_length = n + n * (n - 1) / 2;
205 if (opt->clusters->nclusters > 0) {
206 int nn = n + opt->clusters->nclusters * 2;
207 int clap_length = nn + nn * (nn - 1) / 2;
208 float *clap =
N_GNEW(clap_length,
float);
212 for (i = 0; i < nn; i++) {
213 for (j = 0; j < nn - i; j++) {
214 if (i < n && j < n - i) {
218 if (j == 1 && i % 2 == 1) {
233 lap_length = clap_length;
237 degrees =
N_GNEW(n,
double);
239 for (i = 0; i < n - 1; i++) {
242 for (j = 1; j < n - i; j++, count++) {
245 degrees[i + j] -= val;
247 degrees[i] -= degree;
249 for (step = n, count = 0, i = 0; i < n; i++, count += step, step--) {
250 lap2[count] = (float) degrees[i];
253 coords =
N_GNEW(dim,
float *);
254 f_storage =
N_GNEW(dim * n,
float);
255 for (i = 0; i < dim; i++) {
256 coords[i] = f_storage + i * n;
257 for (j = 0; j < n; j++) {
258 coords[i][j] = j < orig_n ? (float) (d_coords[i][j]) : 0;
265 constant_term = (float) (n * (n - 1) / 2);
272 b[0] =
N_GNEW(dim * n,
float);
273 for (k = 1; k < dim; k++) {
277 tmp_coords =
N_GNEW(n,
float);
278 dist_accumulator =
N_GNEW(n,
float);
280 old_stress = DBL_MAX;
282 if ((cMajEnvHor = initCMajVPSC(n, lap2, graph, opt, 0)) ==
NULL) {
286 if ((cMajEnvVrt = initCMajVPSC(n, lap2, graph, opt, opt->diredges)) ==
NULL) {
291 lap1 =
N_GNEW(lap_length,
float);
293 for (converged =
FALSE, iterations = 0;
294 iterations < maxi && !converged; iterations++) {
299 for (count = 0, i = 0; i < n - 1; i++) {
307 for (k = 0; k < dim; k++) {
319 for (j = 0; j < len; j++) {
320 if (dist_accumulator[j] >= FLT_MAX
321 || dist_accumulator[j] < 0) {
322 dist_accumulator[j] = 0;
328 for (j = 0; j < len; j++, count++) {
329 val = lap1[count] *= dist_accumulator[j];
331 degrees[i + j + 1] -= val;
333 degrees[i] -= degree;
335 for (step = n, count = 0, i = 0; i < n; i++, count += step, step--) {
336 lap1[count] = (float) degrees[i];
340 for (k = 0; k < dim; k++) {
350 for (k = 0; k < dim; k++) {
354 new_stress += constant_term;
355 for (k = 0; k < dim; k++) {
360 #ifdef ALTERNATIVE_STRESS_CALC
362 double mat_stress = new_stress;
363 double compute_stress(
float **coords,
float *lap,
int dim,
365 new_stress = compute_stress(coords, lap2, dim, n);
366 if (fabs(mat_stress - new_stress) /
367 min(mat_stress, new_stress) > 0.001) {
369 "Diff stress vals: %lf %lf (iteration #%d)\n",
370 mat_stress, new_stress, iterations);
375 if (
Verbose && (iterations % 1 == 0)) {
376 fprintf(stderr,
"%.3f ", new_stress);
377 if (iterations % 10 == 0)
378 fprintf(stderr,
"\n");
380 converged = new_stress < old_stress
381 && fabs(new_stress - old_stress) / fabs(old_stress + 1e-10) <
387 old_stress = new_stress;
392 if ((iterations >= maxi - 1 || converged) && opt->noverlap == 1
393 && nsizeScale < 0.999) {
396 fprintf(stderr,
"nsizescale=%f,iterations=%d\n",
397 nsizeScale, iterations);
415 if (opt->noverlap == 1 && nsizeScale > 0.001) {
416 generateNonoverlapConstraints(cMajEnvHor, nsizeScale, coords,
421 if (cMajEnvHor->m > 0) {
424 mosek_quad_solve_sep(cMajEnvHor->mosekEnv, n, b[0],
428 constrained_majorization_vpsc(cMajEnvHor, b[0], coords[0],
429 localConstrMajorIterations);
440 if (opt->noverlap == 1 && nsizeScale > 0.001) {
441 generateNonoverlapConstraints(cMajEnvVrt, nsizeScale, coords,
444 if (cMajEnvVrt->m > 0) {
447 mosek_quad_solve_sep(cMajEnvVrt->mosekEnv, n, b[1],
451 if (constrained_majorization_vpsc(cMajEnvVrt, b[1], coords[1],
452 localConstrMajorIterations) < 0) {
462 fprintf(stderr,
"\nfinal e = %f %d iterations %.2f sec\n",
465 deleteCMajEnvVPSC(cMajEnvHor);
466 deleteCMajEnvVPSC(cMajEnvVrt);
468 if (opt->noverlap == 2) {
470 removeoverlaps(orig_n, coords, opt);
474 if (coords !=
NULL) {
475 for (i = 0; i < dim; i++) {
476 for (j = 0; j < orig_n; j++) {
477 d_coords[i][j] = coords[i][j];
489 free(dist_accumulator);
void vectors_additionf(int n, float *vector1, float *vector2, float *result)
void invert_sqrt_vec(int n, float *vec)
void orthog1(int n, double *vec)
void invert_vec(int n, float *vec)
int initLayout(vtx_data *graph, int n, int dim, double **coords, node_t **nodes)
void set_vector_val(int n, double val, double *result)
void set_vector_valf(int n, float val, float *result)
int agerr(agerrlevel_t level, const char *fmt,...)
void square_vec(int n, float *vec)
void vectors_mult_additionf(int n, float *vector1, float alpha, float *vector2)
double vectors_inner_productf(int n, float *vector1, float *vector2)
void sqrt_vecf(int n, float *source, float *target)
float * circuitModel(vtx_data *graph, int nG)
float * compute_apsp_packed(vtx_data *graph, int n)
Agraph_t * graph(char *name)
EXTERN unsigned char Verbose
float * compute_apsp_artifical_weights_packed(vtx_data *graph, int n)
float * mdsModel(vtx_data *graph, int nG)
void right_mult_with_vector_ff(float *packed_matrix, int n, float *vector, float *result)
int conjugate_gradient_mkernel(float *A, float *x, float *b, int n, double tol, int max_iterations)