Spaces:
Running
Running
Create rl_script.js
Browse files- rl_script.js +99 -0
rl_script.js
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// rl_script.js
|
2 |
+
|
3 |
+
const grid = document.getElementById('grid');
|
4 |
+
const startButton = document.getElementById('startButton');
|
5 |
+
const stepButton = document.getElementById('stepButton');
|
6 |
+
const resetButton = document.getElementById('resetButton');
|
7 |
+
|
8 |
+
const gridRows = 5;
|
9 |
+
const gridCols = 5;
|
10 |
+
|
11 |
+
const initialState = { row: 0, col: 0 };
|
12 |
+
const goalState = { row: 4, col: 4 };
|
13 |
+
const obstacles = [{ row: 1, col: 1 }, { row: 2, col: 2 }, { row: 3, 3 }];
|
14 |
+
|
15 |
+
let agentState = { ...initialState };
|
16 |
+
let gridCells = [];
|
17 |
+
|
18 |
+
function createGrid() {
|
19 |
+
for (let row = 0; row < gridRows; row++) {
|
20 |
+
for (let col = 0; col < gridCols; col++) {
|
21 |
+
const cell = document.createElement('div');
|
22 |
+
cell.classList.add('cell');
|
23 |
+
if (row === agentState.row && col === agentState.col) {
|
24 |
+
cell.classList.add('agent');
|
25 |
+
} else if (row === goalState.row && col === goalState.col) {
|
26 |
+
cell.classList.add('goal');
|
27 |
+
} else if (obstacles.some(o => o.row === row && o.col === col)) {
|
28 |
+
cell.classList.add('obstacle');
|
29 |
+
}
|
30 |
+
grid.appendChild(cell);
|
31 |
+
gridCells.push(cell);
|
32 |
+
}
|
33 |
+
}
|
34 |
+
}
|
35 |
+
|
36 |
+
function moveAgent(action) {
|
37 |
+
const { row, col } = agentState;
|
38 |
+
let newRow = row;
|
39 |
+
let newCol = col;
|
40 |
+
|
41 |
+
switch (action) {
|
42 |
+
case 'up':
|
43 |
+
newRow = Math.max(0, row - 1);
|
44 |
+
break;
|
45 |
+
case 'down':
|
46 |
+
newRow = Math.min(gridRows - 1, row + 1);
|
47 |
+
break;
|
48 |
+
case 'left':
|
49 |
+
newCol = Math.max(0, col - 1);
|
50 |
+
break;
|
51 |
+
case 'right':
|
52 |
+
newCol = Math.min(gridCols - 1, col + 1);
|
53 |
+
break;
|
54 |
+
}
|
55 |
+
|
56 |
+
if (!obstacles.some(o => o.row === newRow && o.col === newCol)) {
|
57 |
+
agentState = { row: newRow, col: newCol };
|
58 |
+
}
|
59 |
+
|
60 |
+
updateGrid();
|
61 |
+
}
|
62 |
+
|
63 |
+
function updateGrid() {
|
64 |
+
gridCells.forEach((cell, index) => {
|
65 |
+
const row = Math.floor(index / gridCols);
|
66 |
+
const col = index % gridCols;
|
67 |
+
cell.classList.remove('agent', 'goal', 'obstacle');
|
68 |
+
if (row === agentState.row && col === agentState.col) {
|
69 |
+
cell.classList.add('agent');
|
70 |
+
} else if (row === goalState.row && col === goalState.col) {
|
71 |
+
cell.classList.add('goal');
|
72 |
+
} else if (obstacles.some(o => o.row === row && o.col === col)) {
|
73 |
+
cell.classList.add('obstacle');
|
74 |
+
}
|
75 |
+
});
|
76 |
+
}
|
77 |
+
|
78 |
+
function startRL() {
|
79 |
+
// Initialize the agent and grid
|
80 |
+
agentState = { ...initialState };
|
81 |
+
updateGrid();
|
82 |
+
}
|
83 |
+
|
84 |
+
function stepRL() {
|
85 |
+
// Simple policy: Move randomly
|
86 |
+
const actions = ['up', 'down', 'left', 'right'];
|
87 |
+
const action = actions[Math.floor(Math.random() * actions.length)];
|
88 |
+
moveAgent(action);
|
89 |
+
}
|
90 |
+
|
91 |
+
function resetRL() {
|
92 |
+
startRL();
|
93 |
+
}
|
94 |
+
|
95 |
+
createGrid();
|
96 |
+
|
97 |
+
startButton.addEventListener('click', startRL);
|
98 |
+
stepButton.addEventListener('click', stepRL);
|
99 |
+
resetButton.addEventListener('click', resetRL);
|