Example #1
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;
        }
        public Dictionary<int, Battery> GetBatteriesForReservation(List<int> path, List<double> distance, double range, int batteryID)
        {
            // result: int: BatteryStationID
            // result: Battery: The battery at the given station
            var result = new Dictionary<int, Battery>();

            // Get the battery type of the given battery
            var batteryType = new BatteryCtr().FindBattery(batteryID).BatteryType;

            // Used for remembering when last changed battery
            int indexOfBatteryShift = 0;
            bool batteriesFound = false;

            if (batteryType != null)
            {
                for (int i = 0; i < path.Count && !batteriesFound; i++)
                {
                    // Get the distance to the first location
                    double dist = distance.ElementAt(i);

                    // If that distance is bigger than the range of the battery
                    if (dist > range)
                    {
                        // Get the batteryStation ID of the station before
                        int bsID = path[i - 1];

                        // Find available battery on the previous batterystation
                        Battery foundBattery = FindBatteryForReservation(i, path, batteryType.ID, indexOfBatteryShift);
                        if (foundBattery != null)
                        {
                            // Add the batterystation ID and the found battery to the result
                            // dictionary
                            result.Add((int)foundBattery.BatteryStationID, foundBattery);

                            // If the batterystation ID of the found battery is equals to batterystation ID
                            // where it's optimal to change battery
                            if (foundBattery.BatteryStationID == bsID)
                            {
                                // Save the index of the battery change
                                indexOfBatteryShift = i - 1;
                            }
                            else
                            {
                                // Find out where the battery was changed
                                indexOfBatteryShift = GetIndexOfBatteryShift(foundBattery, path);
                            }
                            BatteryCtr batteryCtr = new BatteryCtr();

                            // Find the new range of the new battery
                            range = batteryCtr.CalculateRange(foundBattery.Id);

                            // Accumulate the distance
                            range += distance[indexOfBatteryShift];
                        }
                        else
                        {
                            // Note that the batterystation does not have a battery for booking
                            result.Add(bsID, null);

                            // Break the loop
                            batteriesFound = true;
                        }
                    }
                }
            }
            return result;
        }