[67e53c96d7] | 1 | /* |
---|
[430bd7f7e9] | 2 | * TSPSG: TSP Solver and Generator |
---|
[5354a01311] | 3 | * Copyright (C) 2007-2009 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 | |
---|
[e664262f7d] | 26 | CTSPSolver::CTSPSolver() |
---|
[430bd7f7e9] | 27 | : nCities(0) |
---|
[e664262f7d] | 28 | { |
---|
| 29 | } |
---|
[67e53c96d7] | 30 | |
---|
[430bd7f7e9] | 31 | void CTSPSolver::cleanup() |
---|
| 32 | { |
---|
| 33 | route.clear(); |
---|
[9cf98b9bd6] | 34 | mayNotBeOptimal = false; |
---|
[430bd7f7e9] | 35 | } |
---|
| 36 | |
---|
| 37 | double CTSPSolver::findMinInRow(int nRow, tMatrix matrix, int exc) |
---|
[e664262f7d] | 38 | { |
---|
[2bc8e278b7] | 39 | double min = INFINITY; |
---|
[e664262f7d] | 40 | for (int k = 0; k < nCities; k++) |
---|
[c10297cf73] | 41 | if (((k != exc)) && (min > matrix.at(nRow).at(k))) |
---|
| 42 | min = matrix.at(nRow).at(k); |
---|
[2bc8e278b7] | 43 | return min == INFINITY ? 0 : min; |
---|
[e664262f7d] | 44 | } |
---|
[67e53c96d7] | 45 | |
---|
[430bd7f7e9] | 46 | double CTSPSolver::findMinInCol(int nCol, tMatrix matrix, int exr) |
---|
[67e53c96d7] | 47 | { |
---|
[2bc8e278b7] | 48 | double min = INFINITY; |
---|
[e664262f7d] | 49 | for (int k = 0; k < nCities; k++) |
---|
[c10297cf73] | 50 | if ((k != exr) && (min > matrix.at(k).at(nCol))) |
---|
| 51 | min = matrix.at(k).at(nCol); |
---|
[2bc8e278b7] | 52 | return min == INFINITY ? 0 : min; |
---|
[67e53c96d7] | 53 | } |
---|
| 54 | |
---|
[430bd7f7e9] | 55 | void CTSPSolver::subRow(tMatrix &matrix, int nRow, double val) |
---|
| 56 | { |
---|
| 57 | for (int k = 0; k < nCities; k++) |
---|
| 58 | if (k != nRow) |
---|
| 59 | matrix[nRow][k] -= val; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | void CTSPSolver::subCol(tMatrix &matrix, int nCol, double val) |
---|
| 63 | { |
---|
| 64 | for (int k = 0; k < nCities; k++) |
---|
| 65 | if (k != nCol) |
---|
| 66 | matrix[k][nCol] -= val; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | double CTSPSolver::align(tMatrix &matrix) |
---|
| 70 | { |
---|
| 71 | double r = 0; |
---|
| 72 | double min; |
---|
| 73 | for (int k = 0; k < nCities; k++) { |
---|
| 74 | min = findMinInRow(k,matrix); |
---|
| 75 | if (min > 0) { |
---|
| 76 | r += min; |
---|
| 77 | subRow(matrix,k,min); |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | for (int k = 0; k < nCities; k++) { |
---|
| 81 | min = findMinInCol(k,matrix); |
---|
| 82 | if (min > 0) { |
---|
| 83 | r += min; |
---|
| 84 | subCol(matrix,k,min); |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | return r; |
---|
| 88 | } |
---|
| 89 | |
---|
[9cf98b9bd6] | 90 | bool CTSPSolver::findCandidate(tMatrix matrix, int &nRow, int &nCol) |
---|
[430bd7f7e9] | 91 | { |
---|
| 92 | nRow = -1; |
---|
| 93 | nCol = -1; |
---|
| 94 | bool alts = false; |
---|
[9cf98b9bd6] | 95 | double h = -1; |
---|
[430bd7f7e9] | 96 | double sum; |
---|
| 97 | for (int r = 0; r < nCities; r++) |
---|
| 98 | for (int c = 0; c < nCities; c++) |
---|
[6dfdef0c3e] | 99 | // if ((matrix.at(r).at(c) == 0) && !forbidden.values(r).contains(c)) { |
---|
[c10297cf73] | 100 | if (matrix.at(r).at(c) == 0) { |
---|
[430bd7f7e9] | 101 | sum = findMinInRow(r,matrix,c) + findMinInCol(c,matrix,r); |
---|
| 102 | if (sum > h) { |
---|
| 103 | h = sum; |
---|
| 104 | nRow = r; |
---|
| 105 | nCol = c; |
---|
| 106 | alts = false; |
---|
[6dfdef0c3e] | 107 | } else if ((sum == h) && !hasSubCycles(r,c)) |
---|
[430bd7f7e9] | 108 | alts = true; |
---|
| 109 | } |
---|
| 110 | return alts; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | bool CTSPSolver::hasSubCycles(int nRow, int nCol) |
---|
| 114 | { |
---|
| 115 | if ((nRow < 0) || (nCol < 0) || route.isEmpty() || !(route.size() < nCities - 1) || !route.contains(nCol)) |
---|
| 116 | return false; |
---|
| 117 | int i = nCol; |
---|
| 118 | while (true) { |
---|
| 119 | if ((i = route[i]) == nRow) |
---|
| 120 | return true; |
---|
| 121 | if (!route.contains(i)) |
---|
| 122 | return false; |
---|
| 123 | } |
---|
| 124 | return false; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | // TODO: Comment the algorithm |
---|
| 128 | sStep *CTSPSolver::solve(int numCities, tMatrix task, QWidget *parent) |
---|
[67e53c96d7] | 129 | { |
---|
| 130 | if (numCities <= 1) |
---|
| 131 | return NULL; |
---|
[430bd7f7e9] | 132 | cleanup(); |
---|
[e664262f7d] | 133 | nCities = numCities; |
---|
[430bd7f7e9] | 134 | QProgressDialog pd(parent); |
---|
| 135 | QProgressBar *pb = new QProgressBar(&pd); |
---|
| 136 | pb->setAlignment(Qt::AlignCenter); |
---|
| 137 | pb->setFormat(trUtf8("%v of %m parts found")); |
---|
| 138 | pd.setBar(pb); |
---|
| 139 | pd.setMaximum(nCities); |
---|
| 140 | pd.setMinimumDuration(1000); |
---|
| 141 | pd.setLabelText(trUtf8("Calculating optimal route...")); |
---|
| 142 | pd.setWindowTitle(trUtf8("Solution Progress")); |
---|
| 143 | pd.setWindowModality(Qt::ApplicationModal); |
---|
| 144 | pd.setValue(0); |
---|
| 145 | |
---|
[e664262f7d] | 146 | sStep *step = new sStep(); |
---|
| 147 | step->matrix = task; |
---|
[9cf98b9bd6] | 148 | step->price = align(step->matrix); |
---|
[e664262f7d] | 149 | root = step; |
---|
[67e53c96d7] | 150 | |
---|
[430bd7f7e9] | 151 | sStep *left, *right; |
---|
| 152 | int nRow, nCol; |
---|
[9cf98b9bd6] | 153 | bool firstStep = true; |
---|
| 154 | double check; |
---|
| 155 | while (this->route.size() < nCities) { |
---|
[aaf2113307] | 156 | // forbidden.clear(); |
---|
[9cf98b9bd6] | 157 | step->alts = findCandidate(step->matrix,nRow,nCol); |
---|
[430bd7f7e9] | 158 | while (hasSubCycles(nRow,nCol)) { |
---|
[aaf2113307] | 159 | // forbidden[nRow] = nCol; |
---|
[430bd7f7e9] | 160 | step->matrix[nRow][nCol] = INFINITY; |
---|
| 161 | step->price += align(step->matrix); |
---|
[9cf98b9bd6] | 162 | step->alts = findCandidate(step->matrix,nRow,nCol); |
---|
[430bd7f7e9] | 163 | } |
---|
| 164 | if ((nRow == -1) || (nCol == -1) || pd.wasCanceled()) { |
---|
| 165 | root = NULL; |
---|
| 166 | break; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | // Route with (nRow,nCol) path |
---|
| 170 | right = new sStep(); |
---|
| 171 | right->matrix = step->matrix; |
---|
| 172 | for (int k = 0; k < nCities; k++) { |
---|
| 173 | if (k != nCol) |
---|
| 174 | right->matrix[nRow][k] = INFINITY; |
---|
| 175 | if (k != nRow) |
---|
| 176 | right->matrix[k][nCol] = INFINITY; |
---|
| 177 | } |
---|
| 178 | right->price = step->price + align(right->matrix); |
---|
| 179 | // Forbid selected route to exclude its reuse in next steps. |
---|
| 180 | right->matrix[nCol][nRow] = INFINITY; |
---|
| 181 | right->matrix[nRow][nCol] = INFINITY; |
---|
| 182 | |
---|
| 183 | // Route without (nRow,nCol) path |
---|
| 184 | left = new sStep(); |
---|
| 185 | left->matrix = step->matrix; |
---|
| 186 | left->matrix[nRow][nCol] = INFINITY; |
---|
| 187 | left->price = step->price + align(left->matrix); |
---|
| 188 | |
---|
| 189 | step->candidate.nRow = nRow; |
---|
| 190 | step->candidate.nCol = nCol; |
---|
| 191 | step->plNode = left; |
---|
| 192 | step->prNode = right; |
---|
| 193 | |
---|
| 194 | if (right->price <= left->price) { |
---|
| 195 | // Route with (nRow,nCol) path is cheaper |
---|
| 196 | step = right; |
---|
[9cf98b9bd6] | 197 | this->route[nRow] = nCol; |
---|
| 198 | pd.setValue(this->route.size()); |
---|
| 199 | if (firstStep) { |
---|
| 200 | check = left->price; |
---|
| 201 | firstStep = false; |
---|
| 202 | } |
---|
[430bd7f7e9] | 203 | } else { |
---|
| 204 | // Route without (nRow,nCol) path is cheaper |
---|
| 205 | step = left; |
---|
| 206 | qApp->processEvents(); |
---|
[9cf98b9bd6] | 207 | if (firstStep) { |
---|
| 208 | check = right->price; |
---|
| 209 | firstStep = false; |
---|
| 210 | } |
---|
[430bd7f7e9] | 211 | } |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | if (!root && !pd.wasCanceled()) { |
---|
[aaf2113307] | 215 | pd.reset(); |
---|
| 216 | QMessageBox(QMessageBox::Warning,trUtf8("Solution Result"),trUtf8("Unable to find solution.\nMaybe, this task has no solutions."),QMessageBox::Ok,parent).exec(); |
---|
[430bd7f7e9] | 217 | } |
---|
| 218 | |
---|
[aaf2113307] | 219 | qApp->processEvents(); |
---|
| 220 | |
---|
[9cf98b9bd6] | 221 | if (root) { |
---|
| 222 | route = this->route; |
---|
| 223 | mayNotBeOptimal = (check < step->price); |
---|
| 224 | } |
---|
[430bd7f7e9] | 225 | return root; |
---|
[67e53c96d7] | 226 | } |
---|
[9cf98b9bd6] | 227 | |
---|
| 228 | QString CTSPSolver::getSortedPath() const |
---|
| 229 | { |
---|
| 230 | if (!root || route.isEmpty() || (route.size() != nCities)) |
---|
| 231 | return QString(); |
---|
| 232 | |
---|
| 233 | int i = 0; // We start from City 1 |
---|
| 234 | QString path = trUtf8("City %1").arg(1) + " -> "; |
---|
| 235 | while ((i = route[i]) != 0) { |
---|
| 236 | path += trUtf8("City %1").arg(i + 1) + " -> "; |
---|
| 237 | } |
---|
| 238 | // And finish in City 1, too |
---|
| 239 | path += trUtf8("City %1").arg(1); |
---|
| 240 | |
---|
| 241 | return path; |
---|
| 242 | } |
---|
| 243 | |
---|
[55c4b858e9] | 244 | QString CTSPSolver::getVersionId() |
---|
| 245 | { |
---|
| 246 | return QString("$Id$"); |
---|
| 247 | } |
---|
| 248 | |
---|
[9cf98b9bd6] | 249 | bool CTSPSolver::isOptimal() const |
---|
| 250 | { |
---|
| 251 | return !mayNotBeOptimal; |
---|
| 252 | } |
---|