1 | /* |
---|
2 | * TSPSG: TSP Solver and Generator |
---|
3 | * Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name> |
---|
4 | * |
---|
5 | * $Id: tspsolver.cpp 71 2009-12-07 16:06:44Z laleppa $ |
---|
6 | * $URL: https://tspsg.svn.sourceforge.net/svnroot/tspsg/trunk/src/tspsolver.cpp $ |
---|
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 | |
---|
26 | //! Class constructor |
---|
27 | CTSPSolver::CTSPSolver() |
---|
28 | : nCities(0) |
---|
29 | { |
---|
30 | } |
---|
31 | |
---|
32 | /*! |
---|
33 | * \brief Returns the sorted optimal path, starting from City 1. |
---|
34 | * \return A string, containing sorted optimal path. |
---|
35 | */ |
---|
36 | QString CTSPSolver::getSortedPath() const |
---|
37 | { |
---|
38 | if (!root || route.isEmpty() || (route.size() != nCities)) |
---|
39 | return QString(); |
---|
40 | |
---|
41 | int i = 0; // We start from City 1 |
---|
42 | QString path = trUtf8("City %1").arg(1) + " -> "; |
---|
43 | while ((i = route[i]) != 0) { |
---|
44 | path += trUtf8("City %1").arg(i + 1) + " -> "; |
---|
45 | } |
---|
46 | // And finish in City 1, too |
---|
47 | path += trUtf8("City %1").arg(1); |
---|
48 | |
---|
49 | return path; |
---|
50 | } |
---|
51 | |
---|
52 | /*! |
---|
53 | * \brief Returns CTSPSolver's version ID. |
---|
54 | * \return A string: <b>\$Id: tspsolver.cpp 71 2009-12-07 16:06:44Z laleppa $</b>. |
---|
55 | */ |
---|
56 | QString CTSPSolver::getVersionId() |
---|
57 | { |
---|
58 | return QString("$Id: tspsolver.cpp 71 2009-12-07 16:06:44Z laleppa $"); |
---|
59 | } |
---|
60 | |
---|
61 | /*! |
---|
62 | * \brief Returns whether or not the solution is definitely optimal. |
---|
63 | * \return \c true if solution is definitely optimal, otherwise \c false. |
---|
64 | * |
---|
65 | * The solution may need some further interations to determine whether it is optimal. |
---|
66 | * In such cases this function returns \c false. |
---|
67 | */ |
---|
68 | bool CTSPSolver::isOptimal() const |
---|
69 | { |
---|
70 | return !mayNotBeOptimal; |
---|
71 | } |
---|
72 | |
---|
73 | /*! |
---|
74 | * \brief Solves the given task. |
---|
75 | * \param numCities Number of cities in the task. |
---|
76 | * \param task The matrix of city-to-city travel costs. |
---|
77 | * \param parent The parent widget for displaying messages and dialogs. |
---|
78 | * \return Pointer to the root of the solution tree. |
---|
79 | * |
---|
80 | * \todo TODO: Comment the algorithm. |
---|
81 | */ |
---|
82 | sStep *CTSPSolver::solve(int numCities, tMatrix task, QWidget *parent) |
---|
83 | { |
---|
84 | if (numCities <= 1) |
---|
85 | return NULL; |
---|
86 | cleanup(); |
---|
87 | nCities = numCities; |
---|
88 | QProgressDialog pd(parent); |
---|
89 | QProgressBar *pb = new QProgressBar(&pd); |
---|
90 | pb->setAlignment(Qt::AlignCenter); |
---|
91 | pb->setFormat(trUtf8("%v of %m parts found")); |
---|
92 | pd.setBar(pb); |
---|
93 | pd.setMaximum(nCities); |
---|
94 | pd.setMinimumDuration(1000); |
---|
95 | pd.setLabelText(trUtf8("Calculating optimal route...")); |
---|
96 | pd.setWindowTitle(trUtf8("Solution Progress")); |
---|
97 | pd.setWindowModality(Qt::ApplicationModal); |
---|
98 | pd.setValue(0); |
---|
99 | |
---|
100 | sStep *step = new sStep(); |
---|
101 | step->matrix = task; |
---|
102 | step->price = align(step->matrix); |
---|
103 | root = step; |
---|
104 | |
---|
105 | sStep *left, *right; |
---|
106 | int nRow, nCol; |
---|
107 | bool firstStep = true; |
---|
108 | double check; |
---|
109 | while (this->route.size() < nCities) { |
---|
110 | // forbidden.clear(); |
---|
111 | step->alts = findCandidate(step->matrix,nRow,nCol); |
---|
112 | while (hasSubCycles(nRow,nCol)) { |
---|
113 | // forbidden[nRow] = nCol; |
---|
114 | step->matrix[nRow][nCol] = INFINITY; |
---|
115 | step->price += align(step->matrix); |
---|
116 | step->alts = findCandidate(step->matrix,nRow,nCol); |
---|
117 | } |
---|
118 | if ((nRow == -1) || (nCol == -1) || pd.wasCanceled()) { |
---|
119 | root = NULL; |
---|
120 | break; |
---|
121 | } |
---|
122 | |
---|
123 | // Route with (nRow,nCol) path |
---|
124 | right = new sStep(); |
---|
125 | right->matrix = step->matrix; |
---|
126 | for (int k = 0; k < nCities; k++) { |
---|
127 | if (k != nCol) |
---|
128 | right->matrix[nRow][k] = INFINITY; |
---|
129 | if (k != nRow) |
---|
130 | right->matrix[k][nCol] = INFINITY; |
---|
131 | } |
---|
132 | right->price = step->price + align(right->matrix); |
---|
133 | // Forbid selected route to exclude its reuse in next steps. |
---|
134 | right->matrix[nCol][nRow] = INFINITY; |
---|
135 | right->matrix[nRow][nCol] = INFINITY; |
---|
136 | |
---|
137 | // Route without (nRow,nCol) path |
---|
138 | left = new sStep(); |
---|
139 | left->matrix = step->matrix; |
---|
140 | left->matrix[nRow][nCol] = INFINITY; |
---|
141 | left->price = step->price + align(left->matrix); |
---|
142 | |
---|
143 | step->candidate.nRow = nRow; |
---|
144 | step->candidate.nCol = nCol; |
---|
145 | step->plNode = left; |
---|
146 | step->prNode = right; |
---|
147 | |
---|
148 | if (right->price <= left->price) { |
---|
149 | // Route with (nRow,nCol) path is cheaper |
---|
150 | step = right; |
---|
151 | this->route[nRow] = nCol; |
---|
152 | pd.setValue(this->route.size()); |
---|
153 | if (firstStep) { |
---|
154 | check = left->price; |
---|
155 | firstStep = false; |
---|
156 | } |
---|
157 | } else { |
---|
158 | // Route without (nRow,nCol) path is cheaper |
---|
159 | step = left; |
---|
160 | qApp->processEvents(); |
---|
161 | if (firstStep) { |
---|
162 | check = right->price; |
---|
163 | firstStep = false; |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | if (!root && !pd.wasCanceled()) { |
---|
169 | pd.reset(); |
---|
170 | QMessageBox(QMessageBox::Warning,trUtf8("Solution Result"),trUtf8("Unable to find solution.\nMaybe, this task has no solutions."),QMessageBox::Ok,parent).exec(); |
---|
171 | } |
---|
172 | |
---|
173 | qApp->processEvents(); |
---|
174 | |
---|
175 | if (root) { |
---|
176 | route = this->route; |
---|
177 | mayNotBeOptimal = (check < step->price); |
---|
178 | } |
---|
179 | return root; |
---|
180 | } |
---|
181 | |
---|
182 | /* Privates **********************************************************/ |
---|
183 | |
---|
184 | double CTSPSolver::align(tMatrix &matrix) |
---|
185 | { |
---|
186 | double r = 0; |
---|
187 | double min; |
---|
188 | for (int k = 0; k < nCities; k++) { |
---|
189 | min = findMinInRow(k,matrix); |
---|
190 | if (min > 0) { |
---|
191 | r += min; |
---|
192 | subRow(matrix,k,min); |
---|
193 | } |
---|
194 | } |
---|
195 | for (int k = 0; k < nCities; k++) { |
---|
196 | min = findMinInCol(k,matrix); |
---|
197 | if (min > 0) { |
---|
198 | r += min; |
---|
199 | subCol(matrix,k,min); |
---|
200 | } |
---|
201 | } |
---|
202 | return r; |
---|
203 | } |
---|
204 | |
---|
205 | void CTSPSolver::cleanup() |
---|
206 | { |
---|
207 | route.clear(); |
---|
208 | mayNotBeOptimal = false; |
---|
209 | } |
---|
210 | |
---|
211 | bool CTSPSolver::findCandidate(const tMatrix &matrix, int &nRow, int &nCol) const |
---|
212 | { |
---|
213 | nRow = -1; |
---|
214 | nCol = -1; |
---|
215 | bool alts = false; |
---|
216 | double h = -1; |
---|
217 | double sum; |
---|
218 | for (int r = 0; r < nCities; r++) |
---|
219 | for (int c = 0; c < nCities; c++) |
---|
220 | // if ((matrix.at(r).at(c) == 0) && !forbidden.values(r).contains(c)) { |
---|
221 | if (matrix.at(r).at(c) == 0) { |
---|
222 | sum = findMinInRow(r,matrix,c) + findMinInCol(c,matrix,r); |
---|
223 | if (sum > h) { |
---|
224 | h = sum; |
---|
225 | nRow = r; |
---|
226 | nCol = c; |
---|
227 | alts = false; |
---|
228 | } else if ((sum == h) && !hasSubCycles(r,c)) |
---|
229 | alts = true; |
---|
230 | } |
---|
231 | return alts; |
---|
232 | } |
---|
233 | |
---|
234 | double CTSPSolver::findMinInCol(int nCol, const tMatrix &matrix, int exr) const |
---|
235 | { |
---|
236 | double min = INFINITY; |
---|
237 | for (int k = 0; k < nCities; k++) |
---|
238 | if ((k != exr) && (min > matrix.at(k).at(nCol))) |
---|
239 | min = matrix.at(k).at(nCol); |
---|
240 | return min == INFINITY ? 0 : min; |
---|
241 | } |
---|
242 | |
---|
243 | double CTSPSolver::findMinInRow(int nRow, const tMatrix &matrix, int exc) const |
---|
244 | { |
---|
245 | double min = INFINITY; |
---|
246 | for (int k = 0; k < nCities; k++) |
---|
247 | if (((k != exc)) && (min > matrix.at(nRow).at(k))) |
---|
248 | min = matrix.at(nRow).at(k); |
---|
249 | return min == INFINITY ? 0 : min; |
---|
250 | } |
---|
251 | |
---|
252 | bool CTSPSolver::hasSubCycles(int nRow, int nCol) const |
---|
253 | { |
---|
254 | if ((nRow < 0) || (nCol < 0) || route.isEmpty() || !(route.size() < nCities - 1) || !route.contains(nCol)) |
---|
255 | return false; |
---|
256 | int i = nCol; |
---|
257 | while (true) { |
---|
258 | if ((i = route[i]) == nRow) |
---|
259 | return true; |
---|
260 | if (!route.contains(i)) |
---|
261 | return false; |
---|
262 | } |
---|
263 | return false; |
---|
264 | } |
---|
265 | |
---|
266 | void CTSPSolver::subCol(tMatrix &matrix, int nCol, double val) |
---|
267 | { |
---|
268 | for (int k = 0; k < nCities; k++) |
---|
269 | if (k != nCol) |
---|
270 | matrix[k][nCol] -= val; |
---|
271 | } |
---|
272 | |
---|
273 | void CTSPSolver::subRow(tMatrix &matrix, int nRow, double val) |
---|
274 | { |
---|
275 | for (int k = 0; k < nCities; k++) |
---|
276 | if (k != nRow) |
---|
277 | matrix[nRow][k] -= val; |
---|
278 | } |
---|