43#define quad_prog_tol 1e-4
49constrained_majorization_vpsc(CMajEnvVPSC * e,
float *b,
float *place,
53 float *g, *old_place, *d;
57 int n = e->nv + e->nldv;
58 bool converged =
false;
60 static int call_no = 0;
63 if (max_iterations == 0)
66 old_place = e->fArray2;
69 for (i = 0; i < n; i++) {
73 for (i = 0; i < n; i++) {
78 float prev_stress = 0;
79 for (i = 0; i < n; i++) {
80 prev_stress += 2 * b[i] * place[i];
81 for (j = 0; j < n; j++) {
82 prev_stress -= e->A[i][j] * place[j] * place[i];
85 FILE *logfile = fopen(
"constrained_majorization_log",
"a");
88 for (counter = 0; counter < max_iterations && !converged; counter++) {
91 float numerator = 0, denominator = 0, r;
94 for (i = 0; i < n; i++) {
95 old_place[i] = place[i];
97 for (j = 0; j < n; j++) {
98 g[i] -= 2 * e->A[i][j] * place[j];
101 for (i = 0; i < n; i++) {
102 numerator += g[i] * g[i];
104 for (j = 0; j < n; j++) {
105 r += 2 * e->A[i][j] * g[j];
107 denominator -= r * g[i];
109 if (denominator != 0)
110 alpha = numerator / denominator;
113 for (i = 0; i < n; i++) {
114 place[i] -=
alpha * g[i];
118 for (i = 0; i < n; i++) {
122 for (i = 0; i < n; i++) {
129 for (i = 0; i < n; i++) {
130 d[i] = place[i] - old_place[i];
133 numerator = 0, denominator = 0;
134 for (i = 0; i < n; i++) {
135 numerator += g[i] * d[i];
137 for (j = 0; j < n; j++) {
138 r += 2 * e->A[i][j] * d[j];
140 denominator += r * d[i];
142 if (denominator != 0.0)
143 beta = numerator / denominator;
147 for (i = 0; i < n; i++) {
151 if (beta > 0 && beta < 1.0) {
152 place[i] = old_place[i] + beta * d[i];
154 test += fabsf(place[i] - old_place[i]);
158 for (i = 0; i < n; i++) {
159 stress += 2 * b[i] * place[i];
160 for (j = 0; j < n; j++) {
161 stress -= e->A[i][j] * place[j] * place[i];
164 fprintf(logfile,
"%d: stress=%f, test=%f, %s\n", call_no, stress,
165 test, (stress >= prev_stress) ?
"No Improvement" :
"");
166 prev_stress = stress;
168 if (test > quad_prog_tol) {
187CMajEnvVPSC *initCMajVPSC(
int n,
float *packedMat,
vtx_data *
graph,
188 ipsep_options * opt,
int diredges)
193 CMajEnvVPSC *e =
gv_alloc(
sizeof(CMajEnvVPSC));
197 e->nldv = 2 * opt->clusters.nclusters;
203 for (i = 0; i < n; i++) {
209 fprintf(stderr,
" generate edge constraints...\n");
210 for (i = 0; i < e->nv; i++) {
211 for (
size_t j = 1; j <
graph[i].nedges; j++) {
212 if (
graph[i].edists[j] > 0.01) {
219 for (i = 0; i < e->nv; i++) {
220 for (
size_t j = 1; j <
graph[i].nedges; j++) {
221 int u = i, v =
graph[i].edges[j];
222 if (
graph[i].edists[j] > 0) {
228 }
else if (diredges == 2) {
229 int *ordering =
NULL, *ls =
NULL, cvar;
231 DigColaLevel *levels;
234 if (compute_hierarchy(
graph, e->nv, 1e-2, 1e-1,
NULL, &ordering, &ls,
235 &e->ndv))
return NULL;
236 levels = assign_digcola_levels(ordering, e->nv, ls, e->ndv);
238 fprintf(stderr,
"Found %d DiG-CoLa boundaries\n", e->ndv);
240 get_num_digcola_constraints(levels, e->ndv + 1) + e->ndv - 1;
244 for (i = 0; i < n; i++) {
249 for (i = 0; i < e->ndv; i++) {
254 halfgap = opt->edge_gap;
255 for (i = 0; i < e->ndv; i++) {
258 for (
int j = 0; j < levels[i].num_nodes; j++) {
264 for (
int j = 0; j < levels[i + 1].num_nodes; j++) {
267 e->vs[levels[i + 1].nodes[j]], halfgap);
271 for (i = 0; i < e->ndv - 1; i++) {
276 if (opt->clusters.nclusters > 0) {
278 nConCs = 2 * opt->clusters.nvars;
280 for (i = 0; i < e->gm; i++) {
285 for (i = 0; i < opt->clusters.nclusters; i++) {
286 for (
int j = 0; j < opt->clusters.clustersizes[i]; j++) {
287 Variable *v = e->vs[opt->clusters.clusters[i][j]];
288 Variable *cl = e->vs[e->nv + 2 * i];
289 Variable *cr = e->vs[e->nv + 2 * i + 1];
299 e->vpsc =
newIncVPSC(n + e->ndv, e->vs, e->gm, e->gcs);
303 if (packedMat !=
NULL) {
304 e->A = unpackMatrix(packedMat, n);
307 e->fArray1 =
gv_calloc(n,
sizeof(
float));
308 e->fArray2 =
gv_calloc(n,
sizeof(
float));
309 e->fArray3 =
gv_calloc(n,
sizeof(
float));
312 " initCMajVPSC done: %d global constraints generated.\n",
317void deleteCMajEnvVPSC(CMajEnvVPSC * e)
326 if (e->cs != e->gcs && e->gcs !=
NULL)
329 for (i = 0; i < e->nv + e->nldv + e->ndv; i++) {
351void generateNonoverlapConstraints(CMajEnvVPSC * e,
355 bool transitiveClosure,
360 int n = e->nv + e->nldv;
362 bool genclusters = opt->clusters.nclusters > 0;
365 n -= 2 * opt->clusters.nclusters;
371 nsizeScale *= 1.0001f;
373 for (i = 0; i < n; i++) {
375 coords[0][i] - nsizeScale * opt->nsize[i].x / 2.0 -
378 coords[0][i] + nsizeScale * opt->nsize[i].x / 2.0 +
381 coords[1][i] - nsizeScale * opt->nsize[i].y / 2.0 -
384 coords[1][i] + nsizeScale * opt->nsize[i].y / 2.0 +
390 int* cm =
gv_calloc(opt->clusters.nclusters + 1,
sizeof(
int));
391 for (i = 0; i < opt->clusters.nclusters; i++) {
392 int cn = opt->clusters.clustersizes[i];
397 container.
LL.
x = container.
LL.
y = DBL_MAX;
398 container.
UR.
x = container.
UR.
y = -DBL_MAX;
399 for (j = 0; j < cn; j++) {
400 int iv = opt->clusters.clusters[i][j];
402 B2BF(bb[iv], cbb[j]);
405 B2BF(container, opt->clusters.bb[i]);
406 cvs[cn] = e->vs[n + 2 * i];
407 cvs[cn + 1] = e->vs[n + 2 * i + 1];
408 B2BF(container, cbb[cn]);
409 B2BF(container, cbb[cn + 1]);
411 cbb[cn].
UR.
x = container.
LL.
x + 0.0001;
412 cbb[cn + 1].
LL.
x = container.
UR.
x - 0.0001;
417 cbb[cn].
UR.
y = container.
LL.
y + 0.0001;
418 cbb[cn + 1].
LL.
y = container.
UR.
y - 0.0001;
427 int cn = opt->clusters.ntoplevel + opt->clusters.nclusters;
430 for (i = 0; i < opt->clusters.ntoplevel; i++) {
431 int iv = opt->clusters.toplevel[i];
433 B2BF(bb[iv], cbb[i]);
436 for (i = opt->clusters.ntoplevel; i < cn; i++) {
438 j = i - opt->clusters.ntoplevel;
439 B2BF(opt->clusters.bb[j], cbb[i]);
441 i = opt->clusters.nclusters;
450 for (i = opt->clusters.ntoplevel; i < cn; i++) {
452 j = i - opt->clusters.ntoplevel;
460 dgap = -(cbb[i].
UR.
x - cbb[i].
LL.
x) / 2.0;
462 dgap = -(cbb[i].
UR.
y - cbb[i].
LL.
y) / 2.0;
490 mol += cm[opt->clusters.nclusters];
495 for (i = 0; i < opt->clusters.nclusters + 1; i++) {
497 for (j = 0; j < cm[i]; j++) {
498 *csolptr++ = cscl[i][j];
515 for (i = e->gm; i < e->m; i++) {
534 for (i = 0; i < e->m; i++) {
536 e->cs[i] = e->gcs[i];
538 e->cs[i] = csol[i - e->gm];
545 fprintf(stderr,
" generated %d constraints\n", e->m);
546 e->vpsc =
newIncVPSC(e->nv + e->nldv + e->ndv, e->vs, e->m, e->cs);
554void removeoverlaps(
int n,
float **coords, ipsep_options * opt)
557 CMajEnvVPSC *e = initCMajVPSC(n,
NULL,
NULL, opt, 0);
558 generateNonoverlapConstraints(e, 1.0, coords, 0,
true, opt);
560 for (i = 0; i < n; i++) {
563 generateNonoverlapConstraints(e, 1.0, coords, 1,
false, opt);
565 for (i = 0; i < n; i++) {
568 deleteCMajEnvVPSC(e);
574DigColaLevel *assign_digcola_levels(
int *ordering,
int n,
int *level_inds,
578 DigColaLevel *l =
gv_calloc(num_divisions + 1,
sizeof(DigColaLevel));
580 l[0].num_nodes = level_inds[0];
581 l[0].nodes =
gv_calloc(l[0].num_nodes,
sizeof(
int));
582 for (i = 0; i < l[0].num_nodes; i++) {
583 l[0].nodes[i] = ordering[i];
586 for (i = 1; i < num_divisions; i++) {
587 l[i].num_nodes = level_inds[i] - level_inds[i - 1];
588 l[i].nodes =
gv_calloc(l[i].num_nodes,
sizeof(
int));
589 for (j = 0; j < l[i].num_nodes; j++) {
590 l[i].nodes[j] = ordering[level_inds[i - 1] + j];
594 if (num_divisions > 0) {
595 l[num_divisions].num_nodes = n - level_inds[num_divisions - 1];
596 l[num_divisions].nodes =
gv_calloc(l[num_divisions].num_nodes,
sizeof(
int));
597 for (i = 0; i < l[num_divisions].num_nodes; i++) {
598 l[num_divisions].nodes[i] =
599 ordering[level_inds[num_divisions - 1] + i];
609int get_num_digcola_constraints(DigColaLevel * levels,
int num_levels)
612 for (i = 1; i < num_levels; i++) {
613 nc += levels[i].num_nodes + levels[i - 1].num_nodes;
615 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)
geometric functions (e.g. on points and boxes)
Agraph_t * graph(char *name)
A constraint determines a minimum or exact spacing required between two variables.