| 1 |
/** |
| 2 |
* \file |
| 3 |
* \brief Solve an instance of the "Variable Placement with Separation |
| 4 |
* Constraints" problem. |
| 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 <cassert> |
| 23 |
#include <vpsc/constraint.h> |
| 24 |
#include <vpsc/block.h> |
| 25 |
#include <vpsc/blocks.h> |
| 26 |
#include <vpsc/solve_VPSC.h> |
| 27 |
#include <math.h> |
| 28 |
#include <memory> |
| 29 |
#include <sstream> |
| 30 |
#include <fstream> |
| 31 |
#include <stdexcept> |
| 32 |
using std::ios; |
| 33 |
using std::ofstream; |
| 34 |
|
| 35 |
using std::ostringstream; |
| 36 |
using std::list; |
| 37 |
using std::set; |
| 38 |
|
| 39 |
#ifndef RECTANGLE_OVERLAP_LOGGING |
| 40 |
#define RECTANGLE_OVERLAP_LOGGING 0 |
| 41 |
#endif |
| 42 |
|
| 43 |
IncVPSC::IncVPSC(const unsigned n, Variable *vs[], const unsigned m_, |
| 44 |
Constraint *cs_[]) |
| 45 |
: VPSC(n, vs, m_, cs_) { |
| 46 |
inactive.assign(cs_, cs_ + m_); |
| 47 |
for(Constraint *c : inactive) { |
| 48 |
c->active=false; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
VPSC::VPSC(const unsigned n, Variable *vs[], const unsigned m_, |
| 53 |
Constraint *cs_[]) |
| 54 |
: bs(n, vs), cs(cs_), m(m_) { |
| 55 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 56 |
printBlocks(); |
| 57 |
assert(!constraintGraphIsCyclic(n,vs)); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// useful in debugging |
| 62 |
void VPSC::printBlocks() { |
| 63 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 64 |
ofstream f(LOGFILE,ios::app); |
| 65 |
for(Block *b : bs) { |
| 66 |
f<<" "<<*b<<"\n"; |
| 67 |
} |
| 68 |
for(unsigned i=0;i<m;i++) { |
| 69 |
f<<" "<<*cs[i]<<"\n"; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
/** |
| 74 |
* Produces a feasible - though not necessarily optimal - solution by |
| 75 |
* examining blocks in the partial order defined by the directed acyclic |
| 76 |
* graph of constraints. For each block (when processing left to right) we |
| 77 |
* maintain the invariant that all constraints to the left of the block |
| 78 |
* (incoming constraints) are satisfied. This is done by repeatedly merging |
| 79 |
* blocks into bigger blocks across violated constraints (most violated |
| 80 |
* first) fixing the position of variables inside blocks relative to one |
| 81 |
* another so that constraints internal to the block are satisfied. |
| 82 |
*/ |
| 83 |
void VPSC::satisfy() { |
| 84 |
list<Variable*> vs=bs.totalOrder(); |
| 85 |
for(Variable *v : vs) { |
| 86 |
if(!v->block->deleted) { |
| 87 |
bs.mergeLeft(v->block); |
| 88 |
} |
| 89 |
} |
| 90 |
bs.cleanup(); |
| 91 |
for(unsigned i=0;i<m;i++) { |
| 92 |
if(cs[i]->slack()<-0.0000001) { |
| 93 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 94 |
ofstream f(LOGFILE,ios::app); |
| 95 |
f<<"Error: Unsatisfied constraint: "<<*cs[i]<<"\n"; |
| 96 |
} |
| 97 |
//assert(cs[i]->slack()>-0.0000001); |
| 98 |
throw std::runtime_error("Unsatisfied constraint"); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
void VPSC::refine() { |
| 104 |
for (bool solved = false; !solved;) { |
| 105 |
solved=true; |
| 106 |
for(Block *b : bs) { |
| 107 |
b->setUpInConstraints(); |
| 108 |
b->setUpOutConstraints(); |
| 109 |
} |
| 110 |
for(Block *b : bs) { |
| 111 |
Constraint *c=b->findMinLM(); |
| 112 |
if(c!=nullptr && c->lm<0) { |
| 113 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 114 |
ofstream f(LOGFILE,ios::app); |
| 115 |
f<<"Split on constraint: "<<*c<<"\n"; |
| 116 |
} |
| 117 |
// Split on c |
| 118 |
Block *l=nullptr, *r=nullptr; |
| 119 |
bs.split(b,l,r,c); |
| 120 |
bs.cleanup(); |
| 121 |
// split alters the block set so we have to restart |
| 122 |
solved=false; |
| 123 |
break; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
for(unsigned i=0;i<m;i++) { |
| 128 |
if(cs[i]->slack()<-0.0000001) { |
| 129 |
assert(cs[i]->slack()>-0.0000001); |
| 130 |
throw std::runtime_error("Unsatisfied constraint"); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
/** |
| 135 |
* Calculate the optimal solution. After using satisfy() to produce a |
| 136 |
* feasible solution, refine() examines each block to see if further |
| 137 |
* refinement is possible by splitting the block. This is done repeatedly |
| 138 |
* until no further improvement is possible. |
| 139 |
*/ |
| 140 |
void VPSC::solve() { |
| 141 |
satisfy(); |
| 142 |
refine(); |
| 143 |
} |
| 144 |
|
| 145 |
void IncVPSC::solve() { |
| 146 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 147 |
ofstream f(LOGFILE,ios::app); |
| 148 |
f<<"solve_inc()...\n"; |
| 149 |
} |
| 150 |
double lastcost,cost = bs.cost(); |
| 151 |
do { |
| 152 |
lastcost=cost; |
| 153 |
satisfy(); |
| 154 |
splitBlocks(); |
| 155 |
cost = bs.cost(); |
| 156 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 157 |
ofstream f(LOGFILE,ios::app); |
| 158 |
f<<" cost="<<cost<<"\n"; |
| 159 |
} |
| 160 |
} while(fabs(lastcost-cost)>0.0001); |
| 161 |
} |
| 162 |
/** |
| 163 |
* incremental version of satisfy that allows refinement after blocks are |
| 164 |
* moved. |
| 165 |
* |
| 166 |
* - move blocks to new positions |
| 167 |
* - repeatedly merge across most violated constraint until no more |
| 168 |
* violated constraints exist |
| 169 |
* |
| 170 |
* Note: there is a special case to handle when the most violated constraint |
| 171 |
* is between two variables in the same block. Then, we must split the block |
| 172 |
* over an active constraint between the two variables. We choose the |
| 173 |
* constraint with the most negative lagrangian multiplier. |
| 174 |
*/ |
| 175 |
void IncVPSC::satisfy() { |
| 176 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 177 |
ofstream f(LOGFILE,ios::app); |
| 178 |
f<<"satisfy_inc()...\n"; |
| 179 |
} |
| 180 |
splitBlocks(); |
| 181 |
long splitCtr = 0; |
| 182 |
Constraint* v = nullptr; |
| 183 |
while(mostViolated(inactive,v)<-0.0000001) { |
| 184 |
assert(!v->active); |
| 185 |
Block *lb = v->left->block, *rb = v->right->block; |
| 186 |
if(lb != rb) { |
| 187 |
lb->merge(rb,v); |
| 188 |
} else { |
| 189 |
if(splitCtr++>10000) { |
| 190 |
throw std::runtime_error("Cycle Error!"); |
| 191 |
} |
| 192 |
// constraint is within block, need to split first |
| 193 |
inactive.push_back(lb->splitBetween(v->left,v->right,lb,rb)); |
| 194 |
lb->merge(rb,v); |
| 195 |
bs.insert(lb); |
| 196 |
} |
| 197 |
} |
| 198 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 199 |
ofstream f(LOGFILE,ios::app); |
| 200 |
f<<" finished merges.\n"; |
| 201 |
} |
| 202 |
bs.cleanup(); |
| 203 |
for(unsigned i=0;i<m;i++) { |
| 204 |
v=cs[i]; |
| 205 |
if(v->slack()<-0.0000001) { |
| 206 |
//assert(cs[i]->slack()>-0.0000001); |
| 207 |
ostringstream s; |
| 208 |
s<<"Unsatisfied constraint: "<<*v; |
| 209 |
throw std::runtime_error(s.str()); |
| 210 |
} |
| 211 |
} |
| 212 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 213 |
ofstream f(LOGFILE,ios::app); |
| 214 |
f<<" finished cleanup.\n"; |
| 215 |
printBlocks(); |
| 216 |
} |
| 217 |
} |
| 218 |
void IncVPSC::moveBlocks() { |
| 219 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 220 |
ofstream f(LOGFILE,ios::app); |
| 221 |
f<<"moveBlocks()...\n"; |
| 222 |
} |
| 223 |
for(Block *b : bs) { |
| 224 |
b->wposn = b->desiredWeightedPosition(); |
| 225 |
b->posn = b->wposn / b->weight; |
| 226 |
} |
| 227 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 228 |
ofstream f(LOGFILE,ios::app); |
| 229 |
f<<" moved blocks.\n"; |
| 230 |
} |
| 231 |
} |
| 232 |
void IncVPSC::splitBlocks() { |
| 233 |
moveBlocks(); |
| 234 |
splitCnt=0; |
| 235 |
// Split each block if necessary on min LM |
| 236 |
for(Block *b : bs) { |
| 237 |
Constraint* v=b->findMinLM(); |
| 238 |
if(v!=nullptr && v->lm < -0.0000001) { |
| 239 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 240 |
ofstream f(LOGFILE,ios::app); |
| 241 |
f<<" found split point: "<<*v<<" lm="<<v->lm<<"\n"; |
| 242 |
} |
| 243 |
splitCnt++; |
| 244 |
Block *b2 = v->left->block, *l=nullptr, *r=nullptr; |
| 245 |
assert(v->left->block == v->right->block); |
| 246 |
double pos = b2->posn; |
| 247 |
b2->split(l,r,v); |
| 248 |
l->posn=r->posn=pos; |
| 249 |
l->wposn = l->posn * l->weight; |
| 250 |
r->wposn = r->posn * r->weight; |
| 251 |
bs.insert(l); |
| 252 |
bs.insert(r); |
| 253 |
b2->deleted=true; |
| 254 |
inactive.push_back(v); |
| 255 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 256 |
ofstream f(LOGFILE,ios::app); |
| 257 |
f<<" new blocks: "<<*l<<" and "<<*r<<"\n"; |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 262 |
ofstream f(LOGFILE,ios::app); |
| 263 |
f<<" finished splits.\n"; |
| 264 |
} |
| 265 |
bs.cleanup(); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Scan constraint list for the most violated constraint, or the first equality |
| 270 |
* constraint |
| 271 |
*/ |
| 272 |
double IncVPSC::mostViolated(ConstraintList &l, Constraint* &v) { |
| 273 |
double minSlack = DBL_MAX; |
| 274 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 275 |
ofstream f(LOGFILE,ios::app); |
| 276 |
f<<"Looking for most violated...\n"; |
| 277 |
} |
| 278 |
ConstraintList::iterator end = l.end(); |
| 279 |
ConstraintList::iterator deletePoint = end; |
| 280 |
for(ConstraintList::iterator i=l.begin();i!=end;i++) { |
| 281 |
Constraint *c=*i; |
| 282 |
double slack = c->slack(); |
| 283 |
if (slack < minSlack) { |
| 284 |
minSlack=slack; |
| 285 |
v=c; |
| 286 |
deletePoint=i; |
| 287 |
} |
| 288 |
} |
| 289 |
// Because the constraint list is not order dependent we just |
| 290 |
// move the last element over the deletePoint and resize |
| 291 |
// downwards. There is always at least 1 element in the |
| 292 |
// vector because of search. |
| 293 |
if(deletePoint != end && minSlack<-0.0000001) { |
| 294 |
*deletePoint = l.back(); |
| 295 |
l.pop_back(); |
| 296 |
} |
| 297 |
if (RECTANGLE_OVERLAP_LOGGING) { |
| 298 |
ofstream f(LOGFILE,ios::app); |
| 299 |
f<<" most violated is: "<<*v<<"\n"; |
| 300 |
} |
| 301 |
return minSlack; |
| 302 |
} |
| 303 |
|
| 304 |
#include <map> |
| 305 |
using std::map; |
| 306 |
using std::vector; |
| 307 |
struct node { |
| 308 |
set<node*> in; |
| 309 |
set<node*> out; |
| 310 |
}; |
| 311 |
// useful in debugging - cycles would be BAD |
| 312 |
bool VPSC::constraintGraphIsCyclic(const unsigned n, Variable *vs[]) { |
| 313 |
map<Variable*, node*> varmap; |
| 314 |
vector<std::unique_ptr<node>> graph; |
| 315 |
for(unsigned i=0;i<n;i++) { |
| 316 |
graph.emplace_back(new node); |
| 317 |
varmap[vs[i]]=graph.back().get(); |
| 318 |
} |
| 319 |
for(unsigned i=0;i<n;i++) { |
| 320 |
for(Constraint *c : vs[i]->in) { |
| 321 |
Variable *l=c->left; |
| 322 |
varmap[vs[i]]->in.insert(varmap[l]); |
| 323 |
} |
| 324 |
|
| 325 |
for(Constraint *c : vs[i]->out) { |
| 326 |
Variable *r=c->right; |
| 327 |
varmap[vs[i]]->out.insert(varmap[r]); |
| 328 |
} |
| 329 |
} |
| 330 |
while(!graph.empty()) { |
| 331 |
node *u=nullptr; |
| 332 |
vector<std::unique_ptr<node>>::iterator i=graph.begin(); |
| 333 |
for(;i!=graph.end();i++) { |
| 334 |
u=(*i).get(); |
| 335 |
if(u->in.empty()) { |
| 336 |
break; |
| 337 |
} |
| 338 |
} |
| 339 |
if(i==graph.end()) { |
| 340 |
//cycle found! |
| 341 |
return true; |
| 342 |
} else { |
| 343 |
graph.erase(i); |
| 344 |
for(node *v : u->out) { |
| 345 |
v->in.erase(u); |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
return false; |
| 350 |
} |
| 351 |
|
| 352 |
// useful in debugging - cycles would be BAD |
| 353 |
bool VPSC::blockGraphIsCyclic() { |
| 354 |
map<Block*, node*> bmap; |
| 355 |
vector<std::unique_ptr<node>> graph; |
| 356 |
for(Block *b : bs) { |
| 357 |
graph.emplace_back(new node); |
| 358 |
bmap[b]=graph.back().get(); |
| 359 |
} |
| 360 |
for(Block *b : bs) { |
| 361 |
b->setUpInConstraints(); |
| 362 |
Constraint *c=b->findMinInConstraint(); |
| 363 |
while(c!=nullptr) { |
| 364 |
Block *l=c->left->block; |
| 365 |
bmap[b]->in.insert(bmap[l]); |
| 366 |
b->deleteMinInConstraint(); |
| 367 |
c=b->findMinInConstraint(); |
| 368 |
} |
| 369 |
|
| 370 |
b->setUpOutConstraints(); |
| 371 |
c=b->findMinOutConstraint(); |
| 372 |
while(c!=nullptr) { |
| 373 |
Block *r=c->right->block; |
| 374 |
bmap[b]->out.insert(bmap[r]); |
| 375 |
b->deleteMinOutConstraint(); |
| 376 |
c=b->findMinOutConstraint(); |
| 377 |
} |
| 378 |
} |
| 379 |
while(!graph.empty()) { |
| 380 |
node *u=nullptr; |
| 381 |
vector<std::unique_ptr<node>>::iterator i=graph.begin(); |
| 382 |
for(;i!=graph.end();i++) { |
| 383 |
u=(*i).get(); |
| 384 |
if(u->in.empty()) { |
| 385 |
break; |
| 386 |
} |
| 387 |
} |
| 388 |
if(i==graph.end()) { |
| 389 |
//cycle found! |
| 390 |
return true; |
| 391 |
} |
| 392 |
graph.erase(i); |
| 393 |
for(node *v : u->out) { |
| 394 |
v->in.erase(u); |
| 395 |
} |
| 396 |
} |
| 397 |
return false; |
| 398 |
} |
| 399 |
/** |
| 400 |
* @dir lib/vpsc |
| 401 |
* @brief library for solving the |
| 402 |
* %Variable Placement with Separation Constraints problem |
| 403 |
* for lib/neatogen |
| 404 |
* |
| 405 |
* This is a quadratic programming problem in which the squared differences |
| 406 |
* between a placement vector and some ideal placement are minimized subject |
| 407 |
* to a set of separation constraints. This is very useful in a number of layout problems. |
| 408 |
* |
| 409 |
* References: |
| 410 |
* - https://www.adaptagrams.org/documentation/libvpsc.html |
| 411 |
* - Tim Dwyer, Kim Marriott, and Peter J. Stuckey. Fast node overlap removal. |
| 412 |
* In Proceedings 13th International Symposium on Graph Drawing (GD '05), |
| 413 |
* volume 3843 of LNCS, pages 153-164. Springer, 2006. |
| 414 |
*/ |