Changeset 74 in tspsg-svn for trunk/src/tspsolver.cpp
- Timestamp:
- Dec 16, 2009, 11:22:05 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/tspsolver.cpp
r71 r74 26 26 //! Class constructor 27 27 CTSPSolver::CTSPSolver() 28 : nCities(0) 28 : nCities(0), root(NULL) 29 29 { 30 30 } … … 80 80 * \todo TODO: Comment the algorithm. 81 81 */ 82 sStep *CTSPSolver::solve(int numCities, tMatrix task, QWidget *parent)82 SStep *CTSPSolver::solve(int numCities, TMatrix task, QWidget *parent) 83 83 { 84 84 if (numCities <= 1) … … 98 98 pd.setValue(0); 99 99 100 sStep *step = new sStep();100 SStep *step = new SStep(); 101 101 step->matrix = task; 102 102 step->price = align(step->matrix); 103 103 root = step; 104 104 105 sStep *left, *right;105 SStep *left, *right; 106 106 int nRow, nCol; 107 107 bool firstStep = true; … … 117 117 } 118 118 if ((nRow == -1) || (nCol == -1) || pd.wasCanceled()) { 119 root = NULL;119 cleanup(); 120 120 break; 121 121 } 122 122 123 123 // Route with (nRow,nCol) path 124 right = new sStep();124 right = new SStep(); 125 125 right->matrix = step->matrix; 126 126 for (int k = 0; k < nCities; k++) { … … 136 136 137 137 // Route without (nRow,nCol) path 138 left = new sStep();138 left = new SStep(); 139 139 left->matrix = step->matrix; 140 140 left->matrix[nRow][nCol] = INFINITY; … … 180 180 } 181 181 182 CTSPSolver::~CTSPSolver() 183 { 184 if (root != NULL) 185 deleteNode(root); 186 } 187 182 188 /* Privates **********************************************************/ 183 189 184 double CTSPSolver::align( tMatrix &matrix)190 double CTSPSolver::align(TMatrix &matrix) 185 191 { 186 192 double r = 0; … … 207 213 route.clear(); 208 214 mayNotBeOptimal = false; 209 } 210 211 bool CTSPSolver::findCandidate(const tMatrix &matrix, int &nRow, int &nCol) const 215 if (root != NULL) 216 deleteNode(root); 217 } 218 219 void CTSPSolver::deleteNode(SStep *node) 220 { 221 if (node->plNode != NULL) 222 deleteNode(node->plNode); 223 if (node->prNode != NULL) 224 deleteNode(node->prNode); 225 delete node; 226 node = NULL; 227 } 228 229 QList<TCandidate> CTSPSolver::findCandidate(const TMatrix &matrix, int &nRow, int &nCol) const 212 230 { 213 231 nRow = -1; 214 232 nCol = -1; 215 bool alts = false; 233 QList<TCandidate> alts; 234 TCandidate cand; 216 235 double h = -1; 217 236 double sum; … … 225 244 nRow = r; 226 245 nCol = c; 227 alts = false; 228 } else if ((sum == h) && !hasSubCycles(r,c)) 229 alts = true; 246 alts.clear(); 247 } else if ((sum == h) && !hasSubCycles(r,c)) { 248 cand.nRow = r; 249 cand.nCol = c; 250 alts.append(cand); 251 } 230 252 } 231 253 return alts; 232 254 } 233 255 234 double CTSPSolver::findMinInCol(int nCol, const tMatrix &matrix, int exr) const256 double CTSPSolver::findMinInCol(int nCol, const TMatrix &matrix, int exr) const 235 257 { 236 258 double min = INFINITY; … … 241 263 } 242 264 243 double CTSPSolver::findMinInRow(int nRow, const tMatrix &matrix, int exc) const265 double CTSPSolver::findMinInRow(int nRow, const TMatrix &matrix, int exc) const 244 266 { 245 267 double min = INFINITY; … … 264 286 } 265 287 266 void CTSPSolver::subCol( tMatrix &matrix, int nCol, double val)288 void CTSPSolver::subCol(TMatrix &matrix, int nCol, double val) 267 289 { 268 290 for (int k = 0; k < nCities; k++) … … 271 293 } 272 294 273 void CTSPSolver::subRow( tMatrix &matrix, int nRow, double val)295 void CTSPSolver::subRow(TMatrix &matrix, int nRow, double val) 274 296 { 275 297 for (int k = 0; k < nCities; k++)
Note: See TracChangeset
for help on using the changeset viewer.