Example #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);
        }
Example #2
0
        /// <summary>
        /// Calculates the No-depot VRP solution.
        /// </summary>
        /// <param name="weights"></param>
        /// <param name="locations"></param>
        /// <returns></returns>
        public override int[][] CalculateNoDepot(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++)
            {
                // get the route.
                IRoute current = routes.Route(idx);

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

                OsmSharp.Logging.Log.TraceEvent("OsmSharp.Routing.VRP.WithDepot.MaxTime.MaxTimeRouter", System.Diagnostics.TraceEventType.Information,
                                                "Route {0}: {1}s", idx, vrp_solution_weights[idx]);

                // 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);
        }
Example #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);
        }
Example #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()));
        }
Example #5
0
 /// <summary>
 /// Creates a new maxtime calculator.
 /// </summary>
 /// <param name="problem"></param>
 public MaxTimeCalculator(MaxTimeProblem problem)
 {
     _problem = problem;
 }
Example #6
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));
 }
Example #7
0
 /// <summary>
 /// Executing a solver function.
 /// </summary>
 /// <param name="problem"></param>
 /// <returns></returns>
 internal abstract MaxTimeSolution Solve(MaxTimeProblem problem);
Example #8
0
        /// <summary>
        /// Calculates the No-depot VRP solution.
        /// </summary>
        /// <param name="weights"></param>
        /// <param name="locations"></param>
        /// <returns></returns>
        public override int[][] CalculateNoDepot(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++)
            {
                // get the route.
                IRoute current = routes.Route(idx);

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

                OsmSharp.Logging.Log.TraceEvent("OsmSharp.Routing.VRP.WithDepot.MaxTime.MaxTimeRouter", TraceEventType.Information,
                    "Route {0}: {1}s", idx, vrp_solution_weights[idx]);

                // 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;
        }
Example #9
0
 /// <summary>
 /// Creates a new maxtime calculator.
 /// </summary>
 /// <param name="problem"></param>
 public MaxTimeCalculator(MaxTimeProblem problem)
 {
     _problem = problem;
 }
Example #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;
 }
Example #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());
 }
Example #12
0
 /// <summary>
 /// Executing a solver function.
 /// </summary>
 /// <param name="problem"></param>
 /// <returns></returns>
 internal abstract MaxTimeSolution Solve(MaxTimeProblem problem);
Example #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);
 }
Example #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;
        }