Ejemplo n.º 1
0
        private void Recalculate()
        {
            if (!testing)
            {
                return;
            }
            G = new Graph();
            CapacitatedVehicleRoutingProblem problem = CapacitatedVehicleRoutingProblem.FromFile("Benchmarks/A-n53-k7.vrp");
            var nodes = problem.NodeProvider.GetNodes();

            for (int i = 0; i < nodes.Count; i++)
            {
                TspLibNet.Graph.Nodes.Node2D node = (TspLibNet.Graph.Nodes.Node2D)nodes[i];
                double  demand = problem.DemandProvider.GetDemand(nodes[i]);
                Vertice v      = new Vertice();
                v.X      = node.X;
                v.Y      = node.Y;
                v.Demand = demand;
                G.Vertices.Add(v);
            }
            G.RecalculateVertices();
            solution = G.FindDVRPSolution();
            double usage = G.CalculateUsage(solution);

            textBox1.Text = usage.ToString();
        }
        // TODO: no opt tours for SOP, VRP, ATSP with known distance for validation

        private static void AssertTourDistance(FileType problemType, string problemFileName, string tourFileName, int expectedDistance)
        {
            var problemTspFile = Path.Combine(RootDir, problemFileName);
            var tourTspFile    = TspFile.Load(Path.Combine(RootDir, tourFileName));

            Assert.IsNotNull(problemTspFile);
            Assert.IsNotNull(tourTspFile);

            IProblem problem = null;

            switch (problemType)
            {
            case FileType.TSP:
            case FileType.ATSP:
                problem = TravelingSalesmanProblem.FromFile(problemTspFile);
                break;

            case FileType.CVRP:
                problem = CapacitatedVehicleRoutingProblem.FromFile(problemTspFile);
                break;

            case FileType.SOP:
                problem = SequentialOrderingProblem.FromFile(problemTspFile);
                break;
            }

            Assert.IsNotNull(problem);

            ITour tour = Tour.FromTspFile(tourTspFile);

            Assert.IsNotNull(tour);
            Assert.AreEqual(expectedDistance, problem.TourDistance(tour));
        }
Ejemplo n.º 3
0
        private static void RunCalculation(string file, string timeStamp)
        {
            TspFile tspfile = TspFile.Load(file);
            CapacitatedVehicleRoutingProblem problem = CapacitatedVehicleRoutingProblem.FromFile(file);
            Graph Graph = new Graph(_maxDemand: tspfile.Capacity);
            var   nodes = problem.NodeProvider.GetNodes();

            for (int i = 0; i < nodes.Count; i++)
            {
                TspLibNet.Graph.Nodes.Node2D node = (TspLibNet.Graph.Nodes.Node2D)nodes[i];
                double  demand = problem.DemandProvider.GetDemand(nodes[i]);
                Vertice v      = new Vertice();
                v.X      = node.X;
                v.Y      = node.Y;
                v.Demand = demand;
                Graph.Vertices.Add(v);
            }
            Graph.RecalculateVertices();
            var maxDistance = Graph.Edges.SelectMany(x => x).Max(x => x.Weigth) * 200;

            Graph.maxDistance = maxDistance;
            var    solution  = Graph.FindDVRPSolution();
            double usage     = Graph.CalculateUsage(solution);
            string paramsCSV = file + "," + Graph.GetParamsCSV() + ",\"" + tspfile.Comment + "\"," + usage + "\n";

            System.IO.File.AppendAllText(@"Benchmarks" + timeStamp + ".csv", paramsCSV);
        }
Ejemplo n.º 4
0
        private int GetNotUsedClosestMarketIndex(int currentMarkerIndex)
        {
            CapacitatedVehicleRoutingProblem problem = ((CapacitatedVehicleRoutingProblem)Problem);
            int   nextMarket = -1;
            float bestScore  = int.MaxValue;

            for (int i = 0; i < Problem.Dimensions; i++)
            {
                bool canUse = true;
                for (int j = 0; j < Answer.Length; j++)
                {
                    if (Answer[j] == i)
                    {
                        canUse = false;
                    }
                }
                if (canUse)
                {
                    float score = problem.Markets[currentMarkerIndex + 1].GetDistanceBetweenMarkets(problem.Markets[i + 1]);
                    if (bestScore > score)
                    {
                        bestScore  = score;
                        nextMarket = i;
                    }
                }
            }
            return(nextMarket);
        }
        public void ConstructCapacitatedVehicleRoutingProblems()
        {
            var files = new List <string>(Directory.EnumerateFiles(RootDir, "*.vrp", SearchOption.AllDirectories));

            foreach (var fileName in files)
            {
                var problem = CapacitatedVehicleRoutingProblem.FromFile(fileName);
                Assert.IsNotNull(problem);
            }
        }