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) {
182static DigColaLevel *assign_digcola_levels(
const int *ordering,
int n,
183 int *level_inds,
int num_divisions);
193CMajEnvVPSC *initCMajVPSC(
int n,
float *packedMat,
vtx_data *
graph,
194 ipsep_options * opt,
int diredges)
199 CMajEnvVPSC *e =
gv_alloc(
sizeof(CMajEnvVPSC));
203 e->nldv = 2 * opt->clusters.nclusters;
209 for (i = 0; i < n; i++) {
215 fprintf(stderr,
" generate edge constraints...\n");
216 for (i = 0; i < e->nv; i++) {
217 for (
size_t j = 1; j <
graph[i].nedges; j++) {
218 if (
graph[i].edists[j] > 0) {
225 for (i = 0; i < e->nv; i++) {
226 for (
size_t j = 1; j <
graph[i].nedges; j++) {
227 int u = i, v =
graph[i].edges[j];
228 if (
graph[i].edists[j] > 0) {
234 }
else if (diredges == 2) {
235 int *ordering =
NULL, *ls =
NULL, cvar;
237 DigColaLevel *levels;
240 if (compute_hierarchy(
graph, e->nv, 1e-2, 1e-1,
NULL, &ordering, &ls,
241 &e->ndv))
return NULL;
242 levels = assign_digcola_levels(ordering, e->nv, ls, e->ndv);
245 fprintf(stderr,
"Found %d DiG-CoLa boundaries\n", e->ndv);
247 get_num_digcola_constraints(levels, e->ndv + 1) + e->ndv - 1;
251 for (i = 0; i < n; i++) {
256 for (i = 0; i < e->ndv; i++) {
261 halfgap = opt->edge_gap;
262 for (i = 0; i < e->ndv; i++) {
265 for (
int j = 0; j < levels[i].num_nodes; j++) {
271 for (
int j = 0; j < levels[i + 1].num_nodes; j++) {
274 e->vs[levels[i + 1].nodes[j]], halfgap);
278 for (i = 0; i < e->ndv - 1; i++) {
283 if (opt->clusters.nclusters > 0) {
285 nConCs = 2 * opt->clusters.nvars;
287 for (i = 0; i < e->gm; i++) {
292 for (i = 0; i < opt->clusters.nclusters; i++) {
293 for (
int j = 0; j < opt->clusters.clustersizes[i]; j++) {
294 Variable *v = e->vs[opt->clusters.clusters[i][j]];
295 Variable *cl = e->vs[e->nv + 2 * i];
296 Variable *cr = e->vs[e->nv + 2 * i + 1];
306 e->vpsc =
newIncVPSC(n + e->ndv, e->vs, e->gm, e->gcs);
310 if (packedMat !=
NULL) {
311 e->A = unpackMatrix(packedMat, n);
314 e->fArray1 =
gv_calloc(n,
sizeof(
float));
315 e->fArray2 =
gv_calloc(n,
sizeof(
float));
316 e->fArray3 =
gv_calloc(n,
sizeof(
float));
319 " initCMajVPSC done: %d global constraints generated.\n",
324void deleteCMajEnvVPSC(CMajEnvVPSC * e)
333 if (e->cs != e->gcs && e->gcs !=
NULL)
336 for (i = 0; i < e->nv + e->nldv + e->ndv; i++) {
358void generateNonoverlapConstraints(CMajEnvVPSC * e,
362 bool transitiveClosure,
367 int n = e->nv + e->nldv;
369 bool genclusters = opt->clusters.nclusters > 0;
372 n -= 2 * opt->clusters.nclusters;
378 nsizeScale *= 1.0001f;
380 for (i = 0; i < n; i++) {
382 coords[0][i] - nsizeScale * opt->nsize[i].x / 2.0 -
385 coords[0][i] + nsizeScale * opt->nsize[i].x / 2.0 +
388 coords[1][i] - nsizeScale * opt->nsize[i].y / 2.0 -
391 coords[1][i] + nsizeScale * opt->nsize[i].y / 2.0 +
397 int* cm =
gv_calloc(opt->clusters.nclusters + 1,
sizeof(
int));
398 for (i = 0; i < opt->clusters.nclusters; i++) {
399 int cn = opt->clusters.clustersizes[i];
404 container.
LL.
x = container.
LL.
y = DBL_MAX;
405 container.
UR.
x = container.
UR.
y = -DBL_MAX;
406 for (j = 0; j < cn; j++) {
407 int iv = opt->clusters.clusters[i][j];
409 B2BF(bb[iv], cbb[j]);
412 B2BF(container, opt->clusters.bb[i]);
413 cvs[cn] = e->vs[n + 2 * i];
414 cvs[cn + 1] = e->vs[n + 2 * i + 1];
415 B2BF(container, cbb[cn]);
416 B2BF(container, cbb[cn + 1]);
418 cbb[cn].
UR.
x = container.
LL.
x + 0.0001;
419 cbb[cn + 1].
LL.
x = container.
UR.
x - 0.0001;
424 cbb[cn].
UR.
y = container.
LL.
y + 0.0001;
425 cbb[cn + 1].
LL.
y = container.
UR.
y - 0.0001;
434 int cn = opt->clusters.ntoplevel + opt->clusters.nclusters;
437 for (i = 0; i < opt->clusters.ntoplevel; i++) {
438 int iv = opt->clusters.toplevel[i];
440 B2BF(bb[iv], cbb[i]);
443 for (i = opt->clusters.ntoplevel; i < cn; i++) {
445 j = i - opt->clusters.ntoplevel;
446 B2BF(opt->clusters.bb[j], cbb[i]);
448 i = opt->clusters.nclusters;
457 for (i = opt->clusters.ntoplevel; i < cn; i++) {
459 j = i - opt->clusters.ntoplevel;
467 dgap = -(cbb[i].
UR.
x - cbb[i].
LL.
x) / 2.0;
469 dgap = -(cbb[i].
UR.
y - cbb[i].
LL.
y) / 2.0;
497 mol += cm[opt->clusters.nclusters];
502 for (i = 0; i < opt->clusters.nclusters + 1; i++) {
504 for (j = 0; j < cm[i]; j++) {
505 *csolptr++ = cscl[i][j];
522 for (i = e->gm; i < e->m; i++) {
541 for (i = 0; i < e->m; i++) {
543 e->cs[i] = e->gcs[i];
545 e->cs[i] = csol[i - e->gm];
552 fprintf(stderr,
" generated %d constraints\n", e->m);
553 e->vpsc =
newIncVPSC(e->nv + e->nldv + e->ndv, e->vs, e->m, e->cs);
561void removeoverlaps(
int n,
float **coords, ipsep_options * opt)
564 CMajEnvVPSC *e = initCMajVPSC(n,
NULL,
NULL, opt, 0);
565 generateNonoverlapConstraints(e, 1.0, coords, 0,
true, opt);
567 for (i = 0; i < n; i++) {
570 generateNonoverlapConstraints(e, 1.0, coords, 1,
false, opt);
572 for (i = 0; i < n; i++) {
575 deleteCMajEnvVPSC(e);
581static DigColaLevel *assign_digcola_levels(
const int *ordering,
int n,
582 int *level_inds,
int 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 (i = 0; i < l[0].num_nodes; i++) {
589 l[0].nodes[i] = ordering[i];
592 for (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 (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,
int num_levels)
618 for (i = 1; i < num_levels; i++) {
619 nc += levels[i].num_nodes + levels[i - 1].num_nodes;
621 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.