| 1 |
/** |
| 2 |
* |
| 3 |
* Authors: |
| 4 |
* Tim Dwyer <[email protected]> |
| 5 |
* |
| 6 |
* Copyright (C) 2005 Authors |
| 7 |
* |
| 8 |
* This version is released under the CPL (Common Public License) with |
| 9 |
* the Graphviz distribution. |
| 10 |
* A version is also available under the LGPL as part of the Adaptagrams |
| 11 |
* project: http://sourceforge.net/projects/adaptagrams. |
| 12 |
* If you make improvements or bug fixes to this code it would be much |
| 13 |
* appreciated if you could also contribute those changes back to the |
| 14 |
* Adaptagrams repository. |
| 15 |
*/ |
| 16 |
|
| 17 |
/********************************************************** |
| 18 |
* |
| 19 |
* Solve a quadratic function f(X) = X' e->A X + b X |
| 20 |
* subject to a set of separation constraints e->cs |
| 21 |
* |
| 22 |
* Tim Dwyer, 2006 |
| 23 |
**********************************************************/ |
| 24 |
|
| 25 |
#include "config.h" |
| 26 |
|
| 27 |
#include <common/geomprocs.h> |
| 28 |
#include <neatogen/digcola.h> |
| 29 |
#include <stdbool.h> |
| 30 |
#include <util/alloc.h> |
| 31 |
#ifdef IPSEPCOLA |
| 32 |
#include <math.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <time.h> |
| 35 |
#include <stdio.h> |
| 36 |
#include <float.h> |
| 37 |
#include <assert.h> |
| 38 |
#include <neatogen/matrix_ops.h> |
| 39 |
#include <neatogen/kkutils.h> |
| 40 |
#include <vpsc/csolve_VPSC.h> |
| 41 |
#include <neatogen/quad_prog_vpsc.h> |
| 42 |
#include <neatogen/quad_prog_solver.h> |
| 43 |
|
| 44 |
/* #define CONMAJ_LOGGING 1 */ |
| 45 |
#define quad_prog_tol 1e-4 |
| 46 |
|
| 47 |
/* |
| 48 |
* Use gradient-projection to solve Variable Placement with Separation Constraints problem. |
| 49 |
*/ |
| 50 |
int |
| 51 |
constrained_majorization_vpsc(CMajEnvVPSC * e, float *b, float *place, |
| 52 |
int max_iterations) |
| 53 |
{ |
| 54 |
int i, j, counter; |
| 55 |
float *g, *old_place, *d; |
| 56 |
/* for laplacian computation need number of real vars and those |
| 57 |
* dummy vars included in lap |
| 58 |
*/ |
| 59 |
int n = e->nv + e->nldv; |
| 60 |
bool converged = false; |
| 61 |
#ifdef CONMAJ_LOGGING |
| 62 |
static int call_no = 0; |
| 63 |
#endif /* CONMAJ_LOGGING */ |
| 64 |
|
| 65 |
if (max_iterations == 0) |
| 66 |
return 0; |
| 67 |
g = e->fArray1; |
| 68 |
old_place = e->fArray2; |
| 69 |
d = e->fArray3; |
| 70 |
if (e->m > 0) { |
| 71 |
for (i = 0; i < n; i++) { |
| 72 |
setVariableDesiredPos(e->vs[i], place[i]); |
| 73 |
} |
| 74 |
satisfyVPSC(e->vpsc); |
| 75 |
for (i = 0; i < n; i++) { |
| 76 |
place[i] = getVariablePos(e->vs[i]); |
| 77 |
} |
| 78 |
} |
| 79 |
#ifdef CONMAJ_LOGGING |
| 80 |
float prev_stress = 0; |
| 81 |
for (i = 0; i < n; i++) { |
| 82 |
prev_stress += 2 * b[i] * place[i]; |
| 83 |
for (j = 0; j < n; j++) { |
| 84 |
prev_stress -= e->A[i][j] * place[j] * place[i]; |
| 85 |
} |
| 86 |
} |
| 87 |
FILE *logfile = fopen("constrained_majorization_log", "a"); |
| 88 |
#endif |
| 89 |
|
| 90 |
for (counter = 0; counter < max_iterations && !converged; counter++) { |
| 91 |
float test = 0; |
| 92 |
float alpha, beta; |
| 93 |
float numerator = 0, denominator = 0, r; |
| 94 |
converged = true; |
| 95 |
/* find steepest descent direction */ |
| 96 |
for (i = 0; i < n; i++) { |
| 97 |
old_place[i] = place[i]; |
| 98 |
g[i] = 2 * b[i]; |
| 99 |
for (j = 0; j < n; j++) { |
| 100 |
g[i] -= 2 * e->A[i][j] * place[j]; |
| 101 |
} |
| 102 |
} |
| 103 |
for (i = 0; i < n; i++) { |
| 104 |
numerator += g[i] * g[i]; |
| 105 |
r = 0; |
| 106 |
for (j = 0; j < n; j++) { |
| 107 |
r += 2 * e->A[i][j] * g[j]; |
| 108 |
} |
| 109 |
denominator -= r * g[i]; |
| 110 |
} |
| 111 |
if (denominator != 0) |
| 112 |
alpha = numerator / denominator; |
| 113 |
else |
| 114 |
alpha = 1.0; |
| 115 |
for (i = 0; i < n; i++) { |
| 116 |
place[i] -= alpha * g[i]; |
| 117 |
} |
| 118 |
if (e->m > 0) { |
| 119 |
/* project to constraint boundary */ |
| 120 |
for (i = 0; i < n; i++) { |
| 121 |
setVariableDesiredPos(e->vs[i], place[i]); |
| 122 |
} |
| 123 |
satisfyVPSC(e->vpsc); |
| 124 |
for (i = 0; i < n; i++) { |
| 125 |
place[i] = getVariablePos(e->vs[i]); |
| 126 |
} |
| 127 |
} |
| 128 |
/* set place to the intersection of old_place-g and boundary and |
| 129 |
* compute d, the vector from intersection pnt to projection pnt |
| 130 |
*/ |
| 131 |
for (i = 0; i < n; i++) { |
| 132 |
d[i] = place[i] - old_place[i]; |
| 133 |
} |
| 134 |
/* now compute beta */ |
| 135 |
numerator = 0, denominator = 0; |
| 136 |
for (i = 0; i < n; i++) { |
| 137 |
numerator += g[i] * d[i]; |
| 138 |
r = 0; |
| 139 |
for (j = 0; j < n; j++) { |
| 140 |
r += 2 * e->A[i][j] * d[j]; |
| 141 |
} |
| 142 |
denominator += r * d[i]; |
| 143 |
} |
| 144 |
if (denominator != 0.0) |
| 145 |
beta = numerator / denominator; |
| 146 |
else |
| 147 |
beta = 1.0; |
| 148 |
|
| 149 |
for (i = 0; i < n; i++) { |
| 150 |
/* beta > 1.0 takes us back outside the feasible region |
| 151 |
* beta < 0 clearly not useful and may happen due to numerical imp. |
| 152 |
*/ |
| 153 |
if (beta > 0 && beta < 1.0) { |
| 154 |
place[i] = old_place[i] + beta * d[i]; |
| 155 |
} |
| 156 |
test += fabsf(place[i] - old_place[i]); |
| 157 |
} |
| 158 |
#ifdef CONMAJ_LOGGING |
| 159 |
float stress = 0; |
| 160 |
for (i = 0; i < n; i++) { |
| 161 |
stress += 2 * b[i] * place[i]; |
| 162 |
for (j = 0; j < n; j++) { |
| 163 |
stress -= e->A[i][j] * place[j] * place[i]; |
| 164 |
} |
| 165 |
} |
| 166 |
fprintf(logfile, "%d: stress=%f, test=%f, %s\n", call_no, stress, |
| 167 |
test, (stress >= prev_stress) ? "No Improvement" : ""); |
| 168 |
prev_stress = stress; |
| 169 |
#endif |
| 170 |
if (test > quad_prog_tol) { |
| 171 |
converged = false; |
| 172 |
} |
| 173 |
} |
| 174 |
#ifdef CONMAJ_LOGGING |
| 175 |
call_no++; |
| 176 |
fclose(logfile); |
| 177 |
#endif |
| 178 |
return counter; |
| 179 |
} |
| 180 |
|
| 181 |
/* |
| 182 |
* Set up environment and global constraints (dir-edge constraints, containment constraints |
| 183 |
* etc). |
| 184 |
* |
| 185 |
* diredges: 0=no dir edge constraints |
| 186 |
* 1=one separation constraint for each edge (in acyclic subgraph) |
| 187 |
* 2=DiG-CoLa level constraints |
| 188 |
*/ |
| 189 |
CMajEnvVPSC *initCMajVPSC(int n, float *packedMat, vtx_data * graph, |
| 190 |
ipsep_options * opt, int diredges) |
| 191 |
{ |
| 192 |
int i; |
| 193 |
/* nv is the number of real nodes */ |
| 194 |
int nConCs; |
| 195 |
CMajEnvVPSC *e = gv_alloc(sizeof(CMajEnvVPSC)); |
| 196 |
e->A = NULL; |
| 197 |
/* if we have clusters then we'll need two constraints for each var in |
| 198 |
* a cluster */ |
| 199 |
e->nldv = 2 * opt->clusters.nclusters; |
| 200 |
e->nv = n - e->nldv; |
| 201 |
e->ndv = 0; |
| 202 |
|
| 203 |
e->gcs = NULL; |
| 204 |
e->vs = gv_calloc(n, sizeof(Variable*)); |
| 205 |
for (i = 0; i < n; i++) { |
| 206 |
e->vs[i] = newVariable(i, 1.0, 1.0); |
| 207 |
} |
| 208 |
e->gm = 0; |
| 209 |
if (diredges == 1) { |
| 210 |
if (Verbose) |
| 211 |
fprintf(stderr, " generate edge constraints...\n"); |
| 212 |
for (i = 0; i < e->nv; i++) { |
| 213 |
for (size_t j = 1; j < graph[i].nedges; j++) { |
| 214 |
if (graph[i].edists[j] > 0.01) { |
| 215 |
e->gm++; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
e->gcs = newConstraints(e->gm); |
| 220 |
e->gm = 0; |
| 221 |
for (i = 0; i < e->nv; i++) { |
| 222 |
for (size_t j = 1; j < graph[i].nedges; j++) { |
| 223 |
int u = i, v = graph[i].edges[j]; |
| 224 |
if (graph[i].edists[j] > 0) { |
| 225 |
e->gcs[e->gm++] = |
| 226 |
newConstraint(e->vs[u], e->vs[v], opt->edge_gap); |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
} else if (diredges == 2) { |
| 231 |
int *ordering = NULL, *ls = NULL, cvar; |
| 232 |
double halfgap; |
| 233 |
DigColaLevel *levels; |
| 234 |
Variable **vs = e->vs; |
| 235 |
/* e->ndv is the number of dummy variables required, one for each boundary */ |
| 236 |
if (compute_hierarchy(graph, e->nv, 1e-2, 1e-1, NULL, &ordering, &ls, |
| 237 |
&e->ndv)) return NULL; |
| 238 |
levels = assign_digcola_levels(ordering, e->nv, ls, e->ndv); |
| 239 |
if (Verbose) |
| 240 |
fprintf(stderr, "Found %d DiG-CoLa boundaries\n", e->ndv); |
| 241 |
e->gm = |
| 242 |
get_num_digcola_constraints(levels, e->ndv + 1) + e->ndv - 1; |
| 243 |
e->gcs = newConstraints(e->gm); |
| 244 |
e->gm = 0; |
| 245 |
e->vs = gv_calloc(n + e->ndv, sizeof(Variable*)); |
| 246 |
for (i = 0; i < n; i++) { |
| 247 |
e->vs[i] = vs[i]; |
| 248 |
} |
| 249 |
free(vs); |
| 250 |
/* create dummy vars */ |
| 251 |
for (i = 0; i < e->ndv; i++) { |
| 252 |
/* dummy vars should have 0 weight */ |
| 253 |
cvar = n + i; |
| 254 |
e->vs[cvar] = newVariable(cvar, 1.0, 0.000001); |
| 255 |
} |
| 256 |
halfgap = opt->edge_gap; |
| 257 |
for (i = 0; i < e->ndv; i++) { |
| 258 |
cvar = n + i; |
| 259 |
/* outgoing constraints for each var in level below boundary */ |
| 260 |
for (int j = 0; j < levels[i].num_nodes; j++) { |
| 261 |
e->gcs[e->gm++] = |
| 262 |
newConstraint(e->vs[levels[i].nodes[j]], e->vs[cvar], |
| 263 |
halfgap); |
| 264 |
} |
| 265 |
/* incoming constraints for each var in level above boundary */ |
| 266 |
for (int j = 0; j < levels[i + 1].num_nodes; j++) { |
| 267 |
e->gcs[e->gm++] = |
| 268 |
newConstraint(e->vs[cvar], |
| 269 |
e->vs[levels[i + 1].nodes[j]], halfgap); |
| 270 |
} |
| 271 |
} |
| 272 |
/* constraints between adjacent boundary dummy vars */ |
| 273 |
for (i = 0; i < e->ndv - 1; i++) { |
| 274 |
e->gcs[e->gm++] = |
| 275 |
newConstraint(e->vs[n + i], e->vs[n + i + 1], 0); |
| 276 |
} |
| 277 |
} |
| 278 |
if (opt->clusters.nclusters > 0) { |
| 279 |
Constraint **ecs = e->gcs; |
| 280 |
nConCs = 2 * opt->clusters.nvars; |
| 281 |
e->gcs = newConstraints(e->gm + nConCs); |
| 282 |
for (i = 0; i < e->gm; i++) { |
| 283 |
e->gcs[i] = ecs[i]; |
| 284 |
} |
| 285 |
if (ecs != NULL) |
| 286 |
deleteConstraints(0, ecs); |
| 287 |
for (i = 0; i < opt->clusters.nclusters; i++) { |
| 288 |
for (int j = 0; j < opt->clusters.clustersizes[i]; j++) { |
| 289 |
Variable *v = e->vs[opt->clusters.clusters[i][j]]; |
| 290 |
Variable *cl = e->vs[e->nv + 2 * i]; |
| 291 |
Variable *cr = e->vs[e->nv + 2 * i + 1]; |
| 292 |
e->gcs[e->gm++] = newConstraint(cl, v, 0); |
| 293 |
e->gcs[e->gm++] = newConstraint(v, cr, 0); |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
e->m = 0; |
| 299 |
e->cs = NULL; |
| 300 |
if (e->gm > 0) { |
| 301 |
e->vpsc = newIncVPSC(n + e->ndv, e->vs, e->gm, e->gcs); |
| 302 |
e->m = e->gm; |
| 303 |
e->cs = e->gcs; |
| 304 |
} |
| 305 |
if (packedMat != NULL) { |
| 306 |
e->A = unpackMatrix(packedMat, n); |
| 307 |
} |
| 308 |
|
| 309 |
e->fArray1 = gv_calloc(n, sizeof(float)); |
| 310 |
e->fArray2 = gv_calloc(n, sizeof(float)); |
| 311 |
e->fArray3 = gv_calloc(n, sizeof(float)); |
| 312 |
if (Verbose) |
| 313 |
fprintf(stderr, |
| 314 |
" initCMajVPSC done: %d global constraints generated.\n", |
| 315 |
e->m); |
| 316 |
return e; |
| 317 |
} |
| 318 |
|
| 319 |
void deleteCMajEnvVPSC(CMajEnvVPSC * e) |
| 320 |
{ |
| 321 |
int i; |
| 322 |
if (e->A != NULL) { |
| 323 |
free(e->A[0]); |
| 324 |
free(e->A); |
| 325 |
} |
| 326 |
if (e->m > 0) { |
| 327 |
deleteVPSC(e->vpsc); |
| 328 |
if (e->cs != e->gcs && e->gcs != NULL) |
| 329 |
deleteConstraints(0, e->gcs); |
| 330 |
deleteConstraints(e->m, e->cs); |
| 331 |
for (i = 0; i < e->nv + e->nldv + e->ndv; i++) { |
| 332 |
deleteVariable(e->vs[i]); |
| 333 |
} |
| 334 |
free(e->vs); |
| 335 |
} |
| 336 |
free(e->fArray1); |
| 337 |
free(e->fArray2); |
| 338 |
free(e->fArray3); |
| 339 |
free(e); |
| 340 |
} |
| 341 |
|
| 342 |
/* generate non-overlap constraints inside each cluster, including dummy |
| 343 |
* nodes at bounds of cluster |
| 344 |
* generate constraints again for top level nodes and clusters treating |
| 345 |
* clusters as rectangles of dim (l,r,b,t) |
| 346 |
* for each cluster map in-constraints to l out-constraints to r |
| 347 |
* |
| 348 |
* For now, we'll keep the global containment constraints already |
| 349 |
* generated for each cluster, and simply generate non-overlap constraints |
| 350 |
* for all nodes and then an additional set of non-overlap constraints for |
| 351 |
* clusters that we'll map back to the dummy vars as above. |
| 352 |
*/ |
| 353 |
void generateNonoverlapConstraints(CMajEnvVPSC * e, |
| 354 |
float nsizeScale, |
| 355 |
float **coords, |
| 356 |
int k, |
| 357 |
bool transitiveClosure, |
| 358 |
ipsep_options * opt) |
| 359 |
{ |
| 360 |
Constraint **csol, **csolptr; |
| 361 |
int i, j, mol = 0; |
| 362 |
int n = e->nv + e->nldv; |
| 363 |
boxf* bb = gv_calloc(n, sizeof(boxf)); |
| 364 |
bool genclusters = opt->clusters.nclusters > 0; |
| 365 |
if (genclusters) { |
| 366 |
/* n is the number of real variables, not dummy cluster vars */ |
| 367 |
n -= 2 * opt->clusters.nclusters; |
| 368 |
} |
| 369 |
if (k == 0) { |
| 370 |
/* grow a bit in the x dimension, so that if overlap is resolved |
| 371 |
* horizontally then it won't be considered overlapping vertically |
| 372 |
*/ |
| 373 |
nsizeScale *= 1.0001f; |
| 374 |
} |
| 375 |
for (i = 0; i < n; i++) { |
| 376 |
bb[i].LL.x = |
| 377 |
coords[0][i] - nsizeScale * opt->nsize[i].x / 2.0 - |
| 378 |
opt->gap.x / 2.0; |
| 379 |
bb[i].UR.x = |
| 380 |
coords[0][i] + nsizeScale * opt->nsize[i].x / 2.0 + |
| 381 |
opt->gap.x / 2.0; |
| 382 |
bb[i].LL.y = |
| 383 |
coords[1][i] - nsizeScale * opt->nsize[i].y / 2.0 - |
| 384 |
opt->gap.y / 2.0; |
| 385 |
bb[i].UR.y = |
| 386 |
coords[1][i] + nsizeScale * opt->nsize[i].y / 2.0 + |
| 387 |
opt->gap.y / 2.0; |
| 388 |
} |
| 389 |
if (genclusters) { |
| 390 |
Constraint ***cscl = gv_calloc(opt->clusters.nclusters + 1, |
| 391 |
sizeof(Constraint**)); |
| 392 |
int* cm = gv_calloc(opt->clusters.nclusters + 1, sizeof(int)); |
| 393 |
for (i = 0; i < opt->clusters.nclusters; i++) { |
| 394 |
int cn = opt->clusters.clustersizes[i]; |
| 395 |
Variable** cvs = gv_calloc(cn + 2, sizeof(Variable*)); |
| 396 |
boxf* cbb = gv_calloc(cn + 2, sizeof(boxf)); |
| 397 |
/* compute cluster bounding bb */ |
| 398 |
boxf container; |
| 399 |
container.LL.x = container.LL.y = DBL_MAX; |
| 400 |
container.UR.x = container.UR.y = -DBL_MAX; |
| 401 |
for (j = 0; j < cn; j++) { |
| 402 |
int iv = opt->clusters.clusters[i][j]; |
| 403 |
cvs[j] = e->vs[iv]; |
| 404 |
B2BF(bb[iv], cbb[j]); |
| 405 |
EXPANDBB(&container, bb[iv]); |
| 406 |
} |
| 407 |
B2BF(container, opt->clusters.bb[i]); |
| 408 |
cvs[cn] = e->vs[n + 2 * i]; |
| 409 |
cvs[cn + 1] = e->vs[n + 2 * i + 1]; |
| 410 |
B2BF(container, cbb[cn]); |
| 411 |
B2BF(container, cbb[cn + 1]); |
| 412 |
if (k == 0) { |
| 413 |
cbb[cn].UR.x = container.LL.x + 0.0001; |
| 414 |
cbb[cn + 1].LL.x = container.UR.x - 0.0001; |
| 415 |
cm[i] = |
| 416 |
genXConstraints(cn + 2, cbb, cvs, &cscl[i], |
| 417 |
transitiveClosure); |
| 418 |
} else { |
| 419 |
cbb[cn].UR.y = container.LL.y + 0.0001; |
| 420 |
cbb[cn + 1].LL.y = container.UR.y - 0.0001; |
| 421 |
cm[i] = genYConstraints(cn + 2, cbb, cvs, &cscl[i]); |
| 422 |
} |
| 423 |
mol += cm[i]; |
| 424 |
free (cvs); |
| 425 |
free (cbb); |
| 426 |
} |
| 427 |
/* generate top level constraints */ |
| 428 |
{ |
| 429 |
int cn = opt->clusters.ntoplevel + opt->clusters.nclusters; |
| 430 |
Variable** cvs = gv_calloc(cn, sizeof(Variable*)); |
| 431 |
boxf* cbb = gv_calloc(cn, sizeof(boxf)); |
| 432 |
for (i = 0; i < opt->clusters.ntoplevel; i++) { |
| 433 |
int iv = opt->clusters.toplevel[i]; |
| 434 |
cvs[i] = e->vs[iv]; |
| 435 |
B2BF(bb[iv], cbb[i]); |
| 436 |
} |
| 437 |
/* make dummy variables for clusters */ |
| 438 |
for (i = opt->clusters.ntoplevel; i < cn; i++) { |
| 439 |
cvs[i] = newVariable(123 + i, 1, 1); |
| 440 |
j = i - opt->clusters.ntoplevel; |
| 441 |
B2BF(opt->clusters.bb[j], cbb[i]); |
| 442 |
} |
| 443 |
i = opt->clusters.nclusters; |
| 444 |
if (k == 0) { |
| 445 |
cm[i] = |
| 446 |
genXConstraints(cn, cbb, cvs, &cscl[i], |
| 447 |
transitiveClosure); |
| 448 |
} else { |
| 449 |
cm[i] = genYConstraints(cn, cbb, cvs, &cscl[i]); |
| 450 |
} |
| 451 |
/* remap constraints from tmp dummy vars to cluster l and r vars */ |
| 452 |
for (i = opt->clusters.ntoplevel; i < cn; i++) { |
| 453 |
double dgap; |
| 454 |
j = i - opt->clusters.ntoplevel; |
| 455 |
/* dgap is the change in required constraint gap. |
| 456 |
* since we are going from a source rectangle the size |
| 457 |
* of the cluster bounding box to a zero width (in x dim, |
| 458 |
* zero height in y dim) rectangle, the change will be |
| 459 |
* half the bb width. |
| 460 |
*/ |
| 461 |
if (k == 0) { |
| 462 |
dgap = -(cbb[i].UR.x - cbb[i].LL.x) / 2.0; |
| 463 |
} else { |
| 464 |
dgap = -(cbb[i].UR.y - cbb[i].LL.y) / 2.0; |
| 465 |
} |
| 466 |
remapInConstraints(cvs[i], e->vs[n + 2 * j], dgap); |
| 467 |
remapOutConstraints(cvs[i], e->vs[n + 2 * j + 1], dgap); |
| 468 |
/* there may be problems with cycles between |
| 469 |
* cluster non-overlap and diredge constraints, |
| 470 |
* to resolve: |
| 471 |
* |
| 472 |
* for each constraint c:v->cvs[i]: |
| 473 |
* if exists diredge constraint u->v where u in c: |
| 474 |
* remap v->cl to cr->v (gap = height(v)/2) |
| 475 |
* |
| 476 |
* in = getInConstraints(cvs[i]) |
| 477 |
* for(c : in) { |
| 478 |
* assert(c.right==cvs[i]); |
| 479 |
* vin = getOutConstraints(v=c.left) |
| 480 |
* for(d : vin) { |
| 481 |
* if(d.left.cluster==i): |
| 482 |
* tmp = d.left |
| 483 |
* d.left = d.right |
| 484 |
* d.right = tmp |
| 485 |
* d.gap = height(d.right)/2 |
| 486 |
* } |
| 487 |
* } |
| 488 |
* |
| 489 |
*/ |
| 490 |
deleteVariable(cvs[i]); |
| 491 |
} |
| 492 |
mol += cm[opt->clusters.nclusters]; |
| 493 |
free (cvs); |
| 494 |
free (cbb); |
| 495 |
} |
| 496 |
csolptr = csol = newConstraints(mol); |
| 497 |
for (i = 0; i < opt->clusters.nclusters + 1; i++) { |
| 498 |
/* copy constraints into csol */ |
| 499 |
for (j = 0; j < cm[i]; j++) { |
| 500 |
*csolptr++ = cscl[i][j]; |
| 501 |
} |
| 502 |
deleteConstraints(0, cscl[i]); |
| 503 |
} |
| 504 |
free (cscl); |
| 505 |
free (cm); |
| 506 |
} else { |
| 507 |
if (k == 0) { |
| 508 |
mol = genXConstraints(n, bb, e->vs, &csol, transitiveClosure); |
| 509 |
} else { |
| 510 |
mol = genYConstraints(n, bb, e->vs, &csol); |
| 511 |
} |
| 512 |
} |
| 513 |
/* remove constraints from previous iteration */ |
| 514 |
if (e->m > 0) { |
| 515 |
/* can't reuse instance of VPSC when constraints change! */ |
| 516 |
deleteVPSC(e->vpsc); |
| 517 |
for (i = e->gm; i < e->m; i++) { |
| 518 |
/* delete previous overlap constraints */ |
| 519 |
deleteConstraint(e->cs[i]); |
| 520 |
} |
| 521 |
/* just delete the array, not the elements */ |
| 522 |
if (e->cs != e->gcs) |
| 523 |
deleteConstraints(0, e->cs); |
| 524 |
} |
| 525 |
/* if we have no global constraints then the overlap constraints |
| 526 |
* are all we have to worry about. |
| 527 |
* Otherwise, we have to copy the global and overlap constraints |
| 528 |
* into the one array |
| 529 |
*/ |
| 530 |
if (e->gm == 0) { |
| 531 |
e->m = mol; |
| 532 |
e->cs = csol; |
| 533 |
} else { |
| 534 |
e->m = mol + e->gm; |
| 535 |
e->cs = newConstraints(e->m); |
| 536 |
for (i = 0; i < e->m; i++) { |
| 537 |
if (i < e->gm) { |
| 538 |
e->cs[i] = e->gcs[i]; |
| 539 |
} else { |
| 540 |
e->cs[i] = csol[i - e->gm]; |
| 541 |
} |
| 542 |
} |
| 543 |
/* just delete the array, not the elements */ |
| 544 |
deleteConstraints(0, csol); |
| 545 |
} |
| 546 |
if (Verbose) |
| 547 |
fprintf(stderr, " generated %d constraints\n", e->m); |
| 548 |
e->vpsc = newIncVPSC(e->nv + e->nldv + e->ndv, e->vs, e->m, e->cs); |
| 549 |
free (bb); |
| 550 |
} |
| 551 |
|
| 552 |
/* |
| 553 |
* Statically remove overlaps, that is remove all overlaps by moving each node as |
| 554 |
* little as possible. |
| 555 |
*/ |
| 556 |
void removeoverlaps(int n, float **coords, ipsep_options * opt) |
| 557 |
{ |
| 558 |
int i; |
| 559 |
CMajEnvVPSC *e = initCMajVPSC(n, NULL, NULL, opt, 0); |
| 560 |
generateNonoverlapConstraints(e, 1.0, coords, 0, true, opt); |
| 561 |
solveVPSC(e->vpsc); |
| 562 |
for (i = 0; i < n; i++) { |
| 563 |
coords[0][i] = getVariablePos(e->vs[i]); |
| 564 |
} |
| 565 |
generateNonoverlapConstraints(e, 1.0, coords, 1, false, opt); |
| 566 |
solveVPSC(e->vpsc); |
| 567 |
for (i = 0; i < n; i++) { |
| 568 |
coords[1][i] = getVariablePos(e->vs[i]); |
| 569 |
} |
| 570 |
deleteCMajEnvVPSC(e); |
| 571 |
} |
| 572 |
|
| 573 |
/* |
| 574 |
unpack the "ordering" array into an array of DigColaLevel |
| 575 |
*/ |
| 576 |
DigColaLevel *assign_digcola_levels(int *ordering, int n, int *level_inds, |
| 577 |
int num_divisions) |
| 578 |
{ |
| 579 |
int i, j; |
| 580 |
DigColaLevel *l = gv_calloc(num_divisions + 1, sizeof(DigColaLevel)); |
| 581 |
/* first level */ |
| 582 |
l[0].num_nodes = level_inds[0]; |
| 583 |
l[0].nodes = gv_calloc(l[0].num_nodes, sizeof(int)); |
| 584 |
for (i = 0; i < l[0].num_nodes; i++) { |
| 585 |
l[0].nodes[i] = ordering[i]; |
| 586 |
} |
| 587 |
/* second through second last level */ |
| 588 |
for (i = 1; i < num_divisions; i++) { |
| 589 |
l[i].num_nodes = level_inds[i] - level_inds[i - 1]; |
| 590 |
l[i].nodes = gv_calloc(l[i].num_nodes, sizeof(int)); |
| 591 |
for (j = 0; j < l[i].num_nodes; j++) { |
| 592 |
l[i].nodes[j] = ordering[level_inds[i - 1] + j]; |
| 593 |
} |
| 594 |
} |
| 595 |
/* last level */ |
| 596 |
if (num_divisions > 0) { |
| 597 |
l[num_divisions].num_nodes = n - level_inds[num_divisions - 1]; |
| 598 |
l[num_divisions].nodes = gv_calloc(l[num_divisions].num_nodes, sizeof(int)); |
| 599 |
for (i = 0; i < l[num_divisions].num_nodes; i++) { |
| 600 |
l[num_divisions].nodes[i] = |
| 601 |
ordering[level_inds[num_divisions - 1] + i]; |
| 602 |
} |
| 603 |
} |
| 604 |
return l; |
| 605 |
} |
| 606 |
|
| 607 |
/********************* |
| 608 |
get number of separation constraints based on the number of nodes in each level |
| 609 |
ie, num_sep_constraints = sum_i^{num_levels-1} (|L[i]|+|L[i+1]|) |
| 610 |
**********************/ |
| 611 |
int get_num_digcola_constraints(DigColaLevel * levels, int num_levels) |
| 612 |
{ |
| 613 |
int i, nc = 0; |
| 614 |
for (i = 1; i < num_levels; i++) { |
| 615 |
nc += levels[i].num_nodes + levels[i - 1].num_nodes; |
| 616 |
} |
| 617 |
nc += levels[0].num_nodes + levels[num_levels - 1].num_nodes; |
| 618 |
return nc; |
| 619 |
} |
| 620 |
|
| 621 |
#endif /* IPSEPCOLA */ |