-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStable_diamandBETA.html
734 lines (656 loc) · 27.5 KB
/
Stable_diamandBETA.html
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Optimized DIAMOND Simulation with Combined Agent Strategies</title>
<style>
/* Styles for the simulation */
body {
font-family: Arial, sans-serif;
background-color: #fafafa;
margin: 0;
padding: 20px;
}
.container {
max-width: 1280px;
margin: auto;
}
h1 {
color: #222;
text-align: center;
}
.canvas-container {
display: flex;
justify-content: space-around;
flex-wrap: wrap;
}
canvas {
border: 1px solid #999;
background-color: #fff;
margin: 10px;
}
.controls {
margin-top: 20px;
display: flex;
flex-wrap: wrap;
gap: 10px;
align-items: center;
}
.controls label {
margin-right: 5px;
}
.info-panel {
margin-top: 20px;
background-color: #eaeaea;
padding: 15px;
border-radius: 8px;
}
.agent-info {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.agent-info div {
flex: 1;
}
.slider-label {
display: flex;
align-items: center;
}
.slider-label input {
margin-left: 5px;
}
.data-visualization {
margin-top: 20px;
}
.chart-container {
width: 100%;
height: 300px;
}
</style>
</head>
<body>
<div class="container">
<h1>Optimized DIAMOND Simulation with Combined Agent Strategies</h1>
<div class="canvas-container">
<div>
<h2>World State</h2>
<canvas id="world-canvas" width="600" height="600"></canvas>
</div>
<div>
<h2>Agent's Prediction</h2>
<canvas id="prediction-canvas" width="600" height="600"></canvas>
</div>
</div>
<div class="controls">
<button id="reset-button">Reset</button>
<button id="step-button">Step</button>
<button id="run-button">Run</button>
<label for="diffusion-steps">Diffusion Steps:</label>
<input type="number" id="diffusion-steps" value="1" min="1" max="3">
<label for="learning-rate">Learning Rate:</label>
<input type="number" id="learning-rate" value="0.1" min="0.01" max="1" step="0.01">
<div class="slider-label">
<label for="agent-speed">Agent Speed:</label>
<input type="range" id="agent-speed" value="5" min="1" max="10">
</div>
<label for="num-agents">Number of Agents:</label>
<input type="number" id="num-agents" value="1" min="1" max="5">
</div>
<div class="info-panel">
<div class="agent-info" id="agent-info">
<!-- Agent information will be populated here -->
</div>
<p>Total Steps: <span id="total-steps">0</span></p>
<p>Average Prediction Accuracy: <span id="prediction-accuracy">0%</span></p>
</div>
<div class="data-visualization">
<h2>Agent Performance Metrics</h2>
<canvas id="reward-chart" class="chart-container"></canvas>
<canvas id="energy-chart" class="chart-container"></canvas>
</div>
</div>
<!-- Include Chart.js library before the simulation script -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.min.js"></script>
<!-- Simulation script -->
<script>
// Constants
const GRID_SIZE = 60;
const CELL_SIZE = 10;
const CANVAS_SIZE = GRID_SIZE * CELL_SIZE;
const FOG_OF_WAR_RADIUS = 5;
const MAX_ENERGY = 200; // Increased from 100 to 200
// DOM elements
const worldCanvas = document.getElementById('world-canvas');
const predictionCanvas = document.getElementById('prediction-canvas');
const worldCtx = worldCanvas.getContext('2d');
const predictionCtx = predictionCanvas.getContext('2d');
const resetButton = document.getElementById('reset-button');
const stepButton = document.getElementById('step-button');
const runButton = document.getElementById('run-button');
const diffusionStepsInput = document.getElementById('diffusion-steps');
const learningRateInput = document.getElementById('learning-rate');
const agentSpeedInput = document.getElementById('agent-speed');
const numAgentsInput = document.getElementById('num-agents');
const agentInfoContainer = document.getElementById('agent-info');
const totalStepsDisplay = document.getElementById('total-steps');
const predictionAccuracyDisplay = document.getElementById('prediction-accuracy');
const rewardChartCtx = document.getElementById('reward-chart').getContext('2d');
const energyChartCtx = document.getElementById('energy-chart').getContext('2d');
// Helper functions for Base64 encoding/decoding Unicode strings
function b64EncodeUnicode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
function b64DecodeUnicode(str) {
return decodeURIComponent(Array.prototype.map.call(atob(str), function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
// Simulation classes
class Environment {
constructor(gridSize) {
this.gridSize = gridSize;
this.state = this.createInitialState();
this.obstacles = this.createObstacles();
this.terrain = this.createTerrain();
this.traps = this.createTraps();
this.movingObstacles = this.createMovingObstacles();
this.energyPacks = this.createEnergyPacks();
}
createInitialState() {
let state = Array.from({ length: this.gridSize }, () =>
new Float32Array(this.gridSize).fill(0)
);
for (let i = 0; i < 20; i++) {
const x = Math.floor(Math.random() * this.gridSize);
const y = Math.floor(Math.random() * this.gridSize);
state[y][x] = Math.random() * 5 + 5;
}
return state;
}
createObstacles() {
let obstacles = Array.from({ length: this.gridSize }, () =>
new Array(this.gridSize).fill(false)
);
for (let i = 0; i < 100; i++) { // Adjusted obstacle count
const x = Math.floor(Math.random() * this.gridSize);
const y = Math.floor(Math.random() * this.gridSize);
obstacles[y][x] = true;
}
return obstacles;
}
createMovingObstacles() {
let movingObstacles = [];
for (let i = 0; i < 10; i++) {
const x = Math.floor(Math.random() * this.gridSize);
const y = Math.floor(Math.random() * this.gridSize);
movingObstacles.push({ x, y, dx: Math.random() < 0.5 ? 1 : -1, dy: Math.random() < 0.5 ? 1 : -1 });
}
return movingObstacles;
}
updateMovingObstacles() {
this.movingObstacles.forEach(obstacle => {
const oldX = obstacle.x;
const oldY = obstacle.y;
obstacle.x += obstacle.dx;
obstacle.y += obstacle.dy;
if (obstacle.x < 0 || obstacle.x >= this.gridSize) {
obstacle.dx *= -1;
obstacle.x += obstacle.dx * 2;
}
if (obstacle.y < 0 || obstacle.y >= this.gridSize) {
obstacle.dy *= -1;
obstacle.y += obstacle.dy * 2;
}
this.obstacles[oldY][oldX] = false;
if (!this.obstacles[obstacle.y][obstacle.x]) {
this.obstacles[obstacle.y][obstacle.x] = true;
} else {
obstacle.dx *= -1;
obstacle.dy *= -1;
obstacle.x = oldX;
obstacle.y = oldY;
}
});
}
createTerrain() {
let terrain = Array.from({ length: this.gridSize }, () =>
new Array(this.gridSize).fill('normal')
);
return terrain;
}
createTraps() {
let traps = Array.from({ length: this.gridSize }, () =>
new Array(this.gridSize).fill(false)
);
return traps;
}
createEnergyPacks() {
let energyPacks = [];
for (let i = 0; i < 30; i++) { // Increased number of energy packs
const x = Math.floor(Math.random() * this.gridSize);
const y = Math.floor(Math.random() * this.gridSize);
energyPacks.push({ x, y });
}
return energyPacks;
}
updateEnergyPacks() {
const maxEnergyPacks = 30; // Desired number of energy packs
const packsToAdd = maxEnergyPacks - this.energyPacks.length;
for (let i = 0; i < packsToAdd; i++) {
const x = Math.floor(Math.random() * this.gridSize);
const y = Math.floor(Math.random() * this.gridSize);
if (!this.obstacles[y][x]) {
this.energyPacks.push({ x, y });
}
}
}
diffuse(steps) {
for (let s = 0; s < steps; s++) {
let newState = this.state.map(row => new Float32Array(row));
for (let y = 0; y < this.gridSize; y++) {
for (let x = 0; x < this.gridSize; x++) {
if (this.obstacles[y][x]) continue;
let sum = this.state[y][x];
let count = 1;
const neighbors = [
[x - 1, y],
[x + 1, y],
[x, y - 1],
[x, y + 1]
];
neighbors.forEach(([nx, ny]) => {
if (
nx >= 0 &&
nx < this.gridSize &&
ny >= 0 &&
ny < this.gridSize &&
!this.obstacles[ny][nx]
) {
sum += this.state[ny][nx];
count++;
}
});
newState[y][x] = sum / count;
}
}
this.state = newState;
}
}
}
class Agent {
constructor(environment, id) {
this.id = id;
this.env = environment;
this.position = [CANVAS_SIZE / 2, CANVAS_SIZE / 2];
this.predictionState = this.env.createInitialState();
this.path = [];
this.totalReward = 0;
this.totalSteps = 0;
this.energy = MAX_ENERGY;
}
move() {
if (this.energy <= 0) {
console.log(`Agent ${this.id} has run out of energy.`);
return;
}
const speed = parseInt(agentSpeedInput.value);
let dx = 0, dy = 0;
[dx, dy] = this.combinedStrategyMove(speed);
let newX = this.position[0] + dx;
let newY = this.position[1] + dy;
newX = Math.max(0, Math.min(CANVAS_SIZE - 1, newX));
newY = Math.max(0, Math.min(CANVAS_SIZE - 1, newY));
const gridX = Math.floor(newX / CELL_SIZE);
const gridY = Math.floor(newY / CELL_SIZE);
if (!this.env.obstacles[gridY][gridX]) {
this.position = [newX, newY];
this.path.push([...this.position]);
if (this.path.length > 100) {
this.path.shift();
}
} else {
console.log(`Agent ${this.id} encountered an obstacle.`);
}
this.energy -= 0.5; // Reduced energy consumption
}
combinedStrategyMove(speed) {
if (this.energy < MAX_ENERGY * 0.5) {
return this.moveTowardsEnergyPack(speed);
} else {
return this.randomMove(speed);
}
}
moveTowardsEnergyPack(speed) {
const x = Math.floor(this.position[0] / CELL_SIZE);
const y = Math.floor(this.position[1] / CELL_SIZE);
let closestPack = null;
let minDistance = Infinity;
this.env.energyPacks.forEach(pack => {
const distance = Math.abs(pack.x - x) + Math.abs(pack.y - y);
if (distance < minDistance) {
minDistance = distance;
closestPack = pack;
}
});
if (closestPack) {
const dx = Math.sign(closestPack.x - x);
const dy = Math.sign(closestPack.y - y);
return [dx * CELL_SIZE, dy * CELL_SIZE];
} else {
return this.randomMove(speed);
}
}
randomMove(speed) {
const angle = Math.random() * 2 * Math.PI;
const dx = Math.cos(angle) * speed;
const dy = Math.sin(angle) * speed;
return [dx, dy];
}
updatePrediction(learningRate) {
const x = Math.floor(this.position[0] / CELL_SIZE);
const y = Math.floor(this.position[1] / CELL_SIZE);
const observedReward = this.getCellReward(x, y);
this.predictionState[y][x] += learningRate * (observedReward - this.predictionState[y][x]);
}
getCellReward(x, y) {
let reward = this.env.state[y][x];
const energyPackIndex = this.env.energyPacks.findIndex(pack => pack.x === x && pack.y === y);
if (energyPackIndex !== -1) {
this.energy = Math.min(this.energy + 50, MAX_ENERGY);
this.env.energyPacks.splice(energyPackIndex, 1);
console.log(`Agent ${this.id} picked up an energy pack.`);
}
return reward;
}
getReward() {
const x = Math.floor(this.position[0] / CELL_SIZE);
const y = Math.floor(this.position[1] / CELL_SIZE);
const reward = this.env.state[y][x];
return reward;
}
}
class Simulation {
constructor() {
this.env = new Environment(GRID_SIZE);
this.agents = [];
this.isRunning = false;
this.totalSteps = 0;
this.dataLog = [];
this.rewardChart = null;
this.energyChart = null;
this.initializeAgents();
this.initializeCharts();
this.updateDisplay();
this.draw();
// Optimization variables
this.lastFrameTime = performance.now();
this.targetFPS = 30;
this.frameInterval = 1000 / this.targetFPS;
}
initializeAgents() {
const numAgents = parseInt(numAgentsInput.value);
this.agents = [];
for (let i = 0; i < numAgents; i++) {
this.agents.push(new Agent(this.env, i + 1));
}
this.updateAgentInfoDisplay();
}
initializeCharts() {
if (this.rewardChart) {
this.rewardChart.destroy();
}
if (this.energyChart) {
this.energyChart.destroy();
}
const labels = [];
const rewardDatasets = [];
const energyDatasets = [];
this.agents.forEach(agent => {
rewardDatasets.push({
label: `Agent ${agent.id}`,
data: [],
borderColor: `hsl(${(agent.id * 50) % 360}, 100%, 50%)`,
fill: false
});
energyDatasets.push({
label: `Agent ${agent.id}`,
data: [],
borderColor: `hsl(${(agent.id * 50) % 360}, 100%, 50%)`,
fill: false
});
});
this.rewardChart = new Chart(rewardChartCtx, {
type: 'line',
data: {
labels: labels,
datasets: rewardDatasets
},
options: {
responsive: true,
animation: false,
plugins: {
title: {
display: true,
text: 'Total Reward Over Time'
}
}
}
});
this.energyChart = new Chart(energyChartCtx, {
type: 'line',
data: {
labels: labels,
datasets: energyDatasets
},
options: {
responsive: true,
animation: false,
plugins: {
title: {
display: true,
text: 'Energy Level Over Time'
}
}
}
});
}
start() {
if (!this.isRunning) {
this.isRunning = true;
this.lastFrameTime = performance.now();
this.loop();
}
}
stop() {
this.isRunning = false;
}
reset() {
this.stop();
this.env = new Environment(GRID_SIZE);
this.initializeAgents();
this.totalSteps = 0;
this.dataLog = [];
this.initializeCharts();
this.updateDisplay();
this.draw();
}
step() {
const diffusionSteps = Math.min(parseInt(diffusionStepsInput.value), 3);
const learningRate = parseFloat(learningRateInput.value);
// Diffuse less frequently to improve performance
if (this.totalSteps % 2 === 0) {
this.env.diffuse(diffusionSteps);
}
// Regenerate energy packs periodically
if (this.totalSteps % 50 === 0) { // Every 50 steps
this.env.updateEnergyPacks();
}
this.env.updateMovingObstacles();
this.agents.forEach(agent => {
agent.move();
agent.updatePrediction(learningRate);
const reward = agent.getReward();
agent.totalReward += reward;
agent.totalSteps++;
this.dataLog.push({
agentId: agent.id,
step: this.totalSteps,
position: [...agent.position],
reward: reward,
energy: agent.energy
});
});
// Limit dataLog size to prevent memory issues
if (this.dataLog.length > 1000) {
this.dataLog.splice(0, this.dataLog.length - 1000);
}
this.totalSteps++;
this.updateDisplay();
this.updateCharts();
this.draw();
}
loop() {
if (!this.isRunning) return;
const currentTime = performance.now();
const elapsed = currentTime - this.lastFrameTime;
if (elapsed > this.frameInterval) {
this.lastFrameTime = currentTime - (elapsed % this.frameInterval);
this.step();
}
requestAnimationFrame(() => this.loop());
}
updateDisplay() {
totalStepsDisplay.textContent = this.totalSteps;
const avgAccuracy = this.calculateAveragePredictionAccuracy();
predictionAccuracyDisplay.textContent = `${(avgAccuracy * 100).toFixed(2)}%`;
this.updateAgentInfoDisplay();
}
updateAgentInfoDisplay() {
agentInfoContainer.innerHTML = '';
this.agents.forEach(agent => {
const [x, y] = agent.position;
const agentDiv = document.createElement('div');
agentDiv.innerHTML = `
<p><strong>Agent ${agent.id}</strong></p>
<p>Position: (${Math.round(x)}, ${Math.round(y)})</p>
<p>Reward: ${agent.totalReward.toFixed(2)}</p>
<p>Energy: ${agent.energy.toFixed(1)}</p>
<p>Steps: ${agent.totalSteps}</p>
`;
agentInfoContainer.appendChild(agentDiv);
});
}
updateCharts() {
if (!this.rewardChart || !this.energyChart) return;
// Limit the number of data points to prevent memory issues
const maxDataPoints = 100;
if (this.rewardChart.data.labels.length > maxDataPoints) {
this.rewardChart.data.labels.shift();
this.rewardChart.data.datasets.forEach(dataset => dataset.data.shift());
}
if (this.energyChart.data.labels.length > maxDataPoints) {
this.energyChart.data.labels.shift();
this.energyChart.data.datasets.forEach(dataset => dataset.data.shift());
}
this.rewardChart.data.labels.push(this.totalSteps);
this.energyChart.data.labels.push(this.totalSteps);
this.agents.forEach((agent, index) => {
if (this.rewardChart.data.datasets[index]) {
this.rewardChart.data.datasets[index].data.push(agent.totalReward);
}
if (this.energyChart.data.datasets[index]) {
this.energyChart.data.datasets[index].data.push(agent.energy);
}
});
// Update charts only if they are in the document
if (document.contains(this.rewardChart.canvas)) {
this.rewardChart.update('none');
}
if (document.contains(this.energyChart.canvas)) {
this.energyChart.update('none');
}
}
calculateAveragePredictionAccuracy() {
let totalAccuracy = 0;
this.agents.forEach(agent => {
let totalDiff = 0;
for (let y = 0; y < GRID_SIZE; y++) {
for (let x = 0; x < GRID_SIZE; x++) {
totalDiff += Math.abs(this.env.state[y][x] - agent.predictionState[y][x]);
}
}
const accuracy = 1 - (totalDiff / (GRID_SIZE * GRID_SIZE * 10));
totalAccuracy += accuracy;
});
return totalAccuracy / this.agents.length;
}
draw() {
worldCtx.clearRect(0, 0, CANVAS_SIZE, CANVAS_SIZE);
predictionCtx.clearRect(0, 0, CANVAS_SIZE, CANVAS_SIZE);
for (let y = 0; y < GRID_SIZE; y++) {
for (let x = 0; x < GRID_SIZE; x++) {
this.drawCell(worldCtx, x, y, this.env.state[y][x]);
this.drawCell(predictionCtx, x, y, this.agents[0].predictionState[y][x]);
}
}
// Draw agents
this.agents.forEach(agent => {
this.drawAgent(worldCtx, agent);
this.drawAgent(predictionCtx, agent);
});
}
drawCell(ctx, x, y, value) {
if (this.env.obstacles[y][x]) {
ctx.fillStyle = '#444';
} else if (this.env.energyPacks.some(pack => pack.x === x && pack.y === y)) {
ctx.fillStyle = '#FFD700'; // Gold color for energy packs
} else {
const intensity = Math.min(255, Math.floor(value * 25));
ctx.fillStyle = `rgb(0, ${intensity}, 0)`;
}
ctx.fillRect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
drawAgent(ctx, agent) {
ctx.fillStyle = `hsl(${(agent.id * 50) % 360}, 100%, 50%)`;
const [ax, ay] = agent.position;
ctx.beginPath();
ctx.arc(ax, ay, 5, 0, 2 * Math.PI);
ctx.fill();
ctx.strokeStyle = `hsla(${(agent.id * 50) % 360}, 100%, 50%, 0.5)`;
ctx.lineWidth = 2;
ctx.beginPath();
agent.path.forEach((pos, index) => {
if (index === 0) {
ctx.moveTo(pos[0], pos[1]);
} else {
ctx.lineTo(pos[0], pos[1]);
}
});
ctx.stroke();
}
}
let simulation = new Simulation();
resetButton.addEventListener('click', () => {
simulation.reset();
runButton.textContent = 'Run';
});
stepButton.addEventListener('click', () => simulation.step());
runButton.addEventListener('click', () => {
if (simulation.isRunning) {
simulation.stop();
runButton.textContent = 'Run';
} else {
simulation.start();
runButton.textContent = 'Stop';
}
});
numAgentsInput.addEventListener('change', () => {
simulation.initializeAgents();
simulation.initializeCharts();
});
</script>
</body>
</html>