source: tspsg/src/tspsolver.cpp @ 9cda6e0f5d

0.1.3.145-beta1-symbian0.1.4.170-beta2-bb10appveyorimgbotreadme
Last change on this file since 9cda6e0f5d was 9cda6e0f5d, checked in by Oleksii Serdiuk, 14 years ago

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