Spaces:
Running
Running
File size: 3,913 Bytes
b40c1b0 d66854f b40c1b0 d66854f b40c1b0 d66854f 71961ca b40c1b0 d66854f b40c1b0 d66854f b40c1b0 d66854f b40c1b0 d66854f 71961ca d66854f b40c1b0 d66854f b40c1b0 |
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 |
// rl_script.js
const grid = document.getElementById('grid');
const startButton = document.getElementById('startButton');
const stepButton = document.getElementById('stepButton');
const resetButton = document.getElementById('resetButton');
const learningGraphCanvas = document.getElementById('learningGraph');
const gridRows = 5;
const gridCols = 5;
const initialState = { row: 0, col: 0 };
const goalState = { row: 4, col: 4 };
const obstacles = [{ row: 1, col: 1 }, { row: 2, col: 2 }, { row: 3, col: 3 }];
let agentState = { ...initialState };
let gridCells = [];
let stepCount = 0;
let stepHistory = [];
let myChart = null; // Global variable to hold the chart instance
function createGrid() {
for (let row = 0; row < gridRows; row++) {
for (let col = 0; col < gridCols; col++) {
const cell = document.createElement('div');
cell.classList.add('cell');
if (row === agentState.row && col === agentState.col) {
cell.classList.add('agent');
} else if (row === goalState.row && col === goalState.col) {
cell.classList.add('goal');
} else if (obstacles.some(o => o.row === row && o.col === col)) {
cell.classList.add('obstacle');
}
grid.appendChild(cell);
gridCells.push(cell);
}
}
}
function moveAgent(action) {
const { row, col } = agentState;
let newRow = row;
let newCol = col;
switch (action) {
case 'up':
newRow = Math.max(0, row - 1);
break;
case 'down':
newRow = Math.min(gridRows - 1, row + 1);
break;
case 'left':
newCol = Math.max(0, col - 1);
break;
case 'right':
newCol = Math.min(gridCols - 1, col + 1);
break;
}
if (!obstacles.some(o => o.row === newRow && o.col === newCol)) {
agentState = { row: newRow, col: newCol };
stepCount++;
stepHistory.push(stepCount);
updateLearningGraph();
}
updateGrid();
}
function updateGrid() {
gridCells.forEach((cell, index) => {
const row = Math.floor(index / gridCols);
const col = index % gridCols;
cell.classList.remove('agent', 'goal', 'obstacle');
if (row === agentState.row && col === agentState.col) {
cell.classList.add('agent');
} else if (row === goalState.row && col === goalState.col) {
cell.classList.add('goal');
} else if (obstacles.some(o => o.row === row && o.col === col)) {
cell.classList.add('obstacle');
}
});
}
function startRL() {
agentState = { ...initialState };
stepCount = 0;
stepHistory = [];
updateGrid();
updateLearningGraph();
}
function stepRL() {
const actions = ['up', 'down', 'left', 'right'];
const action = actions[Math.floor(Math.random() * actions.length)];
moveAgent(action);
}
function resetRL() {
startRL();
}
function updateLearningGraph() {
const ctx = learningGraphCanvas.getContext('2d');
// Destroy the existing chart if it exists
if (myChart) {
myChart.destroy();
}
// Create a new chart
myChart = new Chart(ctx, {
type: 'line',
data: {
labels: Array.from({ length: stepHistory.length }, (_, i) => i + 1),
datasets: [{
label: 'Steps Taken',
data: stepHistory,
borderColor: 'rgba(75, 192, 192, 1)',
fill: false
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
}
createGrid();
updateLearningGraph();
startButton.addEventListener('click', startRL);
stepButton.addEventListener('click', stepRL);
resetButton.addEventListener('click', resetRL); |