Changeset 107 in tspsg-svn for trunk/src/tspsolver.h
- Timestamp:
- Apr 25, 2010, 4:36:27 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/tspsolver.h
r99 r107 6 6 * $URL$ 7 7 * 8 * \brief Defines #TMatrix typedef, SCandidate and SStep structs and CTSPSolver class.8 * \brief Defines TSPSolver namespace and everything needed for solving TSP tasks. 9 9 * 10 10 * <b>TSPSG: TSP Solver and Generator</b> … … 29 29 #define TSPSOLVER_H 30 30 31 #include "globals.h" 32 33 #include "tspmodel.h" 34 35 //! A matrix of city-to-city travel costs. 36 typedef QList<QList<double> > TMatrix; 31 #include <QtCore> 32 #include <limits> 37 33 38 34 /*! 39 * \brief A structure that represents a candidate for branching. 35 * \def INFINITY 36 * \brief This value means infinity :-) 37 * 38 * Some libraries already have \c INFINITY defined. 39 * We need to redefine it for the \c INFINITY to always have the same value. 40 40 */ 41 struct SCandidate { 42 int nRow; //!< A zero-based row number of the candidate 43 int nCol; //!< A zero-based column number of the candidate 41 #ifdef INFINITY 42 #undef INFINITY 43 #endif 44 #define INFINITY std::numeric_limits<double>::infinity() 44 45 45 //! Assigns default values 46 SCandidate() { 47 nCol = nRow = -1; 48 } 49 //! An operator == implementation 50 bool operator ==(const SCandidate &cand) const{51 return ((cand.nRow == nRow) && (cand.nCol == nCol)); 52 } 53 };46 /*! 47 * \brief A TSP Solver namespace. 48 * 49 * This namespace contains all the stuff required for solving TSP tasks. 50 */ 51 namespace TSPSolver { 52 53 //! A matrix of city-to-city travel costs 54 typedef QList<QList<double> > TMatrix; 54 55 55 56 /*! … … 59 60 */ 60 61 struct SStep { 62 //! A structure that represents a candidate for branching 63 struct SCandidate { 64 int nRow; //!< A zero-based row number of the candidate 65 int nCol; //!< A zero-based column number of the candidate 66 67 //! Assigns default values 68 SCandidate() { 69 nCol = nRow = -1; 70 } 71 //! An operator == implementation 72 bool operator ==(const SCandidate &cand) const { 73 return ((cand.nRow == nRow) && (cand.nCol == nCol)); 74 } 75 }; 76 77 //! An enum that describes possible selection of the next step 78 enum NextStep { 79 NoNextStep, //!< No next step (end of solution) 80 LeftBranch, //!< Left branch was selected for the next step 81 RightBranch //!< Right branch was selected for the next step 82 }; 83 61 84 TMatrix matrix; //!< This step's matrix 62 85 double price; //!< The price of travel to this step 63 SCandidate candidate; //!< A candiadate for branching in the current matrix 86 87 SCandidate candidate; //!< A candiadate for branching in the current step 64 88 QList<SCandidate> alts; //!< A list of alternative branching candidates 65 89 SStep *pNode; //!< Pointer to the parent step 66 90 SStep *plNode; //!< Pointer to the left branch step 67 91 SStep *prNode; //!< Pointer to the right branch step 92 NextStep next; //!< Indicates what branch was selected for the next step 68 93 69 94 //! Assigns default values … … 71 96 price = -1; 72 97 pNode = plNode = prNode = NULL; 98 next = NoNextStep; 73 99 } 74 100 }; … … 88 114 void cleanup(bool processEvents = false); 89 115 QString getSortedPath() const; 116 int getTotalSteps() const; 90 117 bool isOptimal() const; 91 118 SStep *solve(int numCities, const TMatrix &task); … … 105 132 private: 106 133 bool mayNotBeOptimal, canceled; 107 int nCities ;134 int nCities, total; 108 135 SStep *root; 109 136 QHash<int,int> route; … … 113 140 void deleteTree(SStep *&root, bool processEvents = false); 114 141 void denormalize(TMatrix &matrix) const; 115 QList<S Candidate> findCandidate(const TMatrix &matrix, int &nRow, int &nCol) const;142 QList<SStep::SCandidate> findCandidate(const TMatrix &matrix, int &nRow, int &nCol) const; 116 143 double findMinInCol(int nCol, const TMatrix &matrix, int exr = -1) const; 117 144 double findMinInRow(int nRow, const TMatrix &matrix, int exc = -1) const; 145 void finishRoute(); 118 146 bool hasSubCycles(int nRow, int nCol) const; 119 147 void normalize(TMatrix &matrix) const; … … 122 150 }; 123 151 152 } 153 124 154 #ifdef DEBUG 125 QDebug operator<<(QDebug dbg, const T Matrix &matrix);126 QDebug operator<<(QDebug dbg, const SCandidate &candidate);155 QDebug operator<<(QDebug dbg, const TSPSolver::TMatrix &matrix); 156 QDebug operator<<(QDebug dbg, const TSPSolver::SStep::SCandidate &candidate); 127 157 #endif // DEBUG 128 158
Note: See TracChangeset
for help on using the changeset viewer.