Changeset 60 in tspsg-svn for trunk/src/tspsolver.cpp
- Timestamp:
- Sep 2, 2009, 2:37:39 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/tspsolver.cpp
r55 r60 33 33 { 34 34 route.clear(); 35 mayNotBeOptimal = false; 35 36 } 36 37 … … 88 89 } 89 90 90 bool CTSPSolver::findCandidate(tMatrix matrix, int &nRow, int &nCol, double &h) 91 { 92 h = -1; 91 bool CTSPSolver::findCandidate(tMatrix matrix, int &nRow, int &nCol) 92 { 93 93 nRow = -1; 94 94 nCol = -1; 95 95 bool alts = false; 96 double h = -1; 96 97 double sum; 97 98 for (int r = 0; r < nCities; r++) … … 132 133 cleanup(); 133 134 nCities = numCities; 134 double s;135 135 QProgressDialog pd(parent); 136 136 QProgressBar *pb = new QProgressBar(&pd); … … 147 147 sStep *step = new sStep(); 148 148 step->matrix = task; 149 150 s = align(step->matrix); 151 step->price = s; 149 step->price = align(step->matrix); 152 150 root = step; 153 151 154 152 sStep *left, *right; 155 153 int nRow, nCol; 156 while (route.size() < nCities) { 154 bool firstStep = true; 155 double check; 156 while (this->route.size() < nCities) { 157 157 // forbidden.clear(); 158 step->alts = findCandidate(step->matrix,nRow,nCol ,s);158 step->alts = findCandidate(step->matrix,nRow,nCol); 159 159 while (hasSubCycles(nRow,nCol)) { 160 160 // forbidden[nRow] = nCol; 161 161 step->matrix[nRow][nCol] = INFINITY; 162 162 step->price += align(step->matrix); 163 step->alts = findCandidate(step->matrix,nRow,nCol ,s);163 step->alts = findCandidate(step->matrix,nRow,nCol); 164 164 } 165 165 if ((nRow == -1) || (nCol == -1) || pd.wasCanceled()) { … … 196 196 // Route with (nRow,nCol) path is cheaper 197 197 step = right; 198 route[nRow] = nCol; 199 pd.setValue(route.size()); 198 this->route[nRow] = nCol; 199 pd.setValue(this->route.size()); 200 if (firstStep) { 201 check = left->price; 202 firstStep = false; 203 } 200 204 } else { 201 205 // Route without (nRow,nCol) path is cheaper 202 206 step = left; 203 207 qApp->processEvents(); 208 if (firstStep) { 209 check = right->price; 210 firstStep = false; 211 } 204 212 } 205 213 } … … 212 220 qApp->processEvents(); 213 221 222 if (root) { 223 route = this->route; 224 mayNotBeOptimal = (check < step->price); 225 } 214 226 return root; 215 227 } 228 229 QString CTSPSolver::getSortedPath() const 230 { 231 if (!root || route.isEmpty() || (route.size() != nCities)) 232 return QString(); 233 234 int i = 0; // We start from City 1 235 QString path = trUtf8("City %1").arg(1) + " -> "; 236 while ((i = route[i]) != 0) { 237 path += trUtf8("City %1").arg(i + 1) + " -> "; 238 } 239 // And finish in City 1, too 240 path += trUtf8("City %1").arg(1); 241 242 return path; 243 } 244 245 bool CTSPSolver::isOptimal() const 246 { 247 return !mayNotBeOptimal; 248 }
Note: See TracChangeset
for help on using the changeset viewer.