Free cookie consent management tool by TermsFeed Policy Generator

Changeset 7806


Ignore:
Timestamp:
05/14/12 15:46:27 (12 years ago)
Author:
jkarder
Message:

#1331: VRPPathRelinker now performs tour matching

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.VehicleRouting/3.3/PathRelinkers/VRPPathRelinker.cs

    r7793 r7806  
    2121
    2222using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2325using HeuristicLab.Common;
    2426using HeuristicLab.Core;
     
    2628using HeuristicLab.Optimization.Operators;
    2729using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     30using HeuristicLab.Problems.VehicleRouting.Encodings;
     31using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
    2832
    2933namespace HeuristicLab.Problems.VehicleRouting {
     
    4448
    4549    public static ItemArray<IItem> Apply(IItem initiator, IItem guide, PercentValue n) {
    46       // TODO: path relinking
    47       return new ItemArray<IItem>();
     50      PotvinEncoding sol1 = initiator.Clone() as PotvinEncoding;
     51      PotvinEncoding sol2 = guide.Clone() as PotvinEncoding;
     52      PotvinEncoding sol3 = new PotvinEncoding();
     53
     54      // match tours
     55      foreach (Tour tour1 in sol1.Tours) {
     56        int highestMatch = -1;
     57        Tour bestMatchingTour = null;
     58        foreach (Tour tour2 in sol2.Tours) {
     59          int matchingCities = MatchingCities(tour1, tour2);
     60          if (matchingCities > highestMatch) {
     61            bestMatchingTour = tour2;
     62            highestMatch = matchingCities;
     63          }
     64        }
     65        sol2.Tours.Remove(bestMatchingTour);
     66        sol3.Tours.Add(bestMatchingTour);
     67      }
     68      // add all remaining tours
     69      sol3.Tours.AddRange(sol2.Tours);
     70
     71      IList<PotvinEncoding> solutions = new List<PotvinEncoding>();
     72      // TODO: add local search
     73
     74      IList<IItem> selection = new List<IItem>();
     75      if (solutions.Count > 0) {
     76        int noSol = (int)(solutions.Count * n.Value);
     77        if (noSol <= 0) noSol++;
     78        double stepSize = (double)solutions.Count / (double)noSol;
     79        for (int i = 0; i < noSol; i++)
     80          selection.Add(solutions.ElementAt((int)((i + 1) * stepSize - stepSize * 0.5)));
     81      }
     82
     83      return new ItemArray<IItem>(selection);
     84    }
     85
     86    private static int MatchingCities(Tour tour1, Tour tour2) {
     87      return tour1.Cities.Intersect(tour2.Cities).Count();
    4888    }
    4989
Note: See TracChangeset for help on using the changeset viewer.