Пример #1
0
        private void handleToolStripMenuClick(RunType runType)
        {
            this.reset();

            tbElapsedTime.Text = " Running...";
            tbCostOfTour.Text  = " Running...";
            Refresh();

            Solver solver;

            if (runType == RunType.BRANCH_AND_BOUND)
            {
                BranchAndBoundSolver babs = new BranchAndBoundSolver(CityData);
                CityData = babs.solve();
            }
            else if (runType == RunType.GREEDY)
            {
                CityData = GreedySolver.solve(CityData);
            }
            else if (runType == RunType.FANCY)
            {
                solver   = new FancySolver();
                CityData = solver.solve(CityData);
            }
            else  // runType == RunType.DEFAULT
            {
                solver   = new DefaultSolver();
                CityData = solver.solve(CityData);
            }

            tbCostOfTour.Text   = CityData.bssf.costOfRoute.ToString();
            tbElapsedTime.Text  = CityData.timeElasped.ToString();
            tbNumSolutions.Text = CityData.solutions.ToString();
            Invalidate();                          // force a refresh.
        }
Пример #2
0
        public TSPSolution solve(TSPInput input)
        {
            GreedySolver s = new GreedySolver();

            best = s.solve(input).computeDistance() + 1;

            bestSolution = new int[input.nodesCount];
            List <int> current = new List <int>();

            current.Add(0);
            List <int> remaining = new List <int>();

            for (int i = 1; i < input.nodesCount; i++)
            {
                remaining.Add(i);
            }
            trySolve(input, current, remaining, 0);
            TSPSolution result = new TSPSolution(input);

            for (int i = 0; i < input.nodesCount - 1; i++)
            {
                result.setSuccessor(bestSolution[i], bestSolution[i + 1]);
            }
            result.setSuccessor(bestSolution[input.nodesCount - 1], 0);
            return(result);
        }
Пример #3
0
        private PermutationStandard initialize2(TSPInput input)
        {
            var solver = new GreedySolver();
            var result = solver.solve(input);

            return(new PermutationStandard(result));
        }
        // finds the best tour possible using the branch and bound method of attack
        // For an example of what to return, see DefaultSolver.solve() method.
        public Problem solve()
        {
            Stopwatch timer = new Stopwatch();

            timer.Start();

            State beginningState = new State(cityData.size);

            SolverHelper.initializeState(beginningState, cityData);
            this.q.Enqueue(beginningState);
            Problem determineBSSFProblem = SolverHelper.clone(cityData);

            this.cityData.bssf = GreedySolver.solve(determineBSSFProblem).bssf;

            while (q.Count > 0)
            {
                recur(q.Dequeue());
                //Clear out any states that have a BSSF less than
                //that of the current
            }

            timer.Stop();
            cityData.timeElasped = timer.Elapsed;

            return(cityData);
        }
Пример #5
0
        public static void greedyHard()
        {
            Problem cityData = new Problem(1, 20, 60, HardMode.Modes.Hard);

            cityData = GreedySolver.solve(cityData);
            if (cityData.bssf.costOfRoute != 4928)
            {
                throw new SystemException("Incorrect cost");
            }
        }
Пример #6
0
        public static void greedyEasy()
        {
            Problem cityData = new Problem(1, 5, 60, HardMode.Modes.Easy);

            cityData = GreedySolver.solve(cityData);
            if (cityData.bssf.costOfRoute != 2060)
            {
                throw new SystemException("Incorrect cost");
            }
        }
Пример #7
0
        /////////////////////////////////////////////////////////////////////////////////////////////
        // These additional solver methods will be implemented as part of the group project.
        ////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// finds the greedy tour starting from each city and keeps the best (valid) one
        /// </summary>
        /// <returns>results array for GUI that contains three ints: cost of solution, time spent to find solution, number of solutions found during search (not counting initial BSSF estimate)</returns>
        public string[] greedySolveProblem()
        {
            string[] results = new string[3];

            GreedySolver greedy = new GreedySolver(Cities, bssf, results);

            bssf = greedy.Solve();

            return(results);
        }
Пример #8
0
        /// <summary>
        /// performs a Branch and Bound search of the state space of partial tours
        /// stops when time limit expires and uses BSSF as solution
        /// </summary>
        /// <returns>results array for GUI that contains three ints: cost of solution, time spent to find solution, number of solutions found during search (not counting initial BSSF estimate)</returns>
        public string[] bBSolveProblem()
        {
            string[] results = new string[3];

            //Find a smart starting bssf
            GreedySolver greedy = new GreedySolver(Cities, bssf, results);

            bssf = greedy.Solve();

            BranchAndBoundSolver brancher = new BranchAndBoundSolver(Cities, bssf, results, time_limit);

            bssf = brancher.Solve();

            return(results);
        }