42#define quad_prog_tol 1e-4
48constrained_majorization_vpsc(CMajEnvVPSC * e,
float *b,
float *place,
52 float *g, *old_place, *d;
56 int n = e->nv + e->nldv;
57 bool converged =
false;
59 static int call_no = 0;
62 if (max_iterations == 0)
65 old_place = e->fArray2;
68 for (i = 0; i < n; i++) {
72 for (i = 0; i < n; i++) {
77 float prev_stress = 0;
78 for (i = 0; i < n; i++) {
79 prev_stress += 2 * b[i] * place[i];
80 for (j = 0; j < n; j++) {
81 prev_stress -= e->A[i][j] * place[j] * place[i];
84 FILE *logfile = fopen(
"constrained_majorization_log",
"a");
87 for (counter = 0; counter < max_iterations && !converged; counter++) {
90 float numerator = 0, denominator = 0, r;
93 for (i = 0; i < n; i++) {
94 old_place[i] = place[i];
96 for (j = 0; j < n; j++) {
97 g[i] -= 2 * e->A[i][j] * place[j];
100 for (i = 0; i < n; i++) {
101 numerator += g[i] * g[i];
103 for (j = 0; j < n; j++) {
104 r += 2 * e->A[i][j] * g[j];
106 denominator -= r * g[i];
108 if (denominator != 0)
109 alpha = numerator / denominator;
112 for (i = 0; i < n; i++) {
113 place[i] -=
alpha * g[i];
117 for (i = 0; i < n; i++) {
121 for (i = 0; i < n; i++) {
128 for (i = 0; i < n; i++) {
129 d[i] = place[i] - old_place[i];
132 numerator = 0, denominator = 0;
133 for (i = 0; i < n; i++) {
134 numerator += g[i] * d[i];
136 for (j = 0; j < n; j++) {
137 r += 2 * e->A[i][j] * d[j];
139 denominator += r * d[i];
141 if (denominator != 0.0)
142 beta = numerator / denominator;
146 for (i = 0; i < n; i++) {
150 if (beta > 0 && beta < 1.0) {
151 place[i] = old_place[i] + beta * d[i];
153 test += fabsf(place[i] - old_place[i]);
157 for (i = 0; i < n; i++) {
158 stress += 2 * b[i] * place[i];
159 for (j = 0; j < n; j++) {
160 stress -= e->A[i][j] * place[j] * place[i];
163 fprintf(logfile,
"%d: stress=%f, test=%f, %s\n", call_no, stress,
164 test, (stress >= prev_stress) ?
"No Improvement" :
"");
165 prev_stress = stress;
167 if (test > quad_prog_tol) {
186CMajEnvVPSC *initCMajVPSC(
int n,
float *packedMat,
vtx_data *
graph,
187 ipsep_options * opt,
int diredges)
192 CMajEnvVPSC *e =
gv_alloc(
sizeof(CMajEnvVPSC));
196 e->nldv = 2 * opt->clusters.nclusters;
202 for (i = 0; i < n; i++) {
208 fprintf(stderr,
" generate edge constraints...\n");
209 for (i = 0; i < e->nv; i++) {
210 for (
size_t j = 1; j <
graph[i].nedges; j++) {
211 if (
graph[i].edists[j] > 0.01) {
218 for (i = 0; i < e->nv; i++) {
219 for (
size_t j = 1; j <
graph[i].nedges; j++) {
220 int u = i, v =
graph[i].edges[j];
221 if (
graph[i].edists[j] > 0) {
227 }
else if (diredges == 2) {
228 int *ordering =
NULL, *ls =
NULL, cvar;
230 DigColaLevel *levels;
233 if (compute_hierarchy(
graph, e->nv, 1e-2, 1e-1,
NULL, &ordering, &ls,
234 &e->ndv))
return NULL;
235 levels = assign_digcola_levels(ordering, e->nv, ls, e->ndv);
237 fprintf(stderr,
"Found %d DiG-CoLa boundaries\n", e->ndv);
239 get_num_digcola_constraints(levels, e->ndv + 1) + e->ndv - 1;
243 for (i = 0; i < n; i++) {
248 for (i = 0; i < e->ndv; i++) {
253 halfgap = opt->edge_gap;
254 for (i = 0; i < e->ndv; i++) {
257 for (
int j = 0; j < levels[i].num_nodes; j++) {
263 for (
int j = 0; j < levels[i + 1].num_nodes; j++) {
266 e->vs[levels[i + 1].nodes[j]], halfgap);
270 for (i = 0; i < e->ndv - 1; i++) {
275 if (opt->clusters.nclusters > 0) {
277 nConCs = 2 * opt->clusters.nvars;
279 for (i = 0; i < e->gm; i++) {
284 for (i = 0; i < opt->clusters.nclusters; i++) {
285 for (
int j = 0; j < opt->clusters.clustersizes[i]; j++) {
286 Variable *v = e->vs[opt->clusters.clusters[i][j]];
287 Variable *cl = e->vs[e->nv + 2 * i];
288 Variable *cr = e->vs[e->nv + 2 * i + 1];
298 e->vpsc =
newIncVPSC(n + e->ndv, e->vs, e->gm, e->gcs);
302 if (packedMat !=
NULL) {
303 e->A = unpackMatrix(packedMat, n);
306 e->fArray1 =
gv_calloc(n,
sizeof(
float));
307 e->fArray2 =
gv_calloc(n,
sizeof(
float));
308 e->fArray3 =
gv_calloc(n,
sizeof(
float));
311 " initCMajVPSC done: %d global constraints generated.\n",
316void deleteCMajEnvVPSC(CMajEnvVPSC * e)
325 if (e->cs != e->gcs && e->gcs !=
NULL)
328 for (i = 0; i < e->nv + e->nldv + e->ndv; i++) {
350void generateNonoverlapConstraints(CMajEnvVPSC * e,
354 bool transitiveClosure,
359 int n = e->nv + e->nldv;
361 bool genclusters = opt->clusters.nclusters > 0;
364 n -= 2 * opt->clusters.nclusters;
370 nsizeScale *= 1.0001f;
372 for (i = 0; i < n; i++) {
374 coords[0][i] - nsizeScale * opt->nsize[i].x / 2.0 -
377 coords[0][i] + nsizeScale * opt->nsize[i].x / 2.0 +
380 coords[1][i] - nsizeScale * opt->nsize[i].y / 2.0 -
383 coords[1][i] + nsizeScale * opt->nsize[i].y / 2.0 +
389 int* cm =
gv_calloc(opt->clusters.nclusters + 1,
sizeof(
int));
390 for (i = 0; i < opt->clusters.nclusters; i++) {
391 int cn = opt->clusters.clustersizes[i];
396 container.
LL.
x = container.
LL.
y = DBL_MAX;
397 container.
UR.
x = container.
UR.
y = -DBL_MAX;
398 for (j = 0; j < cn; j++) {
399 int iv = opt->clusters.clusters[i][j];
401 B2BF(bb[iv], cbb[j]);
404 B2BF(container, opt->clusters.bb[i]);
405 cvs[cn] = e->vs[n + 2 * i];
406 cvs[cn + 1] = e->vs[n + 2 * i + 1];
407 B2BF(container, cbb[cn]);
408 B2BF(container, cbb[cn + 1]);
410 cbb[cn].
UR.
x = container.
LL.
x + 0.0001;
411 cbb[cn + 1].
LL.
x = container.
UR.
x - 0.0001;
416 cbb[cn].
UR.
y = container.
LL.
y + 0.0001;
417 cbb[cn + 1].
LL.
y = container.
UR.
y - 0.0001;
426 int cn = opt->clusters.ntoplevel + opt->clusters.nclusters;
429 for (i = 0; i < opt->clusters.ntoplevel; i++) {
430 int iv = opt->clusters.toplevel[i];
432 B2BF(bb[iv], cbb[i]);
435 for (i = opt->clusters.ntoplevel; i < cn; i++) {
437 j = i - opt->clusters.ntoplevel;
438 B2BF(opt->clusters.bb[j], cbb[i]);
440 i = opt->clusters.nclusters;
449 for (i = opt->clusters.ntoplevel; i < cn; i++) {
451 j = i - opt->clusters.ntoplevel;
459 dgap = -(cbb[i].
UR.
x - cbb[i].
LL.
x) / 2.0;
461 dgap = -(cbb[i].
UR.
y - cbb[i].
LL.
y) / 2.0;
489 mol += cm[opt->clusters.nclusters];
494 for (i = 0; i < opt->clusters.nclusters + 1; i++) {
496 for (j = 0; j < cm[i]; j++) {
497 *csolptr++ = cscl[i][j];
514 for (i = e->gm; i < e->m; i++) {
533 for (i = 0; i < e->m; i++) {
535 e->cs[i] = e->gcs[i];
537 e->cs[i] = csol[i - e->gm];
544 fprintf(stderr,
" generated %d constraints\n", e->m);
545 e->vpsc =
newIncVPSC(e->nv + e->nldv + e->ndv, e->vs, e->m, e->cs);
553void removeoverlaps(
int n,
float **coords, ipsep_options * opt)
556 CMajEnvVPSC *e = initCMajVPSC(n,
NULL,
NULL, opt, 0);
557 generateNonoverlapConstraints(e, 1.0, coords, 0,
true, opt);
559 for (i = 0; i < n; i++) {
562 generateNonoverlapConstraints(e, 1.0, coords, 1,
false, opt);
564 for (i = 0; i < n; i++) {
567 deleteCMajEnvVPSC(e);
573DigColaLevel *assign_digcola_levels(
int *ordering,
int n,
int *level_inds,
577 DigColaLevel *l =
gv_calloc(num_divisions + 1,
sizeof(DigColaLevel));
579 l[0].num_nodes = level_inds[0];
580 l[0].nodes =
gv_calloc(l[0].num_nodes,
sizeof(
int));
581 for (i = 0; i < l[0].num_nodes; i++) {
582 l[0].nodes[i] = ordering[i];
585 for (i = 1; i < num_divisions; i++) {
586 l[i].num_nodes = level_inds[i] - level_inds[i - 1];
587 l[i].nodes =
gv_calloc(l[i].num_nodes,
sizeof(
int));
588 for (j = 0; j < l[i].num_nodes; j++) {
589 l[i].nodes[j] = ordering[level_inds[i - 1] + j];
593 if (num_divisions > 0) {
594 l[num_divisions].num_nodes = n - level_inds[num_divisions - 1];
595 l[num_divisions].nodes =
gv_calloc(l[num_divisions].num_nodes,
sizeof(
int));
596 for (i = 0; i < l[num_divisions].num_nodes; i++) {
597 l[num_divisions].nodes[i] =
598 ordering[level_inds[num_divisions - 1] + i];
608int get_num_digcola_constraints(DigColaLevel * levels,
int num_levels)
611 for (i = 1; i < num_levels; i++) {
612 nc += levels[i].num_nodes + levels[i - 1].num_nodes;
614 nc += levels[0].num_nodes + levels[num_levels - 1].num_nodes;
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
static void * gv_alloc(size_t size)
void setVariableDesiredPos(Variable *v, double desiredPos)
Variable * newVariable(int id, double desiredPos, double weight)
Bridge for C programs to access solve_VPSC (which is in C++)
Constraint ** newConstraints(int m)
void deleteConstraint(Constraint *c)
VPSC * newIncVPSC(int n, Variable *vs[], int m, Constraint *cs[])
void deleteVariable(Variable *v)
void deleteConstraints(int m, Constraint **cs)
Constraint * newConstraint(Variable *left, Variable *right, double gap)
void remapInConstraints(Variable *u, Variable *v, double dgap)
int genXConstraints(int n, boxf *bb, Variable **vs, Constraint ***cs, bool transitiveClosure)
void remapOutConstraints(Variable *u, Variable *v, double dgap)
double getVariablePos(const Variable *v)
void satisfyVPSC(VPSC *vpsc)
int genYConstraints(int n, boxf *bb, Variable **vs, Constraint ***cs)
void deleteVPSC(VPSC *vpsc)
void solveVPSC(VPSC *vpsc)
Agraph_t * graph(char *name)
A constraint determines a minimum or exact spacing required between two variables.