30 #define localConstrMajorIterations 15
31 #define levels_sep_tol 1e-1
53 boolean directionalityExist =
FALSE;
55 float *dist_accumulator =
NULL;
56 float *tmp_coords =
NULL;
61 double *degrees =
NULL;
64 float *f_storage =
NULL;
65 float **coords =
NULL;
68 float **unpackedLap =
NULL;
69 CMajEnv *cMajEnv =
NULL;
76 double abs_tol = 1e-2;
78 double relative_tol = levels_sep_tol;
79 int *ordering =
NULL, *levels =
NULL;
85 double old_stress, new_stress;
89 float *hierarchy_boundaries;
91 if (graph[0].edists !=
NULL) {
92 for (i = 0; i < n; i++) {
93 for (j = 1; j < graph[i].
nedges; j++) {
94 directionalityExist = directionalityExist
95 || (graph[i].edists[j] != 0);
99 if (!directionalityExist) {
101 d_coords, nodes, dim, opts,
115 d_coords + 1, nodes, dim - 1,
116 opts, model, 15) < 0)
119 for (i = 0; i < n; i++) {
120 d_coords[dim - 1][i] = d_coords[1][i];
126 if (compute_y_coords(graph, n, y, n)) {
130 if (compute_hierarchy(graph, n, abs_tol, relative_tol, y, &ordering,
131 &levels, &num_levels)) {
135 if (num_levels < 1) {
138 d_coords, nodes, dim,
142 if (levels_gap > 0) {
144 double displacement = 0;
146 for (i = 0; i < num_levels; i++) {
149 levels_gap - (y[ordering[levels[i]]] +
151 y[ordering[levels[i] - 1]]));
152 stop = i < num_levels - 1 ? levels[i + 1] : n;
153 for (j = levels[i]; j < stop; j++) {
154 y[ordering[j]] += displacement;
159 if (IMDS_given_dim(graph, n, y, x,
Epsilon)) {
166 if (compute_hierarchy(graph, n, abs_tol, relative_tol,
NULL, &ordering,
167 &levels, &num_levels)) {
175 hierarchy_boundaries =
N_GNEW(num_levels,
float);
191 fprintf(stderr,
"Calculating subset model");
197 "graph is disconnected. Hence, the circuit model\n");
199 "is undefined. Reverting to the shortest path model.\n");
203 fprintf(stderr,
"Calculating MDS model");
208 fprintf(stderr,
"Calculating shortest paths");
213 fprintf(stderr,
"Setting initial positions");
218 length = n + n * (n - 1) / 2;
219 for (i = 0; i < length; i++) {
220 if (Dij[i] > diameter) {
221 diameter = (
int) Dij[i];
229 for (i = 0; i < dim; i++) {
230 for (j = 0; j < n; j++) {
231 if (fabs(d_coords[i][j]) >
max) {
232 max = fabs(d_coords[i][j]);
236 for (i = 0; i < dim; i++) {
237 for (j = 0; j < n; j++) {
238 d_coords[i][j] *= 10 /
max;
243 if (levels_gap > 0) {
244 int length = n + n * (n - 1) / 2;
245 double sum1, sum2, scale_ratio;
247 sum1 = (float) (n * (n - 1) / 2);
249 for (count = 0, i = 0; i < n - 1; i++) {
251 for (j = i + 1; j < n; j++, count++) {
252 sum2 +=
distance_kD(d_coords, dim, i, j) / Dij[count];
255 scale_ratio = sum2 / sum1;
257 for (i = 0; i < length; i++) {
258 Dij[i] *= (float) scale_ratio;
266 for (i = 0; i < dim; i++) {
271 y_0 = d_coords[1][0];
272 for (i = 0; i < n; i++) {
273 d_coords[1][i] -= y_0;
276 coords =
N_GNEW(dim,
float *);
277 f_storage =
N_GNEW(dim * n,
float);
278 for (i = 0; i < dim; i++) {
279 coords[i] = f_storage + i * n;
280 for (j = 0; j < n; j++) {
281 coords[i][j] = (float) (d_coords[i][j]);
288 constant_term = (float) (n * (n - 1) / 2);
298 lap_length = n + n * (n - 1) / 2;
305 degrees =
N_GNEW(n,
double);
307 for (i = 0; i < n - 1; i++) {
310 for (j = 1; j < n - i; j++, count++) {
313 degrees[i + j] -= val;
315 degrees[i] -= degree;
317 for (step = n, count = 0, i = 0; i < n; i++, count += step, step--) {
318 lap2[count] = (float) degrees[i];
323 if (n > max_nodes_in_mem) {
324 #define FILENAME "tmp_Dij$$$.bin"
325 fp = fopen(FILENAME,
"wb");
326 fwrite(lap2,
sizeof(
float), lap_length, fp);
337 b[0] =
N_GNEW(dim * n,
float);
338 for (k = 1; k < dim; k++) {
342 tmp_coords =
N_GNEW(n,
float);
343 dist_accumulator =
N_GNEW(n,
float);
345 if (n <= max_nodes_in_mem) {
347 lap1 =
N_GNEW(lap_length,
float);
351 fp = fopen(FILENAME,
"rb");
356 old_stress = DBL_MAX;
358 unpackedLap = unpackMatrix(lap2, n);
360 initConstrainedMajorization(lap2, n, ordering, levels, num_levels);
362 for (converged =
FALSE, iterations = 0;
363 iterations < maxi && !converged; iterations++) {
368 if (n <= max_nodes_in_mem) {
376 for (count = 0, i = 0; i < n - 1; i++) {
384 for (k = 0; k < dim; k++) {
396 for (j = 0; j < len; j++) {
397 if (dist_accumulator[j] >= FLT_MAX
398 || dist_accumulator[j] < 0) {
399 dist_accumulator[j] = 0;
405 for (j = 0; j < len; j++, count++) {
406 val = lap1[count] *= dist_accumulator[j];
408 degrees[i + j + 1] -= val;
410 degrees[i] -= degree;
412 for (step = n, count = 0, i = 0; i < n; i++, count += step, step--) {
413 lap1[count] = (float) degrees[i];
417 for (k = 0; k < dim; k++) {
427 for (k = 0; k < dim; k++) {
431 new_stress += constant_term;
433 if (n > max_nodes_in_mem) {
436 fread(lap2,
sizeof(
float), lap_length, fp);
439 for (k = 0; k < dim; k++) {
444 #ifdef ALTERNATIVE_STRESS_CALC
446 double mat_stress = new_stress;
447 double compute_stress(
float **coords,
float *lap,
int dim,
449 new_stress = compute_stress(coords, lap2, dim, n);
450 if (fabs(mat_stress - new_stress) /
451 min(mat_stress, new_stress) > 0.001) {
453 "Diff stress vals: %lf %lf (iteration #%d)\n",
454 mat_stress, new_stress, iterations);
460 fabs(new_stress - old_stress) / fabs(old_stress + 1e-10) <
462 converged = converged || (iterations > 1
463 && new_stress > old_stress);
467 old_stress = new_stress;
469 for (k = 0; k < dim; k++) {
484 constrained_majorization_new_with_gaps(cMajEnv, b[k],
486 localConstrMajorIterations,
487 hierarchy_boundaries,
500 free(hierarchy_boundaries);
501 deleteCMajEnv(cMajEnv);
503 if (coords !=
NULL) {
504 for (i = 0; i < dim; i++) {
505 for (j = 0; j < n; j++) {
506 d_coords[i][j] = coords[i][j];
518 free(dist_accumulator);
524 if (n <= max_nodes_in_mem) {
539 free(unpackedLap[0]);
void vectors_additionf(int n, float *vector1, float *vector2, float *result)
void invert_sqrt_vec(int n, float *vec)
void 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)
double distance_kD(double **coords, int dim, int i, int j)
void sqrt_vecf(int n, float *source, float *target)
int stress_majorization_kD_mkernel(vtx_data *graph, int n, int nedges_graph, double **d_coords, node_t **nodes, int dim, int opts, int model, int maxi)
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)