Пример #1
0
        public static void TSPTest(VRPParser benchmark) 
        {
            int k = 1;
            Point[] points = new Point[benchmark.Num_Locations];

            for (int i = 0; i <= benchmark.Num_Visits; i++)
            {
                List<double> point_coords = new List<double>();

                point_coords.Add(benchmark.Location_Coord[i][0]);
                point_coords.Add(benchmark.Location_Coord[i][1]);

                if (i == 0) point_coords.Add(0);
                else point_coords.Add(benchmark.Time_Avail[i - 1] + benchmark.Duration[i - 1]);

                points[i] = new Point(point_coords);
            }

            var watch = Stopwatch.StartNew();
           // int[] route = TSPTrianIneq.calculate(points);
            watch.Stop();

            var elapsedMs = watch.ElapsedMilliseconds;
            Console.Write("");
        }
Пример #2
0
        public static VRPParser Test1()
        {
            VRPParser parser = new VRPParser();

            parser.Name           = "io2_8a";
            parser.Num_Depots     = 1;
            parser.Num_Capacities = 1;
            parser.Num_Visits     = 8;
            parser.Num_Locations  = 9;
            parser.Num_Vehicles   = 8;
            parser.Capacites      = 100;

            int[] depots = { 0 };
            parser.Depots_IDs = depots;

            int[] demands = { -23, -26, -26, -29, -13, -17, -36, -25 };
            parser.Demands = demands;

            int[][] location_coords = new int[9][]
            {
                new int[] { 0, 0 },
                new int[] { 5, 61 },
                new int[] { 74, 34 },
                new int[] { -9, 92 },
                new int[] { -8, 84 },
                new int[] { -54, 20 },
                new int[] { -33, 72 },
                new int[] { -94, -79 },
                new int[] { 71, 68 }
            };
            parser.Location_Coord = location_coords;

            int[] depot_location = { 0 };
            parser.Depot_Location = depot_location;

            int[] visit_location = { 1, 2, 3, 4, 5, 6, 7, 8 };
            parser.Visit_Location = visit_location;

            int[] duration = { 20, 20, 20, 20, 20, 20, 20, 20 };
            parser.Duration = duration;

            int[][] depot_time = new int[1][]
            {
                new int[] { 0, 560 }
            };
            parser.Depot_Time_Window = depot_time;

            int[] time_avail = { 334, 318, 53, 292, 82, 157, 301, 128 };
            parser.Time_Avail = time_avail;

            return(parser);
        }
Пример #3
0
        public static VRPParser Test1()
        {
            VRPParser parser = new VRPParser();

            parser.Name = "io2_8a";
            parser.Num_Depots = 1;
            parser.Num_Capacities = 1;
            parser.Num_Visits = 8;
            parser.Num_Locations = 9;
            parser.Num_Vehicles = 8;
            parser.Capacites = 100;

            int[] depots = {0};
            parser.Depots_IDs = depots;

            int[] demands = {-23, -26, -26, -29, -13, -17, -36, -25};
            parser.Demands = demands;

            int[][] location_coords = new int[9][]
            {
                new int[] {0,0},
                new int[] {5,61}, 
                new int[] {74,34}, 
                new int[] {-9,92}, 
                new int[] {-8,84}, 
                new int[] {-54,20}, 
                new int[] {-33,72}, 
                new int[] {-94,-79}, 
                new int[] {71,68}
            };
            parser.Location_Coord = location_coords;

            int[] depot_location = { 0 };
            parser.Depot_Location = depot_location;

            int[] visit_location = {1,2,3,4,5,6,7,8};
            parser.Visit_Location = visit_location;

            int[] duration = {20,20,20,20,20,20,20,20};
            parser.Duration = duration;

            int[][] depot_time = new int[1][]
            {
                new int[] {0, 560}
            };
            parser.Depot_Time_Window = depot_time;

            int[] time_avail = { 334, 318, 53, 292, 82, 157, 301, 128 };
            parser.Time_Avail = time_avail;

            return parser;
        }
Пример #4
0
        public static Result calculate(VRPParser benchmark, float cutOffTime)
        {
            int num_depots = benchmark.Num_Depots;
            List<int> nextDay = new List<int>();

            /* Convert benchmark into points. */
            List<Point> points = new List<Point>();
            for (int j = 0; j < benchmark.Num_Locations; j++)
            {
                int time;
                if (j - 1 < 0) time = 0;
                else time = benchmark.Time_Avail[j - 1];
                
                points.Add(new Point(benchmark.Location_Coord[j][0],benchmark.Location_Coord[j][1], time));
            }

            /* Get rid of the points which are appearing after cut off */
            List<Point> tmp = new List<Point>();
            for (int i = 0; i < points.Count; i++ )
            {
                if (points[i].Z < cutOffTime)
                {
                    tmp.Add(points[i]);

                }
                else nextDay.Add(i); 
            }
            points = tmp;
           

            /* Convert points into a graph. */
            Graph graph = new Graph(points.Count);
            for (int i = 0; i < points.Count; i++)
            {
                for (int j = i + 1; j < points.Count; j++)
                {
                    graph.addEdge(i, j, euclidDistance(points[i], points[j]), distance2D(points[i], points[j]));
                }
            }
            PrimAlgorithm.calculate(graph, 0);
            int[] route = preorderWalk(graph, 0);
            Result result = new Result(route, nieUmiemCSharpaWiecJestTaZmienna, nextDay);

            return result;
        }
Пример #5
0
        public static SolveRequestMessage loadDataFromDisc(String filePath)
        {
            SolveRequestMessage solveRequestMessage;
            StreamReader streamReader = new StreamReader(filePath);
            string text = streamReader.ReadToEnd();
            VRPParser benchmark = new VRPParser(text);

            string problemType="";
            byte[] data;
            streamReader.Close();

            String extension = Path.GetExtension(filePath);

            if (extension == ".vrp")
            {
                problemType = "DVRP";
            }
            else
            {
                Console.WriteLine(">> Unsupported problem type. Please load a problem with one of the following problem types: \n *DVRP");
                return null;
            }

            data = DataSerialization.ObjectToByteArray(benchmark);
        //    data = GetBytes(filePath);
            solveRequestMessage = new SolveRequestMessage(problemType, data);
            Console.WriteLine(" >> Success");
            return solveRequestMessage;
        }
Пример #6
0
 public static VRPParser getBenchmark(String filePath)
 {
     StreamReader streamReader = new StreamReader(filePath);
     string text = streamReader.ReadToEnd();
     VRPParser benchmark = new VRPParser(text);
     return benchmark;
 }
Пример #7
0
        public static void FullSolveTest(VRPParser benchmark) 
        {
            /******************* DIVIDE *************************/


            // Combine coords (x, y) and time_avail (z)
            List<Point> data = new List<Point>();
            for (int i = 0; i < benchmark.Num_Visits; i++)
            {
                List<double> point_coords = new List<double>();

                // does not include depots - which is what we want
                int loc_index = benchmark.Visit_Location[i];

                point_coords.Add(benchmark.Location_Coord[loc_index][0]);
                point_coords.Add(benchmark.Location_Coord[loc_index][1]);
                point_coords.Add(benchmark.Time_Avail[loc_index - 1] + benchmark.Duration[loc_index - 1]);

                data.Add(new Point(point_coords));
            }

            // get optimal number of clusters
            PredictionStrength ps = new PredictionStrength(data);
            ps.Compute(true);
            int k = ps.BestK;

            // compute clusters
            KMeans clusters = new KMeans(data, k);
            clusters.Compute();

            // create k benchmarks for k solvers
            VRPParser[] partial_benchmarks = new VRPParser[k];
            for (int i = 0; i < k; i++) 
            {
                VRPParser partial_benchmark = new VRPParser();
                List<int> cluster_indecies = clusters.GetCluterIndecies(i);

                /************ COMMON ****************/
                int num_depots = benchmark.Num_Depots;
                int num_visits = cluster_indecies.Count;
                int num_locations = cluster_indecies.Count + num_depots;

                partial_benchmark.Num_Visits = num_visits;
                partial_benchmark.Num_Depots = num_depots;
                partial_benchmark.Name = benchmark.Name;
                partial_benchmark.Num_Capacities = benchmark.Num_Capacities;
                partial_benchmark.Num_Vehicles = 1;
                partial_benchmark.Capacites = benchmark.Capacites;

                partial_benchmark.Depots_IDs = new int[benchmark.Depots_IDs.Length];
                benchmark.Depots_IDs.CopyTo(partial_benchmark.Depots_IDs, 0);

                partial_benchmark.Depot_Location = new int[benchmark.Depot_Location.Length];
                benchmark.Depot_Location.CopyTo(partial_benchmark.Depot_Location, 0);

                partial_benchmark.Depot_Time_Window = new int[benchmark.Depot_Time_Window.Length][];
                for (int p = 0; p < partial_benchmark.Depot_Time_Window.Length; p++) 
                {
                    partial_benchmark.Depot_Time_Window[p] = new int[benchmark.Depot_Time_Window[p].Length];
                    benchmark.Depot_Time_Window[p].CopyTo(partial_benchmark.Depot_Time_Window[p], 0);
                }

                /************ LOCATION_COORD ****************/
                partial_benchmark.Num_Locations = num_locations;

                int[][] location_coord = new int[num_locations][];
                // get all depots locations
                for (int j = 0; j < num_depots; j++)
                {
                    location_coord[j] = new int[2];
                    location_coord[j][0] = benchmark.Location_Coord[j][0];
                    location_coord[j][1] = benchmark.Location_Coord[j][1];
                }

                // get all partial clients locations
                for (int j = num_depots; j < num_locations; j++) 
                {
                    location_coord[j] = new int[2];
                    int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j - num_depots]];

                    location_coord[j][0] = benchmark.Location_Coord[clientNodeIndex][0];
                    location_coord[j][1] = benchmark.Location_Coord[clientNodeIndex][1];
                }
                partial_benchmark.Location_Coord = location_coord;

                /************ DEMAND ****************/
                int[] demands = new int[num_visits];
                for (int j = 0; j < num_visits; j++)
                {
                    int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j]];
                    demands[j] = benchmark.Demands[clientNodeIndex - num_depots];
                }
                partial_benchmark.Demands = demands;

                /************ VISIT_LOCATION ****************/
                int[] visit_location = new int[num_visits];
                for (int j = 0; j < num_visits; j++)
                {
                    int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j]] - num_depots;

                    visit_location[j] = clientNodeIndex;
                    //visit_location[j] = j + num_depots;
                }
                partial_benchmark.Visit_Location = visit_location;

                /************ DURATION ****************/
                int[] duration = new int[num_visits];
                for (int j = 0; j < num_visits; j++)
                {
                    int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j]];
                    duration[j] = benchmark.Duration[clientNodeIndex - num_depots];
                }
                partial_benchmark.Duration = duration;

                /************ TIME_AVAIL ****************/
                int[] time_avail = new int[num_visits];
                for (int j = 0; j < num_visits; j++)
                {
                    int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j]];
                    time_avail[j] = benchmark.Time_Avail[clientNodeIndex - num_depots];
                }
                partial_benchmark.Time_Avail = time_avail;

                partial_benchmarks[i] = partial_benchmark;
            }

            /******************* SOLVE *************************/
            float CUT_OFF_COEFFICIENT = 0.2f;
            List<Result> results = new List<Result>();
            List<int> nextDay = new List<int>();
            float cutOffTime = CUT_OFF_COEFFICIENT * benchmark.Depot_Time_Window[0][1];
            for (int i = 0; i < partial_benchmarks.Length; i++)
            {
                results.Add(TSPTrianIneq.calculate(partial_benchmarks[i], cutOffTime));
            }
            /******************* MERGE *************************/

            for(int j = 0; j < results.Count; j++) 
            {
                for (int i = partial_benchmarks[j].Num_Depots; i < results[j].route.Length - partial_benchmarks[j].Num_Depots; i++)
                    results[j].route[i] = partial_benchmarks[j].Visit_Location[results[j].route[i] - partial_benchmarks[j].Num_Depots] + partial_benchmarks[j].Num_Depots;
            }

            for (int j = 0; j < results.Count; j++) 
            for (int i = 0; i < results[j].nextDay.Count; i++)
            {
                results[j].nextDay[i] = partial_benchmarks[j].Visit_Location[results[j].nextDay[i] - partial_benchmarks[j].Num_Depots] + partial_benchmarks[j].Num_Depots;
            }


            Console.Write("asd");
        }
Пример #8
0
        public override byte[][] DivideProblem(int threadCount)
        {
            /****************** DESERIALIZE ************************/

            BinaryFormatter formatter = new BinaryFormatter();
            VRPParser benchmark = (VRPParser)formatter.Deserialize(new MemoryStream(_problemData));

            /******************* DIVIDE *************************/

            // Combine coords (x, y) and time_avail (z)
            List<Point> data = new List<Point>();
            for (int i = 0; i < benchmark.Num_Visits; i++)
            {
                List<double> point_coords = new List<double>();

                // does not include depots - which is what we want
                int loc_index = benchmark.Visit_Location[i];

                point_coords.Add(benchmark.Location_Coord[loc_index][0]);
                point_coords.Add(benchmark.Location_Coord[loc_index][1]);
                point_coords.Add(benchmark.Time_Avail[loc_index - 1] + benchmark.Duration[loc_index - 1]);

                data.Add(new Point(point_coords));
            }

            // get optimal number of clusters
            //PredictionStrength ps = new PredictionStrength(data);
            //ps.Compute(true);
            //int k = ps.BestK;
            
            int max_k = 5;
            int start_k = 1;

            // prepare byte array for all partial solutions
            int solutionsSize = 0;
            for (int i = start_k; i <= max_k; i++)
                solutionsSize += i;

            int temporarySolutionIndex = 0;
            byte[][] temporarySolution = new byte[solutionsSize][];

            for (int k = start_k; k <= max_k; k++)
            {
                // compute clusters
                KMeans clusters = new KMeans(data, k);
                clusters.Compute();

                // create k benchmarks for k solvers
                VRPParser[] partial_benchmarks = new VRPParser[k];
                for (int i = 0; i < k; i++)
                {
                    VRPParser partial_benchmark = new VRPParser();
                    List<int> cluster_indecies = clusters.GetCluterIndecies(i);

                    partial_benchmark.ID = k;

                    /************ COMMON ****************/
                    int num_depots = benchmark.Num_Depots;
                    int num_visits = cluster_indecies.Count;
                    int num_locations = cluster_indecies.Count + num_depots;

                    partial_benchmark.Num_Visits = num_visits;
                    partial_benchmark.Num_Depots = num_depots;
                    partial_benchmark.Name = benchmark.Name;
                    partial_benchmark.Num_Capacities = benchmark.Num_Capacities;
                    partial_benchmark.Num_Vehicles = 1;
                    partial_benchmark.Capacites = benchmark.Capacites;

                    partial_benchmark.Depots_IDs = new int[benchmark.Depots_IDs.Length];
                    benchmark.Depots_IDs.CopyTo(partial_benchmark.Depots_IDs, 0);

                    partial_benchmark.Depot_Location = new int[benchmark.Depot_Location.Length];
                    benchmark.Depot_Location.CopyTo(partial_benchmark.Depot_Location, 0);

                    partial_benchmark.Depot_Time_Window = new int[benchmark.Depot_Time_Window.Length][];
                    for (int p = 0; p < partial_benchmark.Depot_Time_Window.Length; p++)
                    {
                        partial_benchmark.Depot_Time_Window[p] = new int[benchmark.Depot_Time_Window[p].Length];
                        benchmark.Depot_Time_Window[p].CopyTo(partial_benchmark.Depot_Time_Window[p], 0);
                    }

                    /************ LOCATION_COORD ****************/
                    partial_benchmark.Num_Locations = num_locations;

                    int[][] location_coord = new int[num_locations][];
                    // get all depots locations
                    for (int j = 0; j < num_depots; j++)
                    {
                        location_coord[j] = new int[2];
                        location_coord[j][0] = benchmark.Location_Coord[j][0];
                        location_coord[j][1] = benchmark.Location_Coord[j][1];
                    }

                    // get all partial clients locations
                    for (int j = num_depots; j < num_locations; j++)
                    {
                        location_coord[j] = new int[2];
                        int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j - num_depots]];

                        location_coord[j][0] = benchmark.Location_Coord[clientNodeIndex][0];
                        location_coord[j][1] = benchmark.Location_Coord[clientNodeIndex][1];
                    }
                    partial_benchmark.Location_Coord = location_coord;

                    /************ DEMAND ****************/
                    int[] demands = new int[num_visits];
                    for (int j = 0; j < num_visits; j++)
                    {
                        int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j]];
                        demands[j] = benchmark.Demands[clientNodeIndex - num_depots];
                    }
                    partial_benchmark.Demands = demands;

                    /************ VISIT_LOCATION ****************/
                    int[] visit_location = new int[num_visits];
                    for (int j = 0; j < num_visits; j++)
                    {
                        int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j]] - num_depots;

                        visit_location[j] = clientNodeIndex;
                        //visit_location[j] = j + num_depots;
                    }
                    partial_benchmark.Visit_Location = visit_location;

                    /************ DURATION ****************/
                    int[] duration = new int[num_visits];
                    for (int j = 0; j < num_visits; j++)
                    {
                        int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j]];
                        duration[j] = benchmark.Duration[clientNodeIndex - num_depots];
                    }
                    partial_benchmark.Duration = duration;

                    /************ TIME_AVAIL ****************/
                    int[] time_avail = new int[num_visits];
                    for (int j = 0; j < num_visits; j++)
                    {
                        int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j]];
                        time_avail[j] = benchmark.Time_Avail[clientNodeIndex - num_depots];
                    }
                    partial_benchmark.Time_Avail = time_avail;

                    partial_benchmarks[i] = partial_benchmark;
                }

                /************ SERIALIZATION ******************/
                for (int i = 0; i < partial_benchmarks.Count(); i++)
                {
                    temporarySolution[temporarySolutionIndex++] = DataSerialization.ObjectToByteArray(partial_benchmarks[i]);
                }
            }

            return temporarySolution;
        }