[65] | 1 | /*! |
---|
[67] | 2 | * \file tspsolver.h |
---|
[87] | 3 | * \author Copyright © 2007-2010 Lёppa <contacts[at]oleksii[dot]name> |
---|
[12] | 4 | * |
---|
| 5 | * $Id: tspsolver.h 99 2010-03-22 20:45:16Z laleppa $ |
---|
| 6 | * $URL: https://tspsg.svn.sourceforge.net/svnroot/tspsg/trunk/src/tspsolver.h $ |
---|
| 7 | * |
---|
[76] | 8 | * \brief Defines #TMatrix typedef, SCandidate and SStep structs and CTSPSolver class. |
---|
[67] | 9 | * |
---|
[65] | 10 | * <b>TSPSG: TSP Solver and Generator</b> |
---|
| 11 | * |
---|
[12] | 12 | * This file is part of TSPSG. |
---|
| 13 | * |
---|
| 14 | * TSPSG is free software: you can redistribute it and/or modify |
---|
| 15 | * it under the terms of the GNU General Public License as published by |
---|
| 16 | * the Free Software Foundation, either version 3 of the License, or |
---|
| 17 | * (at your option) any later version. |
---|
| 18 | * |
---|
| 19 | * TSPSG is distributed in the hope that it will be useful, |
---|
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 22 | * GNU General Public License for more details. |
---|
| 23 | * |
---|
| 24 | * You should have received a copy of the GNU General Public License |
---|
| 25 | * along with TSPSG. If not, see <http://www.gnu.org/licenses/>. |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | #ifndef TSPSOLVER_H |
---|
| 29 | #define TSPSOLVER_H |
---|
| 30 | |
---|
[31] | 31 | #include "globals.h" |
---|
[12] | 32 | |
---|
[64] | 33 | #include "tspmodel.h" |
---|
| 34 | |
---|
[65] | 35 | //! A matrix of city-to-city travel costs. |
---|
[89] | 36 | typedef QList<QList<double> > TMatrix; |
---|
[13] | 37 | |
---|
[65] | 38 | /*! |
---|
[74] | 39 | * \brief A structure that represents a candidate for branching. |
---|
| 40 | */ |
---|
[76] | 41 | struct SCandidate { |
---|
[74] | 42 | int nRow; //!< A zero-based row number of the candidate |
---|
| 43 | int nCol; //!< A zero-based column number of the candidate |
---|
| 44 | |
---|
| 45 | //! Assigns default values |
---|
[76] | 46 | SCandidate() { |
---|
[74] | 47 | nCol = nRow = -1; |
---|
| 48 | } |
---|
| 49 | //! An operator == implementation |
---|
[76] | 50 | bool operator ==(const SCandidate &cand) const { |
---|
[74] | 51 | return ((cand.nRow == nRow) && (cand.nCol == nCol)); |
---|
| 52 | } |
---|
| 53 | }; |
---|
| 54 | |
---|
| 55 | /*! |
---|
[65] | 56 | * \brief This structure represents one step of solving. |
---|
| 57 | * |
---|
| 58 | * A tree of such elements will represent the solving process. |
---|
| 59 | */ |
---|
[74] | 60 | struct SStep { |
---|
| 61 | TMatrix matrix; //!< This step's matrix |
---|
[89] | 62 | double price; //!< The price of travel to this step |
---|
[76] | 63 | SCandidate candidate; //!< A candiadate for branching in the current matrix |
---|
| 64 | QList<SCandidate> alts; //!< A list of alternative branching candidates |
---|
[90] | 65 | SStep *pNode; //!< Pointer to the parent step |
---|
[74] | 66 | SStep *plNode; //!< Pointer to the left branch step |
---|
| 67 | SStep *prNode; //!< Pointer to the right branch step |
---|
[65] | 68 | |
---|
| 69 | //! Assigns default values |
---|
[74] | 70 | SStep() { |
---|
| 71 | price = -1; |
---|
[90] | 72 | pNode = plNode = prNode = NULL; |
---|
[65] | 73 | } |
---|
[12] | 74 | }; |
---|
| 75 | |
---|
[67] | 76 | /*! |
---|
| 77 | * \brief This class solves Travelling Salesman Problem task. |
---|
[87] | 78 | * \author Copyright © 2007-2010 Lёppa <contacts[at]oleksii[dot]name> |
---|
[67] | 79 | */ |
---|
[99] | 80 | class CTSPSolver: public QObject |
---|
[12] | 81 | { |
---|
[99] | 82 | Q_OBJECT |
---|
[42] | 83 | |
---|
[12] | 84 | public: |
---|
[99] | 85 | static QString getVersionId(); |
---|
| 86 | |
---|
| 87 | CTSPSolver(QObject *parent = NULL); |
---|
| 88 | void cleanup(bool processEvents = false); |
---|
[60] | 89 | QString getSortedPath() const; |
---|
| 90 | bool isOptimal() const; |
---|
[99] | 91 | SStep *solve(int numCities, const TMatrix &task); |
---|
| 92 | bool wasCanceled() const; |
---|
[74] | 93 | ~CTSPSolver(); |
---|
[42] | 94 | |
---|
[99] | 95 | public slots: |
---|
| 96 | void cancel(); |
---|
| 97 | |
---|
| 98 | signals: |
---|
| 99 | /*! |
---|
| 100 | * \brief This signal is emitted once every time a part of the route is found. |
---|
| 101 | * \param n Indicates the number of the route parts found. |
---|
| 102 | */ |
---|
| 103 | void routePartFound(int n); |
---|
| 104 | |
---|
[13] | 105 | private: |
---|
[99] | 106 | bool mayNotBeOptimal, canceled; |
---|
[13] | 107 | int nCities; |
---|
[74] | 108 | SStep *root; |
---|
[42] | 109 | QHash<int,int> route; |
---|
[99] | 110 | mutable QMutex mutex; |
---|
[67] | 111 | |
---|
[89] | 112 | double align(TMatrix &matrix); |
---|
[99] | 113 | void deleteTree(SStep *&root, bool processEvents = false); |
---|
| 114 | void denormalize(TMatrix &matrix) const; |
---|
[76] | 115 | QList<SCandidate> findCandidate(const TMatrix &matrix, int &nRow, int &nCol) const; |
---|
[89] | 116 | double findMinInCol(int nCol, const TMatrix &matrix, int exr = -1) const; |
---|
| 117 | double findMinInRow(int nRow, const TMatrix &matrix, int exc = -1) const; |
---|
[71] | 118 | bool hasSubCycles(int nRow, int nCol) const; |
---|
[99] | 119 | void normalize(TMatrix &matrix) const; |
---|
[89] | 120 | void subCol(TMatrix &matrix, int nCol, double val); |
---|
| 121 | void subRow(TMatrix &matrix, int nRow, double val); |
---|
[12] | 122 | }; |
---|
| 123 | |
---|
[98] | 124 | #ifdef DEBUG |
---|
| 125 | QDebug operator<<(QDebug dbg, const TMatrix &matrix); |
---|
[99] | 126 | QDebug operator<<(QDebug dbg, const SCandidate &candidate); |
---|
| 127 | #endif // DEBUG |
---|
[98] | 128 | |
---|
[12] | 129 | #endif // TSPSOLVER_H |
---|