[caef58b531] | 1 | /*! |
---|
[e0fcac5f2c] | 2 | * \file tspsolver.h |
---|
[caef58b531] | 3 | * \author Copyright © 2007-2009 Lёppa <contacts[at]oleksii[dot]name> |
---|
[67e53c96d7] | 4 | * |
---|
| 5 | * $Id$ |
---|
| 6 | * $URL$ |
---|
| 7 | * |
---|
[fcaa74f7d7] | 8 | * \brief Defines #TMatrix typedef, SCandidate and SStep structs and CTSPSolver class. |
---|
[e0fcac5f2c] | 9 | * |
---|
[caef58b531] | 10 | * <b>TSPSG: TSP Solver and Generator</b> |
---|
| 11 | * |
---|
[67e53c96d7] | 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 | |
---|
[993d5af6f6] | 31 | #include "globals.h" |
---|
[67e53c96d7] | 32 | |
---|
[bc1b8837b6] | 33 | #include "tspmodel.h" |
---|
| 34 | |
---|
[caef58b531] | 35 | //! A matrix of city-to-city travel costs. |
---|
[f0464480db] | 36 | typedef QList<QList<double> > TMatrix; |
---|
| 37 | |
---|
| 38 | /*! |
---|
| 39 | * \brief A structure that represents a candidate for branching. |
---|
| 40 | */ |
---|
[fcaa74f7d7] | 41 | struct SCandidate { |
---|
[f0464480db] | 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 |
---|
[fcaa74f7d7] | 46 | SCandidate() { |
---|
[f0464480db] | 47 | nCol = nRow = -1; |
---|
| 48 | } |
---|
| 49 | //! An operator == implementation |
---|
[fcaa74f7d7] | 50 | bool operator ==(const SCandidate &cand) const { |
---|
[f0464480db] | 51 | return ((cand.nRow == nRow) && (cand.nCol == nCol)); |
---|
| 52 | } |
---|
| 53 | }; |
---|
[e664262f7d] | 54 | |
---|
[caef58b531] | 55 | /*! |
---|
| 56 | * \brief This structure represents one step of solving. |
---|
| 57 | * |
---|
| 58 | * A tree of such elements will represent the solving process. |
---|
| 59 | */ |
---|
[f0464480db] | 60 | struct SStep { |
---|
| 61 | TMatrix matrix; //!< This step's matrix |
---|
[caef58b531] | 62 | double price; //!< The price of travel to this step |
---|
[fcaa74f7d7] | 63 | SCandidate candidate; //!< A candiadate for branching in the current matrix |
---|
| 64 | QList<SCandidate> alts; //!< A list of alternative branching candidates |
---|
[f0464480db] | 65 | SStep *plNode; //!< Pointer to the left branch step |
---|
| 66 | SStep *prNode; //!< Pointer to the right branch step |
---|
[caef58b531] | 67 | |
---|
| 68 | //! Assigns default values |
---|
[f0464480db] | 69 | SStep() { |
---|
| 70 | price = -1; |
---|
[caef58b531] | 71 | plNode = prNode = NULL; |
---|
| 72 | } |
---|
[67e53c96d7] | 73 | }; |
---|
| 74 | |
---|
[e0fcac5f2c] | 75 | /*! |
---|
| 76 | * \brief This class solves Travelling Salesman Problem task. |
---|
| 77 | * \author Copyright © 2007-2009 Lёppa <contacts[at]oleksii[dot]name> |
---|
| 78 | * |
---|
| 79 | * \todo TODO: Deletion of solution tree on destroy and cleanup. |
---|
| 80 | */ |
---|
[67e53c96d7] | 81 | class CTSPSolver |
---|
| 82 | { |
---|
[430bd7f7e9] | 83 | Q_DECLARE_TR_FUNCTIONS(CTSPSolver) |
---|
| 84 | |
---|
[67e53c96d7] | 85 | public: |
---|
| 86 | CTSPSolver(); |
---|
[9cf98b9bd6] | 87 | QString getSortedPath() const; |
---|
[e0fcac5f2c] | 88 | static QString getVersionId(); |
---|
[9cf98b9bd6] | 89 | bool isOptimal() const; |
---|
[f0464480db] | 90 | SStep *solve(int numCities, TMatrix task, QWidget *parent = 0); |
---|
| 91 | ~CTSPSolver(); |
---|
[430bd7f7e9] | 92 | |
---|
[e664262f7d] | 93 | private: |
---|
[9cf98b9bd6] | 94 | bool mayNotBeOptimal; |
---|
[e664262f7d] | 95 | int nCities; |
---|
[f0464480db] | 96 | SStep *root; |
---|
[430bd7f7e9] | 97 | QHash<int,int> route; |
---|
[aaf2113307] | 98 | // QHash<int,int> forbidden; |
---|
[e0fcac5f2c] | 99 | |
---|
[f0464480db] | 100 | double align(TMatrix &matrix); |
---|
[430bd7f7e9] | 101 | void cleanup(); |
---|
[140109febb] | 102 | void deleteNode(SStep *&node); |
---|
[fcaa74f7d7] | 103 | QList<SCandidate> findCandidate(const TMatrix &matrix, int &nRow, int &nCol) const; |
---|
[f0464480db] | 104 | double findMinInCol(int nCol, const TMatrix &matrix, int exr = -1) const; |
---|
| 105 | double findMinInRow(int nRow, const TMatrix &matrix, int exc = -1) const; |
---|
[0ac9690913] | 106 | bool hasSubCycles(int nRow, int nCol) const; |
---|
[f0464480db] | 107 | void subCol(TMatrix &matrix, int nCol, double val); |
---|
| 108 | void subRow(TMatrix &matrix, int nRow, double val); |
---|
[67e53c96d7] | 109 | }; |
---|
| 110 | |
---|
| 111 | #endif // TSPSOLVER_H |
---|