1 | /*! |
---|
2 | * \file tspsolver.h |
---|
3 | * \author Copyright © 2007-2010 Lёppa <contacts[at]oleksii[dot]name> |
---|
4 | * |
---|
5 | * $Id$ |
---|
6 | * $URL$ |
---|
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 |
---|
42 | #undef INFINITY |
---|
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 { |
---|
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 | |
---|
84 | TMatrix matrix; //!< This step's matrix |
---|
85 | double price; //!< The price of travel to this step |
---|
86 | |
---|
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 |
---|
93 | |
---|
94 | //! Assigns default values |
---|
95 | SStep() { |
---|
96 | price = -1; |
---|
97 | pNode = plNode = prNode = NULL; |
---|
98 | next = NoNextStep; |
---|
99 | } |
---|
100 | }; |
---|
101 | |
---|
102 | /*! |
---|
103 | * \brief This class solves Travelling Salesman Problem task. |
---|
104 | * \author Copyright © 2007-2010 Lёppa <contacts[at]oleksii[dot]name> |
---|
105 | */ |
---|
106 | class CTSPSolver: public QObject |
---|
107 | { |
---|
108 | Q_OBJECT |
---|
109 | |
---|
110 | public: |
---|
111 | static QString getVersionId(); |
---|
112 | |
---|
113 | CTSPSolver(QObject *parent = NULL); |
---|
114 | void cleanup(bool processEvents = false); |
---|
115 | QString getSortedPath(const QString &city, const QString &separator = " -> ") const; |
---|
116 | int getTotalSteps() const; |
---|
117 | bool isOptimal() const; |
---|
118 | SStep *solve(int numCities, const TMatrix &task); |
---|
119 | bool wasCanceled() const; |
---|
120 | ~CTSPSolver(); |
---|
121 | |
---|
122 | public slots: |
---|
123 | void cancel(); |
---|
124 | |
---|
125 | signals: |
---|
126 | /*! |
---|
127 | * \brief This signal is emitted once every time a part of the route is found. |
---|
128 | * \param n Indicates the number of the route parts found. |
---|
129 | */ |
---|
130 | void routePartFound(int n); |
---|
131 | |
---|
132 | private: |
---|
133 | bool mayNotBeOptimal, canceled; |
---|
134 | int nCities, total; |
---|
135 | SStep *root; |
---|
136 | QHash<int,int> route; |
---|
137 | mutable QMutex mutex; |
---|
138 | |
---|
139 | double align(TMatrix &matrix); |
---|
140 | void deleteTree(SStep *&root, bool processEvents = false); |
---|
141 | void denormalize(TMatrix &matrix) const; |
---|
142 | QList<SStep::SCandidate> findCandidate(const TMatrix &matrix, int &nRow, int &nCol) const; |
---|
143 | double findMinInCol(int nCol, const TMatrix &matrix, int exr = -1) const; |
---|
144 | double findMinInRow(int nRow, const TMatrix &matrix, int exc = -1) const; |
---|
145 | void finishRoute(); |
---|
146 | bool hasSubCycles(int nRow, int nCol) const; |
---|
147 | void normalize(TMatrix &matrix) const; |
---|
148 | void subCol(TMatrix &matrix, int nCol, double val); |
---|
149 | void subRow(TMatrix &matrix, int nRow, double val); |
---|
150 | }; |
---|
151 | |
---|
152 | } |
---|
153 | |
---|
154 | #ifdef DEBUG |
---|
155 | QDebug operator<<(QDebug dbg, const TSPSolver::TMatrix &matrix); |
---|
156 | QDebug operator<<(QDebug dbg, const TSPSolver::SStep::SCandidate &candidate); |
---|
157 | #endif // DEBUG |
---|
158 | |
---|
159 | #endif // TSPSOLVER_H |
---|