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
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Ant Colony Optimization with Controls</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
font-family: Arial, sans-serif;
|
10 |
+
display: flex;
|
11 |
+
flex-direction: column;
|
12 |
+
align-items: center;
|
13 |
+
justify-content: center;
|
14 |
+
height: 100vh;
|
15 |
+
margin: 0;
|
16 |
+
background: #f4f4f4;
|
17 |
+
}
|
18 |
+
canvas {
|
19 |
+
border: 1px solid #000;
|
20 |
+
background: #fff;
|
21 |
+
margin-top: 20px;
|
22 |
+
}
|
23 |
+
#controls {
|
24 |
+
margin-top: 20px;
|
25 |
+
display: flex;
|
26 |
+
flex-direction: column;
|
27 |
+
align-items: center;
|
28 |
+
}
|
29 |
+
.control-group {
|
30 |
+
margin: 10px 0;
|
31 |
+
display: flex;
|
32 |
+
align-items: center;
|
33 |
+
}
|
34 |
+
.control-group label {
|
35 |
+
margin-right: 10px;
|
36 |
+
}
|
37 |
+
button {
|
38 |
+
padding: 10px 20px;
|
39 |
+
font-size: 16px;
|
40 |
+
margin: 5px;
|
41 |
+
cursor: pointer;
|
42 |
+
}
|
43 |
+
</style>
|
44 |
+
</head>
|
45 |
+
<body>
|
46 |
+
<h1>Ant Colony Optimization Simulation</h1>
|
47 |
+
<canvas id="canvas" width="600" height="400"></canvas>
|
48 |
+
<div id="controls">
|
49 |
+
<div class="control-group">
|
50 |
+
<label for="numAnts">Number of Ants:</label>
|
51 |
+
<input type="number" id="numAnts" value="200" min="1" max="500">
|
52 |
+
</div>
|
53 |
+
<div class="control-group">
|
54 |
+
<label for="evaporationRate">Evaporation Rate:</label>
|
55 |
+
<input type="range" id="evaporationRate" min="0.8" max="0.99" step="0.01" value="0.95">
|
56 |
+
<span id="evaporationRateValue">0.95</span>
|
57 |
+
</div>
|
58 |
+
<div class="control-group">
|
59 |
+
<label for="pheromoneDeposit">Pheromone Deposit:</label>
|
60 |
+
<input type="number" id="pheromoneDeposit" value="5" min="1" max="20">
|
61 |
+
</div>
|
62 |
+
<div class="control-group">
|
63 |
+
<label for="gridSize">Grid Size:</label>
|
64 |
+
<input type="number" id="gridSize" value="10" min="5" max="20">
|
65 |
+
</div>
|
66 |
+
<button id="start">Start Simulation</button>
|
67 |
+
<button id="reset">Reset</button>
|
68 |
+
</div>
|
69 |
+
|
70 |
+
<script>
|
71 |
+
// Canvas setup
|
72 |
+
const canvas = document.getElementById('canvas');
|
73 |
+
const ctx = canvas.getContext('2d');
|
74 |
+
|
75 |
+
// Control elements
|
76 |
+
const numAntsInput = document.getElementById('numAnts');
|
77 |
+
const evaporationRateInput = document.getElementById('evaporationRate');
|
78 |
+
const pheromoneDepositInput = document.getElementById('pheromoneDeposit');
|
79 |
+
const gridSizeInput = document.getElementById('gridSize');
|
80 |
+
const evaporationRateValue = document.getElementById('evaporationRateValue');
|
81 |
+
|
82 |
+
// Simulation parameters
|
83 |
+
let gridSize = parseInt(gridSizeInput.value);
|
84 |
+
let gridWidth = canvas.width / gridSize;
|
85 |
+
let gridHeight = canvas.height / gridSize;
|
86 |
+
const nest = { x: 1, y: Math.floor(gridHeight / 2) };
|
87 |
+
const food = { x: gridWidth - 2, y: Math.floor(gridHeight / 2) };
|
88 |
+
let pheromoneGrid = Array.from({ length: gridWidth }, () => Array(gridHeight).fill(0));
|
89 |
+
let ants = [];
|
90 |
+
let numAnts = parseInt(numAntsInput.value);
|
91 |
+
let evaporationRate = parseFloat(evaporationRateInput.value);
|
92 |
+
let pheromoneDeposit = parseInt(pheromoneDepositInput.value);
|
93 |
+
|
94 |
+
// Initialize ants
|
95 |
+
function initializeAnts() {
|
96 |
+
ants = [];
|
97 |
+
for (let i = 0; i < numAnts; i++) {
|
98 |
+
ants.push({
|
99 |
+
x: nest.x,
|
100 |
+
y: nest.y,
|
101 |
+
hasFood: false,
|
102 |
+
});
|
103 |
+
}
|
104 |
+
}
|
105 |
+
|
106 |
+
// Random movement for ants
|
107 |
+
function randomMove(ant) {
|
108 |
+
const directions = [
|
109 |
+
{ dx: -1, dy: 0 }, // Left
|
110 |
+
{ dx: 1, dy: 0 }, // Right
|
111 |
+
{ dx: 0, dy: -1 }, // Up
|
112 |
+
{ dx: 0, dy: 1 }, // Down
|
113 |
+
];
|
114 |
+
const randomDirection = directions[Math.floor(Math.random() * directions.length)];
|
115 |
+
ant.x = Math.max(0, Math.min(gridWidth - 1, ant.x + randomDirection.dx));
|
116 |
+
ant.y = Math.max(0, Math.min(gridHeight - 1, ant.y + randomDirection.dy));
|
117 |
+
}
|
118 |
+
|
119 |
+
// Draw grid and elements
|
120 |
+
function draw() {
|
121 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
122 |
+
|
123 |
+
// Draw pheromone trails
|
124 |
+
for (let x = 0; x < gridWidth; x++) {
|
125 |
+
for (let y = 0; y < gridHeight; y++) {
|
126 |
+
if (pheromoneGrid[x][y] > 0) {
|
127 |
+
const intensity = pheromoneGrid[x][y] / 10;
|
128 |
+
ctx.fillStyle = `rgba(0, 0, 255, ${intensity})`;
|
129 |
+
ctx.fillRect(x * gridSize, y * gridSize, gridSize, gridSize);
|
130 |
+
}
|
131 |
+
}
|
132 |
+
}
|
133 |
+
|
134 |
+
// Draw nest and food
|
135 |
+
ctx.fillStyle = 'green';
|
136 |
+
ctx.fillRect(nest.x * gridSize, nest.y * gridSize, gridSize, gridSize);
|
137 |
+
ctx.fillStyle = 'red';
|
138 |
+
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize, gridSize);
|
139 |
+
|
140 |
+
// Draw ants
|
141 |
+
ctx.fillStyle = 'black';
|
142 |
+
ants.forEach(ant => {
|
143 |
+
ctx.beginPath();
|
144 |
+
ctx.arc((ant.x + 0.5) * gridSize, (ant.y + 0.5) * gridSize, gridSize / 4, 0, Math.PI * 2);
|
145 |
+
ctx.fill();
|
146 |
+
});
|
147 |
+
}
|
148 |
+
|
149 |
+
// Move ants
|
150 |
+
function moveAnts() {
|
151 |
+
ants.forEach(ant => {
|
152 |
+
randomMove(ant); // Move randomly
|
153 |
+
|
154 |
+
// Check if ant reaches food or nest
|
155 |
+
if (ant.x === food.x && ant.y === food.y) {
|
156 |
+
ant.hasFood = true;
|
157 |
+
}
|
158 |
+
if (ant.x === nest.x && ant.y === nest.y) {
|
159 |
+
ant.hasFood = false;
|
160 |
+
}
|
161 |
+
|
162 |
+
// Deposit pheromone if carrying food
|
163 |
+
if (ant.hasFood) {
|
164 |
+
pheromoneGrid[ant.x][ant.y] += pheromoneDeposit;
|
165 |
+
}
|
166 |
+
});
|
167 |
+
}
|
168 |
+
|
169 |
+
// Evaporate pheromones
|
170 |
+
function evaporatePheromones() {
|
171 |
+
for (let x = 0; x < gridWidth; x++) {
|
172 |
+
for (let y = 0; y < gridHeight; y++) {
|
173 |
+
pheromoneGrid[x][y] *= evaporationRate;
|
174 |
+
}
|
175 |
+
}
|
176 |
+
}
|
177 |
+
|
178 |
+
// Update grid size
|
179 |
+
function updateGridSize() {
|
180 |
+
gridSize = parseInt(gridSizeInput.value);
|
181 |
+
gridWidth = canvas.width / gridSize;
|
182 |
+
gridHeight = canvas.height / gridSize;
|
183 |
+
pheromoneGrid = Array.from({ length: gridWidth }, () => Array(gridHeight).fill(0));
|
184 |
+
initializeAnts();
|
185 |
+
}
|
186 |
+
|
187 |
+
// Simulation loop
|
188 |
+
function simulate() {
|
189 |
+
moveAnts();
|
190 |
+
evaporatePheromones();
|
191 |
+
draw();
|
192 |
+
requestAnimationFrame(simulate);
|
193 |
+
}
|
194 |
+
|
195 |
+
// Event listeners
|
196 |
+
document.getElementById('start').addEventListener('click', () => {
|
197 |
+
updateGridSize();
|
198 |
+
simulate();
|
199 |
+
});
|
200 |
+
|
201 |
+
document.getElementById('reset').addEventListener('click', () => {
|
202 |
+
updateGridSize();
|
203 |
+
pheromoneGrid.forEach((row, x) => row.fill(0));
|
204 |
+
draw();
|
205 |
+
});
|
206 |
+
|
207 |
+
numAntsInput.addEventListener('input', () => {
|
208 |
+
numAnts = parseInt(numAntsInput.value);
|
209 |
+
initializeAnts();
|
210 |
+
});
|
211 |
+
|
212 |
+
evaporationRateInput.addEventListener('input', () => {
|
213 |
+
evaporationRate = parseFloat(evaporationRateInput.value);
|
214 |
+
evaporationRateValue.textContent = evaporationRate.toFixed(2);
|
215 |
+
});
|
216 |
+
|
217 |
+
pheromoneDepositInput.addEventListener('input', () => {
|
218 |
+
pheromoneDeposit = parseInt(pheromoneDepositInput.value);
|
219 |
+
});
|
220 |
+
|
221 |
+
gridSizeInput.addEventListener('input', () => {
|
222 |
+
updateGridSize();
|
223 |
+
});
|
224 |
+
</script>
|
225 |
+
</body>
|
226 |
+
</html>
|