protected override void Manipulate(IRandom random, GVREncoding individual) {
      int customer = random.Next(1, individual.Cities + 1);
      Tour tour;
      int position;
      individual.FindCustomer(customer, out tour, out position);

      tour.Stops.RemoveAt(position);

      //with a probability of 1/(2*V) create a new tour, else insert at another position
      if (individual.GetTours().Count > 0 &&
        individual.GetTours().Count < ProblemInstance.Vehicles.Value &&
        random.Next(individual.GetTours().Count * 2) == 0) {
        Tour newTour = new Tour();
        newTour.Stops.Add(customer);

        individual.Tours.Add(newTour);
      } else {
        Tour newTour = individual.Tours[random.Next(individual.Tours.Count)];
        int newPosition = random.Next(newTour.Stops.Count + 1);

        newTour.Stops.Insert(newPosition, customer);
      }

      if (tour.Stops.Count == 0)
        individual.Tours.Remove(tour);
    }
    protected override void Manipulate(IRandom random, GVREncoding individual) {
      Tour tour = individual.Tours[random.Next(individual.Tours.Count)];
      int breakPoint1 = random.Next(tour.Stops.Count);
      int length = random.Next(1, tour.Stops.Count - breakPoint1 + 1);

      List<int> displaced = tour.Stops.GetRange(breakPoint1, length);
      tour.Stops.RemoveRange(breakPoint1, length);
      //with a probability of 1/(2*V) create a new tour, else insert at another position
      if (individual.GetTours().Count > 0 &&
        individual.GetTours().Count < ProblemInstance.Vehicles.Value &&
        random.Next(individual.GetTours().Count * 2) == 0) {
        Tour newTour = new Tour();
        newTour.Stops.InsertRange(0, displaced);

        individual.Tours.Add(newTour);
      } else {
        Tour newTour = individual.Tours[random.Next(individual.Tours.Count)];
        int newPosition = newTour.Stops.Count;

        newTour.Stops.InsertRange(newPosition, displaced);
      }

      if (tour.Stops.Count == 0)
        individual.Tours.Remove(tour);
    }
    protected override void Manipulate(IRandom random, GVREncoding individual) {
      Tour tour = individual.Tours[random.Next(individual.Tours.Count)];
      int breakPoint1 = random.Next(tour.Stops.Count);
      int length = random.Next(1, tour.Stops.Count - breakPoint1 + 1);

      tour.Stops.Reverse(breakPoint1, length);
    }
    private GVREncoding Crossover(IRandom random, GVREncoding parent1, GVREncoding parent2) {
      GVREncoding child = parent1.Clone() as GVREncoding;

      Tour tour = parent2.Tours[random.Next(parent2.Tours.Count)];
      int breakPoint1 = random.Next(tour.Stops.Count);
      int length = random.Next(1, tour.Stops.Count - breakPoint1 + 1);
      List<int> subroute = tour.Stops.GetRange(breakPoint1, length);

      //remove duplicates
      List<Tour> toBeRemoved = new List<Tour>();

      foreach (Tour route in child.Tours) {
        foreach (int city in subroute) {
          route.Stops.Remove(city);
        }

        if (route.Stops.Count == 0)
          toBeRemoved.Add(route);
      }
      foreach (Tour route in toBeRemoved) {
        child.Tours.Remove(route);
      }

      //choose nearest customer
      double minDistance = -1;
      int customer = -1;
      for (int i = 1; i <= ProblemInstance.Cities.Value; i++) {
        if (!subroute.Contains(i)) {
          double distance = ProblemInstance.GetDistance(subroute[0], i, child);

          if (customer == -1 || distance < minDistance) {
            customer = i;
            minDistance = distance;
          }
        }
      }

      //insert
      if (customer != -1) {
        Tour newTour;
        int newPosition;
        child.FindCustomer(customer, out newTour, out newPosition);
        newTour.Stops.InsertRange(newPosition + 1, subroute);
      } else {
        //special case -> only one tour, whole tour has been chosen as subroute
        child = parent1.Clone() as GVREncoding;
      }

      return child;
    }
    protected override void Manipulate(IRandom random, GVREncoding individual) {
      int customer1 = random.Next(1, individual.Cities + 1);
      int customer2 = random.Next(1, individual.Cities + 1);

      Tour tour1;
      int pos1;
      individual.FindCustomer(customer1, out tour1, out pos1);

      Tour tour2;
      int pos2;
      individual.FindCustomer(customer2, out tour2, out pos2);

      int temp = tour1.Stops[pos1];
      tour1.Stops[pos1] = tour2.Stops[pos2];
      tour2.Stops[pos2] = temp;
    }
Beispiel #6
0
    public static GVREncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance problemInstance) {
      GVREncoding solution = new GVREncoding(problemInstance);

      TourEncoding.ConvertFrom(encoding, solution, problemInstance);

      return solution;
    }
Beispiel #7
0
 protected GVREncoding(GVREncoding original, Cloner cloner)
   : base(original, cloner) {
 }
Beispiel #8
0
    public static GVREncoding ConvertFrom(List<int> route, IVRPProblemInstance problemInstance) {
      GVREncoding solution = new GVREncoding(problemInstance);

      TourEncoding.ConvertFrom(route, solution);

      return solution;
    }
Beispiel #9
0
 protected abstract void Manipulate(IRandom random, GVREncoding individual);
Beispiel #10
0
        private GVREncoding Crossover(IRandom random, GVREncoding parent1, GVREncoding parent2)
        {
            GVREncoding child = parent1.Clone() as GVREncoding;

            Tour       tour        = parent2.Tours[random.Next(parent2.Tours.Count)];
            int        breakPoint1 = random.Next(tour.Stops.Count);
            int        length      = random.Next(1, tour.Stops.Count - breakPoint1 + 1);
            List <int> subroute    = tour.Stops.GetRange(breakPoint1, length);

            //remove duplicates
            List <Tour> toBeRemoved = new List <Tour>();

            foreach (Tour route in child.Tours)
            {
                foreach (int city in subroute)
                {
                    route.Stops.Remove(city);
                }

                if (route.Stops.Count == 0)
                {
                    toBeRemoved.Add(route);
                }
            }
            foreach (Tour route in toBeRemoved)
            {
                child.Tours.Remove(route);
            }

            //choose nearest customer
            double minDistance = -1;
            int    customer    = -1;

            for (int i = 1; i <= ProblemInstance.Cities.Value; i++)
            {
                if (!subroute.Contains(i))
                {
                    double distance = ProblemInstance.GetDistance(subroute[0], i, child);

                    if (customer == -1 || distance < minDistance)
                    {
                        customer    = i;
                        minDistance = distance;
                    }
                }
            }

            //insert
            if (customer != -1)
            {
                Tour newTour;
                int  newPosition;
                child.FindCustomer(customer, out newTour, out newPosition);
                newTour.Stops.InsertRange(newPosition + 1, subroute);
            }
            else
            {
                //special case -> only one tour, whole tour has been chosen as subroute
                child = parent1.Clone() as GVREncoding;
            }

            return(child);
        }
Beispiel #11
0
 protected abstract void Manipulate(IRandom random, GVREncoding individual);