| 1 |
/** |
| 2 |
* \brief A block is a group of variables that must be moved together to improve |
| 3 |
* the goal function without violating already active constraints. |
| 4 |
* The variables in a block are spanned by a tree of active constraints. |
| 5 |
* |
| 6 |
* Authors: |
| 7 |
* Tim Dwyer <[email protected]> |
| 8 |
* |
| 9 |
* Copyright (C) 2005 Authors |
| 10 |
* |
| 11 |
* This version is released under the CPL (Common Public License) with |
| 12 |
* the Graphviz distribution. |
| 13 |
* A version is also available under the LGPL as part of the Adaptagrams |
| 14 |
* project: https://github.com/mjwybrow/adaptagrams. |
| 15 |
* If you make improvements or bug fixes to this code it would be much |
| 16 |
* appreciated if you could also contribute those changes back to the |
| 17 |
* Adaptagrams repository. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "config.h" |
| 21 |
|
| 22 |
#include <algorithm> |
| 23 |
#include <cassert> |
| 24 |
#include <ostream> |
| 25 |
#include <vector> |
| 26 |
#include <vpsc/constraint.h> |
| 27 |
#include <vpsc/block.h> |
| 28 |
#include <vpsc/blocks.h> |
| 29 |
#include <fstream> |
| 30 |
using std::ios; |
| 31 |
using std::ofstream; |
| 32 |
using std::vector; |
| 33 |
|
| 34 |
#ifndef RECTANGLE_OVERLAP_LOGGING |
| 35 |
#define RECTANGLE_OVERLAP_LOGGING 0 |
| 36 |
#endif |
| 37 |
|
| 38 |
/// `>` comparator for constraints |
| 39 |
static bool gt(const Constraint *const lhs, const Constraint *const rhs) { |
| 40 |
return compareConstraints(rhs, lhs); |
| 41 |
} |
| 42 |
|
| 43 |
/// create a heap within a vector |
| 44 |
/// |
| 45 |
/// The standard library’s heap functionality is all structured around creating |
| 46 |
/// a max-heap but we want a min-heap. So we flip the comparator we give it. |
| 47 |
static void make_heap(std::vector<Constraint *> &heap) { |
| 48 |
std::make_heap(heap.begin(), heap.end(), gt); |
| 49 |
} |
| 50 |
|
| 51 |
/// add all elements from `heap2` into the heap `heap1` |
| 52 |
static void merge_heaps(std::vector<Constraint *> &heap1, |
| 53 |
const std::vector<Constraint *> &heap2) { |
| 54 |
heap1.insert(heap1.end(), heap2.begin(), heap2.end()); |
| 55 |
make_heap(heap1); |
| 56 |
} |
| 57 |
|
| 58 |
/// get the minimum heap element |
| 59 |
static Constraint *findMin(std::vector<Constraint *> &heap) { |
| 60 |
assert(std::is_heap(heap.begin(), heap.end(), gt)); |
| 61 |
return heap.front(); |
| 62 |
} |
| 63 |
|
| 64 |
/// remove the minimum heap element |
| 65 |
static void deleteMin(std::vector<Constraint *> &heap) { |
| 66 |
assert(std::is_heap(heap.begin(), heap.end(), gt)); |
| 67 |
std::pop_heap(heap.begin(), heap.end(), gt); |
| 68 |
heap.pop_back(); |
| 69 |
} |
| 70 |
|
| 71 |
/// add an item to a heap |
| 72 |
static void insert(std::vector<Constraint *> &heap, Constraint *c) { |
| 73 |
assert(std::is_heap(heap.begin(), heap.end(), gt)); |
| 74 |
heap.push_back(c); |
| 75 |
std::push_heap(heap.begin(), heap.end(), gt); |
| 76 |
} |
| 77 |
|
| 78 |
void Block::addVariable(Variable *v) { |
| 79 |
v->block=this; |
| 80 |
vars.push_back(v); |
| 81 |
weight+=v->weight; |
| 82 |
wposn += v->weight * (v->desiredPosition - v->offset); |
| 83 |
posn=wposn/weight; |
| 84 |
} |
| 85 |
Block::Block(Variable *v) { |
| 86 |
timeStamp=0; |
| 87 |
posn=weight=wposn=0; |
| 88 |
deleted=false; |
| 89 |
if(v!=nullptr) { |
| 90 |
v->offset=0; |
| 91 |
addVariable(v); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
double Block::desiredWeightedPosition() { |
| 96 |
double wp = 0; |
| 97 |
for (const Variable *v : vars) { |
| 98 |
wp += (v->desiredPosition - v->offset) * v->weight; |
| 99 |
} |
| 100 |
return wp; |
| 101 |
} |
| 102 |
void Block::setUpInConstraints() { |
| 103 |
in = setUpConstraintHeap(true); |
| 104 |
} |
| 105 |
void Block::setUpOutConstraints() { |
| 106 |
out = setUpConstraintHeap(false); |
| 107 |
} |
| 108 |
|
| 109 |
std::vector<Constraint *> Block::setUpConstraintHeap(bool use_in) { |
| 110 |
std::vector<Constraint *> h; |
| 111 |
for (Variable *v : vars) { |
| 112 |
vector<Constraint*> *cs= use_in ? &v->in : &v->out; |
| 113 |
for (Constraint *c : *cs) { |
| 114 |
c->timeStamp=blockTimeCtr; |
| 115 |
if ((c->left->block != this && use_in) || (c->right->block != this && !use_in)) { |
| 116 |
h.push_back(c); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
make_heap(h); |
| 121 |
return h; |
| 122 |
} |
| 123 |
void Block::merge(Block* b, Constraint* c) { |
| 124 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 125 |
ofstream f(LOGFILE,ios::app); |
| 126 |
f<<" merging on: "<<*c<<",c->left->offset="<<c->left->offset<<",c->right->offset="<<c->right->offset<<"\n"; |
| 127 |
} |
| 128 |
const double dist = c->right->offset - c->left->offset - c->gap; |
| 129 |
Block *l=c->left->block; |
| 130 |
Block *r=c->right->block; |
| 131 |
if (vars.size() < b->vars.size()) { |
| 132 |
r->merge(l,c,dist); |
| 133 |
} else { |
| 134 |
l->merge(r,c,-dist); |
| 135 |
} |
| 136 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 137 |
ofstream f(LOGFILE,ios::app); |
| 138 |
f<<" merged block="<<(b->deleted?*this:*b)<<"\n"; |
| 139 |
} |
| 140 |
} |
| 141 |
/** |
| 142 |
* Merges b into this block across c. Can be either a |
| 143 |
* right merge or a left merge |
| 144 |
* @param b block to merge into this |
| 145 |
* @param c constraint being merged |
| 146 |
* @param distance separation required to satisfy c |
| 147 |
*/ |
| 148 |
void Block::merge(Block *b, Constraint *c, double dist) { |
| 149 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 150 |
ofstream f(LOGFILE,ios::app); |
| 151 |
f<<" merging: "<<*b<<"dist="<<dist<<"\n"; |
| 152 |
} |
| 153 |
c->active=true; |
| 154 |
wposn+=b->wposn-dist*b->weight; |
| 155 |
weight+=b->weight; |
| 156 |
posn=wposn/weight; |
| 157 |
for (Variable *v : b->vars) { |
| 158 |
v->block=this; |
| 159 |
v->offset+=dist; |
| 160 |
vars.push_back(v); |
| 161 |
} |
| 162 |
b->deleted=true; |
| 163 |
} |
| 164 |
|
| 165 |
void Block::mergeIn(Block *b) { |
| 166 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 167 |
ofstream f(LOGFILE,ios::app); |
| 168 |
f<<" merging constraint heaps... \n"; |
| 169 |
} |
| 170 |
// We check the top of the heaps to remove possible internal constraints |
| 171 |
findMinInConstraint(); |
| 172 |
b->findMinInConstraint(); |
| 173 |
merge_heaps(in, b->in); |
| 174 |
} |
| 175 |
void Block::mergeOut(Block *b) { |
| 176 |
findMinOutConstraint(); |
| 177 |
b->findMinOutConstraint(); |
| 178 |
merge_heaps(out, b->out); |
| 179 |
} |
| 180 |
Constraint *Block::findMinInConstraint() { |
| 181 |
Constraint *v = nullptr; |
| 182 |
vector<Constraint*> outOfDate; |
| 183 |
while (!in.empty()) { |
| 184 |
v = findMin(in); |
| 185 |
const Block *lb = v->left->block; |
| 186 |
const Block *rb = v->right->block; |
| 187 |
// rb may not be this if called between merge and mergeIn |
| 188 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 189 |
ofstream f(LOGFILE,ios::app); |
| 190 |
f<<" checking constraint ... "<<*v; |
| 191 |
f<<" timestamps: left="<<lb->timeStamp<<" right="<<rb->timeStamp<<" constraint="<<v->timeStamp<<"\n"; |
| 192 |
} |
| 193 |
if(lb == rb) { |
| 194 |
// constraint has been merged into the same block |
| 195 |
if(RECTANGLE_OVERLAP_LOGGING && v->slack()<0) { |
| 196 |
ofstream f(LOGFILE,ios::app); |
| 197 |
f<<" violated internal constraint found! "<<*v<<"\n"; |
| 198 |
f<<" lb="<<*lb<<"\n"; |
| 199 |
f<<" rb="<<*rb<<"\n"; |
| 200 |
} |
| 201 |
deleteMin(in); |
| 202 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 203 |
ofstream f(LOGFILE,ios::app); |
| 204 |
f<<" ... skipping internal constraint\n"; |
| 205 |
} |
| 206 |
} else if(v->timeStamp < lb->timeStamp) { |
| 207 |
// block at other end of constraint has been moved since this |
| 208 |
deleteMin(in); |
| 209 |
outOfDate.push_back(v); |
| 210 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 211 |
ofstream f(LOGFILE,ios::app); |
| 212 |
f<<" reinserting out of date (reinsert later)\n"; |
| 213 |
} |
| 214 |
} else { |
| 215 |
break; |
| 216 |
} |
| 217 |
} |
| 218 |
for (Constraint *c : outOfDate) { |
| 219 |
c->timeStamp=blockTimeCtr; |
| 220 |
insert(in, c); |
| 221 |
} |
| 222 |
if(in.empty()) { |
| 223 |
v=nullptr; |
| 224 |
} else { |
| 225 |
v = findMin(in); |
| 226 |
} |
| 227 |
return v; |
| 228 |
} |
| 229 |
Constraint *Block::findMinOutConstraint() { |
| 230 |
if(out.empty()) return nullptr; |
| 231 |
Constraint *v = findMin(out); |
| 232 |
while (v->left->block == v->right->block) { |
| 233 |
deleteMin(out); |
| 234 |
if(out.empty()) return nullptr; |
| 235 |
v = findMin(out); |
| 236 |
} |
| 237 |
return v; |
| 238 |
} |
| 239 |
void Block::deleteMinInConstraint() { |
| 240 |
deleteMin(in); |
| 241 |
} |
| 242 |
void Block::deleteMinOutConstraint() { |
| 243 |
deleteMin(out); |
| 244 |
} |
| 245 |
inline bool Block::canFollowLeft(const Constraint *c, const Variable *last) { |
| 246 |
return c->left->block==this && c->active && last!=c->left; |
| 247 |
} |
| 248 |
inline bool Block::canFollowRight(const Constraint *c, const Variable *last) { |
| 249 |
return c->right->block==this && c->active && last!=c->right; |
| 250 |
} |
| 251 |
|
| 252 |
// computes the derivative of v and the lagrange multipliers |
| 253 |
// of v's out constraints (as the recursive sum of those below. |
| 254 |
// Does not backtrack over u. |
| 255 |
// also records the constraint with minimum lagrange multiplier |
| 256 |
// in min_lm |
| 257 |
double Block::compute_dfdv(Variable *v, Variable *u, Constraint *&min_lm) { |
| 258 |
double dfdv=v->weight*(v->position() - v->desiredPosition); |
| 259 |
for (Constraint *c : v->out) { |
| 260 |
if(canFollowRight(c,u)) { |
| 261 |
dfdv+=c->lm=compute_dfdv(c->right,v,min_lm); |
| 262 |
if(min_lm==nullptr||c->lm<min_lm->lm) min_lm=c; |
| 263 |
} |
| 264 |
} |
| 265 |
for (Constraint *c : v->in) { |
| 266 |
if(canFollowLeft(c,u)) { |
| 267 |
dfdv-=c->lm=-compute_dfdv(c->left,v,min_lm); |
| 268 |
if(min_lm==nullptr||c->lm<min_lm->lm) min_lm=c; |
| 269 |
} |
| 270 |
} |
| 271 |
return dfdv; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
// computes dfdv for each variable and uses the sum of dfdv on either side of |
| 276 |
// the constraint c to compute the lagrangian multiplier lm_c. |
| 277 |
// The top level v and r are variables between which we want to find the |
| 278 |
// constraint with the smallest lm. |
| 279 |
// When we find r we pass nullptr to subsequent recursive calls, |
| 280 |
// thus r=nullptr indicates constraints are not on the shortest path. |
| 281 |
// Similarly, m is initially nullptr and is only assigned a value if the next |
| 282 |
// variable to be visited is r or if a possible min constraint is returned from |
| 283 |
// a nested call (rather than nullptr). |
| 284 |
// Then, the search for the m with minimum lm occurs as we return from |
| 285 |
// the recursion (checking only constraints traversed left-to-right |
| 286 |
// in order to avoid creating any new violations). |
| 287 |
Block::Pair Block::compute_dfdv_between(Variable* r, Variable* v, Variable* u, |
| 288 |
Direction dir = NONE, bool changedDirection = false) { |
| 289 |
double dfdv=v->weight*(v->position() - v->desiredPosition); |
| 290 |
Constraint *m=nullptr; |
| 291 |
for (Constraint *c : v->in) { |
| 292 |
if(canFollowLeft(c,u)) { |
| 293 |
if(dir==RIGHT) { |
| 294 |
changedDirection = true; |
| 295 |
} |
| 296 |
if(c->left==r) { |
| 297 |
r=nullptr; m=c; |
| 298 |
} |
| 299 |
Pair p=compute_dfdv_between(r,c->left,v, |
| 300 |
LEFT,changedDirection); |
| 301 |
dfdv -= c->lm = -p.first; |
| 302 |
if(r && p.second) |
| 303 |
m = p.second; |
| 304 |
} |
| 305 |
} |
| 306 |
for (Constraint *c : v->out) { |
| 307 |
if(canFollowRight(c,u)) { |
| 308 |
if(dir==LEFT) { |
| 309 |
changedDirection = true; |
| 310 |
} |
| 311 |
if(c->right==r) { |
| 312 |
r=nullptr; m=c; |
| 313 |
} |
| 314 |
Pair p=compute_dfdv_between(r,c->right,v, |
| 315 |
RIGHT,changedDirection); |
| 316 |
dfdv += c->lm = p.first; |
| 317 |
if(r && p.second) |
| 318 |
m = changedDirection && c->lm < p.second->lm |
| 319 |
? c |
| 320 |
: p.second; |
| 321 |
} |
| 322 |
} |
| 323 |
return Pair(dfdv,m); |
| 324 |
} |
| 325 |
|
| 326 |
// resets LMs for all active constraints to 0 by |
| 327 |
// traversing active constraint tree starting from v, |
| 328 |
// not back tracking over u |
| 329 |
void Block::reset_active_lm(Variable *v, Variable *u) { |
| 330 |
for (Constraint *c : v->out) { |
| 331 |
if(canFollowRight(c,u)) { |
| 332 |
c->lm=0; |
| 333 |
reset_active_lm(c->right,v); |
| 334 |
} |
| 335 |
} |
| 336 |
for (Constraint *c : v->in) { |
| 337 |
if(canFollowLeft(c,u)) { |
| 338 |
c->lm=0; |
| 339 |
reset_active_lm(c->left,v); |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
/** |
| 344 |
* finds the constraint with the minimum lagrange multiplier, that is, the constraint |
| 345 |
* that most wants to split |
| 346 |
*/ |
| 347 |
Constraint *Block::findMinLM() { |
| 348 |
Constraint *min_lm=nullptr; |
| 349 |
reset_active_lm(vars.front(),nullptr); |
| 350 |
compute_dfdv(vars.front(),nullptr,min_lm); |
| 351 |
return min_lm; |
| 352 |
} |
| 353 |
Constraint *Block::findMinLMBetween(Variable* lv, Variable* rv) { |
| 354 |
Constraint *min_lm=nullptr; |
| 355 |
reset_active_lm(vars.front(),nullptr); |
| 356 |
min_lm=compute_dfdv_between(rv,lv,nullptr).second; |
| 357 |
return min_lm; |
| 358 |
} |
| 359 |
|
| 360 |
// populates block b by traversing the active constraint tree adding variables as they're |
| 361 |
// visited. Starts from variable v and does not backtrack over variable u. |
| 362 |
void Block::populateSplitBlock(Block *b, Variable *v, Variable *u) { |
| 363 |
b->addVariable(v); |
| 364 |
for (Constraint *c : v->in) { |
| 365 |
if (canFollowLeft(c,u)) |
| 366 |
populateSplitBlock(b, c->left, v); |
| 367 |
} |
| 368 |
for (Constraint *c : v->out) { |
| 369 |
if (canFollowRight(c,u)) |
| 370 |
populateSplitBlock(b, c->right, v); |
| 371 |
} |
| 372 |
} |
| 373 |
/** |
| 374 |
* Block needs to be split because of a violated constraint between vl and vr. |
| 375 |
* We need to search the active constraint tree between l and r and find the constraint |
| 376 |
* with min lagrangrian multiplier and split at that point. |
| 377 |
* Returns the split constraint |
| 378 |
*/ |
| 379 |
Constraint* Block::splitBetween(Variable* vl, Variable* vr, Block* &lb, Block* &rb) { |
| 380 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 381 |
ofstream f(LOGFILE,ios::app); |
| 382 |
f<<" need to split between: "<<*vl<<" and "<<*vr<<"\n"; |
| 383 |
} |
| 384 |
Constraint *c=findMinLMBetween(vl, vr); |
| 385 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 386 |
ofstream f(LOGFILE,ios::app); |
| 387 |
f<<" going to split on: "<<*c<<"\n"; |
| 388 |
} |
| 389 |
split(lb,rb,c); |
| 390 |
deleted = true; |
| 391 |
return c; |
| 392 |
} |
| 393 |
/** |
| 394 |
* Creates two new blocks, l and r, and splits this block across constraint c, |
| 395 |
* placing the left subtree of constraints (and associated variables) into l |
| 396 |
* and the right into r. |
| 397 |
*/ |
| 398 |
void Block::split(Block* &l, Block* &r, Constraint* c) { |
| 399 |
c->active=false; |
| 400 |
l=new Block(); |
| 401 |
populateSplitBlock(l,c->left,c->right); |
| 402 |
r=new Block(); |
| 403 |
populateSplitBlock(r,c->right,c->left); |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Computes the cost (squared euclidean distance from desired positions) of the |
| 408 |
* current positions for variables in this block |
| 409 |
*/ |
| 410 |
double Block::cost() { |
| 411 |
double c = 0; |
| 412 |
for (const Variable *v : vars) { |
| 413 |
const double diff = v->position() - v->desiredPosition; |
| 414 |
c += v->weight * diff * diff; |
| 415 |
} |
| 416 |
return c; |
| 417 |
} |
| 418 |
|
| 419 |
std::ostream& operator <<(std::ostream &os, const Block &b) { |
| 420 |
os<<"Block:"; |
| 421 |
for(const Variable *v : b.vars) { |
| 422 |
os<<" "<<*v; |
| 423 |
} |
| 424 |
if(b.deleted) { |
| 425 |
os<<" Deleted!"; |
| 426 |
} |
| 427 |
return os; |
| 428 |
} |