48#define quad_prog_tol 1e-4
54constrained_majorization_vpsc(CMajEnvVPSC * e,
float *b,
float *place,
58 float *g, *old_place, *d;
62 const size_t n = e->nv + e->nldv;
63 bool converged =
false;
65 static int call_no = 0;
68 if (max_iterations == 0)
71 old_place = e->fArray2;
74 for (
size_t i = 0; i < n; i++) {
78 for (
size_t i = 0; i < n; i++) {
83 float prev_stress = 0;
84 for (
size_t i = 0; i < n; i++) {
85 prev_stress += 2 * b[i] * place[i];
86 for (
size_t j = 0; j < n; j++) {
87 prev_stress -= e->A[i][j] * place[j] * place[i];
90 FILE *logfile = fopen(
"constrained_majorization_log",
"a");
93 for (counter = 0; counter < max_iterations && !converged; counter++) {
96 float numerator = 0, denominator = 0, r;
99 for (
size_t i = 0; i < n; i++) {
100 old_place[i] = place[i];
102 for (
size_t j = 0; j < n; j++) {
103 g[i] -= 2 * e->A[i][j] * place[j];
106 for (
size_t i = 0; i < n; i++) {
107 numerator += g[i] * g[i];
109 for (
size_t j = 0; j < n; j++) {
110 r += 2 * e->A[i][j] * g[j];
112 denominator -= r * g[i];
116 for (
size_t i = 0; i < n; i++) {
117 place[i] -=
alpha * g[i];
121 for (
size_t i = 0; i < n; i++) {
125 for (
size_t i = 0; i < n; i++) {
132 for (
size_t i = 0; i < n; i++) {
133 d[i] = place[i] - old_place[i];
136 numerator = 0, denominator = 0;
137 for (
size_t i = 0; i < n; i++) {
138 numerator += g[i] * d[i];
140 for (
size_t j = 0; j < n; j++) {
141 r += 2 * e->A[i][j] * d[j];
143 denominator += r * d[i];
148 for (
size_t i = 0; i < n; i++) {
152 if (beta > 0 && beta < 1.0) {
153 place[i] = old_place[i] + beta * d[i];
155 test += fabsf(place[i] - old_place[i]);
159 for (
size_t i = 0; i < n; i++) {
160 stress += 2 * b[i] * place[i];
161 for (
size_t j = 0; j < n; j++) {
162 stress -= e->A[i][j] * place[j] * place[i];
165 fprintf(logfile,
"%d: stress=%f, test=%f, %s\n", call_no, stress,
166 test, (stress >= prev_stress) ?
"No Improvement" :
"");
167 prev_stress = stress;
169 if (test > quad_prog_tol) {
180static DigColaLevel *assign_digcola_levels(
const int *ordering,
size_t n,
182 size_t num_divisions);
192CMajEnvVPSC *initCMajVPSC(
int n,
float *packedMat,
vtx_data *
graph,
193 ipsep_options * opt,
int diredges)
196 CMajEnvVPSC *e =
gv_alloc(
sizeof(CMajEnvVPSC));
200 e->nldv = 2 * opt->clusters.nclusters;
206 for (
int i = 0; i < n; i++) {
212 fprintf(stderr,
" generate edge constraints...\n");
213 for (
size_t i = 0; i < e->nv; i++) {
214 for (
size_t j = 1; j <
graph[i].nedges; j++) {
215 if (
graph[i].edists[j] > 0) {
222 for (
size_t i = 0; i < e->nv; i++) {
223 for (
size_t j = 1; j <
graph[i].nedges; j++) {
225 int v =
graph[i].edges[j];
226 if (
graph[i].edists[j] > 0) {
232 }
else if (diredges == 2) {
233 int *ordering =
NULL, *ls =
NULL, cvar;
235 DigColaLevel *levels;
238 if (compute_hierarchy(
graph, e->nv, 1e-2, 1e-1,
NULL, &ordering, &ls,
239 &e->ndv))
return NULL;
240 levels = assign_digcola_levels(ordering, e->nv, ls, e->ndv);
243 fprintf(stderr,
"Found %" PRISIZE_T " DiG-CoLa boundaries\n", e->ndv);
245 get_num_digcola_constraints(levels, e->ndv + 1) + e->ndv - 1;
249 for (
int i = 0; i < n; i++) {
254 for (
size_t i = 0; i < e->ndv; i++) {
259 halfgap = opt->edge_gap;
260 for (
size_t i = 0; i < e->ndv; i++) {
263 for (
int j = 0; j < levels[i].num_nodes; j++) {
269 for (
int j = 0; j < levels[i + 1].num_nodes; j++) {
272 e->vs[levels[i + 1].nodes[j]], halfgap);
276 for (
size_t i = 0; i < e->ndv - 1; i++) {
281 if (opt->clusters.nclusters > 0) {
283 const size_t nConCs = 2 * opt->clusters.nvars;
285 for (
size_t i = 0; i < e->gm; i++) {
290 for (
size_t i = 0; i < opt->clusters.nclusters; i++) {
291 for (
size_t j = 0; j < opt->clusters.clustersizes[i]; j++) {
292 Variable *v = e->vs[opt->clusters.clusters[i][j]];
293 Variable *cl = e->vs[e->nv + 2 * i];
294 Variable *cr = e->vs[e->nv + 2 * i + 1];
304 e->vpsc =
newIncVPSC(n + e->ndv, e->vs, e->gm, e->gcs);
308 if (packedMat !=
NULL) {
309 e->A = unpackMatrix(packedMat, n);
312 e->fArray1 =
gv_calloc(n,
sizeof(
float));
313 e->fArray2 =
gv_calloc(n,
sizeof(
float));
314 e->fArray3 =
gv_calloc(n,
sizeof(
float));
317 " initCMajVPSC done: %" PRISIZE_T " global constraints generated.\n",
322void deleteCMajEnvVPSC(CMajEnvVPSC * e)
330 if (e->cs != e->gcs && e->gcs !=
NULL)
333 for (
size_t i = 0; i < e->nv + e->nldv + e->ndv; i++) {
355void generateNonoverlapConstraints(CMajEnvVPSC * e,
359 bool transitiveClosure,
364 size_t n = e->nv + e->nldv;
366 bool genclusters = opt->clusters.nclusters > 0;
369 n -= 2 * opt->clusters.nclusters;
375 nsizeScale *= 1.0001f;
377 for (
size_t i = 0; i < n; i++) {
379 coords[0][i] - nsizeScale * opt->nsize[i].x / 2.0 -
382 coords[0][i] + nsizeScale * opt->nsize[i].x / 2.0 +
385 coords[1][i] - nsizeScale * opt->nsize[i].y / 2.0 -
388 coords[1][i] + nsizeScale * opt->nsize[i].y / 2.0 +
394 int* cm =
gv_calloc(opt->clusters.nclusters + 1,
sizeof(
int));
395 for (
size_t i = 0; i < opt->clusters.nclusters; i++) {
396 const size_t cn = opt->clusters.clustersizes[i];
401 container.
LL.
x = container.
LL.
y = DBL_MAX;
402 container.
UR.
x = container.
UR.
y = -DBL_MAX;
403 for (
size_t j = 0; j < cn; j++) {
404 const size_t iv = opt->clusters.clusters[i][j];
406 B2BF(bb[iv], cbb[j]);
409 B2BF(container, opt->clusters.bb[i]);
410 cvs[cn] = e->vs[n + 2 * i];
411 cvs[cn + 1] = e->vs[n + 2 * i + 1];
412 B2BF(container, cbb[cn]);
413 B2BF(container, cbb[cn + 1]);
415 cbb[cn].
UR.
x = container.
LL.
x + 0.0001;
416 cbb[cn + 1].
LL.
x = container.
UR.
x - 0.0001;
421 cbb[cn].
UR.
y = container.
LL.
y + 0.0001;
422 cbb[cn + 1].
LL.
y = container.
UR.
y - 0.0001;
431 const size_t cn = opt->clusters.ntoplevel + opt->clusters.nclusters;
434 for (
size_t i = 0; i < opt->clusters.ntoplevel; i++) {
435 const size_t iv = opt->clusters.toplevel[i];
437 B2BF(bb[iv], cbb[i]);
440 for (
size_t i = opt->clusters.ntoplevel; i < cn; i++) {
441 assert(i <= INT_MAX);
443 const size_t j = i - opt->clusters.ntoplevel;
444 B2BF(opt->clusters.bb[j], cbb[i]);
447 const size_t i = opt->clusters.nclusters;
452 const size_t i = opt->clusters.nclusters;
456 for (
size_t i = opt->clusters.ntoplevel; i < cn; i++) {
458 const size_t j = i - opt->clusters.ntoplevel;
466 dgap = -(cbb[i].
UR.
x - cbb[i].
LL.
x) / 2.0;
468 dgap = -(cbb[i].
UR.
y - cbb[i].
LL.
y) / 2.0;
496 mol += cm[opt->clusters.nclusters];
501 for (
size_t i = 0; i < opt->clusters.nclusters + 1; i++) {
503 for (
int j = 0; j < cm[i]; j++) {
504 *csolptr++ = cscl[i][j];
521 for (
size_t i = e->gm; i < e->m; i++) {
540 for (
size_t i = 0; i < e->m; i++) {
542 e->cs[i] = e->gcs[i];
544 e->cs[i] = csol[i - e->gm];
551 fprintf(stderr,
" generated %" PRISIZE_T " constraints\n", e->m);
552 e->vpsc =
newIncVPSC(e->nv + e->nldv + e->ndv, e->vs, e->m, e->cs);
560void removeoverlaps(
int n,
float **coords, ipsep_options * opt)
563 CMajEnvVPSC *e = initCMajVPSC(n,
NULL,
NULL, opt, 0);
564 generateNonoverlapConstraints(e, 1.0, coords, 0,
true, opt);
566 for (i = 0; i < n; i++) {
569 generateNonoverlapConstraints(e, 1.0, coords, 1,
false, opt);
571 for (i = 0; i < n; i++) {
574 deleteCMajEnvVPSC(e);
580static DigColaLevel *assign_digcola_levels(
const int *ordering,
size_t n,
582 size_t num_divisions) {
584 DigColaLevel *l =
gv_calloc(num_divisions + 1,
sizeof(DigColaLevel));
586 l[0].num_nodes = level_inds[0];
587 l[0].nodes =
gv_calloc(l[0].num_nodes,
sizeof(
int));
588 for (
int i = 0; i < l[0].num_nodes; i++) {
589 l[0].nodes[i] = ordering[i];
592 for (
size_t i = 1; i < num_divisions; i++) {
593 l[i].num_nodes = level_inds[i] - level_inds[i - 1];
594 l[i].nodes =
gv_calloc(l[i].num_nodes,
sizeof(
int));
595 for (j = 0; j < l[i].num_nodes; j++) {
596 l[i].nodes[j] = ordering[level_inds[i - 1] + j];
600 if (num_divisions > 0) {
601 l[num_divisions].num_nodes = n - level_inds[num_divisions - 1];
602 l[num_divisions].nodes =
gv_calloc(l[num_divisions].num_nodes,
sizeof(
int));
603 for (
int i = 0; i < l[num_divisions].num_nodes; i++) {
604 l[num_divisions].nodes[i] =
605 ordering[level_inds[num_divisions - 1] + i];
615int get_num_digcola_constraints(DigColaLevel *levels,
size_t num_levels) {
617 for (
size_t i = 1; i < num_levels; i++) {
618 nc += levels[i].num_nodes + levels[i - 1].num_nodes;
620 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)
int genYConstraints(size_t n, boxf *bb, Variable **vs, Constraint ***cs)
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)
Constraint * newConstraint(Variable *left, Variable *right, double gap)
void remapInConstraints(Variable *u, Variable *v, double dgap)
void remapOutConstraints(Variable *u, Variable *v, double dgap)
double getVariablePos(const Variable *v)
void deleteConstraints(size_t m, Constraint **cs)
void satisfyVPSC(VPSC *vpsc)
void deleteVPSC(VPSC *vpsc)
void solveVPSC(VPSC *vpsc)
int genXConstraints(size_t n, boxf *bb, Variable **vs, Constraint ***cs, bool transitiveClosure)
geometric functions (e.g. on points and boxes)
Agraph_t * graph(char *name)
arithmetic overflow helpers
static bool fdivf_overflow(float n, float d, float *res)
A constraint determines a minimum or exact spacing required between two variables.