1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#include <iostream> #include <vector> #include <cstdlib> #include <ctime> #include <algorithm> using namespace std; // Grid dimensions const int WIDTH = 10; const int HEIGHT = 10; // Directions for movement (right, down, left, up) const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; // Ant class class Ant { public: int x, y; Ant(int startX, int startY) : x(startX), y(startY) {} void move() { int direction = rand() % 4; x += dx[direction]; y += dy[direction]; x = max(0, min(WIDTH - 1, x)); y = max(0, min(HEIGHT - 1, y)); } }; // Main function int main() { srand(static_cast<unsigned>(time(0))); // Seed for random number generation vector<Ant> ants; int numAnts = 10; int startX = 0, startY = 0; int goalX = WIDTH - 1, goalY = HEIGHT - 1; // Initialize ants for (int i = 0; i < numAnts; ++i) { ants.emplace_back(startX, startY); } // Simulation loop for (int step = 0; step < 100; ++step) { cout << "Step " << step + 1 << ":" << endl; for (auto& ant : ants) { ant.move(); cout << "Ant at (" << ant.x << ", " << ant.y << ")" << endl; } // Check if any ant reached the goal bool goalReached = false; for (const auto& ant : ants) { if (ant.x == goalX && ant.y == goalY) { goalReached = true; break; } } if (goalReached) { cout << "An ant has reached the goal!" << endl; break; } cout << endl; } return 0; } |
Explanation
- Headers:
<iostream>
: For input and output operations.<vector>
: For using thestd::vector
container to store ants.<cstdlib>
: Forrand()
andsrand()
functions.<ctime>
: Fortime()
function to seed the random number generator.<algorithm>
: For themax
andmin
functions.
- Constants:
WIDTH
andHEIGHT
: Dimensions of the grid.dx
anddy
: Arrays representing possible movement directions (right, down, left, up).
Advertisement - Ant Class:
- Attributes:
x
andy
: Current position of the ant.
- Methods:
move()
: Moves the ant in a random direction and ensures it stays within grid boundaries.
- Attributes:
- Main Function:
- Initialization:
- Seeds the random number generator.
- Creates a vector of ants and initializes their starting position.
- Simulation Loop:
- Runs for a fixed number of steps (100 in this case).
- Each ant moves randomly in one of the four directions.
- Prints the position of each ant after each step.
- Checks if any ant has reached the goal position (
goalX
,goalY
). - If the goal is reached, the simulation ends early.
- Initialization:
Notes:
- Random Movement: The ants move randomly, which is a basic approach. In a more advanced ACO simulation, ants would deposit pheromones, and their movement would be influenced by pheromone levels.
- Grid Boundaries: The movement is constrained within the grid boundaries using
max
andmin
functions. - Simplification: This program provides a basic framework. Real ACO algorithms involve pheromone trails and probabilistic decision-making for more complex optimization tasks.
Advertisement