用HTML做
- User
用HTML做
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
用HTML做
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
好的,我来用HTML、CSS和JavaScript实现一个蚂蚁找食物的算法可视化页面。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>蚂蚁找食物算法可视化</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f0f0f0;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: white;
border-radius: 10px;
padding: 20px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
h1 {
text-align: center;
color: #333;
margin-bottom: 30px;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 15px;
margin-bottom: 20px;
padding: 15px;
background: #f8f9fa;
border-radius: 8px;
}
.control-group {
display: flex;
flex-direction: column;
gap: 5px;
}
.control-group label {
font-size: 12px;
color: #666;
font-weight: bold;
}
.control-group input, .control-group button {
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
}
.control-group button {
background: #007bff;
color: white;
cursor: pointer;
border: none;
transition: background 0.3s;
}
.control-group button:hover {
background: #0056b3;
}
.control-group button:disabled {
background: #6c757d;
cursor: not-allowed;
}
.simulation-area {
display: flex;
gap: 20px;
}
.grid-container {
flex: 1;
}
#grid {
border: 2px solid #333;
cursor: crosshair;
}
.info-panel {
width: 250px;
background: #f8f9fa;
padding: 15px;
border-radius: 8px;
height: fit-content;
}
.info-panel h3 {
margin-top: 0;
color: #333;
}
.info-item {
margin: 10px 0;
padding: 8px;
background: white;
border-radius: 4px;
border-left: 4px solid #007bff;
}
.legend {
margin-top: 20px;
}
.legend-item {
display: flex;
align-items: center;
margin: 8px 0;
}
.legend-color {
width: 20px;
height: 20px;
margin-right: 10px;
border: 1px solid #333;
}
.status {
text-align: center;
margin: 15px 0;
padding: 10px;
border-radius: 5px;
font-weight: bold;
}
.status.running {
background: #d4edda;
color: #155724;
}
.status.stopped {
background: #f8d7da;
color: #721c24;
}
.status.completed {
background: #d1ecf1;
color: #0c5460;
}
</style>
</head>
<body>
<div class="container">
<h1>🐜 蚂蚁找食物算法可视化</h1>
<div class="controls">
<div class="control-group">
<label>网格大小</label>
<input type="number" id="gridSize" value="25" min="10" max="50">
</div>
<div class="control-group">
<label>蚂蚁数量</label>
<input type="number" id="antCount" value="20" min="1" max="100">
</div>
<div class="control-group">
<label>最大步数</label>
<input type="number" id="maxSteps" value="200" min="50" max="500">
</div>
<div class="control-group">
<label>信息素挥发率</label>
<input type="number" id="evaporationRate" value="0.05" min="0.01" max="0.2" step="0.01">
</div>
<div class="control-group">
<label>信息素强度</label>
<input type="number" id="pheromoneStrength" value="10" min="1" max="50">
</div>
<div class="control-group">
<label>动画速度(ms)</label>
<input type="number" id="animationSpeed" value="50" min="10" max="500">
</div>
<div class="control-group">
<label>操作</label>
<button id="initBtn">初始化</button>
</div>
<div class="control-group">
<label> </label>
<button id="startBtn">开始</button>
</div>
<div class="control-group">
<label> </label>
<button id="stopBtn">停止</button>
</div>
<div class="control-group">
<label> </label>
<button id="resetBtn">重置</button>
</div>
</div>
<div class="simulation-area">
<div class="grid-container">
<canvas id="grid" width="600" height="600"></canvas>
<div id="status" class="status stopped">点击"初始化"开始设置环境</div>
</div>
<div class="info-panel">
<h3>实时信息</h3>
<div class="info-item">
<strong>当前迭代:</strong> <span id="iteration">0</span>
</div>
<div class="info-item">
<strong>活跃蚂蚁:</strong> <span id="activeAnts">0</span>
</div>
<div class="info-item">
<strong>找到食物:</strong> <span id="foundFood">0</span>
</div>
<div class="info-item">
<strong>最短路径:</strong> <span id="shortestPath">未找到</span>
</div>
<div class="info-item">
<strong>平均路径:</strong> <span id="averagePath">-</span>
</div>
<div class="legend">
<h4>图例</h4>
<div class="legend-item">
<div class="legend-color" style="background: #4CAF50;"></div>
<span>蚁巢</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background: #FF9800;"></div>
<span>食物</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background: #333;"></div>
<span>障碍物</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background: #2196F3;"></div>
<span>蚂蚁</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background: rgba(255,0,0,0.5);"></div>
<span>信息素</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background: #FF0000; border: 2px solid #000;"></div>
<span>最佳路径</span>
</div>
</div>
<div style="margin-top: 20px; font-size: 12px; color: #666;">
<p><strong>使用说明:</strong></p>
<p>1. 调整参数后点击"初始化"</p>
<p>2. 在网格上点击设置蚁巢(绿色)</p>
<p>3. 再次点击设置食物(橙色)</p>
<p>4. 可选择性添加障碍物(黑色)</p>
<p>5. 点击"开始"运行算法</p>
</div>
</div>
</div>
</div>
<script>
class Ant {
constructor(x, y, maxSteps) {
this.startX = x;
this.startY = y;
this.x = x;
this.y = y;
this.path = [{x, y}];
this.foundFood = false;
this.steps = 0;
this.maxSteps = maxSteps;
this.visited = new Set();
this.visited.add(`${x},${y}`);
}
reset() {
this.x = this.startX;
this.y = this.startY;
this.path = [{x: this.x, y: this.y}];
this.foundFood = false;
this.steps = 0;
this.visited.clear();
this.visited.add(`${this.x},${this.y}`);
}
move(grid, pheromones, foodX, foodY, alpha = 1, beta = 2) {
if (this.foundFood || this.steps >= this.maxSteps) {
return false;
}
const neighbors = this.getValidNeighbors(grid);
if (neighbors.length === 0) {
return false;
}
// 计算每个邻居的吸引力
const probabilities = neighbors.map(neighbor => {
const pheromone = pheromones[neighbor.y][neighbor.x] || 0.1;
const distance = Math.sqrt(
Math.pow(neighbor.x - foodX, 2) + Math.pow(neighbor.y - foodY, 2)
);
const heuristic = distance === 0 ? 100 : 1 / (distance + 0.1);
return Math.pow(pheromone, alpha) * Math.pow(heuristic, beta);
});
// 选择下一个位置
const totalProb = probabilities.reduce((sum, p) => sum + p, 0);
const normalizedProbs = probabilities.map(p => p / totalProb);
const rand = Math.random();
let cumulative = 0;
let selectedIndex = 0;
for (let i = 0; i < normalizedProbs.length; i++) {
cumulative += normalizedProbs[i];
if (rand <= cumulative) {
selectedIndex = i;
break;
}
}
const nextPos = neighbors[selectedIndex];
this.x = nextPos.x;
this.y = nextPos.y;
this.path.push({x: this.x, y: this.y});
this.steps++;
this.visited.add(`${this.x},${this.y}`);
// 检查是否找到食物
if (this.x === foodX && this.y === foodY) {
this.foundFood = true;
return true;
}
return false;
}
getValidNeighbors(grid) {
const neighbors = [];
const directions = [{x: 0, y: 1}, {x: 1, y: 0}, {x: 0, y: -1}, {x: -1, y: 0}];
for (const dir of directions) {
const newX = this.x + dir.x;
const newY = this.y + dir.y;
if (newX >= 0 && newX < grid[0].length &&
newY >= 0 && newY < grid.length &&
grid[newY][newX] !== 'obstacle') {
// 避免立即回头
if (this.path.length > 1) {
const prevPos = this.path[this.path.length - 2];
if (newX === prevPos.x && newY === prevPos.y) {
continue;
}
}
neighbors.push({x: newX, y: newY});
}
}
return neighbors;
}
}
class AntColonySimulation {
constructor() {
this.canvas = document.getElementById('grid');
this.ctx = this.canvas.getContext('2d');
this.gridSize = 25;
this.cellSize = 0;
this.grid = [];
this.pheromones = [];
this.ants = [];
this.nestX = null;
this.nestY = null;
this.foodX = null;
this.foodY = null;
this.isRunning = false;
this.iteration = 0;
this.bestPath = null;
this.bestPathLength = Infinity;
this.setupMode = 'nest'; // 'nest', 'food', 'obstacle'
this.foundPaths = [];
this.initializeEventListeners();
}
initializeEventListeners() {
document.getElementById('initBtn').addEventListener('click', () => this.initialize());
document.getElementById('startBtn').addEventListener('click', () => this.start());
document.getElementById('stopBtn').addEventListener('click', () => this.stop());
document.getElementById('resetBtn').addEventListener('click', () => this.reset());
this.canvas.addEventListener('click', (e) => this.handleCanvasClick(e));
}
initialize() {
this.gridSize = parseInt(document.getElementById('gridSize').value);
this.cellSize = 600 / this.gridSize;
// 初始化网格
this.grid = Array(this.gridSize).fill().map(() => Array(this.gridSize).fill('empty'));
this.pheromones = Array(this.gridSize).fill().map(() => Array(this.gridSize).fill(0.1));
this.nestX = null;
this.nestY = null;
this.foodX = null;
this.foodY = null;
this.ants = [];
this.iteration = 0;
this.bestPath = null;
this.bestPathLength = Infinity;
this.foundPaths = [];
this.setupMode = 'nest';
this.updateStatus('请在网格上点击设置蚁巢位置');
this.updateInfo();
this.draw();
}
handleCanvasClick(e) {
if (this.isRunning)