Adaptagrams
gradient_projection.h
1 /*
2  * vim: ts=4 sw=4 et tw=0 wm=0
3  *
4  * libcola - A library providing force-directed network layout using the
5  * stress-majorization method subject to separation constraints.
6  *
7  * Copyright (C) 2006-2008 Monash University
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  * See the file LICENSE.LGPL distributed with the library.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18  *
19 */
20 
21 #ifndef _GRADIENT_PROJECTION_H
22 #define _GRADIENT_PROJECTION_H
23 
24 #include <iostream>
25 #include <cmath>
26 #include <valarray>
27 
28 #include "libvpsc/solve_VPSC.h"
29 #include "libvpsc/variable.h"
30 #include "libvpsc/constraint.h"
31 #include "libvpsc/rectangle.h"
32 #include "libcola/commondefs.h"
33 #include "libcola/compound_constraints.h"
34 #include "libcola/cluster.h"
35 #include "libcola/sparse_matrix.h"
36 #ifdef MOSEK_AVAILABLE
37 #include "libvpsc/mosek_quad_solve.h"
38 #endif
39 
40 namespace straightener {
41  class Node;
42 }
43 namespace cola {
44 
45 enum SolveWithMosek { Off, Inner, Outer };
46 
47 class GradientProjection {
48 public:
60  GradientProjection(
61  const vpsc::Dim k,
62  std::valarray<double> *denseQ,
63  const double tol,
64  const unsigned max_iterations,
65  CompoundConstraints const *ccs,
66  UnsatisfiableConstraintInfos *unsatisfiableConstraints,
67  NonOverlapConstraintsMode nonOverlapConstraints = None,
68  RootCluster* clusterHierarchy = nullptr,
69  vpsc::Rectangles* rs = nullptr,
70  const bool scaling = false,
71  SolveWithMosek solveWithMosek = Off);
72  static void dumpSquareMatrix(std::valarray<double> const &L) {
73  unsigned n=static_cast<unsigned>(floor(sqrt(static_cast<double>(L.size()))));
74  printf("Matrix %dX%d\n{",n,n);
75  for(unsigned i=0;i<n;i++) {
76  printf("{");
77  for(unsigned j=0;j<n;j++) {
78  char c=j==n-1?'}':',';
79  printf("%f%c",1. * L[i*n+j],c);
80  }
81  char c=i==n-1?'}':',';
82  printf("%c\n",c);
83  }
84  }
85 
86  unsigned getNumStaticVars() const {
87  return numStaticVars;
88  }
89  ~GradientProjection() {
90  //destroyVPSC(solver);
91  for(vpsc::Constraints::iterator i(gcs.begin()); i!=gcs.end(); i++) {
92  delete *i;
93  }
94  gcs.clear();
95  for(unsigned i=0;i<vars.size();i++) {
96  delete vars[i];
97  }
98  }
99  unsigned solve(std::valarray<double> const & b, std::valarray<double> & x);
100  void unfixPos(unsigned i) {
101  if(vars[i]->fixedDesiredPosition) {
102  vars[i]->weight=1;
103  vars[i]->fixedDesiredPosition=false;
104  }
105  }
106  void fixPos(const unsigned i,const double pos) {
107  vars[i]->weight=100000.;
108  vars[i]->desiredPosition=pos;
109  vars[i]->fixedDesiredPosition=true;
110  }
111  vpsc::Dim getDimension() const {
112  return k;
113  }
114  void straighten(
115  cola::SparseMatrix const * Q,
116  std::vector<SeparationConstraint*> const & ccs,
117  std::vector<straightener::Node*> const & snodes);
118  std::valarray<double> const & getFullResult() const {
119  return result;
120  }
121 private:
122  vpsc::IncSolver* setupVPSC();
123  double computeCost(std::valarray<double> const &b,
124  std::valarray<double> const &x) const;
125  double computeSteepestDescentVector(
126  std::valarray<double> const &b, std::valarray<double> const &place,
127  std::valarray<double> &g) const;
128  double computeScaledSteepestDescentVector(
129  std::valarray<double> const &b, std::valarray<double> const &place,
130  std::valarray<double> &g) const;
131  double computeStepSize(
132  std::valarray<double> const & g, std::valarray<double> const & d) const;
133  bool runSolver(std::valarray<double> & result);
134  void destroyVPSC(vpsc::IncSolver *vpsc);
135  vpsc::Dim k;
136  unsigned numStaticVars; // number of variables that persist
137  // throughout iterations
138  const unsigned denseSize; // denseQ has denseSize^2 entries
139  std::valarray<double> *denseQ; // dense square graph laplacian matrix
140  std::valarray<double> scaledDenseQ; // scaled dense square graph laplacian matrix
141  std::vector<vpsc::Rectangle*>* rs;
142  CompoundConstraints const *ccs;
143  UnsatisfiableConstraintInfos *unsatisfiableConstraints;
144  NonOverlapConstraintsMode nonOverlapConstraints;
145  Cluster* clusterHierarchy;
146  double tolerance;
147  unsigned max_iterations;
148  cola::SparseMatrix const * sparseQ; // sparse components of goal function
149  vpsc::Variables vars; // all variables
150  // computations
151  vpsc::Constraints gcs; /* global constraints - persist throughout all
152  iterations */
153  vpsc::Constraints lcs; /* local constraints - only for current iteration */
154  vpsc::Constraints cs; /* working list of constraints: gcs +lcs */
155  std::valarray<double> result;
156 #ifdef MOSEK_AVAILABLE
157  MosekEnv* menv;
158 #endif
159  vpsc::IncSolver* solver;
160  SolveWithMosek solveWithMosek;
161  const bool scaling;
162  std::vector<OrthogonalEdgeConstraint*> orthogonalEdges;
163 };
164 } // namespace cola
165 #endif /* _GRADIENT_PROJECTION_H */
Incremental solver for Variable Placement with Separation Constraints problem instance.
Definition: solve_VPSC.h:105
ProjectionResult solve(vpsc::Variables &vs, vpsc::Constraints &cs, vpsc::Rectangles &rs, unsigned debugLevel=0)
Constructs a solver and attempts to solve the passed constraints on the passed vars.
Definition: gradient_projection.h:40
libvpsc: Variable Placement with Separation Constraints quadratic program solver library.
Definition: assertions.h:61
std::vector< Variable * > Variables
A vector of pointers to Variable objects.
Definition: constraint.h:38
std::vector< Constraint * > Constraints
A vector of pointers to Constraint objects.
Definition: constraint.h:125
std::vector< Rectangle * > Rectangles
A vector of pointers to Rectangle objects.
Definition: rectangle.h:246
libcola: Force-directed network layout subject to separation constraints library. ...
Definition: box.cpp:25
Dim
Indicates the x- or y-dimension.
Definition: rectangle.h:41
std::vector< UnsatisfiableConstraintInfo * > UnsatisfiableConstraintInfos
A vector of pointers to UnsatisfiableConstraintInfo objects.
Definition: compound_constraints.h:826
std::vector< CompoundConstraint * > CompoundConstraints
A vector of pointers to CompoundConstraint objects.
Definition: compound_constraints.h:259