source: tspsg/src/tspsolver.h @ 394216e468

0.1.3.145-beta1-symbian0.1.4.170-beta2-bb10appveyorimgbotreadme
Last change on this file since 394216e468 was 394216e468, checked in by Oleksii Serdiuk, 14 years ago
  • Fixed a bug when a solution couldn't be found for some tasks while the task had at least one solution (mostly, tasks with a lot of restrictions).
  • Fixed a bug when Save As dialog always appeared (even for non-Untitled files) when selecting Save in Unsaved Changes dialog.
  • Improved the solution algorithm.
  • Moved progress dialog from CTSPSolver to MainWindow?. CTSPSolver doesn't contain any GUI related code now.

+ Added routePartFound() signal to CTSPSolver which is emitted once every time a part of the route is found.
+ Added cancel() slot and wasCanceled() public function to CTSPSolver to be able to cancel a solution process and to know whether it was canceled.
+ Progress is now shown when generating a solution output.
+ Check for updates functionality (only in Windows version at this moment).

  • Property mode set to 100644
File size: 3.7 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 *
[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.
[e4ae9e95f7]36typedef QList<QList<double> > TMatrix;
[f0464480db]37
38/*!
39 * \brief A structure that represents a candidate for branching.
40 */
[fcaa74f7d7]41struct 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]60struct SStep {
61        TMatrix matrix; //!< This step's matrix
[e4ae9e95f7]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
[3e46075789]65        SStep *pNode; //!< Pointer to the parent step
[f0464480db]66        SStep *plNode; //!< Pointer to the left branch step
67        SStep *prNode; //!< Pointer to the right branch step
[caef58b531]68
69        //! Assigns default values
[f0464480db]70        SStep() {
71                price = -1;
[3e46075789]72                pNode = plNode = prNode = NULL;
[caef58b531]73        }
[67e53c96d7]74};
75
[e0fcac5f2c]76/*!
77 * \brief This class solves Travelling Salesman Problem task.
[1757eb594b]78 * \author Copyright &copy; 2007-2010 Lёppa <contacts[at]oleksii[dot]name>
[e0fcac5f2c]79 */
[394216e468]80class CTSPSolver: public QObject
[67e53c96d7]81{
[394216e468]82        Q_OBJECT
[430bd7f7e9]83
[67e53c96d7]84public:
[e0fcac5f2c]85        static QString getVersionId();
[394216e468]86
87        CTSPSolver(QObject *parent = NULL);
88        void cleanup(bool processEvents = false);
89        QString getSortedPath() const;
[9cf98b9bd6]90        bool isOptimal() const;
[394216e468]91        SStep *solve(int numCities, const TMatrix &task);
92        bool wasCanceled() const;
[f0464480db]93        ~CTSPSolver();
[430bd7f7e9]94
[394216e468]95public slots:
96        void cancel();
97
98signals:
99        /*!
100         * \brief This signal is emitted once every time a part of the route is found.
101         * \param n Indicates the number of the route parts found.
102         */
103        void routePartFound(int n);
104
[e664262f7d]105private:
[394216e468]106        bool mayNotBeOptimal, canceled;
[e664262f7d]107        int nCities;
[f0464480db]108        SStep *root;
[430bd7f7e9]109        QHash<int,int> route;
[394216e468]110        mutable QMutex mutex;
[e0fcac5f2c]111
[e4ae9e95f7]112        double align(TMatrix &matrix);
[394216e468]113        void deleteTree(SStep *&root, bool processEvents = false);
114        void denormalize(TMatrix &matrix) const;
[fcaa74f7d7]115        QList<SCandidate> findCandidate(const TMatrix &matrix, int &nRow, int &nCol) const;
[e4ae9e95f7]116        double findMinInCol(int nCol, const TMatrix &matrix, int exr = -1) const;
117        double findMinInRow(int nRow, const TMatrix &matrix, int exc = -1) const;
[0ac9690913]118        bool hasSubCycles(int nRow, int nCol) const;
[394216e468]119        void normalize(TMatrix &matrix) const;
[e4ae9e95f7]120        void subCol(TMatrix &matrix, int nCol, double val);
121        void subRow(TMatrix &matrix, int nRow, double val);
[67e53c96d7]122};
123
[e2abfd326f]124#ifdef DEBUG
125QDebug operator<<(QDebug dbg, const TMatrix &matrix);
[394216e468]126QDebug operator<<(QDebug dbg, const SCandidate &candidate);
127#endif // DEBUG
[e2abfd326f]128
[67e53c96d7]129#endif // TSPSOLVER_H
Note: See TracBrowser for help on using the repository browser.