source: tspsg/src/tspsolver.h @ 9cda6e0f5d

0.1.3.145-beta1-symbian0.1.4.170-beta2-bb10appveyorimgbotreadme
Last change on this file since 9cda6e0f5d was 9cda6e0f5d, checked in by Oleksii Serdiuk, 14 years ago

+ Added SStep::next that indicates what branch was selected for the next step.
+ Added "Show solution graph" option.
+ New CTSPSolver::getTotalSteps() method that returns a total number of steps in the current solution.

  • Moved SCandidate declaration into SStep declaration.
  • Moved everything in tspsolver.h and tspsolver.cpp into TSPSolver namespace.
  • Force CopyAction? on file drop or it will be deleted after dropping in Windows if MoveAction? was selected.
  • Property mode set to 100644
File size: 4.6 KB
RevLine 
[caef58b531]1/*!
[e0fcac5f2c]2 * \file tspsolver.h
[1757eb594b]3 * \author Copyright &copy; 2007-2010 Lёppa <contacts[at]oleksii[dot]name>
[67e53c96d7]4 *
5 *  $Id$
6 *  $URL$
7 *
[9cda6e0f5d]8 * \brief Defines TSPSolver namespace and everything needed for solving TSP tasks.
[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
[9cda6e0f5d]31#include <QtCore>
32#include <limits>
[67e53c96d7]33
[9cda6e0f5d]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()
[f0464480db]45
46/*!
[9cda6e0f5d]47 * \brief A TSP Solver namespace.
48 *
49 *  This namespace contains all the stuff required for solving TSP tasks.
[f0464480db]50 */
[9cda6e0f5d]51namespace TSPSolver {
[f0464480db]52
[9cda6e0f5d]53//! A matrix of city-to-city travel costs
54typedef QList<QList<double> > TMatrix;
[e664262f7d]55
[caef58b531]56/*!
57 * \brief This structure represents one step of solving.
58 *
59 *  A tree of such elements will represent the solving process.
60 */
[f0464480db]61struct SStep {
[9cda6e0f5d]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
[f0464480db]84        TMatrix matrix; //!< This step's matrix
[e4ae9e95f7]85        double price; //!< The price of travel to this step
[9cda6e0f5d]86
87        SCandidate candidate; //!< A candiadate for branching in the current step
[fcaa74f7d7]88        QList<SCandidate> alts; //!< A list of alternative branching candidates
[3e46075789]89        SStep *pNode; //!< Pointer to the parent step
[f0464480db]90        SStep *plNode; //!< Pointer to the left branch step
91        SStep *prNode; //!< Pointer to the right branch step
[9cda6e0f5d]92        NextStep next; //!< Indicates what branch was selected for the next step
[caef58b531]93
94        //! Assigns default values
[f0464480db]95        SStep() {
96                price = -1;
[3e46075789]97                pNode = plNode = prNode = NULL;
[9cda6e0f5d]98                next = NoNextStep;
[caef58b531]99        }
[67e53c96d7]100};
101
[e0fcac5f2c]102/*!
103 * \brief This class solves Travelling Salesman Problem task.
[1757eb594b]104 * \author Copyright &copy; 2007-2010 Lёppa <contacts[at]oleksii[dot]name>
[e0fcac5f2c]105 */
[394216e468]106class CTSPSolver: public QObject
[67e53c96d7]107{
[394216e468]108        Q_OBJECT
[430bd7f7e9]109
[67e53c96d7]110public:
[e0fcac5f2c]111        static QString getVersionId();
[394216e468]112
113        CTSPSolver(QObject *parent = NULL);
114        void cleanup(bool processEvents = false);
115        QString getSortedPath() const;
[9cda6e0f5d]116        int getTotalSteps() const;
[9cf98b9bd6]117        bool isOptimal() const;
[394216e468]118        SStep *solve(int numCities, const TMatrix &task);
119        bool wasCanceled() const;
[f0464480db]120        ~CTSPSolver();
[430bd7f7e9]121
[394216e468]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
[e664262f7d]132private:
[394216e468]133        bool mayNotBeOptimal, canceled;
[9cda6e0f5d]134        int nCities, total;
[f0464480db]135        SStep *root;
[430bd7f7e9]136        QHash<int,int> route;
[394216e468]137        mutable QMutex mutex;
[e0fcac5f2c]138
[e4ae9e95f7]139        double align(TMatrix &matrix);
[394216e468]140        void deleteTree(SStep *&root, bool processEvents = false);
141        void denormalize(TMatrix &matrix) const;
[9cda6e0f5d]142        QList<SStep::SCandidate> findCandidate(const TMatrix &matrix, int &nRow, int &nCol) const;
[e4ae9e95f7]143        double findMinInCol(int nCol, const TMatrix &matrix, int exr = -1) const;
144        double findMinInRow(int nRow, const TMatrix &matrix, int exc = -1) const;
[9cda6e0f5d]145        void finishRoute();
[0ac9690913]146        bool hasSubCycles(int nRow, int nCol) const;
[394216e468]147        void normalize(TMatrix &matrix) const;
[e4ae9e95f7]148        void subCol(TMatrix &matrix, int nCol, double val);
149        void subRow(TMatrix &matrix, int nRow, double val);
[67e53c96d7]150};
151
[9cda6e0f5d]152}
153
[e2abfd326f]154#ifdef DEBUG
[9cda6e0f5d]155QDebug operator<<(QDebug dbg, const TSPSolver::TMatrix &matrix);
156QDebug operator<<(QDebug dbg, const TSPSolver::SStep::SCandidate &candidate);
[394216e468]157#endif // DEBUG
[e2abfd326f]158
[67e53c96d7]159#endif // TSPSOLVER_H
Note: See TracBrowser for help on using the repository browser.