00001 #ifndef _LAYOUTOPTIMIZATION_H_ 00002 #define _LAYOUTOPTIMIZATION_H_ 00003 00004 #pragma warning( disable : 4786) 00005 00006 #include <set> 00007 #include "GAOptimizer.h" 00008 #include "Graph.h" 00009 00010 #define XMARGIN (GME_GRID_SIZE * 5) 00011 #define YMARGIN (GME_GRID_SIZE * 3) 00012 00013 typedef std::vector<int> IntVec; 00014 typedef std::set<int> IntSet; 00015 00016 class LayoutSolution; 00017 00018 class LayoutOptimizerListener 00019 { 00020 public: 00021 enum ContinueAbortOrCurrent { 00022 CONTINUE, 00023 ABORT, 00024 CURRENT, 00025 }; 00026 virtual ContinueAbortOrCurrent update( int percentage, LayoutSolution * sol, double score ) = 0; 00027 }; 00028 00029 class LayoutOptimizer 00030 { 00031 public: 00032 LayoutOptimizer ( Graph * graph ); 00033 00034 void optimize ( LayoutOptimizerListener * listener = NULL, bool startFromScratch=true ); 00035 00036 private: 00037 Graph * m_graph; 00038 }; 00039 00040 class LayoutOptProblem: public GAOptimizer::IProblem 00041 { 00042 public: 00043 LayoutOptProblem ( Graph * graph, int subGraph, bool startFromScratch ); 00044 00045 00046 00047 virtual GAOptimizer::IGenotype * createRandomSolution ( int i ); 00048 00049 virtual double evaluteSolution ( GAOptimizer::IGenotype * solution ); 00050 00051 int getWidth () { return m_width; } 00052 00053 int getHeight () { return m_height; } 00054 00055 EdgeVec& getEdges () { return m_edges; } 00056 00057 private: 00058 bool m_startFromScratch; 00059 NodeVec m_nodes; 00060 EdgeVec m_edges; 00061 int m_nodeNum; 00062 int m_width; 00063 int m_height; 00064 00065 friend class LayoutSolution; 00066 friend class LayoutOptimizer; 00067 }; 00068 00069 struct NodePos 00070 { 00071 Node * m_node; 00072 int m_x; 00073 int m_y; 00074 int m_tempx; 00075 int m_tempy; 00076 }; 00077 00078 typedef std::vector<NodePos> NodePosVec; 00079 00080 class LayoutSolution: public GAOptimizer::IGenotype 00081 { 00082 public: 00083 LayoutSolution ( LayoutOptProblem * problem ); 00084 00085 void derive ( GAOptimizer::IGenotype * parent1, 00086 GAOptimizer::IGenotype * parent2 ); 00087 00088 void random (); 00089 00090 void copyFromGraph (); 00091 00092 bool areConnectionsCrossed ( Edge * e1, Edge * e2 ); 00093 00094 static bool areLinesCrossed ( int x1, int y1, int x2, int y2, int xp1, int yp1, int xp2, int yp2 ); 00095 00096 int getDirViolations ( Edge * e ); 00097 00098 double getScore (); 00099 00100 NodePosVec& getNodes () { return m_nodes; } 00101 00102 LayoutOptProblem* getProblem () { return m_problem; } 00103 00104 void calcBoundingBox (); 00105 00106 void crop (); 00107 00108 void move ( int dx, int dy ); 00109 00110 void calcEdgeEnds ( Edge * e, int& x1, int& y1, int& x2, int& y2 ); 00111 00112 private: 00113 void calcEdgeEnds ( Edge * e ); 00114 00115 bool isNodePlaceable ( int node, int x, int y ); 00116 00117 void placeNode ( int node, int x, int y ); 00118 00119 void removeNode ( int node ); 00120 00121 void placeNodeNearPos ( int node, int xorg, int yorg ); 00122 00123 void placeNodeToRandomPos ( int node ); 00124 00125 void randomSwap (); 00126 00127 void moveOneToRandomPos (); 00128 00129 void selectRandomNodes ( IntSet& nodes ); 00130 00131 void addSubGraphNodes ( Node * act, IntSet& nodes, Node * prohibited, double cutoff ); 00132 00133 void selectSubGraph ( IntSet& nodes ); 00134 00135 void moveSome (); 00136 00137 void moveOne (); 00138 00139 void mutate (); 00140 00141 00142 LayoutOptProblem * m_problem; 00143 NodePosVec m_nodes; 00144 00145 int m_xmin; 00146 int m_ymin; 00147 int m_xmax; 00148 int m_ymax; 00149 00150 friend class LayoutOptimizer; 00151 }; 00152 00153 00154 00155 #endif // _LAYOUTOPTIMIZATION_H_