1 | /* |
---|
2 | * TSPSG: TSP Solver and Generator |
---|
3 | * Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name> |
---|
4 | * |
---|
5 | * $Id: tspsolver.h 60 2009-09-02 00:37:39Z laleppa $ |
---|
6 | * $URL: https://tspsg.svn.sourceforge.net/svnroot/tspsg/trunk/src/tspsolver.h $ |
---|
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 | #ifndef TSPSOLVER_H |
---|
25 | #define TSPSOLVER_H |
---|
26 | |
---|
27 | #include "globals.h" |
---|
28 | |
---|
29 | typedef QList<QList<double> > tMatrix; |
---|
30 | |
---|
31 | // This structure represents one step of solving |
---|
32 | // The tree of such elements will represent the solving process |
---|
33 | struct sStep { |
---|
34 | tMatrix matrix; // Steps's matrix |
---|
35 | double price; // Price of travel to this step |
---|
36 | struct {int nRow; int nCol;} candidate; // Candiadate for branching in current matrix |
---|
37 | bool alts; // This matrix has alternative candidates |
---|
38 | sStep *plNode, *prNode; // Pointers to left and right branch steps |
---|
39 | sStep() { price = candidate.nRow = candidate.nCol = -1; alts = false; plNode = prNode = NULL; } // Default values |
---|
40 | }; |
---|
41 | |
---|
42 | // TSP Solver class |
---|
43 | class CTSPSolver |
---|
44 | { |
---|
45 | Q_DECLARE_TR_FUNCTIONS(CTSPSolver) |
---|
46 | |
---|
47 | public: |
---|
48 | CTSPSolver(); |
---|
49 | QString getSortedPath() const; |
---|
50 | bool isOptimal() const; |
---|
51 | sStep *solve(int, tMatrix, QWidget *parent = 0); |
---|
52 | |
---|
53 | private: |
---|
54 | bool mayNotBeOptimal; |
---|
55 | int nCities; |
---|
56 | sStep *root; |
---|
57 | QHash<int,int> route; |
---|
58 | // QHash<int,int> forbidden; |
---|
59 | double align(tMatrix &); |
---|
60 | void cleanup(); |
---|
61 | bool findCandidate(tMatrix, int &, int &); |
---|
62 | double findMinInRow(int, tMatrix, int exc = -1); |
---|
63 | double findMinInCol(int, tMatrix, int exr = -1); |
---|
64 | bool hasSubCycles(int, int); |
---|
65 | void subCol(tMatrix &, int, double); |
---|
66 | void subRow(tMatrix &, int, double); |
---|
67 | }; |
---|
68 | |
---|
69 | #endif // TSPSOLVER_H |
---|