Esempio n. 1
0
        /// <summary>
        /// Solves the problem.
        /// </summary>
        /// <returns></returns>
        protected override IRoute DoSolve(IProblem problem)
        {
            // generate some random route first.
            //IRoute route = OsmSharp.Math.TravellingSalesman.Random.RandomSolver.DoSolve(
            //    problem);
            IRoute route = solver.Solve(problem);

            // improve the existing solution.
            double difference;

            this.Improve(problem, route, out difference);

            return(route);
        }
Esempio n. 2
0
        /// <summary>
        /// Does the actual solving.
        /// </summary>
        /// <param name="problem"></param>
        /// <returns></returns>
        protected override IRoute DoSolve(IProblem problem)
        {
            // convert to a symetric problem if needed.
            IProblem _problem = problem;

            if (!_problem.Symmetric)
            {
                _problem  = Convertor.ConvertToSymmetric(_problem);
                _was_asym = true;
            }

            // create the list of customers.
            _customers = new List <int>();
            for (int customer = 0; customer < _problem.Size; customer++)
            {
                _customers.Add(customer);
            }
            _sparse_set = SparseSetHelper.CreateNearestNeighourSet(_problem, _customers, _customers.Count / 10);
            //_sparse_set = SparseSetHelper.CreateNonSparseSet(_problem, _customers);
            // construct a route from the customers.
            //FixedSymmetricRoute init_route = new FixedSymmetricRoute(_customers);

            // construct a random route using best-placement.
            ArbitraryInsertionSolver bp_solver = new ArbitraryInsertionSolver();
            IRoute bp_route = bp_solver.Solve(_problem);
            FixedSymmetricRoute init_route = new FixedSymmetricRoute(bp_route);

            double     init_route_weight = LinKernighanSolver.Weight(_problem, init_route);
            RouteFound route             = new RouteFound()
            {
                Route       = init_route,
                RouteWeight = init_route_weight
            };

            Console.WriteLine("Route {0}:{1}",
                              route.Route.ToString(),
                              route.RouteWeight);

            // step 2.
            EdgeSet     X           = new EdgeSet();
            EdgeSet     Y           = new EdgeSet();
            IList <int> untried_t_1 = new List <int>(route.Route);

            while (untried_t_1.Count > 0)
            {
                // select t1.
                int t_1 = untried_t_1[0];
                untried_t_1.RemoveAt(0);

                // search route with t_1.
                RouteFound t_1_route = this.AfterSelectt1(_problem, route, X, Y, t_1);

                // select the better route.
                if (t_1_route.RouteWeight < route.RouteWeight)
                {
                    untried_t_1 = new List <int>(route.Route);
                    route       = RouteFound.SelectBest(route, t_1_route);
                    X           = new EdgeSet();
                    Y           = new EdgeSet();
                }
            } // step 2 and step 12.

            // convert back to asym solution if needed.
            //result.RemoveAt(result.Count - 1);
            if (_was_asym)
            {
                return(this.ConvertToASymRoute(new List <int>(route.Route)));
            }
            return(route.Route);
        }