public static List <Location> simpleRoute(SquareGrid grid, Location start, Location finish) { List <Location> path = new List <Location>(); AStarSearch astar = new AStarSearch(grid, start, finish); path = astar.ReconstructPath(start, finish, astar.cameFrom); return(path); }
private void generateSpace(int areaSize) { grid = new SquareGrid(areaSize, areaSize, BaseConstants.areaHeight); GridGeneration.fillGrid(grid, areaSize, BaseConstants.areaHeight); groundGrid = new SquareGrid(areaSize, areaSize, 1); groundGrid.walls = grid.walls.Where(location => location.z == 0).ToHashSet(); outputTextBox.Text += $"Space of {areaSize * BaseConstants.PolygonSize / 1000} km2 ({areaSize * areaSize * BaseConstants.areaHeight} polygons) generated successfully\n"; }
private void buttonTSPtruck_Click(object sender, EventArgs e) { var areaSize = Int16.Parse(areaSizeUpDown.Value.ToString()) * 1000 / BaseConstants.PolygonSize; //sets size in nodes Depot = new Location(areaSize / 2, areaSize / 2, 0); generateSpace(areaSize, 1); groundGrid = grid; var orders = generateOrders(areaSize); doTruck(orders); }
public static void buildUnitRoute(SquareGrid grid, List <Order> orders, Truck truck) { while (orders.Count() > 0) { if (truck.status.Equals(Status.Ready)) { var availableDrones = truck.drones.Where(drone => drone.status.Equals(Status.Available)).ToList(); var routeSheets = selectDronesOrders(availableDrones, orders); //TODO: рассмотри кейс, когда их ноль. Сейчас грузовик никуда не поедет. List <Location> truckRoute = new List <Location>(); truckRoute.Add(truck.currentPosition); foreach (var sheet in routeSheets) { sheet.drone.status = Status.Prepairing; truckRoute.Add(sheet.meetingPoint); } if (routeSheets.Count() == 0) { truckRoute.Add(new Location(orders.First().x, orders.First().y, 0)); } for (int i = 0; i < truckRoute.Count() - 1; i++) { var awaitingDrones = truck.drones.Where(drone => drone.status.Equals(Status.Awaitng) && drone.currentPosition.Equals(truck.currentPosition)).ToList(); Vehicle.compareAndUpdateTime(awaitingDrones, truck); Drone.retrieveDrones(truck, awaitingDrones); Truck.doTruckDelivery(grid, truck, truckRoute[i], truckRoute[i + 1]); var deliveredOrder = orders.Find(order => order.x == truckRoute[i + 1].x && order.y == truckRoute[i + 1].y); orders.Remove(deliveredOrder); foreach (var sheet in routeSheets) { Drone.loadDrones(truck, sheet.drone); Drone.doDroneDelivery(sheet, grid); deliveredOrder = orders.Find(order => order.x == sheet.deliveryPoint.x && order.y == sheet.deliveryPoint.y); orders.Remove(deliveredOrder); } routeSheets.Clear(); } Order.sortOrders(ref orders, truck.currentPosition); } } }
public static void doTruckDelivery(SquareGrid grid, Truck truck, Location start, Location deliveryLocation) { truck.status = Status.OnMission; var path = simpleRoute(grid, start, deliveryLocation); var pathLength = path.Count * BaseConstants.PolygonSize; var deliveryTime = pathLength / BaseConstants.TruckSpeed + BaseConstants.DropDeliveryTime; truck.time += deliveryTime; truck.status = Status.Ready; truck.log += $"\nTruck finished delivery to {deliveryLocation} at {deliveryTime.ToString(@"hh\:mm\:ss\")}"; truck.currentPosition = deliveryLocation; var availableDrones = truck.drones.Where(x => x.status.Equals(Status.Available)).ToList(); foreach (var drone in availableDrones) { drone.currentPosition = truck.currentPosition; } }
public static List <Order> generateOrders(SquareGrid grid, Location Depot, int ordersCount, int areaSize) { Random rnd = new Random(); List <Order> ordersList = new List <Order>(); while (ordersCount > 0) { var x = rnd.Next(areaSize); var y = rnd.Next(areaSize); var isWall = true; while (isWall) { if (grid.walls.Contains(new Location(x, y, 0))) { x = rnd.Next(areaSize); y = rnd.Next(areaSize); } else { isWall = false; } } var distanceFromDepot = Math.Sqrt(Math.Pow((x - Depot.x), 2.0) + Math.Pow((y - Depot.y), 2.0)) * BaseConstants.PolygonSize / 1000; bool isDroneFriendly = false; if (rnd.Next(2) == 0) { if (distanceFromDepot < BaseConstants.DroneRange) { isDroneFriendly = true; } } ordersList.Add(new Order(x, y, isDroneFriendly, rnd.Next(100, 6000))); ordersCount--; } return(ordersList); }
public static string[] DrawGrid(SquareGrid grid, AStarSearch astar, Location start, Location goal) { string[] output = new string[4]; // Печать массива cameFrom for (int z = 0; z < 4; z++) { for (var y = 0; y < 41; y++) { for (var x = 0; x < 41; x++) { Location id = new Location(x, y, z); Location ptr = id; if (!astar.cameFrom.TryGetValue(id, out ptr)) { ptr = id; } if (start.x == x && start.y == y && start.z == z) /*Console.Write("\u2191 ");*/ x { ++; output[z] += "S"; } if (goal.x == x && goal.y == y && goal.z == z) /*Console.Write("\u2191 ");*/ x { ++; output[z] += "F"; } if (grid.walls.Contains(id)) /*Console.Write("##");*/ output {
public static void doDroneDelivery(FSTSPRouting.droneRouteSheet routeSheet, SquareGrid grid) { List <Location> path = new List <Location>(); //routeSheet.drone.time += BaseConstants.DroneLoadTime; routeSheet.drone.status = Status.OnMission; path.AddRange(simpleRoute(grid, routeSheet.start, routeSheet.deliveryPoint)); var pathLength = path.Count * BaseConstants.PolygonSize; var deliveryTime = pathLength / BaseConstants.DroneSpeed + BaseConstants.DropDeliveryTime; routeSheet.drone.currentPosition = routeSheet.deliveryPoint; routeSheet.drone.time += deliveryTime; routeSheet.drone.log += $"\nDrone finished delivery to {routeSheet.deliveryPoint} at {deliveryTime.ToString(@"hh\:mm\:ss\")}"; path.Clear(); path.AddRange(simpleRoute(grid, routeSheet.deliveryPoint, routeSheet.meetingPoint)); pathLength = path.Count * BaseConstants.PolygonSize; deliveryTime = pathLength / BaseConstants.DroneSpeed; routeSheet.drone.currentPosition = routeSheet.meetingPoint; routeSheet.drone.time += deliveryTime; routeSheet.drone.log += $"\nDrone arrived to meeting point: {routeSheet.deliveryPoint} at {deliveryTime.ToString(@"hh\:mm\:ss\")}"; routeSheet.drone.status = Status.Awaitng; }
private void generateSpace(int areaSize, int areaHeight) { grid = new SquareGrid(areaSize, areaSize, areaHeight); GridGeneration.fillGrid(grid, areaSize, areaHeight); outputTextBox.Text += $"Space of {areaSize * BaseConstants.PolygonSize / 1000} km2 ({areaSize * areaSize} polygons) generated successfully\n"; }
public static void fillGrid(SquareGrid grid, int areaSize, int areaHeight) { bool[,,] obstacles = new bool[areaSize, areaSize, areaHeight]; Random rnd = new Random(); int threshold = 65; for (int x = 0; x < areaSize; x++) { for (int y = 0; y < areaSize; y++) { obstacles[x, y, 0] = true; } } for (int x = 0; x < areaSize; x++) { if (rnd.Next(100) > threshold) { for (int y = 0; y < areaSize; y++) { obstacles[x, y, 0] = false; } } } for (int y = 0; y < areaSize; y++) { if (rnd.Next(100) > threshold) { for (int x = 0; x < areaSize; x++) { obstacles[x, y, 0] = false; } } } for (int x = 0; x < areaSize; x++) { for (int y = 0; y < areaSize; y++) { if (obstacles[x, y, 0]) { grid.walls.Add(new Location(x, y, 0)); } } } for (int z = 1; z < areaHeight; z++) { for (int x = 0; x < areaSize; x++) { for (int y = 0; y < areaSize; y++) { if ((rnd.Next(100) > threshold || rnd.Next(100) > 20) && obstacles[x, y, z - 1]) { obstacles[x, y, z] = true; grid.walls.Add(new Location(x, y, z)); } else { obstacles[x, y, z] = false; } } } threshold -= 5; } }