Changeset 9cda6e0f5d in tspsg for src/tspsolver.cpp
- Timestamp:
- Apr 25, 2010, 4:36:27 PM (15 years ago)
- Branches:
- 0.1.3.145-beta1-symbian, 0.1.4.170-beta2-bb10, appveyor, imgbot, master, readme
- Children:
- ca3d2a30fa
- Parents:
- 345e7b6132
- git-author:
- Oleksii Serdiuk <contacts@…> (04/25/10 16:36:27)
- git-committer:
- Oleksii Serdiuk <contacts@…> (06/29/12 19:41:31)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/tspsolver.cpp
r345e7b6132 r9cda6e0f5d 27 27 #define MAX_DOUBLE std::numeric_limits<double>::max() 28 28 29 namespace TSPSolver { 30 29 31 /*! 30 32 * \brief Returns CTSPSolver's version ID. … … 41 43 */ 42 44 CTSPSolver::CTSPSolver(QObject *parent) 43 : QObject(parent), nCities(0), root(NULL) {}45 : QObject(parent), nCities(0), total(0), root(NULL) {} 44 46 45 47 /*! 46 48 * \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() Q Application::processEvents(QEventLoop::ExcludeUserInputEvents)\endlink will be called from time to time while cleaning up.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. 48 50 * \warning After call to this function a solution tree returned by the solve() function is no longer valid. 49 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. … … 53 55 void CTSPSolver::cleanup(bool processEvents) 54 56 { 57 #ifdef QAPPLICATION_H 55 58 if (!processEvents) 56 59 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); 60 #endif 57 61 route.clear(); 58 62 mayNotBeOptimal = false; 59 63 if (root != NULL) 60 64 deleteTree(root, processEvents); 65 #ifdef QAPPLICATION_H 61 66 if (!processEvents) 62 67 QApplication::restoreOverrideCursor(); 68 #endif 63 69 } 64 70 … … 84 90 85 91 /*! 92 * \brief Returns a total number of steps in the current solution. 93 * \return A total number of steps or \c 0 if no solution. 94 * \note This is not always the same as the number of cities. 95 */ 96 int CTSPSolver::getTotalSteps() const 97 { 98 return total; 99 } 100 101 /*! 86 102 * \brief Indicates whether or not the solution is definitely optimal. 87 103 * \return \c true if the solution is definitely optimal, otherwise \c false. … … 105 121 SStep *CTSPSolver::solve(int numCities, const TMatrix &task) 106 122 { 107 if (numCities < = 1)123 if (numCities < 3) 108 124 return NULL; 109 125 … … 131 147 bool firstStep = true; 132 148 double check = INFINITY; 133 while (this->route.size() < nCities) { 149 total = 0; 150 while (route.size() < nCities) { 134 151 step->alts = findCandidate(step->matrix,nRow,nCol); 135 152 … … 188 205 if (right->price <= left->price) { 189 206 // Route with (nRow,nCol) path is cheaper 207 step->next = SStep::RightBranch; 190 208 step = right; 191 this->route[nRow] = nCol;192 emit routePartFound( this->route.size());209 route[nRow] = nCol; 210 emit routePartFound(route.size()); 193 211 if (firstStep) { 194 212 check = left->price; … … 197 215 } else { 198 216 // Route without (nRow,nCol) path is cheaper 217 step->next = SStep::LeftBranch; 199 218 step = left; 200 qApp->processEvents();219 QCoreApplication::processEvents(); 201 220 if (firstStep) { 202 221 check = right->price; … … 204 223 } 205 224 } 206 }207 208 route = this->route; 225 total++; 226 } 227 209 228 mayNotBeOptimal = (check < step->price); 210 229 … … 259 278 } 260 279 } 261 return r;280 return (r != MAX_DOUBLE) ? r : INFINITY; 262 281 } 263 282 … … 270 289 forever { 271 290 if (processEvents) 272 Q Application::processEvents(QEventLoop::ExcludeUserInputEvents);291 QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); 273 292 if (step->plNode != NULL) { 274 293 // We have left child node - going inside it … … 282 301 continue; 283 302 } else { 284 // We have no child nodes. Deleting current one.303 // We have no child nodes. Deleting the current one. 285 304 parent = step->pNode; 286 305 delete step; … … 305 324 } 306 325 307 QList<S Candidate> CTSPSolver::findCandidate(const TMatrix &matrix, int &nRow, int &nCol) const326 QList<SStep::SCandidate> CTSPSolver::findCandidate(const TMatrix &matrix, int &nRow, int &nCol) const 308 327 { 309 328 nRow = -1; 310 329 nCol = -1; 311 QList<S Candidate> alts;312 S Candidate cand;330 QList<SStep::SCandidate> alts; 331 SStep::SCandidate cand; 313 332 double h = -1; 314 333 double sum; 315 334 for (int r = 0; r < nCities; r++) 316 335 for (int c = 0; c < nCities; c++) 317 // if ((matrix.at(r).at(c) == 0) && !forbidden.values(r).contains(c)) {318 336 if (matrix.at(r).at(c) == 0) { 319 337 sum = findMinInRow(r,matrix,c) + findMinInCol(c,matrix,r); … … 356 374 return false; 357 375 int i = nCol; 358 while (true){359 if ((i = route [i]) == nRow)376 forever { 377 if ((i = route.value(i)) == nRow) 360 378 return true; 361 379 if (!route.contains(i)) … … 387 405 } 388 406 407 } 408 389 409 #ifdef DEBUG 390 QDebug operator<<(QDebug dbg, const T Matrix &matrix)410 QDebug operator<<(QDebug dbg, const TSPSolver::TMatrix &matrix) 391 411 { 392 412 for (int r = 0; r < matrix.count(); r++) { … … 398 418 } 399 419 400 QDebug operator<<(QDebug dbg, const SCandidate &cand)420 QDebug operator<<(QDebug dbg, const TSPSolver::SStep::SCandidate &cand) 401 421 { 402 422 dbg.nospace() << "(" << cand.nRow << ";" << cand.nCol << ")";
Note: See TracChangeset
for help on using the changeset viewer.