Esempio n. 1
0
        public Car(int id, Depot depot, DateTime startTime, List <Depot> depots, ConcurrentDictionary <int, Client> clients, List <Hotspot> hotspots, Router router)
        {
            ID        = id;
            State     = DrivingState.WAITING;
            Latitude  = depot.Latitude;
            Longitude = depot.Longitude;
            CurrentResolvedLocation = Support.ResolvePoint((float)Latitude, (float)Longitude).ResolvedPoint;
            //DestLatitude = depot.Latitude;
            //DestLongitude = depot.Longitude;

            ChargeLeft   = fullCharge;
            TimesCharged = 0;
            LastDepotID  = depot.UniqueID;

            ClientsOnBoard = new List <Client>();

            LocalTime   = startTime;
            MoneyEarned = 0f;

            Depots   = depots;
            Clients  = clients;
            Hotspots = hotspots;

            CyclePotentialClients = new List <WeightedClient>();
            FirstIndex            = 0;
            CompletedClients      = 0;

            ClientCount = clients.Count;

            Logs   = new List <Log>();
            Router = router;

            /*
             * for(int i = 0; i < Hotspots.Count;  i++)
             * {
             *  Console.WriteLine("Hotspot #{0}: From {1} to {2}, coordinates are ({3},{4})", i, Hotspots[i].TimeFrom, Hotspots[i].TimeTo, Hotspots[i].Coordinates.Latitude, Hotspots[i].Coordinates.Longitude);
             * }*/
            //Console.ReadLine();
        }
Esempio n. 2
0
        public Depot FindNearestDepot(List <Depot> depots, double latitude = double.NaN, double longitude = double.NaN)
        {
            if (latitude == double.NaN || longitude == double.NaN)
            {
                latitude  = Latitude;
                longitude = Longitude;
            }
            double minDistOnFly = double.PositiveInfinity;
            Depot  nearestDepot = null;

            foreach (Depot depot in depots)
            {
                double distOnFly = DistanceBetweenCoordinates(Latitude, Longitude, depot.Latitude, depot.Longitude);
                //Console.WriteLine(distOnFly);
                if (distOnFly < minDistOnFly)
                {
                    minDistOnFly = distOnFly;
                    nearestDepot = depot;
                }
            }
            //throw new SystemException();
            return(nearestDepot);
        }
Esempio n. 3
0
        // 0 - STOP
        // 1 - CONTINUE
        public int Cycle()
        {
            //Check if need recharging
            if (DateTime.Compare(LocalTime, new DateTime(2022, 3, 1, 0, 0, 0).AddHours(48)) > 0)
            {
                Console.WriteLine("Time is up!");
                Console.WriteLine("I earned a total of {0} EUR", MoneyEarned);
                return(0);
            }


            //Console.WriteLine("Checking charge");
            if (State == DrivingState.CHARGING)
            {
                int secondsTillFull = (int)((fullCharge - ChargeLeft) / (chargingSpeed * 3600f));

                LocalTime  = LocalTime.AddSeconds(secondsTillFull);
                State      = DrivingState.WAITING;
                ChargeLeft = fullCharge;

                TimesCharged += 1;
                //Console.WriteLine("Charging car to full, waiting for {0} seconds", secondsTillFull);
                return(1);
            }


            if (ChargeLeft < critcalCharge)
            {
                Depot nearestDepot = FindNearestDepot(Depots);
                Route routeToDepot = null;
                if (nearestDepot == null)
                {
                    Console.WriteLine("FATAL ERROR: Car didn't find ANY depots");
                    return(0);
                }
                else
                {
                    routeToDepot = CalculateRouteTo(nearestDepot.ResolvedLocation);

                    if (routeToDepot == null)
                    {
                        List <Depot> depotCopy;
                        Depot[]      temporaryDepots = new Depot[Depots.Count];
                        Depots.CopyTo(temporaryDepots);
                        depotCopy = temporaryDepots.ToList();
                        depotCopy.Remove(nearestDepot);
                        while (routeToDepot == null && depotCopy.Count != 0)
                        {
                            nearestDepot = FindNearestDepot(depotCopy);
                            routeToDepot = CalculateRouteTo(nearestDepot.ResolvedLocation);
                            depotCopy.Remove(nearestDepot);
                        }
                        if (routeToDepot == null)
                        {
                            Console.WriteLine("FINAL ERROR: No routes to any available depots");
                            return(1);
                        }
                    }
                }
                Console.WriteLine("Car went charging, depot was {0} m away", routeToDepot.TotalDistance);
                //Console.WriteLine(nearestDepot.Latitude + "," + nearestDepot.Longitude);
                RideToDestination(nearestDepot.ResolvedLocation, routeToDepot.TotalDistance, DrivingState.CHARGING);
                LastDepotID = nearestDepot.UniqueID;
                return(1);
            }

            //Looking for clients
            if (CyclePotentialClients.Count == 0)
            {
                if (FirstIndex == Clients.Count - 1)
                {
                    Console.WriteLine("All rides taken!");
                    Console.WriteLine("I earned a total of {0} EUR", MoneyEarned);

                    return(0);
                }
            }
            CyclePotentialClients = WeightPotentialRides(FindSuitableClients(AIWeights.NumberOfPossibleRides));
            if (CyclePotentialClients.Count != 0)
            {
                Client selectedClient = CyclePotentialClients[0].Client;
                //Console.WriteLine("Picking up passenger");

                Route clientRoute = CalculateRouteTo(selectedClient.ResolvedStart);
                if (clientRoute == null)
                {
                    selectedClient.Accessible = false;
                    CyclePotentialClients.RemoveAt(0);
                    //throw new SystemException();
                    return(1);
                }
                double distToClient      = clientRoute.TotalDistance;
                double clientWaitFromNow = selectedClient.StartTime.AddMinutes(3d).Subtract(LocalTime).TotalSeconds;
                //Let's check if we can make it in time
                if (clientWaitFromNow * carSpeedMeter < distToClient)
                {
                    //double distOnFly = DistanceBetweenCoordinates(Latitude, Longitude, selectedClient.StartLatitude, selectedClient.StartLongitude);
                    Console.WriteLine("Client(ID : {0} found, but could not make it in time, missed it by {1} s", selectedClient.Index, (distToClient - (clientWaitFromNow * carSpeedMeter)) / carSpeedMeter);
                    //Console.WriteLine("On fly was {0} m, estimated distance was {1} m, real distance was {2}", distOnFly, distOnFly * EstimationMultiplier(distOnFly), distToClient);
                    //Console.WriteLine("LocalTime: {0}, Final arrival time: {1}, My arrival time: {2}", LocalTime, selectedClient.StartTime.AddMinutes(3d), LocalTime.AddSeconds(distToClient/carSpeedMeter));
                    //Console.WriteLine("Values used in calculation: {0} , {1}", distOnFly * EstimationMultiplier(distOnFly), clientWaitFromNow * carSpeedMeter);
                    //if (selectedClient.Index == 7609) Console.ReadLine();
                    CyclePotentialClients.RemoveAt(0);
                    return(1);
                }

                Route destRoute = CalculateRouteTo(selectedClient.ResolvedStart, selectedClient.ResolvedEnd);
                if (destRoute == null)
                {
                    selectedClient.Accessible = false;
                    CyclePotentialClients.RemoveAt(0);
                    return(1);
                }
                else if (selectedClient.Used || !selectedClient.Accessible)
                {
                    CyclePotentialClients.RemoveAt(0);
                    return(1);
                }
                else
                {
                    selectedClient.Used = true;
                }
                double distToDest = destRoute.TotalDistance;


                //Console.WriteLine("Delivering passenger to destination");
                DateTime rideStartTime  = LocalTime;
                double   startLatitude  = Latitude;
                double   startLongitude = Longitude;

                //OPTIMIZATION - later to be replaced by prebaked distance calculations
                RideToDestination(selectedClient.ResolvedStart, distToClient, DrivingState.TOCLIENT);
                RideToDestination(selectedClient.ResolvedEnd, distToDest, DrivingState.WITHCLIENT);

                selectedClient.Used = true;
                CompletedClients++;
                CyclePotentialClients.Clear();

                //Console.WriteLine("Time spent on road: " + (distToDest + distToClient) / carSpeedMeter);
                //Console.WriteLine("Passenger delivered! Made {0} EUR", selectedClient.RideValue);
                MoneyEarned += (float)selectedClient.RideValue;
                Log log = new Log(ID, selectedClient.Index, rideStartTime, LocalTime, selectedClient.RideValue, startLatitude, startLongitude, Latitude, Longitude, fullCharge - ChargeLeft, TimesCharged, LastDepotID);
                Logs.Add(log);

                ChargeLeft -= ((float)distToClient + (float)distToDest) / 1000f;
                State       = DrivingState.WAITING;

                return(1);
            }
            Console.WriteLine("No client, waiting for {0} minutes");

            LocalTime = LocalTime.AddMinutes(15);
            return(0);
        }