- Timestamp:
- 08/20/10 11:43:31 (14 years ago)
- Location:
- branches/VRP/HeuristicLab.Problems.VehicleRouting/3.3/Encodings
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/GVR/GVREncoding.cs
r4268 r4270 40 40 41 41 public override List<Tour> GetTours(ILookupParameter<DoubleMatrix> distanceMatrix = null, int maxVehicles = int.MaxValue) { 42 List<Tour> tours = new List<Tour>(); 43 Tour newTour = new Tour(); 44 double currentDemand = 0; 42 List<Tour> tours = new List<Tour>(); 45 43 46 int toursProcessed = 0;47 44 foreach (Tour tour in base.Tours) { 48 if (maxVehicles > tours.Count) { 49 newTour = new Tour(); 50 currentDemand = 0; 51 } 45 Tour newTour = new Tour(); 46 double currentDemand = 0; 52 47 53 48 foreach (int city in tour.Cities) { 54 49 currentDemand += demand[city]; 55 50 56 if (maxVehicles > tours.Count && 57 currentDemand > capacity.Value) { 51 if (currentDemand > capacity.Value) { 58 52 if(newTour.Cities.Count > 0) 59 53 tours.Add(newTour); … … 67 61 } 68 62 69 if (newTour.Cities.Count > 0 && 70 maxVehicles > tours.Count) 63 if (newTour.Cities.Count > 0) 71 64 tours.Add(newTour); 65 } 72 66 73 toursProcessed++; 74 } 67 //repair if there are too many vehicles used 68 while (tours.Count > maxVehicles) { 69 Tour tour = tours[tours.Count - 1]; 70 tours[tours.Count - 2].Cities.AddRange(tour.Cities); 71 72 tours.Remove(tour); 73 } 75 74 76 75 return tours; -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/GVR/Manipulators/GVRInsertionManipulator.cs
r4231 r4270 55 55 } else { 56 56 Tour newTour = individual.Tours[random.Next(individual.Tours.Count)]; 57 int newPosition = newTour.Cities.Count;57 int newPosition = random.Next(newTour.Cities.Count + 1); 58 58 59 59 newTour.Cities.Insert(newPosition, customer); -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Prins/Manipulators/PrinsLSManipulator.cs
r4268 r4270 77 77 protected const int depot = -1; 78 78 79 private Tour FindTour(PrinsEncoding individual, int cit iy) {79 private Tour FindTour(PrinsEncoding individual, int city) { 80 80 Tour found = null; 81 81 … … 84 84 85 85 while (found == null && i < tours.Count) { 86 if (tours[i].Cities.Contains(cit iy + 1))86 if (tours[i].Cities.Contains(city)) 87 87 found = tours[i]; 88 88 … … 252 252 if (u != depot) { 253 253 if (v == depot) { 254 Tour tour = FindTour(child, u );254 Tour tour = FindTour(child, u + 1); 255 255 v = tour.Cities[0] - 1; 256 256 InsertBefore(u, v, parent, child); … … 259 259 } 260 260 } 261 262 if(!child.Validate()) {263 }264 261 } 265 262 266 263 private void M2(PrinsEncoding parent, PrinsEncoding child, int u, int v) { 267 264 if (u != depot) { 268 Tour tour = FindTour(child, u );265 Tour tour = FindTour(child, u + 1); 269 266 int iu = tour.Cities.IndexOf(u + 1); 270 267 if (iu < tour.Cities.Count - 1) { … … 272 269 273 270 if (v == depot) { 274 tour = FindTour(child, u );271 tour = FindTour(child, u + 1); 275 272 v = tour.Cities[0] - 1; 276 273 InsertBefore(u, x, v, parent, child); … … 280 277 } 281 278 } 282 283 if (!child.Validate()) {284 }285 279 } 286 280 287 281 private void M3(PrinsEncoding parent, PrinsEncoding child, int u, int v) { 288 282 if (u != depot) { 289 Tour tour = FindTour(child, u );283 Tour tour = FindTour(child, u + 1); 290 284 int iu = tour.Cities.IndexOf(u + 1); 291 285 if (iu < tour.Cities.Count - 1) { … … 293 287 294 288 if (v == depot) { 295 tour = FindTour(child, u );289 tour = FindTour(child, u + 1); 296 290 v = tour.Cities[0] - 1; 297 291 InsertBefore(x, u, v, parent, child); … … 301 295 } 302 296 } 303 304 if (!child.Validate()) {305 }306 297 } 307 298 … … 310 301 Swap(u, v, parent, child); 311 302 } 312 313 if (!child.Validate()) {314 }315 303 } 316 304 317 305 private void M5(PrinsEncoding parent, PrinsEncoding child, int u, int v) { 318 306 if (u != depot && v != depot) { 319 Tour tour = FindTour(child, u );307 Tour tour = FindTour(child, u + 1); 320 308 int iu = tour.Cities.IndexOf(u + 1); 321 309 if (iu < tour.Cities.Count - 1) { … … 326 314 } 327 315 } 328 329 if (!child.Validate()) {330 }331 316 } 332 317 333 318 private void M6(PrinsEncoding parent, PrinsEncoding child, int u, int v) { 334 319 if (u != depot && v != depot) { 335 Tour tour = FindTour(child, u );320 Tour tour = FindTour(child, u + 1); 336 321 int iu = tour.Cities.IndexOf(u + 1); 337 322 if (iu < tour.Cities.Count - 1) { 338 323 int x = tour.Cities[iu + 1] - 1; 339 324 340 tour = FindTour(child, v );325 tour = FindTour(child, v + 1); 341 326 int iv = tour.Cities.IndexOf(v + 1); 342 327 if (iv < tour.Cities.Count - 1) { … … 348 333 } 349 334 } 350 351 if (!child.Validate()) {352 }353 335 } 354 336 355 337 private void M7(PrinsEncoding parent, PrinsEncoding child, int u, int v) { 356 338 if (u != depot && v != depot) { 357 Tour tu = FindTour(child, u );358 Tour tv = FindTour(child, v );339 Tour tu = FindTour(child, u + 1); 340 Tour tv = FindTour(child, v + 1); 359 341 360 342 if (tu.Cities[0] == tv.Cities[0]) { … … 373 355 } 374 356 } 375 376 if (!child.Validate()) {377 }378 357 } 379 358 380 359 private void M8(PrinsEncoding parent, PrinsEncoding child, int u, int v) { 381 360 if (u != depot && v != depot) { 382 Tour tu = FindTour(child, u );383 Tour tv = FindTour(child, v );361 Tour tu = FindTour(child, u + 1); 362 Tour tv = FindTour(child, v + 1); 384 363 385 364 if (tu.Cities[0] != tv.Cities[0]) { … … 398 377 } 399 378 } 400 401 if (!child.Validate()) {402 }403 379 } 404 380 405 381 private void M9(PrinsEncoding parent, PrinsEncoding child, int u, int v) { 406 382 if (u != depot && v != depot) { 407 Tour tu = FindTour(child, u );408 Tour tv = FindTour(child, v );383 Tour tu = FindTour(child, u + 1); 384 Tour tv = FindTour(child, v + 1); 409 385 410 386 if (tu.Cities[0] != tv.Cities[0]) { … … 423 399 } 424 400 } 425 426 if (!child.Validate()) {427 }428 401 } 429 402 … … 492 465 return null; 493 466 } 494 495 protected override void Manipulate(IRandom random, PrinsEncoding individual) {496 List<Tour> tours = individual.GetTours(DistanceMatrixParameter);497 bool improvement = false;498 int iterations = 0;499 500 do {501 int u = depot;502 improvement = false;503 double originalQuality = GetQuality(individual);504 PrinsEncoding child = null;505 506 while (!improvement && u < Cities) {507 int v = depot;508 while (!improvement && v < Cities) {509 if (u != v) {510 child = Manipulate(individual,511 originalQuality, u, v);512 513 improvement = child != null;514 }515 v++;516 }517 u++;518 }519 520 if (improvement) {521 for (int i = 0; i < child.Length; i++) {522 individual[i] = child[i];523 }524 }525 526 iterations++;527 } while (improvement &&528 iterations < Iterations.Value.Value);529 }530 467 } 531 468 }
Note: See TracChangeset
for help on using the changeset viewer.