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