46#define quad_prog_tol 1e-4
52constrained_majorization_vpsc(CMajEnvVPSC * e,
float *b,
float *place,
56 float *g, *old_place, *d;
60 int n = e->nv + e->nldv;
61 bool converged =
false;
63 static int call_no = 0;
66 if (max_iterations == 0)
69 old_place = e->fArray2;
72 for (i = 0; i < n; i++) {
76 for (i = 0; i < n; i++) {
81 float prev_stress = 0;
82 for (i = 0; i < n; i++) {
83 prev_stress += 2 * b[i] * place[i];
84 for (j = 0; j < n; j++) {
85 prev_stress -= e->A[i][j] * place[j] * place[i];
88 FILE *logfile = fopen(
"constrained_majorization_log",
"a");
91 for (counter = 0; counter < max_iterations && !converged; counter++) {
94 float numerator = 0, denominator = 0, r;
97 for (i = 0; i < n; i++) {
98 old_place[i] = place[i];
100 for (j = 0; j < n; j++) {
101 g[i] -= 2 * e->A[i][j] * place[j];
104 for (i = 0; i < n; i++) {
105 numerator += g[i] * g[i];
107 for (j = 0; j < n; j++) {
108 r += 2 * e->A[i][j] * g[j];
110 denominator -= r * g[i];
112 if (denominator != 0)
113 alpha = numerator / denominator;
116 for (i = 0; i < n; i++) {
117 place[i] -=
alpha * g[i];
121 for (i = 0; i < n; i++) {
125 for (i = 0; i < n; i++) {
132 for (i = 0; i < n; i++) {
133 d[i] = place[i] - old_place[i];
136 numerator = 0, denominator = 0;
137 for (i = 0; i < n; i++) {
138 numerator += g[i] * d[i];
140 for (j = 0; j < n; j++) {
141 r += 2 * e->A[i][j] * d[j];
143 denominator += r * d[i];
146 beta = numerator / denominator;
150 for (i = 0; i < n; i++) {
154 if (beta > 0 && beta < 1.0) {
155 place[i] = old_place[i] + beta * d[i];
157 test += fabsf(place[i] - old_place[i]);
161 for (i = 0; i < n; i++) {
162 stress += 2 * b[i] * place[i];
163 for (j = 0; j < n; j++) {
164 stress -= e->A[i][j] * place[j] * place[i];
167 fprintf(logfile,
"%d: stress=%f, test=%f, %s\n", call_no, stress,
168 test, (stress >= prev_stress) ?
"No Improvement" :
"");
169 prev_stress = stress;
171 if (test > quad_prog_tol) {
190CMajEnvVPSC *initCMajVPSC(
int n,
float *packedMat,
vtx_data *
graph,
191 ipsep_options * opt,
int diredges)
196 CMajEnvVPSC *e =
gv_alloc(
sizeof(CMajEnvVPSC));
200 e->nldv = 2 * opt->clusters.nclusters;
206 for (i = 0; i < n; i++) {
212 fprintf(stderr,
" generate edge constraints...\n");
213 for (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 (i = 0; i < e->nv; i++) {
223 for (
size_t j = 1; j <
graph[i].nedges; j++) {
224 int u = i, v =
graph[i].edges[j];
225 if (
graph[i].edists[j] > 0) {
231 }
else if (diredges == 2) {
232 int *ordering =
NULL, *ls =
NULL, cvar;
234 DigColaLevel *levels;
237 if (compute_hierarchy(
graph, e->nv, 1e-2, 1e-1,
NULL, &ordering, &ls,
238 &e->ndv))
return NULL;
239 levels = assign_digcola_levels(ordering, e->nv, ls, e->ndv);
241 fprintf(stderr,
"Found %d DiG-CoLa boundaries\n", e->ndv);
243 get_num_digcola_constraints(levels, e->ndv + 1) + e->ndv - 1;
247 for (i = 0; i < n; i++) {
252 for (i = 0; i < e->ndv; i++) {
257 halfgap = opt->edge_gap;
258 for (i = 0; i < e->ndv; i++) {
261 for (
int j = 0; j < levels[i].num_nodes; j++) {
267 for (
int j = 0; j < levels[i + 1].num_nodes; j++) {
270 e->vs[levels[i + 1].nodes[j]], halfgap);
274 for (i = 0; i < e->ndv - 1; i++) {
279 if (opt->clusters.nclusters > 0) {
281 nConCs = 2 * opt->clusters.nvars;
283 for (i = 0; i < e->gm; i++) {
288 for (i = 0; i < opt->clusters.nclusters; i++) {
289 for (
int j = 0; j < opt->clusters.clustersizes[i]; j++) {
290 Variable *v = e->vs[opt->clusters.clusters[i][j]];
291 Variable *cl = e->vs[e->nv + 2 * i];
292 Variable *cr = e->vs[e->nv + 2 * i + 1];
302 e->vpsc =
newIncVPSC(n + e->ndv, e->vs, e->gm, e->gcs);
306 if (packedMat !=
NULL) {
307 e->A = unpackMatrix(packedMat, n);
310 e->fArray1 =
gv_calloc(n,
sizeof(
float));
311 e->fArray2 =
gv_calloc(n,
sizeof(
float));
312 e->fArray3 =
gv_calloc(n,
sizeof(
float));
315 " initCMajVPSC done: %d global constraints generated.\n",
320void deleteCMajEnvVPSC(CMajEnvVPSC * e)
329 if (e->cs != e->gcs && e->gcs !=
NULL)
332 for (i = 0; i < e->nv + e->nldv + e->ndv; i++) {
354void generateNonoverlapConstraints(CMajEnvVPSC * e,
358 bool transitiveClosure,
363 int n = e->nv + e->nldv;
365 bool genclusters = opt->clusters.nclusters > 0;
368 n -= 2 * opt->clusters.nclusters;
374 nsizeScale *= 1.0001f;
376 for (i = 0; i < n; i++) {
378 coords[0][i] - nsizeScale * opt->nsize[i].x / 2.0 -
381 coords[0][i] + nsizeScale * opt->nsize[i].x / 2.0 +
384 coords[1][i] - nsizeScale * opt->nsize[i].y / 2.0 -
387 coords[1][i] + nsizeScale * opt->nsize[i].y / 2.0 +
393 int* cm =
gv_calloc(opt->clusters.nclusters + 1,
sizeof(
int));
394 for (i = 0; i < opt->clusters.nclusters; i++) {
395 int cn = opt->clusters.clustersizes[i];
400 container.
LL.
x = container.
LL.
y = DBL_MAX;
401 container.
UR.
x = container.
UR.
y = -DBL_MAX;
402 for (j = 0; j < cn; j++) {
403 int iv = opt->clusters.clusters[i][j];
405 B2BF(bb[iv], cbb[j]);
408 B2BF(container, opt->clusters.bb[i]);
409 cvs[cn] = e->vs[n + 2 * i];
410 cvs[cn + 1] = e->vs[n + 2 * i + 1];
411 B2BF(container, cbb[cn]);
412 B2BF(container, cbb[cn + 1]);
414 cbb[cn].
UR.
x = container.
LL.
x + 0.0001;
415 cbb[cn + 1].
LL.
x = container.
UR.
x - 0.0001;
420 cbb[cn].
UR.
y = container.
LL.
y + 0.0001;
421 cbb[cn + 1].
LL.
y = container.
UR.
y - 0.0001;
430 int cn = opt->clusters.ntoplevel + opt->clusters.nclusters;
433 for (i = 0; i < opt->clusters.ntoplevel; i++) {
434 int iv = opt->clusters.toplevel[i];
436 B2BF(bb[iv], cbb[i]);
439 for (i = opt->clusters.ntoplevel; i < cn; i++) {
441 j = i - opt->clusters.ntoplevel;
442 B2BF(opt->clusters.bb[j], cbb[i]);
444 i = opt->clusters.nclusters;
453 for (i = opt->clusters.ntoplevel; i < cn; i++) {
455 j = i - opt->clusters.ntoplevel;
463 dgap = -(cbb[i].
UR.
x - cbb[i].
LL.
x) / 2.0;
465 dgap = -(cbb[i].
UR.
y - cbb[i].
LL.
y) / 2.0;
493 mol += cm[opt->clusters.nclusters];
498 for (i = 0; i < opt->clusters.nclusters + 1; i++) {
500 for (j = 0; j < cm[i]; j++) {
501 *csolptr++ = cscl[i][j];
518 for (i = e->gm; i < e->m; i++) {
537 for (i = 0; i < e->m; i++) {
539 e->cs[i] = e->gcs[i];
541 e->cs[i] = csol[i - e->gm];
548 fprintf(stderr,
" generated %d constraints\n", e->m);
549 e->vpsc =
newIncVPSC(e->nv + e->nldv + e->ndv, e->vs, e->m, e->cs);
557void removeoverlaps(
int n,
float **coords, ipsep_options * opt)
560 CMajEnvVPSC *e = initCMajVPSC(n,
NULL,
NULL, opt, 0);
561 generateNonoverlapConstraints(e, 1.0, coords, 0,
true, opt);
563 for (i = 0; i < n; i++) {
566 generateNonoverlapConstraints(e, 1.0, coords, 1,
false, opt);
568 for (i = 0; i < n; i++) {
571 deleteCMajEnvVPSC(e);
577DigColaLevel *assign_digcola_levels(
int *ordering,
int n,
int *level_inds,
581 DigColaLevel *l =
gv_calloc(num_divisions + 1,
sizeof(DigColaLevel));
583 l[0].num_nodes = level_inds[0];
584 l[0].nodes =
gv_calloc(l[0].num_nodes,
sizeof(
int));
585 for (i = 0; i < l[0].num_nodes; i++) {
586 l[0].nodes[i] = ordering[i];
589 for (i = 1; i < num_divisions; i++) {
590 l[i].num_nodes = level_inds[i] - level_inds[i - 1];
591 l[i].nodes =
gv_calloc(l[i].num_nodes,
sizeof(
int));
592 for (j = 0; j < l[i].num_nodes; j++) {
593 l[i].nodes[j] = ordering[level_inds[i - 1] + j];
597 if (num_divisions > 0) {
598 l[num_divisions].num_nodes = n - level_inds[num_divisions - 1];
599 l[num_divisions].nodes =
gv_calloc(l[num_divisions].num_nodes,
sizeof(
int));
600 for (i = 0; i < l[num_divisions].num_nodes; i++) {
601 l[num_divisions].nodes[i] =
602 ordering[level_inds[num_divisions - 1] + i];
612int get_num_digcola_constraints(DigColaLevel * levels,
int num_levels)
615 for (i = 1; i < num_levels; i++) {
616 nc += levels[i].num_nodes + levels[i - 1].num_nodes;
618 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)
Arithmetic helper functions.
static bool is_exactly_zero(double v)
is a value precisely 0.0?
static bool is_exactly_equal(double a, double b)
are two values precisely the same?
A constraint determines a minimum or exact spacing required between two variables.