Spaces:
Running
Running
Update rl_script.js
Browse files- rl_script.js +36 -3
rl_script.js
CHANGED
@@ -4,16 +4,19 @@ 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++) {
|
@@ -55,6 +58,9 @@ function moveAgent(action) {
|
|
55 |
|
56 |
if (!obstacles.some(o => o.row === newRow && o.col === newCol)) {
|
57 |
agentState = { row: newRow, col: newCol };
|
|
|
|
|
|
|
58 |
}
|
59 |
|
60 |
updateGrid();
|
@@ -76,13 +82,14 @@ function updateGrid() {
|
|
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);
|
@@ -92,7 +99,33 @@ function resetRL() {
|
|
92 |
startRL();
|
93 |
}
|
94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
createGrid();
|
|
|
96 |
|
97 |
startButton.addEventListener('click', startRL);
|
98 |
stepButton.addEventListener('click', stepRL);
|
|
|
4 |
const startButton = document.getElementById('startButton');
|
5 |
const stepButton = document.getElementById('stepButton');
|
6 |
const resetButton = document.getElementById('resetButton');
|
7 |
+
const learningGraphCanvas = document.getElementById('learningGraph');
|
8 |
|
9 |
const gridRows = 5;
|
10 |
const gridCols = 5;
|
11 |
|
12 |
const initialState = { row: 0, col: 0 };
|
13 |
const goalState = { row: 4, col: 4 };
|
14 |
+
const obstacles = [{ row: 1, col: 1 }, { row: 2, col: 2 }, { row: 3, col: 3 }];
|
15 |
|
16 |
let agentState = { ...initialState };
|
17 |
let gridCells = [];
|
18 |
+
let stepCount = 0;
|
19 |
+
let stepHistory = [];
|
20 |
|
21 |
function createGrid() {
|
22 |
for (let row = 0; row < gridRows; row++) {
|
|
|
58 |
|
59 |
if (!obstacles.some(o => o.row === newRow && o.col === newCol)) {
|
60 |
agentState = { row: newRow, col: newCol };
|
61 |
+
stepCount++;
|
62 |
+
stepHistory.push(stepCount);
|
63 |
+
updateLearningGraph();
|
64 |
}
|
65 |
|
66 |
updateGrid();
|
|
|
82 |
}
|
83 |
|
84 |
function startRL() {
|
|
|
85 |
agentState = { ...initialState };
|
86 |
+
stepCount = 0;
|
87 |
+
stepHistory = [];
|
88 |
updateGrid();
|
89 |
+
updateLearningGraph();
|
90 |
}
|
91 |
|
92 |
function stepRL() {
|
|
|
93 |
const actions = ['up', 'down', 'left', 'right'];
|
94 |
const action = actions[Math.floor(Math.random() * actions.length)];
|
95 |
moveAgent(action);
|
|
|
99 |
startRL();
|
100 |
}
|
101 |
|
102 |
+
function updateLearningGraph() {
|
103 |
+
const ctx = learningGraphCanvas.getContext('2d');
|
104 |
+
ctx.clearRect(0, 0, learningGraphCanvas.width, learningGraphCanvas.height);
|
105 |
+
|
106 |
+
new Chart(ctx, {
|
107 |
+
type: 'line',
|
108 |
+
data: {
|
109 |
+
labels: Array.from({ length: stepHistory.length }, (_, i) => i + 1),
|
110 |
+
datasets: [{
|
111 |
+
label: 'Steps Taken',
|
112 |
+
data: stepHistory,
|
113 |
+
borderColor: 'rgba(75, 192, 192, 1)',
|
114 |
+
fill: false
|
115 |
+
}]
|
116 |
+
},
|
117 |
+
options: {
|
118 |
+
scales: {
|
119 |
+
y: {
|
120 |
+
beginAtZero: true
|
121 |
+
}
|
122 |
+
}
|
123 |
+
}
|
124 |
+
});
|
125 |
+
}
|
126 |
+
|
127 |
createGrid();
|
128 |
+
updateLearningGraph();
|
129 |
|
130 |
startButton.addEventListener('click', startRL);
|
131 |
stepButton.addEventListener('click', stepRL);
|