Exemple #1
0
 /// <summary>
 /// Adds a new  possible route to the current existing routes
 /// </summary>
 /// <param name="newRoute"></param>
 public void AddNewRoute(DijkstraRoute newRoute)
 {
     if (!isConnectedToLocation(newRoute.EndPoint))
     {
         routes.Add(newRoute);
     }
 }
        /// <summary>
        /// Create delivery for Vehicle located at warehouseLoc and where it should follow Route
        /// </summary>
        /// <param name="Route"></param>
        /// <param name="Vehicle"></param>
        /// <param name="WarehouseLoc"></param>
        private void createDelivery(DijkstraRoute Route, Vehicle Vehicle, Location WarehouseLoc, int timeStamp)
        {
            Delivery temp = new Delivery(Route, WarehouseLoc, timeStamp);

            createdDeliveries.Add(temp);
            Vehicle.AddDeliveryToQueue(temp);
        }
        /// <summary>
        /// Provide deliveries to shops
        /// </summary>
        private void forseeDeliveries(int timeStamp)
        {
            List <Location> lowStockShops = getShopsWithLowStockOrdered();

            checkDeliveriesIsFinished();

            //filter out shops that have an active delivery
            for (int i = 0; i < lowStockShops.Count; i++)
            {
                foreach (Delivery d in createdDeliveries)
                {
                    if (d.Route.EndPoint == lowStockShops[i])
                    {
                        lowStockShops.RemoveAt(i);
                        i--;
                        break;
                    }
                }
            }

            //Create the fastest deliveries for each shop by looking at available vehicles and the traveltime from warehouse to shop
            foreach (Location s in lowStockShops)
            {
                int           outTime;
                Vehicle       outVehicle;
                DijkstraRoute tempRoute;
                //((Warehouse)warehouses[0].Building).fastestVehicleAvailableTime(out outVehicle, out outTime);

                DijkstraRoute bestRoute     = null;
                int           totalTimeSpan = int.MaxValue;
                Vehicle       bestVehicle   = null;
                Location      bestWarehouse = null;
                for (int i = 0; i < warehouses.Count; i++)
                {
                    ((Warehouse)warehouses[i].Building).fastestVehicleAvailableTime(out outVehicle, out outTime); //get fastest time
                    tempRoute = myDijkstra.GetRouteTo(warehouses[i], s);                                          //get route (can be null)
                    if (tempRoute != null)                                                                        //there is a route from warehouse to shop
                    {
                        if (tempRoute.RouteLenght + outTime < totalTimeSpan)
                        {
                            bestRoute     = tempRoute;
                            totalTimeSpan = tempRoute.RouteLenght + outTime;
                            bestVehicle   = outVehicle;
                            bestWarehouse = warehouses[i];
                        }
                    }
                }
                if (bestRoute != null) //There was a best route found for this delivery
                {
                    createDelivery(bestRoute, bestVehicle, bestWarehouse, timeStamp);
                }
                //Console.WriteLine("New delivery for SHOP" + s.LocationID + " Stock: " + ((Shop)s.Building).Stock);
            }
        }
Exemple #4
0
 public Delivery(DijkstraRoute dRoute, Location startLoc, int timeStamp)
 {
     if (!(dRoute.EndPoint.Building is Shop))
     {
         throw new Exception("Building needs to be a shop!");
     }
     if (dRoute.Route[0].Vertex1 != startLoc && dRoute.Route[0].Vertex2 != startLoc)
     {
         throw new Exception("Unkown startLocation");
     }
     route           = dRoute;
     status          = DeliveryStatus.NOTSTARTED;
     totalTravelTime = 0;
     createTime      = timeStamp;
     if (dRoute.Route[0].Vertex1 == startLoc)
     {
         nextLocation = dRoute.Route[0].Vertex2;
     }
     else
     {
         nextLocation = dRoute.Route[0].Vertex1;
     }
     currentRoad = dRoute.Route[0];
 }
        private DijkstraStart createDijkstraStart(Location startLocation, bool animate) //main methods for creating shortest routes accros the map
        {
            if (!reachableLocations.Contains(startLocation))
            {
                throw new Exception("Unknown location detected");
            }
            List <Road>   currentRoads        = new List <Road>(); //considered roads at the moment
            List <int>    currentRoadsLenghts = new List <int>();  //road lenghts
            DijkstraStart dijkstraStart       = new DijkstraStart(startLocation);

            currentRoads = getRoadsConnectedToLocation(startLocation);
            foreach (Road r in currentRoads)
            {
                currentRoadsLenghts.Add(r.InitialCost);
            }

            int      lowestIndex;
            Road     currentRoad;
            int      currentRoadLenght;
            Location prevLocation = null;
            Location newLocation  = null;

            //ANIMATION
            if (animate)
            {
                // resets all roads to black color
                foreach (Road r in allRoads)
                {
                    r.ResetDrawFields();
                }
            }

            while (currentRoads.Count != 0)
            {
                //get next shortest route
                lowestIndex       = findLowestIndex(currentRoadsLenghts);
                currentRoad       = currentRoads[lowestIndex];
                currentRoadLenght = currentRoadsLenghts[lowestIndex];

                //ANIMATION
                if (animate)
                {
                    // colors the potential roads with yellow
                    foreach (Road r in currentRoads)
                    {
                        r.LineColor = Color.Yellow;
                    }
                    Map.RedrawMapNow();
                    // makes the system wait 2 seconds
                    System.Threading.Thread.Sleep(1000);
                    // after it colors all potential yellow it colors the best green.
                    currentRoad.LineColor = Color.Green;
                }

                //claim new shortest route
                if (dijkstraStart.isConnectedToLocation(currentRoad.Vertex1))
                {
                    prevLocation = currentRoad.Vertex1;
                }
                else
                {
                    newLocation = currentRoad.Vertex1;
                }
                if (dijkstraStart.isConnectedToLocation(currentRoad.Vertex2))
                {
                    prevLocation = currentRoad.Vertex2;
                }
                else
                {
                    newLocation = currentRoad.Vertex2;
                }
                if (prevLocation == null)
                {
                    throw new NullReferenceException();
                }
                if (newLocation == null)
                {
                    throw new NullReferenceException();
                }
                DijkstraRoute previousDijkstraRoute = dijkstraStart.GetRouteTo(prevLocation);
                List <Road>   newRoute;

                //makes a copy of the current route and adds the current road
                if (previousDijkstraRoute != null)
                {
                    newRoute = previousDijkstraRoute.CopyRoute();
                    newRoute.Add(currentRoad);
                }
                else
                {
                    // makes a new route and adds the road to it
                    newRoute = new List <Road>();
                    newRoute.Add(currentRoad);
                }
                DijkstraRoute newDijkstraRoute = new DijkstraRoute(newRoute, newLocation);
                dijkstraStart.AddNewRoute(newDijkstraRoute);

                //Determine new roads and remove currentRoad/exsiting roads
                currentRoads.RemoveAt(lowestIndex);
                currentRoadsLenghts.RemoveAt(lowestIndex);

                List <Road> newAddingRoads = getRoadsConnectedToLocation(newLocation);
                newAddingRoads.Remove(currentRoad);

                //filter out already existing roads in currentRoads
                for (int i = 0; i < newAddingRoads.Count; i++)
                {
                    if (dijkstraStart.isConnectedToLocation(newAddingRoads[i].Vertex1) &&
                        dijkstraStart.isConnectedToLocation(newAddingRoads[i].Vertex2))
                    {
                        if (currentRoads.Contains(newAddingRoads[i]))
                        {
                            int index = currentRoads.IndexOf(newAddingRoads[i]);
                            currentRoads.RemoveAt(index);
                            currentRoadsLenghts.RemoveAt(index);
                        }
                        newAddingRoads.RemoveAt(i);
                        i--;
                    }
                }

                //Adding new roads and calculating new roads lenghts
                foreach (Road r in newAddingRoads)
                {
                    int roadLenght = r.InitialCost + currentRoadLenght;
                    currentRoads.Add(r);
                    currentRoadsLenghts.Add(roadLenght);
                }

                //reseting values
                currentRoad       = null;
                prevLocation      = null;
                newLocation       = null;
                currentRoadLenght = -1;
                lowestIndex       = -1;
            }
            if (!animate)
            {
                return(dijkstraStart);
            }
            else
            {
                Map.RedrawMap();
            }
            return(null);
        }