source: tspsg-svn/trunk/src/tspsolver.h @ 115

Last change on this file since 115 was 110, checked in by laleppa, 15 years ago

+ Added ChangeLog?, Installation Guide and License pages to doxygen generated documentation.

  • Added city and separator parameters to CTSPSolver::getSortedPath() method to make path generation more flexible.
  • Fixed a bug when the solution graph wasn't drwan correctly in some situations.
  • Property svn:keywords set to Id URL
File size: 4.7 KB
RevLine 
[65]1/*!
[67]2 * \file tspsolver.h
[87]3 * \author Copyright &copy; 2007-2010 Lёppa <contacts[at]oleksii[dot]name>
[12]4 *
5 *  $Id: tspsolver.h 110 2010-04-27 23:38:10Z laleppa $
6 *  $URL: https://tspsg.svn.sourceforge.net/svnroot/tspsg/trunk/src/tspsolver.h $
7 *
[107]8 * \brief Defines TSPSolver namespace and everything needed for solving TSP tasks.
[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
[107]31#include <QtCore>
32#include <limits>
[12]33
[107]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()
[64]45
[65]46/*!
[107]47 * \brief A TSP Solver namespace.
48 *
49 *  This namespace contains all the stuff required for solving TSP tasks.
[74]50 */
[107]51namespace TSPSolver {
[74]52
[107]53//! A matrix of city-to-city travel costs
54typedef QList<QList<double> > TMatrix;
[74]55
56/*!
[65]57 * \brief This structure represents one step of solving.
58 *
59 *  A tree of such elements will represent the solving process.
60 */
[74]61struct SStep {
[107]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
[74]84        TMatrix matrix; //!< This step's matrix
[89]85        double price; //!< The price of travel to this step
[107]86
87        SCandidate candidate; //!< A candiadate for branching in the current step
[76]88        QList<SCandidate> alts; //!< A list of alternative branching candidates
[90]89        SStep *pNode; //!< Pointer to the parent step
[74]90        SStep *plNode; //!< Pointer to the left branch step
91        SStep *prNode; //!< Pointer to the right branch step
[107]92        NextStep next; //!< Indicates what branch was selected for the next step
[65]93
94        //! Assigns default values
[74]95        SStep() {
96                price = -1;
[90]97                pNode = plNode = prNode = NULL;
[107]98                next = NoNextStep;
[65]99        }
[12]100};
101
[67]102/*!
103 * \brief This class solves Travelling Salesman Problem task.
[87]104 * \author Copyright &copy; 2007-2010 Lёppa <contacts[at]oleksii[dot]name>
[67]105 */
[99]106class CTSPSolver: public QObject
[12]107{
[99]108        Q_OBJECT
[42]109
[12]110public:
[99]111        static QString getVersionId();
112
113        CTSPSolver(QObject *parent = NULL);
114        void cleanup(bool processEvents = false);
[110]115        QString getSortedPath(const QString &city, const QString &separator = " -> ") const;
[107]116        int getTotalSteps() const;
[60]117        bool isOptimal() const;
[99]118        SStep *solve(int numCities, const TMatrix &task);
119        bool wasCanceled() const;
[74]120        ~CTSPSolver();
[42]121
[99]122public slots:
123        void cancel();
124
125signals:
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
[13]132private:
[99]133        bool mayNotBeOptimal, canceled;
[107]134        int nCities, total;
[74]135        SStep *root;
[42]136        QHash<int,int> route;
[99]137        mutable QMutex mutex;
[67]138
[89]139        double align(TMatrix &matrix);
[99]140        void deleteTree(SStep *&root, bool processEvents = false);
141        void denormalize(TMatrix &matrix) const;
[107]142        QList<SStep::SCandidate> findCandidate(const TMatrix &matrix, int &nRow, int &nCol) const;
[89]143        double findMinInCol(int nCol, const TMatrix &matrix, int exr = -1) const;
144        double findMinInRow(int nRow, const TMatrix &matrix, int exc = -1) const;
[107]145        void finishRoute();
[71]146        bool hasSubCycles(int nRow, int nCol) const;
[99]147        void normalize(TMatrix &matrix) const;
[89]148        void subCol(TMatrix &matrix, int nCol, double val);
149        void subRow(TMatrix &matrix, int nRow, double val);
[12]150};
151
[107]152}
153
[98]154#ifdef DEBUG
[107]155QDebug operator<<(QDebug dbg, const TSPSolver::TMatrix &matrix);
156QDebug operator<<(QDebug dbg, const TSPSolver::SStep::SCandidate &candidate);
[99]157#endif // DEBUG
[98]158
[12]159#endif // TSPSOLVER_H
Note: See TracBrowser for help on using the repository browser.