Ejemplo n.º 1
0
        public TSP(string fileName)
        {
            if (fileName == null)
            {
                fileName = "bays29";
            }

            var rootDirectory = ConfigurationManager.AppSettings["RootDirectory"];

            FileName = fileName;

            var problemTspFile = Path.Combine(rootDirectory, FileName + ".tsp");
            var tourTspFile    = TspFile.Load(Path.Combine(rootDirectory, FileName + ".opt.tour"));

            TheProblem = TravelingSalesmanProblem.FromFile(problemTspFile);
            TheTour    = Tour.FromTspFile(tourTspFile);
            TotalNodes = TheProblem.NodeProvider.CountNodes();

            var dim = new int[TotalNodes];

            for (var i = 0; i < TotalNodes; i++)
            {
                dim[i] = (TheTour.Nodes[i]) - 1;
            }
            OptimalKnown = Evaluate(dim);
        }
        // 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));
        }
        public void ConstructTravelingSalesmanProblems()
        {
            var files = new List <string>(Directory.EnumerateFiles(RootDir, "*.tsp", SearchOption.AllDirectories));

            foreach (var fileName in files)
            {
                var problem = TravelingSalesmanProblem.FromFile(fileName);
                Assert.IsNotNull(problem);
            }
        }
Ejemplo n.º 4
0
        public string StartTsp(int MaxTime, string filename, bool isGenetic, int population)
        {
            //try
            //{
            problem = TravelingSalesmanProblem.FromFile(filename); // create TravelingSalesmanProblem object
            switch (problem.Type)                                  // type of nodes
            {
            case ProblemType.TSP:
                if (isGenetic == false)
                {
                    status = new TSPSymetric(problem, MaxTime).SolveTSP();
                }
                else
                {
                    status = new TSPGenetic().SolveTSP(problem, MaxTime, population, null, false);
                }
                break;

            case ProblemType.ATSP:
                _matrixTable = new MatrixTable(filename);
                if (isGenetic == false)
                {
                    status = new TSPAsymetric(MaxTime, _matrixTable).SolveTSP();
                }
                else
                {
                    status = new TSPGenetic().SolveTSP(null, MaxTime, population, _matrixTable.Matrix, true);
                }
                break;

            default:
                MessageBox.Show("Loaded file is not tsp/atsp file. returning.");
                return(status);
            }
            //}
            //catch (Exception)
            //{
            //    MessageBox.Show("Can't load file " + filename);
            //}
            return(status);
        }