Changeset 9cda6e0f5d in tspsg for src/tspsolver.cpp


Ignore:
Timestamp:
Apr 25, 2010, 4:36:27 PM (15 years ago)
Author:
Oleksii Serdiuk
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)
Message:

+ Added SStep::next that indicates what branch was selected for the next step.
+ Added "Show solution graph" option.
+ New CTSPSolver::getTotalSteps() method that returns a total number of steps in the current solution.

  • Moved SCandidate declaration into SStep declaration.
  • Moved everything in tspsolver.h and tspsolver.cpp into TSPSolver namespace.
  • Force CopyAction? on file drop or it will be deleted after dropping in Windows if MoveAction? was selected.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/tspsolver.cpp

    r345e7b6132 r9cda6e0f5d  
    2727#define MAX_DOUBLE std::numeric_limits<double>::max()
    2828
     29namespace TSPSolver {
     30
    2931/*!
    3032 * \brief Returns CTSPSolver's version ID.
     
    4143 */
    4244CTSPSolver::CTSPSolver(QObject *parent)
    43         : QObject(parent), nCities(0), root(NULL) {}
     45        : QObject(parent), nCities(0), total(0), root(NULL) {}
    4446
    4547/*!
    4648 * \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.
     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.
    4850 * \warning After call to this function a solution tree returned by the solve() function is no longer valid.
    4951 * \note It is not required to call this function manually. This function is always called by solve() at the beginning of the solution process.
     
    5355void CTSPSolver::cleanup(bool processEvents)
    5456{
     57#ifdef QAPPLICATION_H
    5558        if (!processEvents)
    5659                QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
     60#endif
    5761        route.clear();
    5862        mayNotBeOptimal = false;
    5963        if (root != NULL)
    6064                deleteTree(root, processEvents);
     65#ifdef QAPPLICATION_H
    6166        if (!processEvents)
    6267                QApplication::restoreOverrideCursor();
     68#endif
    6369}
    6470
     
    8490
    8591/*!
     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 */
     96int CTSPSolver::getTotalSteps() const
     97{
     98        return total;
     99}
     100
     101/*!
    86102 * \brief Indicates whether or not the solution is definitely optimal.
    87103 * \return \c true if the solution is definitely optimal, otherwise \c false.
     
    105121SStep *CTSPSolver::solve(int numCities, const TMatrix &task)
    106122{
    107         if (numCities <= 1)
     123        if (numCities < 3)
    108124                return NULL;
    109125
     
    131147bool firstStep = true;
    132148double check = INFINITY;
    133         while (this->route.size() < nCities) {
     149        total = 0;
     150        while (route.size() < nCities) {
    134151                step->alts = findCandidate(step->matrix,nRow,nCol);
    135152
     
    188205                if (right->price <= left->price) {
    189206                        // Route with (nRow,nCol) path is cheaper
     207                        step->next = SStep::RightBranch;
    190208                        step = right;
    191                         this->route[nRow] = nCol;
    192                         emit routePartFound(this->route.size());
     209                        route[nRow] = nCol;
     210                        emit routePartFound(route.size());
    193211                        if (firstStep) {
    194212                                check = left->price;
     
    197215                } else {
    198216                        // Route without (nRow,nCol) path is cheaper
     217                        step->next = SStep::LeftBranch;
    199218                        step = left;
    200                         qApp->processEvents();
     219                        QCoreApplication::processEvents();
    201220                        if (firstStep) {
    202221                                check = right->price;
     
    204223                        }
    205224                }
    206         }
    207 
    208         route = this->route;
     225                total++;
     226        }
     227
    209228        mayNotBeOptimal = (check < step->price);
    210229
     
    259278                }
    260279        }
    261         return r;
     280        return (r != MAX_DOUBLE) ? r : INFINITY;
    262281}
    263282
     
    270289        forever {
    271290                if (processEvents)
    272                         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
     291                        QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
    273292                if (step->plNode != NULL) {
    274293                        // We have left child node - going inside it
     
    282301                        continue;
    283302                } else {
    284                         // We have no child nodes. Deleting current one.
     303                        // We have no child nodes. Deleting the current one.
    285304                        parent = step->pNode;
    286305                        delete step;
     
    305324}
    306325
    307 QList<SCandidate> CTSPSolver::findCandidate(const TMatrix &matrix, int &nRow, int &nCol) const
     326QList<SStep::SCandidate> CTSPSolver::findCandidate(const TMatrix &matrix, int &nRow, int &nCol) const
    308327{
    309328        nRow = -1;
    310329        nCol = -1;
    311 QList<SCandidate> alts;
    312 SCandidate cand;
     330QList<SStep::SCandidate> alts;
     331SStep::SCandidate cand;
    313332double h = -1;
    314333double sum;
    315334        for (int r = 0; r < nCities; r++)
    316335                for (int c = 0; c < nCities; c++)
    317 //                      if ((matrix.at(r).at(c) == 0) && !forbidden.values(r).contains(c)) {
    318336                        if (matrix.at(r).at(c) == 0) {
    319337                                sum = findMinInRow(r,matrix,c) + findMinInCol(c,matrix,r);
     
    356374                return false;
    357375int i = nCol;
    358         while (true) {
    359                 if ((i = route[i]) == nRow)
     376        forever {
     377                if ((i = route.value(i)) == nRow)
    360378                        return true;
    361379                if (!route.contains(i))
     
    387405}
    388406
     407}
     408
    389409#ifdef DEBUG
    390 QDebug operator<<(QDebug dbg, const TMatrix &matrix)
     410QDebug operator<<(QDebug dbg, const TSPSolver::TMatrix &matrix)
    391411{
    392412        for (int r = 0; r < matrix.count(); r++) {
     
    398418}
    399419
    400 QDebug operator<<(QDebug dbg, const SCandidate &cand)
     420QDebug operator<<(QDebug dbg, const TSPSolver::SStep::SCandidate &cand)
    401421{
    402422        dbg.nospace() << "(" << cand.nRow << ";" << cand.nCol << ")";
Note: See TracChangeset for help on using the changeset viewer.