Exemple #1
0
        public static newExampleObject loadnewExampleString(string problem)
        {
            #region
            string line;
            //local variables
            int n_depots    = 0;
            int n_cap       = 1;
            int n_visits    = 1;
            int n_locations = 1;
            int n_vehicles  = 1;
            int capacity    = 1;

            List <int>   demands       = new List <int>();
            List <Point> locations     = new List <Point>();
            List <int>   durations     = new List <int>();
            List <int>   TimeWindows   = new List <int>();
            List <int>   TimeAvailable = new List <int>();


            List <int>    LocationList        = new List <int>();
            List <double> DeliverDemandList   = new List <double>();
            List <int>    ServiceDurationList = new List <int>();

            List <int>    ServiceBeginList = new List <int>();
            List <int>    ServiceEndList   = new List <int>();
            List <string> VehicleNameList  = new List <string>();

            List <double> VehicleCapacityList = new List <double>();
            List <int>    X_CoordinateList    = new List <int>();
            List <int>    Y_CoordinateList    = new List <int>();
            double[,] DistanceMatrix  = new Double[4, 4];
            double[,] DriveTimeMatrix = new Double[4, 4];
            #endregion

            {
                try
                {
                    String[] linesOfFile = { };
                    try
                    {
                        linesOfFile = problem.Split('\n');
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("EXCEPTION: " + e.Message);
                    }


                    String[] words;
                    String[] separators = { ":" };

                    //  while ((line = file.ReadLine()) != null)
                    for (int j = 0; j < linesOfFile.Count(); j++)
                    {
                        line = linesOfFile[j];

                        words = line.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                        if (line.Contains("NUM_DEPOTS"))
                        {
                            n_depots = Int32.Parse(words[1]);
                        }
                        if (line.Contains("NUM_CAPACITIES"))
                        {
                            n_cap = Int32.Parse(words[1]);
                        }
                        if (line.Contains("NUM_VISITS"))
                        {
                            n_visits = Int32.Parse(words[1]);
                        }
                        if (line.Contains("NUM_LOCATIONS"))
                        {
                            n_locations = Int32.Parse(words[1]);
                        }
                        if (line.Contains("NUM_VEHICLES"))
                        {
                            n_vehicles = Int32.Parse(words[1]);
                        }

                        if (line.Contains("CAPACITIES"))
                        {
                            capacity = Int32.Parse(words[1]);
                        }

                        if (line.Contains("DEMAND_SECTION"))
                        {
                            for (int i = n_depots; i < n_locations; i++)
                            {
                                //  line = file.ReadLine();
                                j++;
                                line = linesOfFile[j];
                                string[] Words = line.Split(' ');
                                demands.Add(int.Parse(Words[Words.Count() - 1]));
                            }
                        }
                        if (line.Contains("LOCATION_COORD_SECTION"))
                        {
                            for (int i = 0; i < n_locations; i++)
                            {
                                //    line = file.ReadLine();
                                j++;
                                line = linesOfFile[j];
                                string[] Words = line.Split(' ');
                                Point    p     = new Point(int.Parse(Words[3]), int.Parse(Words[4]));
                                locations.Add(p);
                            }
                        }
                        if (line.Contains("DURATION_SECTION"))
                        {
                            for (int i = 0; i < n_depots; i++)
                            {
                                //     line = file.ReadLine();
                                j++;
                                line = linesOfFile[j];
                                string[] Words = line.Split(' ');
                                durations.Add(int.Parse(Words[Words.Count() - 1]));
                            }
                        }
                        if (line.Contains("DEPOT_TIME_WINDOW_SECTION")) //depot
                        {
                            for (int i = 0; i < n_depots; i++)
                            {
                                //   line = file.ReadLine();
                                j++;
                                line = linesOfFile[j];
                                string[] Words = line.Split(' ');
                                TimeWindows.Add(int.Parse(Words[Words.Count() - 1]));///
                            }
                        }
                        if (line.Contains("TIME_AVAIL_SECTION")) //klienci
                        {
                            for (int i = 0; i < demands.Count(); i++)
                            {
                                //  line = file.ReadLine();
                                j++;
                                line = linesOfFile[j];
                                string[] Words = line.Split(' ');
                                TimeAvailable.Add(int.Parse(Words[Words.Count() - 1]));
                            }
                        }
                    } //end of reading

                    for (int i = 0; i < n_locations; i++)
                    {
                        LocationList.Add(i);
                        X_CoordinateList.Add((int)locations[i].X);
                        Y_CoordinateList.Add((int)locations[i].Y);
                    }
                    for (int i = 0; i < n_vehicles; i++)
                    {
                        VehicleNameList.Add((i + 1).ToString());
                        VehicleCapacityList.Add(100);
                    }
                    for (int i = 0; i < demands.Count + 1; i++)
                    {
                        ServiceDurationList.Add(20);
                        ServiceEndList.Add(TimeWindows[0]);
                        // ServiceBeginList.Add(TimeAvailable[i]);
                        if (i + 1 < demands.Count + 1)
                        {
                            DeliverDemandList.Add(-Convert.ToDouble(demands[i], CultureInfo.InvariantCulture));
                        }
                    }


                    for (int i = 0; i < demands.Count + 1; i++)
                    {
                        if (i == 0)
                        {
                            ServiceBeginList.Add(0);
                        }
                        else
                        {
                            ServiceBeginList.Add(TimeAvailable[i - 1]);
                        }
                    }

                    for (int i = 0; i < demands.Count + 1; i++)
                    {
                        int          ServiceEndDepot = TimeWindows[0];
                        const double cutoff          = 0.5;
                        if (Convert.ToInt32(ServiceBeginList[i]) > ServiceEndDepot * cutoff)
                        {
                            ServiceBeginList[i] = 0;
                        }
                    }
                    int[] Location = LocationList.ToArray();
                    DistanceMatrix  = new double[Location.GetLength(0), Location.GetLength(0)];
                    DriveTimeMatrix = new double[Location.GetLength(0), Location.GetLength(0)];
                    int[] X_Coordinate = X_CoordinateList.ToArray();
                    int[] Y_Coordinate = Y_CoordinateList.ToArray();
                    for (int i = 0; i < X_Coordinate.Length; i++)
                    {
                        for (int j = i; j < X_Coordinate.Length; j++)
                        {
                            if (i == j)
                            {
                                DistanceMatrix[i, j]  = 0;
                                DriveTimeMatrix[i, j] = 0;
                            }
                            else
                            {
                                DistanceMatrix[i, j]  = Math.Sqrt((X_Coordinate[i] - X_Coordinate[j]) * (X_Coordinate[i] - X_Coordinate[j]) + (Y_Coordinate[i] - Y_Coordinate[j]) * (Y_Coordinate[i] - Y_Coordinate[j]));
                                DriveTimeMatrix[i, j] = DistanceMatrix[i, j];
                                DistanceMatrix[j, i]  = DistanceMatrix[i, j];
                                DriveTimeMatrix[j, i] = DriveTimeMatrix[i, j];
                            }
                        }
                    }
                }

                catch (Exception a)
                {
                }
            }

            newExampleObject o = new newExampleObject(LocationList.ToArray(), DeliverDemandList.ToArray(), ServiceDurationList.ToArray(),
                                                      ServiceBeginList.ToArray(), ServiceEndList.ToArray(), VehicleNameList.ToArray(), VehicleCapacityList.ToArray(), X_CoordinateList.ToArray(), Y_CoordinateList.ToArray(), DistanceMatrix, DriveTimeMatrix);
            return(o);
        }
Exemple #2
0
    static void Main(string[] args)
    {
        try
        {
            List <int>    LocationList        = new List <int>();
            List <double> DeliverDemandList   = new List <double>();
            List <int>    ServiceDurationList = new List <int>();
            List <int>    ServiceBeginList    = new List <int>();
            List <int>    ServiceEndList      = new List <int>();
            List <string> VehicleNameList     = new List <string>();
            List <double> VehicleCapacityList = new List <double>();
            List <int>    X_CoordinateList    = new List <int>();
            List <int>    Y_CoordinateList    = new List <int>();

            StreamReader InputFile = new System.IO.StreamReader(Directory.GetCurrentDirectory() + @"\d.txt");

            using (InputFile)
            {
                string InputLine = InputFile.ReadLine();
                InputLine = InputFile.ReadLine();

                string[] InputData = InputLine.Split(' ');
                LocationList.Add(Convert.ToInt32(InputData[0]));

                InputLine = InputFile.ReadLine();
                InputLine = InputFile.ReadLine();

                InputData = InputLine.Split(' ');
                for (int i = 0; i < InputData.Length; i++)
                {
                    if (i % 2 == 0)
                    {
                        LocationList.Add(Convert.ToInt32(InputData[i]));
                        VehicleNameList.Add(InputData[i]);
                    }
                    else
                    {
                        DeliverDemandList.Add(-Convert.ToDouble(InputData[i], CultureInfo.InvariantCulture));
                    }
                }


                InputLine = InputFile.ReadLine();
                InputLine = InputFile.ReadLine();

                InputData = InputLine.Split(' ');
                for (int i = 0; i < InputData.Length; i++)
                {
                    if (i % 3 == 0)
                    {
                    }
                    else if (i % 3 == 1)
                    {
                        X_CoordinateList.Add(Convert.ToInt32(InputData[i]));
                    }
                    else
                    {
                        Y_CoordinateList.Add(Convert.ToInt32(InputData[i]));
                    }
                }

                InputLine = InputFile.ReadLine();
                InputLine = InputFile.ReadLine();

                InputData = InputLine.Split(' ');
                for (int i = 0; i < InputData.Length; i++)
                {
                    if (i % 2 != 0)
                    {
                        ServiceDurationList.Add(Convert.ToInt32(InputData[i]));
                    }
                }

                InputLine = InputFile.ReadLine();
                InputLine = InputFile.ReadLine();

                InputData = InputLine.Split(' ');
                ServiceBeginList.Add(Convert.ToInt32(InputData[1]));
                ServiceEndList.Add(Convert.ToInt32(InputData[2]));
                int ServiceEndDepot = Convert.ToInt32(InputData[2]);
                InputLine = InputFile.ReadLine();
                InputLine = InputFile.ReadLine();

                InputData = InputLine.Split(' ');
                for (int i = 0; i < InputData.Length; i++)
                {
                    if (i % 2 != 0)
                    {
                        const double cutoff = 0.5;
                        if (Convert.ToInt32(InputData[i]) > ServiceEndDepot * cutoff)
                        {
                            ServiceBeginList.Add(0);
                        }
                        else
                        {
                            ServiceBeginList.Add(Convert.ToInt32(InputData[i]));
                        }
                        ServiceEndList.Add(ServiceEndDepot);
                        const double AllVehicleCapacity = 100;
                        VehicleCapacityList.Add(AllVehicleCapacity);
                    }
                }
            }

            int[]            Location        = LocationList.ToArray();        //ilosc lokacji, po prostu id
            double[]         DeliverDemand   = DeliverDemandList.ToArray();   //zawsze dodatnie demand
            int[]            ServiceDuration = ServiceDurationList.ToArray(); //stala 20
            int[]            ServiceBegin    = ServiceBeginList.ToArray();
            int[]            ServiceEnd      = ServiceEndList.ToArray();      //available
            string[]         VehicleName     = VehicleNameList.ToArray();     //depot end
            double[]         VehicleCapacity = VehicleCapacityList.ToArray(); //stala 100
            int[]            X_Coordinate    = X_CoordinateList.ToArray();    //wszystko
            int[]            Y_Coordinate    = Y_CoordinateList.ToArray();    //wszystko
            newExampleObject o = ProblemLoader.loadnewExample("problem16.vrp");

            //int[] Location = o.Location;
            //double[] DeliverDemand = o.DeliverDemand;
            //int[] ServiceDuration = o.ServiceDuration;
            //int[] ServiceBegin = o.ServiceBegin;
            //int[] ServiceEnd = o.ServiceEnd;
            //string[] VehicleName = o.VehicleName;
            //double[] VehicleCapacity = o.VehicleCapacity;
            //int[] X_Coordinate = o.X_Coordinate;
            //int[] Y_Coordinate = o.Y_Coordinate;


            Location        = o.Location;
            DeliverDemand   = o.DeliverDemand;
            ServiceDuration = o.ServiceDuration;
            ServiceBegin    = o.ServiceBegin;
            ServiceEnd      = o.ServiceEnd;
            VehicleName     = o.VehicleName;
            VehicleCapacity = o.VehicleCapacity;
            X_Coordinate    = o.X_Coordinate;
            Y_Coordinate    = o.Y_Coordinate;



            //double[,] DistanceMatrix = new double[Location.GetLength(0), Location.GetLength(0)];
            //double[,] DriveTimeMatrix = new double[Location.GetLength(0), Location.GetLength(0)];

            //for(int i=0;i<X_Coordinate.Length;i++)
            //{
            //    for(int j=i;j<X_Coordinate.Length;j++)
            //    {
            //        if(i==j)
            //        {
            //            DistanceMatrix[i,j] = 0;
            //            DriveTimeMatrix[i,j] = 0;

            //        }
            //        else
            //        {
            //            DistanceMatrix[i,j]=Math.Sqrt((X_Coordinate[i] - X_Coordinate[j])*(X_Coordinate[i] - X_Coordinate[j]) + (Y_Coordinate[i] - Y_Coordinate[j])*(Y_Coordinate[i] - Y_Coordinate[j]));
            //            DriveTimeMatrix[i,j] = DistanceMatrix[i,j];
            //            DistanceMatrix[j,i] = DistanceMatrix[i,j];
            //            DriveTimeMatrix[j,i] = DriveTimeMatrix[i,j];
            //        }

            //    }
            //}

            double[,] DistanceMatrix  = o.DistanceMatrix;
            double[,] DriveTimeMatrix = o.DriveTimeMatrix;


            Console.WriteLine("\n" + "Input data files have been successfully read.");

            AlgorithmSolution.Find_Solution(Location, DeliverDemand, ServiceBegin, ServiceEnd, ServiceDuration, DistanceMatrix, DriveTimeMatrix, VehicleName, VehicleCapacity);
        }

        catch (FormatException)
        {
            Console.WriteLine("\n" + "Sorry, You have not entered valid integer number.");
        }

        catch (FileNotFoundException FNFE)
        {
            Console.WriteLine(FNFE.Message);
        }

        catch (IOException IOE)
        {
            Console.WriteLine(IOE.Message);
        }

        Console.ReadKey(true);
        Console.ReadKey();
    }