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