/// <summary>
        /// Constructs an instance of ExtendedCalculator. By constructing
        /// an object of type ExtendedCalculator, you automatically initiate
        /// the calculation of a path between two points, according to a given
        /// planning mode (shortest, quickest, etc.).
        /// </summary>
        public ExtendedCalculator(double range, Battery battery,
                                  int fromID, int toID)
        {
            LocationCtr locCtr = new LocationCtr();
            this.From = locCtr.FindLocation(fromID);
            this.To = locCtr.FindLocation(toID);
            Console.WriteLine("[DEBUG MESSAGE]: ExtendedCalculator() Start ");

            // Initializes graph field along with Distance and Path properties.
            // Finds the most compatible locations based on the given coordinates
            // and initializes the From and To properties with the existing locations.
            InitializeCalculator(range, 10, 5, 10, 5);

            // Initialize the graph used for calculating the path
            // TODO: Implement planningMode. batteryType irrelevant here, it's not used?
            GenerateGraph("plan", battery.BatteryType);

            try
            {
                CalculatePath(From, To);
            }
            catch (Exception e)
            {
                Console.WriteLine("[DEBUG MESSAGE]: Error occured in CalculatePath(): {0}", e.Message);
            }
            Console.WriteLine("[DEBUG MESSAGE]: ExtendedCalculator() End");
        }
        public string GetMapRouteUrl(List<int> routePath)
        {
            string mapUrl = "http://www.google.com/maps/dir/";
            LocationCtr locationCtr = new LocationCtr();

            List<Location> routeLocations = routePath.Select(i => locationCtr.FindLocation(i)).ToList();

            foreach (var i in routeLocations)
            {
                mapUrl += i.Latitude.ToString().Replace(',', '.') + "," + i.Longitude.ToString().Replace(',', '.') + "/";
            }

            return mapUrl;
        }
        public Route PlanRoute(int fromID, int toID, int batteryID)
        {
            LocationCtr locationCtr = new LocationCtr();
            this.From = locationCtr.FindLocation(fromID);
            this.To = locationCtr.FindLocation(toID);
            To.GeoCoordinate.Longitude = To.Longitude;
            To.GeoCoordinate.Latitude = To.Latitude;

            GenerateGraph();

            this.queue = new PriorityQueue(graph.Count());

            route.Range = new BatteryCtr().CalculateRange(batteryID);
            route.Path = CalculatePath();
            route.Distance = GetDistanceList(route.Path);

            return route;
        }
Esempio n. 4
0
        public Route PlanRoute(int fromID, int toID, int batteryID)
        {
            BatteryCtr batteryCtr = new BatteryCtr();
            LocationCtr locationCtr = new LocationCtr();
            this.From = locationCtr.FindLocation(fromID);
            this.To = locationCtr.FindLocation(toID);

            // Initialize graph
            GenerateGraph();

            // Calculate distance the vehicle can travel before recharging
            route.Range = batteryCtr.CalculateRange(batteryID);

            // Determine shortest path using Dijsktras Shortest Path Algorithm
            route.Path = CalculatePath();

            route.Distance = GetDistanceList(route.Path);

            return route;
        }
        private void GenerateGraph(string planningMode, BatteryType batteryType)
        {
            Console.WriteLine("[DEBUG MESSAGE]: GenerateGraph() Start");
            LocationCtr locationCtr = new LocationCtr();

            // Get all locations, filter for type 3 (BatteryStation)
            // l => l.Type == 3 (Return l where l.type is equal to 3)
            List<Location> locations = locationCtr.GetAllLocations().FindAll(l => l.Type == 3);
            List<Connection> connections = locationCtr.GetAllConnections();

            // Iterate over the location collection, add these to the graph.
            foreach (Location l in locations)
            {
                this.graph.AddLocation(l);
            }

            // Iterate over the connection collection, add these to the graph
            // including start vertex, end vertex and edge weight.
            foreach (Connection c in connections)
            {
                //TODO Uncomment
                //this.graph.AddConnection(c.From, c.To, c.Distance);
            }

            Console.WriteLine("[DEBUG MESSAGE]: GenerateGraph() End");
        }
        private Location GetClosestNodeToDest()
        {
            Console.WriteLine("[DEBUG MESSAGE]: GetClosestNodeToDest() Start");
            Location foundLocation = null;
            LocationCtr locationCtr = new LocationCtr();

            // Get all locations from database, filter them using List<T>.FindAll(...) method.
            List<Location> allLocations = locationCtr.GetAllLocations().FindAll(l => l.Type == 3);

            // Set the currently shortest distance to Dest to positive infinity.
            this.MinDistanceToDest = Double.PositiveInfinity;

            // Iterate over the filtered collection, finding the closest matching location.
            foreach (Location l in allLocations)
            {
                // It's necessary to copy information regarding latitude and longitude
                // to their GeoCoordinate siblings. GeoCoordinate is not represented in
                // the database.
                l.GeoCoordinate.Latitude = l.Latitude;
                l.GeoCoordinate.Longitude = l.Longitude;

                // Get the euclidian distance from current location to original start point
                double fromStart = l.GetDistanceToKM(Start.GeoCoordinate);

                // Get the euclidian distance from current location to original end point
                double toDest = l.GetDistanceToKM(Dest.GeoCoordinate);

                // IF, the distance from the original start point to the current location is less than or
                // equal to the original distance
                // AND the distance to the original endpoint is closer than previous locations
                // THEN proceed.
                if (fromStart <= this.FinalStraightLine && this.MinDistanceToDest > toDest)
                {
                    foundLocation = l;
                    this.MinDistanceToDest = fromStart;
                }
            }
            Console.WriteLine("[DEBUG MESSAGE]: GetClosestNodeToDest() End");
            return foundLocation;
        }
        private Battery FindBatteryForReservation(int i, List<int> path, int batteryTypeID, int indexOfLastBatteryShift)
        {
            Battery foundBattery = null;
            BatteryStation bs;
            int j = 1;

            // A battery has not been found, and a stepback does not exceed a previous battery shift
            while (foundBattery == null && j < i && j < i - indexOfLastBatteryShift)
            {
                bs = new LocationCtr().FindBatteryStation(path.ElementAt(i - j));

                // Check for battery in the batterystation list of batteries
                foundBattery = bs.Batteries
                            .Where(b => b.BatteryTypeID == batteryTypeID)
                            .FirstOrDefault(b => b.IsReserved == false);
                j++;
            }
            return foundBattery;
        }
        /// <summary>
        /// Generates the graph with all locations and connections. 
        /// The connections are added to the adjacents list on the location.
        /// </summary>
        private void GenerateGraph()
        {
            LocationCtr locationCtr = new LocationCtr();

            List<Location> locations = locationCtr.GetAllLocations();

            List<Connection> connections = locationCtr.GetAllConnections();

            foreach (Location l in locations)
            {
                l.GeoCoordinate.Longitude = l.Longitude;
                l.GeoCoordinate.Latitude = l.Latitude;
                this.graph.AddLocation(l);
            }

            foreach (Connection c in connections)
            {
                this.graph.AddConnection(c.From, c.To, c.Distance);
            }
        }