TSP Solver and Generator
tspsolver.h
Go to the documentation of this file.
1 
28 #ifndef TSPSOLVER_H
29 #define TSPSOLVER_H
30 
31 #include <limits>
32 
33 #include <QHash>
34 #include <QMutex>
35 #include <QObject>
36 #include <QStringList>
37 
44 #ifndef INFINITY
45 # define INFINITY std::numeric_limits<double>::infinity()
46 #endif
47 
53 namespace TSPSolver {
54 
57 
63 struct SStep {
65  struct SCandidate {
66  int nRow;
67  int nCol;
68 
71  nCol = nRow = -1;
72  }
74  bool operator ==(const SCandidate &cand) const {
75  return ((cand.nRow == nRow) && (cand.nCol == nCol));
76  }
77  };
78 
80  enum NextStep {
84  };
85 
86  TMatrix matrix;
87  double price;
88 
95 
97  SStep() {
98  price = -1;
99  pNode = plNode = prNode = NULL;
100  next = NoNextStep;
101  }
102 };
103 
108 class CTSPSolver: public QObject
109 {
110  Q_OBJECT
111 
112 public:
113  static QString getVersionId();
114 
115  CTSPSolver(QObject *parent = NULL);
116  void cleanup(bool processEvents = false);
117  QString getSortedPath(const QString &city, const QString &separator = QString(" -> ")) const;
118  int getTotalSteps() const;
119  bool isOptimal() const;
120  void setCleanupOnCancel(bool enable = true);
121  SStep *solve(int numCities, const TMatrix &task);
122  bool wasCanceled() const;
123  ~CTSPSolver();
124 
125 public slots:
126  void cancel();
127 
128 signals:
133  void routePartFound(int n);
134 
135 private:
136  bool mayNotBeOptimal, canceled, cc;
137  int nCities, total;
138  SStep *root;
139  QHash<int,int> route;
140  mutable QMutex mutex;
141 
142  double align(TMatrix &matrix);
143  void deleteTree(SStep *&root, bool processEvents = false);
144  void denormalize(TMatrix &matrix) const;
145  QList<SStep::SCandidate> findCandidate(const TMatrix &matrix, int &nRow, int &nCol) const;
146  double findMinInCol(int nCol, const TMatrix &matrix, int exr = -1) const;
147  double findMinInRow(int nRow, const TMatrix &matrix, int exc = -1) const;
148  void finishRoute();
149  bool hasSubCycles(int nRow, int nCol) const;
150  void normalize(TMatrix &matrix) const;
151  void subCol(TMatrix &matrix, int nCol, double val);
152  void subRow(TMatrix &matrix, int nRow, double val);
153 };
154 
155 }
156 
157 #ifdef DEBUG
158 QDebug operator<<(QDebug dbg, const TSPSolver::TMatrix &matrix);
159 QDebug operator<<(QDebug dbg, const TSPSolver::SStep::SCandidate &candidate);
160 #endif // DEBUG
161 
162 #endif // TSPSOLVER_H
A TSP Solver namespace.
Definition: tspsolver.cpp:35
void routePartFound(int n)
This signal is emitted once every time a part of the route is found.
int getTotalSteps() const
Returns a total number of steps in the current solution.
Definition: tspsolver.cpp:100
A structure that represents a candidate for branching.
Definition: tspsolver.h:65
Left branch was selected for the next step.
Definition: tspsolver.h:82
SStep * plNode
Pointer to the left branch step.
Definition: tspsolver.h:92
SCandidate()
Assigns default values.
Definition: tspsolver.h:70
This class solves Travelling Salesman Problem task.
Definition: tspsolver.h:108
SStep * solve(int numCities, const TMatrix &task)
Solves the given task.
Definition: tspsolver.cpp:140
SStep()
Assigns default values.
Definition: tspsolver.h:97
int nRow
A zero-based row number of the candidate.
Definition: tspsolver.h:66
bool isOptimal() const
Indicates whether or not the solution is definitely optimal.
Definition: tspsolver.cpp:112
CTSPSolver(QObject *parent=NULL)
Constructs CTSPSolver object.
Definition: tspsolver.cpp:50
This structure represents one step of solving.
Definition: tspsolver.h:63
void cleanup(bool processEvents=false)
Cleans up the object and frees up memory used by the solution tree.
Definition: tspsolver.cpp:61
double price
The price of travel to this step.
Definition: tspsolver.h:87
SStep * pNode
Pointer to the parent step.
Definition: tspsolver.h:91
QList< QList< double > > TMatrix
A matrix of city-to-city travel costs.
Definition: tspsolver.h:56
int nCol
A zero-based column number of the candidate.
Definition: tspsolver.h:67
static QString getVersionId()
Returns CTSPSolver's version ID.
Definition: tspsolver.cpp:41
Right branch was selected for the next step.
Definition: tspsolver.h:83
bool wasCanceled() const
Indicates whether or not the solution process was canceled.
Definition: tspsolver.cpp:257
QList< SCandidate > alts
A list of alternative branching candidates.
Definition: tspsolver.h:90
SStep * prNode
Pointer to the right branch step.
Definition: tspsolver.h:93
NextStep
An enum that describes possible selection of the next step.
Definition: tspsolver.h:80
bool operator==(const SCandidate &cand) const
An operator == implementation.
Definition: tspsolver.h:74
No next step (end of solution)
Definition: tspsolver.h:81
void cancel()
Cancels the solution process.
Definition: tspsolver.cpp:266
SCandidate candidate
A candiadate for branching in the current step.
Definition: tspsolver.h:89
QObject * parent() const
void setCleanupOnCancel(bool enable=true)
Sets whether or not to call cleanup() on solution cancel.
Definition: tspsolver.cpp:127
TMatrix matrix
This step's matrix.
Definition: tspsolver.h:86
NextStep next
Indicates what branch was selected for the next step.
Definition: tspsolver.h:94
QString getSortedPath(const QString &city, const QString &separator=QString(" -> ")) const
Returns the sorted optimal path, starting from City 1.
Definition: tspsolver.cpp:78