private int SelectCityBiasedByNeighborDistance(IRandom random, Tour tour, IVRPEncoding solution)
        {
            int cityIndex = -1;

            double sum = 0.0;

            double[] probabilities = new double[tour.Stops.Count];
            for (int i = 0; i < tour.Stops.Count; i++)
            {
                int next;
                if (i + 1 >= tour.Stops.Count)
                {
                    next = 0;
                }
                else
                {
                    next = tour.Stops[i + 1];
                }
                double distance = ProblemInstance.GetDistance(
                    tour.Stops[i], next, solution);

                int prev;
                if (i - 1 < 0)
                {
                    prev = 0;
                }
                else
                {
                    prev = tour.Stops[i - 1];
                }
                distance += ProblemInstance.GetDistance(
                    tour.Stops[i], prev, solution);

                probabilities[i] = distance;
                sum += probabilities[i];
            }

            double rand = random.NextDouble() * sum;
            double cumulatedProbabilities = 0.0;
            int    index = 0;

            while (cityIndex == -1 && index < probabilities.Length)
            {
                if (cumulatedProbabilities <= rand && rand <= cumulatedProbabilities + probabilities[index])
                {
                    cityIndex = index;
                }

                cumulatedProbabilities += probabilities[index];
                index++;
            }

            return(cityIndex);
        }
        protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2)
        {
            parent1 = parent1.Clone() as ZhuEncoding;
            parent2 = parent2.Clone() as ZhuEncoding;

            ZhuEncoding child = parent2.Clone() as ZhuEncoding;

            if (parent1.Length != parent2.Length)
            {
                return(child);
            }

            int breakPoint  = random.Next(child.Length);
            int i           = breakPoint;
            int predecessor = breakPoint - 1;

            if (predecessor < 0)
            {
                predecessor = predecessor + child.Length;
            }

            while (i != predecessor)
            {
                if (i == breakPoint)
                {
                    child[i] = parent1[i];
                    Swap(parent2, parent2[i], parent1[i]);
                }

                if (ProblemInstance.GetDistance(
                        child[i] + 1, parent1[(i + 1) % child.Length] + 1, child)
                    <
                    ProblemInstance.GetDistance(
                        child[i] + 1, parent2[(i + 1) % child.Length] + 1, child))
                {
                    child[(i + 1) % child.Length] = parent1[(i + 1) % child.Length];
                    Swap(parent2, parent2[(i + 1) % child.Length], parent1[(i + 1) % child.Length]);
                }
                else
                {
                    child[(i + 1) % child.Length] = parent2[(i + 1) % child.Length];
                    Swap(parent1, parent1[(i + 1) % child.Length], parent2[(i + 1) % child.Length]);
                }

                i = (i + 1) % child.Length;
            }

            return(child);
        }
Example #3
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);
        }
Example #4
0
        protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2)
        {
            List <int> p1 = new List <int>(parent1);
            List <int> p2 = new List <int>(parent2);

            ZhuEncoding child = parent2.Clone() as ZhuEncoding;

            if (parent1.Length != parent2.Length)
            {
                return(child);
            }

            int breakPoint  = random.Next(child.Length);
            int i           = breakPoint;
            int predecessor = breakPoint - 1;

            if (predecessor < 0)
            {
                predecessor = predecessor + child.Length;
            }

            int parent1Index = i;
            int parent2Index = i;

            while (i != predecessor)
            {
                if (i == breakPoint)
                {
                    child[i] = p1[parent1Index];

                    p1.Remove(child[i]);
                    if (parent1Index >= p1.Count)
                    {
                        parent1Index = 0;
                    }

                    p2.Remove(child[i]);
                    if (parent2Index >= p2.Count)
                    {
                        parent2Index = 0;
                    }
                }

                if (ProblemInstance.GetDistance(
                        child[i] + 1, p1[parent1Index] + 1, child)
                    <
                    ProblemInstance.GetDistance(
                        child[i] + 1, p2[parent2Index] + 1, child))
                {
                    child[(i + 1) % child.Length] = p1[parent1Index];
                }
                else
                {
                    child[(i + 1) % child.Length] = p2[parent2Index];
                }

                p1.Remove(child[(i + 1) % child.Length]);
                if (parent1Index >= p1.Count)
                {
                    parent1Index = 0;
                }

                p2.Remove(child[(i + 1) % child.Length]);
                if (parent2Index >= p2.Count)
                {
                    parent2Index = 0;
                }

                i = (i + 1) % child.Length;
            }

            return(child);
        }