Exemplo n.º 1
0
        /// <summary>
        /// Implements the actual logic.
        /// </summary>
        /// <param name="problem"></param>
        /// <param name="customers"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public MaxTimeSolution DoCalculation(MaxTimeProblem problem,
                                             ICollection <int> customers, Second max)
        {
            MaxTimeSolution routes = this.Solve(problem);

            return(routes);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Caculates the DVRP.
        /// </summary>
        /// <param name="weights"></param>
        /// <param name="locations"></param>
        /// <returns></returns>
        public override int[][] CalculateDepot(double[][] weights, GeoCoordinate[] locations)
        {
            // Keeps a local copy of the current calculation points.
            //
            // TODO: find a better solution to make this thread-safe!
            _locations = locations;

            // convert to ints.
            for (int x = 0; x < weights.Length; x++)
            {
                double[] weights_x = weights[x];
                for (int y = 0; y < weights_x.Length; y++)
                {
                    weights_x[y] = weights_x[y];
                }
            }

            // create the problem for the genetic algorithm.
            MatrixProblem  matrix    = MatrixProblem.CreateATSP(weights);
            MaxTimeProblem problem   = new MaxTimeProblem(matrix, this.Max, this.DeliveryTime, 10, 1000);
            List <int>     customers = new List <int>();

            for (int customer = 0; customer < locations.Length; customer++)
            {
                customers.Add(customer);
                problem.CustomerPositions.Add(
                    _locations[customer]);
            }

            MaxTimeSolution routes = this.DoCalculation(problem, customers, this.Max);

            // convert output.
            int[][]  vrp_solution         = new int[routes.Count][];
            double[] vrp_solution_weights = new double[routes.Count];
            for (int idx = 0; idx < routes.Count; idx++)
            {
                OsmSharp.Logging.Log.TraceEvent("OsmSharp.Routing.VRP.WithDepot.MaxTime.MaxTimeRouter", System.Diagnostics.TraceEventType.Information,
                                                "Route {0}: {1}s", idx, routes[idx]);

                // get the route.
                IRoute current = routes.Route(idx);

                // calculate the weight.
                vrp_solution_weights[idx] = problem.Time(current);

                // convert the route.
                List <int> route = new List <int>(current);
                if (current.IsRound)
                {
                    route.Add(route[0]);
                }
                vrp_solution[idx] = route.ToArray();
            }

            // construct and return solution.
            return(vrp_solution);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns true if the routes overlap.
        /// </summary>
        /// <param name="problem"></param>
        /// <param name="route1"></param>
        /// <param name="route2"></param>
        /// <returns></returns>
        protected bool Overlaps(MaxTimeProblem problem, IRoute route1, IRoute route2)
        {
            GeoCoordinateBox route1_box = this.CalculateBox(problem, route1);
            GeoCoordinateBox route2_box = this.CalculateBox(problem, route2);

            if (route1_box != null && route2_box != null)
            {
                return(route1_box.Overlaps(route2_box));
            }
            return(false);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Calculates a bounding box.
        /// </summary>
        /// <param name="problem"></param>
        /// <param name="route"></param>
        /// <returns></returns>
        protected GeoCoordinateBox CalculateBox(MaxTimeProblem problem, IRoute route)
        {
            HashSet <GeoCoordinate> coordinates = new HashSet <GeoCoordinate>();

            foreach (int customer in route)
            {
                coordinates.Add(problem.CustomerPositions[customer]);
            }
            if (coordinates.Count == 0)
            {
                return(null);
            }
            return(new GeoCoordinateBox(coordinates.ToArray()));
        }
Exemplo n.º 5
0
 /// <summary>
 /// Creates a new max time calculator.
 /// </summary>
 /// <param name="problem"></param>
 public MaxTimeCalculator(MaxTimeProblem problem)
 {
     _problem = problem;
 }
Exemplo n.º 6
0
 /// <summary>
 /// Creates a new max time calculator.
 /// </summary>
 /// <param name="problem"></param>
 public MaxTimeCalculator(MaxTimeProblem problem)
 {
     _problem = problem;
 }
Exemplo n.º 7
0
 /// <summary>
 /// Implements the solve function of the IMaxTimeSolver interface
 /// </summary>
 /// <param name="problem"></param>
 /// <returns></returns>
 MaxTimeSolution IMaxTimeSolver.Solve(MaxTimeProblem problem)
 {
     return(this.Solve(problem));
 }
Exemplo n.º 8
0
 /// <summary>
 /// Executing a solver function.
 /// </summary>
 /// <param name="problem"></param>
 /// <returns></returns>
 internal abstract MaxTimeSolution Solve(MaxTimeProblem problem);
Exemplo n.º 9
0
        /// <summary>
        /// Caculates the DVRP.
        /// </summary>
        /// <param name="weights"></param>
        /// <param name="locations"></param>
        /// <returns></returns>
        public override int[][] CalculateDepot(double[][] weights, GeoCoordinate[] locations)
        {
            // Keeps a local copy of the current calculation points.
            //
            // TODO: find a better solution to make this thread-safe!
            _locations = locations;

            // convert to ints.
            for (int x = 0; x < weights.Length; x++)
            {
                double[] weights_x = weights[x];
                for (int y = 0; y < weights_x.Length; y++)
                {
                    weights_x[y] = weights_x[y];
                }
            }

            // create the problem for the genetic algorithm.
            MatrixProblem matrix = MatrixProblem.CreateATSP(weights);
            MaxTimeProblem problem = new MaxTimeProblem(matrix, this.Max, this.DeliveryTime, 10, 1000);
            List<int> customers = new List<int>();
            for (int customer = 0; customer < locations.Length; customer++)
            {
                customers.Add(customer);
                problem.CustomerPositions.Add(
                    _locations[customer]);
            }

            MaxTimeSolution routes = this.DoCalculation(problem, customers, this.Max);

            // convert output.
            int[][] vrp_solution = new int[routes.Count][];
            double[] vrp_solution_weights = new double[routes.Count];
            for (int idx = 0; idx < routes.Count; idx++)
            {
                OsmSharp.Logging.Log.TraceEvent("OsmSharp.Routing.VRP.WithDepot.MaxTime.MaxTimeRouter", TraceEventType.Information,
                    "Route {0}: {1}s", idx, routes[idx]);

                // get the route.
                IRoute current = routes.Route(idx);

                // calculate the weight.
                vrp_solution_weights[idx] = problem.Time(current);

                // convert the route.
                List<int> route = new List<int>(current);
                if (current.IsRound)
                {
                    route.Add(route[0]);
                }
                vrp_solution[idx] = route.ToArray();
            }

            // construct and return solution.
            return vrp_solution;
        }
Exemplo n.º 10
0
 /// <summary>
 /// Returns true if the routes overlap.
 /// </summary>
 /// <param name="problem"></param>
 /// <param name="route1"></param>
 /// <param name="route2"></param>
 /// <returns></returns>
 protected bool Overlaps(MaxTimeProblem problem, IRoute route1, IRoute route2)
 {
     GeoCoordinateBox route1_box = this.CalculateBox(problem, route1);
     GeoCoordinateBox route2_box = this.CalculateBox(problem, route2);
     if (route1_box != null && route2_box != null)
     {
         return route1_box.Overlaps(route2_box);
     }
     return false;
 }
Exemplo n.º 11
0
 /// <summary>
 /// Calculates a bounding box.
 /// </summary>
 /// <param name="problem"></param>
 /// <param name="route"></param>
 /// <returns></returns>
 protected GeoCoordinateBox CalculateBox(MaxTimeProblem problem, IRoute route)
 {
     HashSet<GeoCoordinate> coordinates = new HashSet<GeoCoordinate>();
     foreach (int customer in route)
     {
         coordinates.Add(problem.CustomerPositions[customer]);
     }
     if (coordinates.Count == 0)
     {
         return null;
     }
     return new GeoCoordinateBox(coordinates.ToArray());
 }
Exemplo n.º 12
0
 /// <summary>
 /// Executing a solver function.
 /// </summary>
 /// <param name="problem"></param>
 /// <returns></returns>
 internal abstract MaxTimeSolution Solve(MaxTimeProblem problem);
Exemplo n.º 13
0
 /// <summary>
 /// Implements the solve function of the IMaxTimeSolver interface
 /// </summary>
 /// <param name="problem"></param>
 /// <returns></returns>
 MaxTimeSolution IMaxTimeSolver.Solve(MaxTimeProblem problem)
 {
     return this.Solve(problem);
 }
Exemplo n.º 14
0
        /// <summary>
        /// Implements the actual logic.
        /// </summary>
        /// <param name="problem"></param>
        /// <param name="customers"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public MaxTimeSolution DoCalculation(MaxTimeProblem problem,
            ICollection<int> customers, Second max)
        {
            MaxTimeSolution routes = this.Solve(problem);

            return routes;
        }