public void RideToDestination(RouterPoint endPoint, double distanceCovered, DrivingState endState) { Latitude = endPoint.Latitude; Longitude = endPoint.Longitude; CurrentResolvedLocation = endPoint; int seconds = (int)(distanceCovered / carSpeedMeter + 1d); LocalTime = LocalTime.AddSeconds(seconds); State = endState; }
public void Test_AddSeconds_Ok() { LocalTime localTime = new LocalTime(2, 4, 8); int secondsToAdd = 16; LocalTime localTimeResult = new LocalTime(2, 4, 24); Assert.Equal(localTimeResult, localTime.AddSeconds(secondsToAdd)); localTime = new LocalTime(2, 4, 8); secondsToAdd = 52; localTimeResult = new LocalTime(2, 5, 0); Assert.Equal(localTimeResult, localTime.AddSeconds(secondsToAdd)); localTime = new LocalTime(2, 4, 8); secondsToAdd = 64; localTimeResult = new LocalTime(2, 5, 12); Assert.Equal(localTimeResult, localTime.AddSeconds(secondsToAdd)); localTime = new LocalTime(2, 4, 8); secondsToAdd = 128; localTimeResult = new LocalTime(2, 6, 16); Assert.Equal(localTimeResult, localTime.AddSeconds(secondsToAdd)); localTime = new LocalTime(2, 4, 8); secondsToAdd = -4; localTimeResult = new LocalTime(2, 4, 4); Assert.Equal(localTimeResult, localTime.AddSeconds(secondsToAdd)); localTime = new LocalTime(2, 4, 8); secondsToAdd = -16; localTimeResult = new LocalTime(2, 3, 52); Assert.Equal(localTimeResult, localTime.AddSeconds(secondsToAdd)); localTime = new LocalTime(2, 4, 8); secondsToAdd = -124; localTimeResult = new LocalTime(2, 2, 4); Assert.Equal(localTimeResult, localTime.AddSeconds(secondsToAdd)); }
// 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); }