- Timestamp:
- Sep 2, 2009, 2:37:39 AM (15 years ago)
- Location:
- trunk/src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/mainwindow.cpp
r59 r60 457 457 sStep *step = root; 458 458 n = 1; 459 QString path = "";460 459 while (n <= spinCities->value()) { 461 460 if (step->prNode->prNode != NULL || (step->prNode->prNode == NULL && step->plNode->prNode == NULL)) { … … 467 466 output.append("<p> </p>"); 468 467 } 469 path += QString(" (%1,%2)").arg(step->candidate.nRow + 1).arg(step->candidate.nCol + 1);470 468 } 471 469 if (step->prNode->prNode != NULL) … … 476 474 break; 477 475 } 478 output.append("<p>" + trUtf8("Optimal path:") + "</p>"); 479 output.append("<p> " + path + "</p>"); 476 if (solver.isOptimal()) 477 output.append("<p>" + trUtf8("Optimal path:") + "</p>"); 478 else 479 output.append("<p>" + trUtf8("Resulting path:") + "</p>"); 480 output.append("<p> " + solver.getSortedPath() + "</p>"); 480 481 output.append("<p>" + trUtf8("The price is <b>%1</b> units.").arg(step->price) + "</p>"); 482 if (!solver.isOptimal()) { 483 output.append("<p> </p>"); 484 output.append("<p>" + trUtf8("<b>WARNING!!!</b><br>This result is a record, but it may not be optimal.<br>Iterations need to be continued to check whether this result is optimal or get an optimal one.") + "</p>"); 485 } 481 486 solutionText->setHtml(output.join("")); 482 487 solutionText->setDocumentTitle(trUtf8("Solution of Variant #%1 task").arg(spinVariant->value())); -
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 } -
trunk/src/tspsolver.h
r59 r60 47 47 public: 48 48 CTSPSolver(); 49 QString getSortedPath() const; 50 bool isOptimal() const; 49 51 sStep *solve(int, tMatrix, QWidget *parent = 0); 50 52 51 53 private: 54 bool mayNotBeOptimal; 52 55 int nCities; 53 56 sStep *root; … … 56 59 double align(tMatrix &); 57 60 void cleanup(); 58 bool findCandidate(tMatrix, int &, int & , double &);61 bool findCandidate(tMatrix, int &, int &); 59 62 double findMinInRow(int, tMatrix, int exc = -1); 60 63 double findMinInCol(int, tMatrix, int exr = -1);
Note: See TracChangeset
for help on using the changeset viewer.