[116] | 1 | /*! |
---|
| 2 | * \file tspsolver.h |
---|
[151] | 3 | * \author Copyright © 2007-2011 Lёppa <contacts[at]oleksii[dot]name> |
---|
[116] | 4 | * |
---|
| 5 | * $Id: tspsolver.h 151 2011-01-19 22:27:49Z laleppa $ |
---|
| 6 | * $URL: https://tspsg.svn.sourceforge.net/svnroot/tspsg/trunk/src/tspsolver.h $ |
---|
| 7 | * |
---|
| 8 | * \brief Defines TSPSolver namespace and everything needed for solving TSP tasks. |
---|
| 9 | * |
---|
| 10 | * <b>TSPSG: TSP Solver and Generator</b> |
---|
| 11 | * |
---|
| 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 | #include <QtCore> |
---|
| 32 | #include <limits> |
---|
| 33 | |
---|
| 34 | /*! |
---|
| 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 | */ |
---|
| 41 | #ifdef INFINITY |
---|
[149] | 42 | # undef INFINITY |
---|
[116] | 43 | #endif |
---|
| 44 | #define INFINITY std::numeric_limits<double>::infinity() |
---|
| 45 | |
---|
| 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; |
---|
| 55 | |
---|
| 56 | /*! |
---|
| 57 | * \brief This structure represents one step of solving. |
---|
| 58 | * |
---|
| 59 | * A tree of such elements will represent the solving process. |
---|
| 60 | */ |
---|
| 61 | struct SStep { |
---|
[149] | 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 |
---|
[116] | 66 | |
---|
[149] | 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 | }; |
---|
[116] | 76 | |
---|
[149] | 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 | }; |
---|
[116] | 83 | |
---|
[149] | 84 | TMatrix matrix; //!< This step's matrix |
---|
| 85 | double price; //!< The price of travel to this step |
---|
[116] | 86 | |
---|
[149] | 87 | SCandidate candidate; //!< A candiadate for branching in the current step |
---|
| 88 | QList<SCandidate> alts; //!< A list of alternative branching candidates |
---|
| 89 | SStep *pNode; //!< Pointer to the parent step |
---|
| 90 | SStep *plNode; //!< Pointer to the left branch step |
---|
| 91 | SStep *prNode; //!< Pointer to the right branch step |
---|
| 92 | NextStep next; //!< Indicates what branch was selected for the next step |
---|
[116] | 93 | |
---|
[149] | 94 | //! Assigns default values |
---|
| 95 | SStep() { |
---|
| 96 | price = -1; |
---|
| 97 | pNode = plNode = prNode = NULL; |
---|
| 98 | next = NoNextStep; |
---|
| 99 | } |
---|
[116] | 100 | }; |
---|
| 101 | |
---|
| 102 | /*! |
---|
| 103 | * \brief This class solves Travelling Salesman Problem task. |
---|
[151] | 104 | * \author Copyright © 2007-2011 Lёppa <contacts[at]oleksii[dot]name> |
---|
[116] | 105 | */ |
---|
| 106 | class CTSPSolver: public QObject |
---|
| 107 | { |
---|
[149] | 108 | Q_OBJECT |
---|
[116] | 109 | |
---|
| 110 | public: |
---|
[149] | 111 | static QString getVersionId(); |
---|
[116] | 112 | |
---|
[149] | 113 | CTSPSolver(QObject *parent = NULL); |
---|
| 114 | void cleanup(bool processEvents = false); |
---|
| 115 | QString getSortedPath(const QString &city, const QString &separator = QString(" -> ")) const; |
---|
| 116 | int getTotalSteps() const; |
---|
| 117 | bool isOptimal() const; |
---|
| 118 | void setCleanupOnCancel(bool enable = true); |
---|
| 119 | SStep *solve(int numCities, const TMatrix &task); |
---|
| 120 | bool wasCanceled() const; |
---|
| 121 | ~CTSPSolver(); |
---|
[116] | 122 | |
---|
| 123 | public slots: |
---|
[149] | 124 | void cancel(); |
---|
[116] | 125 | |
---|
| 126 | signals: |
---|
[149] | 127 | /*! |
---|
| 128 | * \brief This signal is emitted once every time a part of the route is found. |
---|
| 129 | * \param n Indicates the number of the route parts found. |
---|
| 130 | */ |
---|
| 131 | void routePartFound(int n); |
---|
[116] | 132 | |
---|
| 133 | private: |
---|
[149] | 134 | bool mayNotBeOptimal, canceled, cc; |
---|
| 135 | int nCities, total; |
---|
| 136 | SStep *root; |
---|
| 137 | QHash<int,int> route; |
---|
| 138 | mutable QMutex mutex; |
---|
[116] | 139 | |
---|
[149] | 140 | double align(TMatrix &matrix); |
---|
| 141 | void deleteTree(SStep *&root, bool processEvents = false); |
---|
| 142 | void denormalize(TMatrix &matrix) const; |
---|
| 143 | QList<SStep::SCandidate> findCandidate(const TMatrix &matrix, int &nRow, int &nCol) const; |
---|
| 144 | double findMinInCol(int nCol, const TMatrix &matrix, int exr = -1) const; |
---|
| 145 | double findMinInRow(int nRow, const TMatrix &matrix, int exc = -1) const; |
---|
| 146 | void finishRoute(); |
---|
| 147 | bool hasSubCycles(int nRow, int nCol) const; |
---|
| 148 | void normalize(TMatrix &matrix) const; |
---|
| 149 | void subCol(TMatrix &matrix, int nCol, double val); |
---|
| 150 | void subRow(TMatrix &matrix, int nRow, double val); |
---|
[116] | 151 | }; |
---|
| 152 | |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | #ifdef DEBUG |
---|
| 156 | QDebug operator<<(QDebug dbg, const TSPSolver::TMatrix &matrix); |
---|
| 157 | QDebug operator<<(QDebug dbg, const TSPSolver::SStep::SCandidate &candidate); |
---|
| 158 | #endif // DEBUG |
---|
| 159 | |
---|
| 160 | #endif // TSPSOLVER_H |
---|