示例#1
0
        public int FindBestInsertionPlace(Tour tour, int city, int positionToAvoid = -1)
        {
            if (tour.Stops.Count == 0)
            {
                return(0);
            }

            int    place      = -1;
            double minQuality = -1;

            VRPEvaluation eval = ProblemInstance.EvaluateTour(tour, this);

            for (int i = 0; i <= tour.Stops.Count; i++)
            {
                if (positionToAvoid != i)
                {
                    bool   feasible;
                    double quality = ProblemInstance.GetInsertionCosts(eval, this, city, 0, i, out feasible);
                    if (place < 0 || quality < minQuality)
                    {
                        place      = i;
                        minQuality = quality;
                    }
                }
            }

            if (place == -1)
            {
                place = 0;
            }

            return(place);
        }
        private bool FindRouteInsertionPlace(
            PotvinEncoding individual,
            Tour tour,
            int city, bool allowInfeasible, out int place)
        {
            place = -1;

            if (tour.Stops.Contains(city))
            {
                return(false);
            }

            if (tour.Stops.Count == 0)
            {
                place = 0;
                return(true);
            }

            double        minDetour        = 0;
            VRPEvaluation eval             = ProblemInstance.EvaluateTour(tour, individual);
            bool          originalFeasible = ProblemInstance.Feasible(eval);

            for (int i = 0; i <= tour.Stops.Count; i++)
            {
                bool   feasible;
                double detour = ProblemInstance.GetInsertionCosts(eval, individual, city, 0, i, out feasible);
                if (feasible || allowInfeasible)
                {
                    if (place < 0 || detour < minDetour)
                    {
                        place     = i;
                        minDetour = detour;
                    }
                }
            }

            return(place >= 0);
        }
示例#3
0
        public override List <Tour> GetTours()
        {
            List <Tour> result = new List <Tour>();

            int cities = ProblemInstance.Cities.Value;

            //Split permutation into vector P
            int[] P = new int[cities + 1];
            for (int i = 0; i <= cities; i++)
            {
                P[i] = -1;
            }

            double[] V = new double[cities + 1];
            V[0] = 0;
            for (int i = 1; i <= cities; i++)
            {
                V[i] = double.MaxValue;
            }

            for (int i = 1; i <= cities; i++)
            {
                int  j        = i;
                Tour tour     = new Tour();
                bool feasible = true;

                do
                {
                    tour.Stops.Add(this[j - 1] + 1);

                    VRPEvaluation eval =
                        ProblemInstance.EvaluateTour(tour, this);

                    double cost = eval.Quality;
                    feasible = ProblemInstance.Feasible(eval);

                    if (feasible || j == i)
                    {
                        if (V[i - 1] + cost < V[j])
                        {
                            V[j] = V[i - 1] + cost;
                            P[j] = i - 1;
                        }
                        j++;
                    }
                } while (j <= cities && feasible);
            }

            //extract VRP solution from vector P
            int  index  = 0;
            int  index2 = cities;
            Tour trip   = null;

            do
            {
                index = P[index2];
                trip  = new Tour();

                for (int k = index + 1; k <= index2; k++)
                {
                    trip.Stops.Add(this[k - 1] + 1);
                }

                if (trip.Stops.Count > 0)
                {
                    result.Add(trip);
                }

                index2 = index;
            } while (index != 0);

            //if there are too many vehicles - repair
            while (result.Count > ProblemInstance.Vehicles.Value)
            {
                Tour tour = result[result.Count - 1];

                //find predecessor / successor in permutation
                int predecessorIndex = Array.IndexOf(this.array, tour.Stops[0] - 1) - 1;
                if (predecessorIndex >= 0)
                {
                    int predecessor = this[predecessorIndex] + 1;

                    foreach (Tour t in result)
                    {
                        int insertPosition = t.Stops.IndexOf(predecessor) + 1;
                        if (insertPosition != -1)
                        {
                            t.Stops.InsertRange(insertPosition, tour.Stops);
                            break;
                        }
                    }
                }
                else
                {
                    int successorIndex = Array.IndexOf(this.array,
                                                       tour.Stops[tour.Stops.Count - 1] - 1) + 1;
                    int successor = this[successorIndex] + 1;

                    foreach (Tour t in result)
                    {
                        int insertPosition = t.Stops.IndexOf(successor);
                        if (insertPosition != -1)
                        {
                            t.Stops.InsertRange(insertPosition, tour.Stops);
                            break;
                        }
                    }
                }

                result.Remove(tour);
            }

            return(result);
        }