This project simulates an ant colony navigating through a graph of rooms to reach the end room using optimized pathfinding. The program ensures efficient movement of ants by prioritizing paths and handling edge cases such as bottlenecks or direct paths from the start to the end room.
- Efficient Pathfinding: Ants follow the shortest path when appropriate and avoid unnecessary moves.
- Multiple Path Handling: Supports multiple paths and ensures ants distribute themselves efficiently.
- Edge Case Handling: Includes logic for cases with no intermediate rooms or when the shortest path is blocked.
- Input: The program reads an input file that defines the graph, including rooms, paths, and the number of ants.
- Initialization:
- Rooms and paths are parsed.
- Ants are assigned to the start room.
- Simulation:
- Ants move along paths toward the end room.
- The shortest path is prioritized for the last ant in the start room.
- Output: The movements of ants are printed turn by turn until all ants reach the end room.
- Shortest Path Priority:
- Identifies the shortest path and reserves it for the last ant.
- Other ants distribute across available paths to minimize congestion.
- Room Occupancy:
- Tracks which ants occupy which rooms.
- Prevents multiple ants from entering the same room in a turn.
- Direct Start-to-End Path:
- If the shortest path connects the start and end directly, ants use it efficiently.
- Blocked Paths:
- Ants wait if their preferred path is blocked, ensuring fairness and preventing deadlocks.
The input file should follow this structure:
- Number of Ants: The first line contains the number of ants.
- Rooms: Each subsequent line defines a room.
- Paths: Defines connections between rooms.
- Example:
4 Start 0 Room1 1 End 2 Start-Room1 Room1-End
The output displays the movements of ants in each turn:
L1-Room1 L2-End
L3-Room1
L1-Room1: Ant 1 moves to Room1.L2-End: Ant 2 reaches the end room.
- Clone the repository:
git clone <repository_url> cd <repository_name>
- Run the program with an input file:
go run main/main.go <input_file>
- Example:
go run main/main.go example00.txt
if len(colony.Ants) == 1 && path != shortPath {
continue
}- Ensures the last ant takes the shortest path.
if nextRoom != nil && !nextRoom.Occupied {
ant.CurrentPosition = nextRoom
if nextRoom.Name == colony.End.Name {
colony.End.Quantity++
}
}- Moves ants forward and updates room occupancy.
- Fork the repository.
- Create a feature branch:
git checkout -b feature/new-feature
- Commit changes and push:
git commit -m "Add new feature" git push origin feature/new-feature - Create a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
Happy Coding!