1 | /* |
---|
2 | * TSPSG: TSP Solver and Generator |
---|
3 | * Copyright (C) 2007-2010 Lёppa <contacts[at]oleksii[dot]name> |
---|
4 | * |
---|
5 | * $Id$ |
---|
6 | * $URL$ |
---|
7 | * |
---|
8 | * This file is part of TSPSG. |
---|
9 | * |
---|
10 | * TSPSG is free software: you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation, either version 3 of the License, or |
---|
13 | * (at your option) any later version. |
---|
14 | * |
---|
15 | * TSPSG is distributed in the hope that it will be useful, |
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | * GNU General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU General Public License |
---|
21 | * along with TSPSG. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "tspsolver.h" |
---|
25 | |
---|
26 | //! \internal \brief A short for maximum double, used internally in the solution algorithm. |
---|
27 | #define MAX_DOUBLE std::numeric_limits<double>::max() |
---|
28 | |
---|
29 | namespace TSPSolver { |
---|
30 | |
---|
31 | /*! |
---|
32 | * \brief Returns CTSPSolver's version ID. |
---|
33 | * \return A string: <b>\$Id$</b>. |
---|
34 | */ |
---|
35 | QString CTSPSolver::getVersionId() |
---|
36 | { |
---|
37 | return QString("$Id$"); |
---|
38 | } |
---|
39 | |
---|
40 | /*! |
---|
41 | * \brief Constructs CTSPSolver object. |
---|
42 | * \param parent A parent object. |
---|
43 | */ |
---|
44 | CTSPSolver::CTSPSolver(QObject *parent) |
---|
45 | : QObject(parent), nCities(0), total(0), root(NULL) {} |
---|
46 | |
---|
47 | /*! |
---|
48 | * \brief Cleans up the object and frees up memory used by the solution tree. |
---|
49 | * \param processEvents If set to \c true then \link QCoreApplication::processEvents() QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents)\endlink will be called from time to time while cleaning up. |
---|
50 | * \warning After call to this function a solution tree returned by the solve() function is no longer valid. |
---|
51 | * \note It is not required to call this function manually. This function is always called by solve() at the beginning of the solution process. |
---|
52 | * |
---|
53 | * \sa solve() |
---|
54 | */ |
---|
55 | void CTSPSolver::cleanup(bool processEvents) |
---|
56 | { |
---|
57 | #ifdef QAPPLICATION_H |
---|
58 | if (!processEvents) |
---|
59 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
60 | #endif |
---|
61 | route.clear(); |
---|
62 | mayNotBeOptimal = false; |
---|
63 | if (root != NULL) |
---|
64 | deleteTree(root, processEvents); |
---|
65 | #ifdef QAPPLICATION_H |
---|
66 | if (!processEvents) |
---|
67 | QApplication::restoreOverrideCursor(); |
---|
68 | #endif |
---|
69 | } |
---|
70 | |
---|
71 | /*! |
---|
72 | * \brief Returns the sorted optimal path, starting from City 1. |
---|
73 | * \param city A string that represents city elements in the path. |
---|
74 | * \param separator A string that represents separators between cities in the path. |
---|
75 | * \return A string, containing sorted optimal path. |
---|
76 | * |
---|
77 | * The resulting path will be in the form \a city+\a separator+\a city+...+\a separator+\a city. |
---|
78 | * \c \%1 in \a city will be replaced by the city number. |
---|
79 | */ |
---|
80 | QString CTSPSolver::getSortedPath(const QString &city, const QString &separator) const |
---|
81 | { |
---|
82 | if (!root || route.isEmpty() || (route.size() != nCities)) |
---|
83 | return QString(); |
---|
84 | |
---|
85 | int i = 0; // We start from City 1 |
---|
86 | QStringList path; |
---|
87 | path << city.arg(1); |
---|
88 | while ((i = route[i]) != 0) { |
---|
89 | path << city.arg(i + 1); |
---|
90 | } |
---|
91 | // And finish in City 1, too |
---|
92 | path << city.arg(1); |
---|
93 | |
---|
94 | return path.join(separator); |
---|
95 | } |
---|
96 | |
---|
97 | /*! |
---|
98 | * \brief Returns a total number of steps in the current solution. |
---|
99 | * \return A total number of steps or \c 0 if no solution. |
---|
100 | * \note This is not always the same as the number of cities. |
---|
101 | */ |
---|
102 | int CTSPSolver::getTotalSteps() const |
---|
103 | { |
---|
104 | return total; |
---|
105 | } |
---|
106 | |
---|
107 | /*! |
---|
108 | * \brief Indicates whether or not the solution is definitely optimal. |
---|
109 | * \return \c true if the solution is definitely optimal, otherwise \c false. |
---|
110 | * |
---|
111 | * The solution may need some further iterations to determine whether or not it is optimal. |
---|
112 | * In such cases this function returns \c false. |
---|
113 | */ |
---|
114 | bool CTSPSolver::isOptimal() const |
---|
115 | { |
---|
116 | return !mayNotBeOptimal; |
---|
117 | } |
---|
118 | |
---|
119 | /*! |
---|
120 | * \brief Solves the given task. |
---|
121 | * \param numCities Number of cities in the task. |
---|
122 | * \param task The matrix of city-to-city travel costs. |
---|
123 | * \return Pointer to the root of the solution tree. |
---|
124 | * |
---|
125 | * \todo TODO: Comment the algorithm. |
---|
126 | */ |
---|
127 | SStep *CTSPSolver::solve(int numCities, const TMatrix &task) |
---|
128 | { |
---|
129 | if (numCities < 3) |
---|
130 | return NULL; |
---|
131 | |
---|
132 | QMutexLocker locker(&mutex); |
---|
133 | cleanup(); |
---|
134 | canceled = false; |
---|
135 | locker.unlock(); |
---|
136 | |
---|
137 | nCities = numCities; |
---|
138 | |
---|
139 | SStep *step = new SStep(); |
---|
140 | step->matrix = task; |
---|
141 | // We need to distinguish the values forbidden by the user |
---|
142 | // from the values forbidden by the algorithm. |
---|
143 | // So we replace user's infinities by the maximum available double value. |
---|
144 | normalize(step->matrix); |
---|
145 | #ifdef DEBUG |
---|
146 | qDebug() << step->matrix; |
---|
147 | #endif // DEBUG |
---|
148 | step->price = align(step->matrix); |
---|
149 | root = step; |
---|
150 | |
---|
151 | SStep *left, *right; |
---|
152 | int nRow, nCol; |
---|
153 | bool firstStep = true; |
---|
154 | double check = INFINITY; |
---|
155 | total = 0; |
---|
156 | while (route.size() < nCities) { |
---|
157 | step->alts = findCandidate(step->matrix,nRow,nCol); |
---|
158 | |
---|
159 | while (hasSubCycles(nRow,nCol)) { |
---|
160 | #ifdef DEBUG |
---|
161 | qDebug() << "Forbidden: (" << nRow << ";" << nCol << ")"; |
---|
162 | #endif // DEBUG |
---|
163 | step->matrix[nRow][nCol] = INFINITY; |
---|
164 | step->price += align(step->matrix); |
---|
165 | step->alts = findCandidate(step->matrix,nRow,nCol); |
---|
166 | } |
---|
167 | |
---|
168 | #ifdef DEBUG |
---|
169 | qDebug() /*<< step->matrix*/ << "Selected: (" << nRow << ";" << nCol << ")"; |
---|
170 | qDebug() << "Alternate:" << step->alts; |
---|
171 | qDebug() << "Step price:" << step->price << endl; |
---|
172 | #endif // DEBUG |
---|
173 | |
---|
174 | locker.relock(); |
---|
175 | if ((nRow == -1) || (nCol == -1) || canceled) { |
---|
176 | cleanup(); |
---|
177 | return NULL; |
---|
178 | } |
---|
179 | locker.unlock(); |
---|
180 | |
---|
181 | // Route with (nRow,nCol) path |
---|
182 | right = new SStep(); |
---|
183 | right->pNode = step; |
---|
184 | right->matrix = step->matrix; |
---|
185 | for (int k = 0; k < nCities; k++) { |
---|
186 | if (k != nCol) |
---|
187 | right->matrix[nRow][k] = INFINITY; |
---|
188 | if (k != nRow) |
---|
189 | right->matrix[k][nCol] = INFINITY; |
---|
190 | } |
---|
191 | right->price = step->price + align(right->matrix); |
---|
192 | // Forbid the selected route to exclude its reuse in next steps. |
---|
193 | right->matrix[nCol][nRow] = INFINITY; |
---|
194 | right->matrix[nRow][nCol] = INFINITY; |
---|
195 | |
---|
196 | // Route without (nRow,nCol) path |
---|
197 | left = new SStep(); |
---|
198 | left->pNode = step; |
---|
199 | left->matrix = step->matrix; |
---|
200 | left->matrix[nRow][nCol] = INFINITY; |
---|
201 | left->price = step->price + align(left->matrix); |
---|
202 | |
---|
203 | step->candidate.nRow = nRow; |
---|
204 | step->candidate.nCol = nCol; |
---|
205 | step->plNode = left; |
---|
206 | step->prNode = right; |
---|
207 | |
---|
208 | // This matrix is not used anymore. Restoring infinities back. |
---|
209 | denormalize(step->matrix); |
---|
210 | |
---|
211 | if (right->price <= left->price) { |
---|
212 | // Route with (nRow,nCol) path is cheaper |
---|
213 | step->next = SStep::RightBranch; |
---|
214 | step = right; |
---|
215 | route[nRow] = nCol; |
---|
216 | emit routePartFound(route.size()); |
---|
217 | if (firstStep) { |
---|
218 | check = left->price; |
---|
219 | firstStep = false; |
---|
220 | } |
---|
221 | } else { |
---|
222 | // Route without (nRow,nCol) path is cheaper |
---|
223 | step->next = SStep::LeftBranch; |
---|
224 | step = left; |
---|
225 | QCoreApplication::processEvents(); |
---|
226 | if (firstStep) { |
---|
227 | check = right->price; |
---|
228 | firstStep = false; |
---|
229 | } |
---|
230 | } |
---|
231 | total++; |
---|
232 | } |
---|
233 | |
---|
234 | mayNotBeOptimal = (check < step->price); |
---|
235 | |
---|
236 | return root; |
---|
237 | } |
---|
238 | |
---|
239 | /*! |
---|
240 | * \brief Indicates whether or not the solution process was canceled. |
---|
241 | * \return \c true if the solution process was canceled, otherwise \c false. |
---|
242 | */ |
---|
243 | bool CTSPSolver::wasCanceled() const |
---|
244 | { |
---|
245 | QMutexLocker locker(&mutex); |
---|
246 | return canceled; |
---|
247 | } |
---|
248 | |
---|
249 | /*! |
---|
250 | * \brief Cancels the solution process. |
---|
251 | */ |
---|
252 | void CTSPSolver::cancel() |
---|
253 | { |
---|
254 | QMutexLocker locker(&mutex); |
---|
255 | canceled = true; |
---|
256 | } |
---|
257 | |
---|
258 | CTSPSolver::~CTSPSolver() |
---|
259 | { |
---|
260 | if (root != NULL) |
---|
261 | deleteTree(root); |
---|
262 | } |
---|
263 | |
---|
264 | /* Privates **********************************************************/ |
---|
265 | |
---|
266 | double CTSPSolver::align(TMatrix &matrix) |
---|
267 | { |
---|
268 | double r = 0; |
---|
269 | double min; |
---|
270 | for (int k = 0; k < nCities; k++) { |
---|
271 | min = findMinInRow(k,matrix); |
---|
272 | if (min > 0) { |
---|
273 | r += min; |
---|
274 | if (min < MAX_DOUBLE) |
---|
275 | subRow(matrix,k,min); |
---|
276 | } |
---|
277 | } |
---|
278 | for (int k = 0; k < nCities; k++) { |
---|
279 | min = findMinInCol(k,matrix); |
---|
280 | if (min > 0) { |
---|
281 | r += min; |
---|
282 | if (min < MAX_DOUBLE) |
---|
283 | subCol(matrix,k,min); |
---|
284 | } |
---|
285 | } |
---|
286 | return (r != MAX_DOUBLE) ? r : INFINITY; |
---|
287 | } |
---|
288 | |
---|
289 | void CTSPSolver::deleteTree(SStep *&root, bool processEvents) |
---|
290 | { |
---|
291 | if (root == NULL) |
---|
292 | return; |
---|
293 | SStep *step = root; |
---|
294 | SStep *parent; |
---|
295 | forever { |
---|
296 | if (processEvents) |
---|
297 | QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); |
---|
298 | if (step->plNode != NULL) { |
---|
299 | // We have left child node - going inside it |
---|
300 | step = step->plNode; |
---|
301 | step->pNode->plNode = NULL; |
---|
302 | continue; |
---|
303 | } else if (step->prNode != NULL) { |
---|
304 | // We have right child node - going inside it |
---|
305 | step = step->prNode; |
---|
306 | step->pNode->prNode = NULL; |
---|
307 | continue; |
---|
308 | } else { |
---|
309 | // We have no child nodes. Deleting the current one. |
---|
310 | parent = step->pNode; |
---|
311 | delete step; |
---|
312 | if (parent != NULL) { |
---|
313 | // Going back to the parent node. |
---|
314 | step = parent; |
---|
315 | } else { |
---|
316 | // We came back to the root node. Finishing. |
---|
317 | root = NULL; |
---|
318 | break; |
---|
319 | } |
---|
320 | } |
---|
321 | } |
---|
322 | } |
---|
323 | |
---|
324 | void CTSPSolver::denormalize(TMatrix &matrix) const |
---|
325 | { |
---|
326 | for (int r = 0; r < nCities; r++) |
---|
327 | for (int c = 0; c < nCities; c++) |
---|
328 | if ((r != c) && (matrix.at(r).at(c) == MAX_DOUBLE)) |
---|
329 | matrix[r][c] = INFINITY; |
---|
330 | } |
---|
331 | |
---|
332 | QList<SStep::SCandidate> CTSPSolver::findCandidate(const TMatrix &matrix, int &nRow, int &nCol) const |
---|
333 | { |
---|
334 | nRow = -1; |
---|
335 | nCol = -1; |
---|
336 | QList<SStep::SCandidate> alts; |
---|
337 | SStep::SCandidate cand; |
---|
338 | double h = -1; |
---|
339 | double sum; |
---|
340 | for (int r = 0; r < nCities; r++) |
---|
341 | for (int c = 0; c < nCities; c++) |
---|
342 | if (matrix.at(r).at(c) == 0) { |
---|
343 | sum = findMinInRow(r,matrix,c) + findMinInCol(c,matrix,r); |
---|
344 | if (sum > h) { |
---|
345 | h = sum; |
---|
346 | nRow = r; |
---|
347 | nCol = c; |
---|
348 | alts.clear(); |
---|
349 | } else if ((sum == h) && !hasSubCycles(r,c)) { |
---|
350 | cand.nRow = r; |
---|
351 | cand.nCol = c; |
---|
352 | alts.append(cand); |
---|
353 | } |
---|
354 | } |
---|
355 | return alts; |
---|
356 | } |
---|
357 | |
---|
358 | double CTSPSolver::findMinInCol(int nCol, const TMatrix &matrix, int exr) const |
---|
359 | { |
---|
360 | double min = INFINITY; |
---|
361 | for (int k = 0; k < nCities; k++) |
---|
362 | if ((k != exr) && (min > matrix.at(k).at(nCol))) |
---|
363 | min = matrix.at(k).at(nCol); |
---|
364 | return (min == INFINITY) ? 0 : min; |
---|
365 | } |
---|
366 | |
---|
367 | double CTSPSolver::findMinInRow(int nRow, const TMatrix &matrix, int exc) const |
---|
368 | { |
---|
369 | double min = INFINITY; |
---|
370 | for (int k = 0; k < nCities; k++) { |
---|
371 | if (((k != exc)) && (min > matrix.at(nRow).at(k))) |
---|
372 | min = matrix.at(nRow).at(k); |
---|
373 | } |
---|
374 | return (min == INFINITY) ? 0 : min; |
---|
375 | } |
---|
376 | |
---|
377 | bool CTSPSolver::hasSubCycles(int nRow, int nCol) const |
---|
378 | { |
---|
379 | if ((nRow < 0) || (nCol < 0) || route.isEmpty() || !(route.size() < nCities - 1) || !route.contains(nCol)) |
---|
380 | return false; |
---|
381 | int i = nCol; |
---|
382 | forever { |
---|
383 | if ((i = route.value(i)) == nRow) |
---|
384 | return true; |
---|
385 | if (!route.contains(i)) |
---|
386 | return false; |
---|
387 | } |
---|
388 | return false; |
---|
389 | } |
---|
390 | |
---|
391 | void CTSPSolver::normalize(TMatrix &matrix) const |
---|
392 | { |
---|
393 | for (int r = 0; r < nCities; r++) |
---|
394 | for (int c = 0; c < nCities; c++) |
---|
395 | if ((r != c) && (matrix.at(r).at(c) == INFINITY)) |
---|
396 | matrix[r][c] = MAX_DOUBLE; |
---|
397 | } |
---|
398 | |
---|
399 | void CTSPSolver::subCol(TMatrix &matrix, int nCol, double val) |
---|
400 | { |
---|
401 | for (int k = 0; k < nCities; k++) |
---|
402 | if (k != nCol) |
---|
403 | matrix[k][nCol] -= val; |
---|
404 | } |
---|
405 | |
---|
406 | void CTSPSolver::subRow(TMatrix &matrix, int nRow, double val) |
---|
407 | { |
---|
408 | for (int k = 0; k < nCities; k++) |
---|
409 | if (k != nRow) |
---|
410 | matrix[nRow][k] -= val; |
---|
411 | } |
---|
412 | |
---|
413 | } |
---|
414 | |
---|
415 | #ifdef DEBUG |
---|
416 | QDebug operator<<(QDebug dbg, const TSPSolver::TMatrix &matrix) |
---|
417 | { |
---|
418 | for (int r = 0; r < matrix.count(); r++) { |
---|
419 | for (int c = 0; c < matrix.at(r).count(); c++) |
---|
420 | dbg.space() << QString::number(matrix.at(r).at(c)).leftJustified(5); |
---|
421 | dbg << endl; |
---|
422 | } |
---|
423 | return dbg; |
---|
424 | } |
---|
425 | |
---|
426 | QDebug operator<<(QDebug dbg, const TSPSolver::SStep::SCandidate &cand) |
---|
427 | { |
---|
428 | dbg.nospace() << "(" << cand.nRow << ";" << cand.nCol << ")"; |
---|
429 | return dbg; |
---|
430 | } |
---|
431 | #endif // DEBUG |
---|