PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/OGDF/ogdf/internal/cluster/MaxCPlanar_Master.h

https://gitlab.com/cjeight/tortoisegit
C Header | 342 lines | 178 code | 64 blank | 100 comment | 3 complexity | 8761309d95ac46b96fe15091c274f8a0 MD5 | raw file
  1. /*
  2. * $Revision: 2523 $
  3. *
  4. * last checkin:
  5. * $Author: gutwenger $
  6. * $Date: 2012-07-02 20:59:27 +0200 (Mon, 02 Jul 2012) $
  7. ***************************************************************/
  8. /** \file
  9. * \brief Declaration of the master class for the Branch&Cut algorithm
  10. * for the Maximum C-Planar SubGraph problem.
  11. *
  12. * This class is managing the optimization.
  13. * Variables and initial constraints are generated and pools are initialized.
  14. *
  15. * \author Mathias Jansen
  16. *
  17. *
  18. * \par License:
  19. * This file is part of the Open Graph Drawing Framework (OGDF).
  20. *
  21. * \par
  22. * Copyright (C)<br>
  23. * See README.txt in the root directory of the OGDF installation for details.
  24. *
  25. * \par
  26. * This program is free software; you can redistribute it and/or
  27. * modify it under the terms of the GNU General Public License
  28. * Version 2 or 3 as published by the Free Software Foundation;
  29. * see the file LICENSE.txt included in the packaging of this file
  30. * for details.
  31. *
  32. * \par
  33. * This program is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. * GNU General Public License for more details.
  37. *
  38. * \par
  39. * You should have received a copy of the GNU General Public
  40. * License along with this program; if not, write to the Free
  41. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  42. * Boston, MA 02110-1301, USA.
  43. *
  44. * \see http://www.gnu.org/copyleft/gpl.html
  45. ***************************************************************/
  46. #ifndef OGDF_MAX_CPLANAR_MASTER_H
  47. #define OGDF_MAX_CPLANAR_MASTER_H
  48. //#include <ogdf/timer.h>
  49. #include <ogdf/basic/GraphCopy.h>
  50. #include <ogdf/internal/cluster/Cluster_EdgeVar.h>
  51. #include <ogdf/internal/cluster/basics.h>
  52. #include <ogdf/basic/Logger.h>
  53. #include <ogdf/basic/ArrayBuffer.h>
  54. #include <abacus/string.h>
  55. namespace ogdf {
  56. class Master : public ABA_MASTER {
  57. friend class Sub;
  58. // Pointers to the given Clustergraph and underlying Graph are stored.
  59. const ClusterGraph *m_C;
  60. const Graph *m_G;
  61. // Each time the primal bound is improved, the integer solution induced Graph is built.
  62. // \a m_solutionGraph is a pointer to the currently best solution induced Graph.
  63. GraphCopy *m_solutionGraph;
  64. List<nodePair> m_allOneEdges; //<! Contains all nodePairs whose variable is set to 1.0
  65. List<nodePair> m_originalOneEdges; //<! Contains original nodePairs whose variable is set to 1.0
  66. List<nodePair> m_connectionOneEdges; //<! Contains connection nodePairs whose variable is set to 1.0
  67. List<edge> m_deletedOriginalEdges; //<! Contains original edges whose variable is set to 0.0
  68. public:
  69. // Construction and default values
  70. Master(
  71. const ClusterGraph &C,
  72. int heuristicLevel=1,
  73. int heuristicRuns=2,
  74. double heuristicOEdgeBound=0.3,
  75. int heuristicNPermLists=5,
  76. int kuratowskiIterations=3,
  77. int subdivisions=10,
  78. int kSupportGraphs=3,
  79. double kuratowskiHigh=0.7,
  80. double kuratowskiLow=0.3,
  81. bool perturbation=false,
  82. double branchingGap=0.4,
  83. const char *time="00:20:00", //maximum computation time
  84. bool dopricing = true,
  85. bool checkCPlanar = false, //just check c-planarity
  86. int numAddVariables = 15,
  87. double strongConstraintViolation = 0.3,
  88. double strongVariableViolation = 0.3,
  89. ABA_MASTER::OUTLEVEL ol=Silent);
  90. // Destruction
  91. virtual ~Master();
  92. // Initialisation of the first Subproblem
  93. virtual ABA_SUB *firstSub();
  94. // Returns the objective function coefficient of C-edges
  95. double epsilon() const {return m_epsilon;}
  96. // Returns the number of variables
  97. int nMaxVars() const {return m_nMaxVars;}
  98. // Returns a pointer to the underlying Graph
  99. const Graph *getGraph() const {return m_G;}
  100. // Returns a pointer to the given Clustergraph.
  101. const ClusterGraph *getClusterGraph() const {return m_C;}
  102. // Updates the "best" Subgraph \a m_solutionGraph found so far and fills edge lists with
  103. // corresponding edges (nodePairs).
  104. void updateBestSubGraph(List<nodePair> &original, List<nodePair> &connection, List<edge> &deleted);
  105. // Returns the optimal solution induced Clustergraph
  106. Graph *solutionInducedGraph() {return (Graph*)m_solutionGraph;}
  107. // Returns nodePairs of original, connection, deleted or all optimal solution edges in list \a edges.
  108. void getAllOptimalSolutionEdges(List<nodePair> &edges) const;
  109. void getOriginalOptimalSolutionEdges(List<nodePair> &edges) const;
  110. void getConnectionOptimalSolutionEdges(List<nodePair> &egdes) const;
  111. void getDeletedEdges(List<edge> &edges) const;
  112. // Get parameters
  113. const int getKIterations() const {return m_nKuratowskiIterations;}
  114. const int getNSubdivisions() const {return m_nSubdivisions;}
  115. const int getNKuratowskiSupportGraphs() const {return m_nKuratowskiSupportGraphs;}
  116. const int getHeuristicLevel() const {return m_heuristicLevel;}
  117. const int getHeuristicRuns() const {return m_nHeuristicRuns;}
  118. const double getKBoundHigh() const {return m_kuratowskiBoundHigh;}
  119. const double getKBoundLow() const {return m_kuratowskiBoundLow;}
  120. const bool perturbation() const {return m_usePerturbation;}
  121. const double branchingOEdgeSelectGap() const {return m_branchingGap;}
  122. const double getHeuristicFractionalBound() const {return m_heuristicFractionalBound;}
  123. const int numberOfHeuristicPermutationLists() const {return m_nHeuristicPermutationLists;}
  124. const bool getMPHeuristic() const {return m_mpHeuristic;}
  125. const bool getCheckCPlanar() const {return m_checkCPlanar;}
  126. const int getNumAddVariables() const {return m_numAddVariables;}
  127. const double getStrongConstraintViolation() const {return m_strongConstraintViolation;}
  128. const double getStrongVariableViolation() const {return m_strongVariableViolation;}
  129. // Read global constraint counter, i.e. the number of added constraints of specific type.
  130. const int addedKConstraints() const {return m_nKConsAdded;}
  131. const int addedCConstraints() const {return m_nCConsAdded;}
  132. // Set parameters
  133. void setKIterations(int n) {m_nKuratowskiIterations = n;}
  134. void setNSubdivisions(int n) {m_nSubdivisions = n;}
  135. void setNKuratowskiSupportGraphs(int n) {m_nKuratowskiSupportGraphs = n;}
  136. void setNHeuristicRuns(int n) {m_nHeuristicRuns = n;}
  137. void setKBoundHigh(double n) {m_kuratowskiBoundHigh = ((n>0.0 && n<1.0) ? n : 0.8);}
  138. void setKBoundLow(double n) {m_kuratowskiBoundLow = ((n>0.0 && n<1.0) ? n : 0.2);}
  139. void heuristicLevel(int level) {m_heuristicLevel = level;}
  140. void setHeuristicRuns(int n) {m_nHeuristicRuns = n;}
  141. void setPertubation(bool b) {m_usePerturbation = b;}
  142. void setHeuristicFractionalBound(double b) {m_heuristicFractionalBound = b;}
  143. void setHeuristicPermutationLists(int n) {m_nHeuristicPermutationLists = n;}
  144. void setMPHeuristic(bool b) {m_mpHeuristic = b;}//!< Switches use of lower bound heuristic
  145. void setNumAddVariables(int i) {m_numAddVariables=i;}
  146. void setStrongConstraintViolation(double d) { m_strongConstraintViolation=d;}
  147. void setStrongVariableViolation(double d) { m_strongVariableViolation=d;}
  148. //! If set to true, PORTA output is written in a file
  149. void setPortaFile(bool b) {m_porta = b;}
  150. // Updating global constraint counter
  151. void updateAddedCCons(int n) {m_nCConsAdded += n;}
  152. void updateAddedKCons(int n) {m_nKConsAdded += n;}
  153. // Returns global primal and dual bounds.
  154. double getPrimalBound() {return globalPrimalBound;}
  155. double getDualBound() {return globalDualBound;}
  156. // Cut pools for connectivity and planarity
  157. //! Returns cut pool for connectivity
  158. ABA_STANDARDPOOL<ABA_CONSTRAINT, ABA_VARIABLE> *getCutConnPool() {return m_cutConnPool;}
  159. //! Returns cut pool for planarity
  160. ABA_STANDARDPOOL<ABA_CONSTRAINT, ABA_VARIABLE> *getCutKuraPool() {return m_cutKuraPool;}
  161. //! Returns true if default cut pool is used. Otherwise, separate
  162. //! connectivity and Kuratowski pools are generated and used.
  163. bool &useDefaultCutPool() { return m_useDefaultCutPool;}
  164. #ifdef OGDF_DEBUG
  165. bool m_solByHeuristic; //solution computed by heuristic or ILP
  166. // Simple output function to print the given graph to the console.
  167. // Used for debugging only.
  168. void printGraph(const Graph &G);
  169. #endif
  170. //! The name of the file that contains the standard, i.e., non-cut,
  171. //! constraints (may be deleted by ABACUS and shouldn't be stored twice)
  172. const char* getStdConstraintsFileName()
  173. {
  174. return "StdConstraints.txt";
  175. }
  176. int getNumInactiveVars() { return m_inactiveVariables.size();}
  177. protected:
  178. // Initializes constraints and variables and an initial dual bound.
  179. virtual void initializeOptimization();
  180. // Function that is invoked at the end of the optimization
  181. virtual void terminateOptimization();
  182. double heuristicInitialLowerBound();
  183. private:
  184. // Computes a dual bound for the optimal solution.
  185. // Tries to find as many edge-disjoint Kuratowski subdivisions as possible.
  186. // If k edge-disjoint groups of subdivisions are found, the upper bound can be
  187. // initialized with number of edges in underlying graph minus k.
  188. double heuristicInitialUpperBound();
  189. // Is invoked by heuristicInitialUpperBound()
  190. void clusterConnection(cluster c, GraphCopy &GC, double &upperBound);
  191. // Computes the graphtheoretical distances of edges incident to node \a u.
  192. void nodeDistances(node u, NodeArray<NodeArray<int> > &dist);
  193. // Parameters
  194. int m_nKuratowskiSupportGraphs; // Maximal number of times the Kuratowski support graph is computed
  195. int m_nKuratowskiIterations; // Maximal number of times BoyerMyrvold is invoked
  196. int m_nSubdivisions; // Maximal number of extracted Kuratowski subdivisions
  197. int m_nMaxVars; // Max Number of variables
  198. int m_heuristicLevel; // Indicates if primal heuristic shall be used or not
  199. int m_nHeuristicRuns; // Counts how often the primal heuristic has been called
  200. bool m_usePerturbation; // Indicates whether C-variables should be perturbated or not
  201. double m_branchingGap; // Modifies the branching behaviour
  202. double m_heuristicFractionalBound;
  203. int m_nHeuristicPermutationLists; // The number of permutation lists used in the primal heuristic
  204. bool m_mpHeuristic; //!< Indicates if simple max planar subgraph heuristic
  205. // should be used to derive lower bound if only root cluster exists
  206. double m_kuratowskiBoundHigh; // Upper bound for deterministic edge addition in computation of the Supportgraph
  207. double m_kuratowskiBoundLow; // Lower bound for deterministic edge deletion in computation of the Supportgraph
  208. int m_numAddVariables; // how many variables should i add maximally per pricing round?
  209. double m_strongConstraintViolation; // when do i consider a constraint strongly violated -> separate in first stage
  210. double m_strongVariableViolation; // when do i consider a variable strongly violated (red.cost) -> separate in first stage
  211. ABA_MASTER::OUTLEVEL m_out; // Determines the output level of ABACUS
  212. ABA_STRING *m_maxCpuTime; // Time threshold for optimization
  213. // The basic objective function coefficient for connection edges.
  214. double m_epsilon;
  215. // If pertubation is used, this variable stores the largest occuring coeff,
  216. // i.e. the one closest to 0. Otherwise it corresponds to \a m_epsilon
  217. double m_largestConnectionCoeff;
  218. // Counters for the number of added constraints
  219. int m_nCConsAdded;
  220. int m_nKConsAdded;
  221. int m_solvesLP;
  222. int m_varsInit;
  223. int m_varsAdded;
  224. int m_varsPotential;
  225. int m_varsMax;
  226. int m_varsCut;
  227. int m_varsKura;
  228. int m_varsPrice;
  229. int m_varsBranch;
  230. int m_activeRepairs;
  231. ArrayBuffer<int> m_repairStat;
  232. inline void clearActiveRepairs() {
  233. if(m_activeRepairs) {
  234. m_repairStat.push(m_activeRepairs);
  235. m_activeRepairs = 0;
  236. }
  237. }
  238. double globalPrimalBound;
  239. double globalDualBound;
  240. inline double getDoubleTime(const ABA_TIMER* act) {
  241. long tempo = act->centiSeconds()+100*act->seconds()+6000*act->minutes()+360000*act->hours();
  242. return ((double) tempo)/ 100.0;
  243. }
  244. //number of calls of the fast max planar subgraph heuristic
  245. const int m_fastHeuristicRuns;
  246. //! Cut pools for connectivity and Kuratowski constraints
  247. ABA_STANDARDPOOL< ABA_CONSTRAINT, ABA_VARIABLE > *m_cutConnPool; //!< Connectivity Cuts
  248. ABA_STANDARDPOOL< ABA_CONSTRAINT, ABA_VARIABLE > *m_cutKuraPool; //!< Kuratowski Cuts
  249. //! Defines if the ABACUS default cut pool or the separate Connectivity
  250. //! and Kuratowski constraint pools are used
  251. bool m_useDefaultCutPool;
  252. //! Defines if only clustered planarity is checked, i.e.,
  253. //! all edges of the original graph need to be fixed to be
  254. //! part of the solution
  255. bool m_checkCPlanar;
  256. //!
  257. double m_delta;
  258. double m_deltaCount;
  259. double nextConnectCoeff() { return (m_checkCPlanar ? -1 : -m_epsilon) + m_deltaCount--*m_delta; } // TODO-TESTING
  260. EdgeVar* createVariable(ListIterator<nodePair>& it) {
  261. ++m_varsAdded;
  262. EdgeVar* v = new EdgeVar(this, nextConnectCoeff(), EdgeVar::CONNECT, (*it).v1, (*it).v2);
  263. v->printMe(Logger::slout());
  264. m_inactiveVariables.del(it);
  265. return v;
  266. }
  267. List<nodePair> m_inactiveVariables;
  268. void generateVariablesForFeasibility(const List<ChunkConnection*>& ccons, List<EdgeVar*>& connectVars);
  269. bool goodVar(node a, node b);
  270. //! If set to true, PORTA output is written in a file
  271. bool m_porta;
  272. //! writes coefficients of all orig and connect variables in constraint con into
  273. //! emptied list coeffs
  274. void getCoefficients(ABA_CONSTRAINT* con, const List<EdgeVar *> & orig,
  275. const List<EdgeVar* > & connect, List<double> & coeffs);
  276. };
  277. }//end namespace
  278. #endif