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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5779997
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
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Simple Q-Learning Grid World Simulation</title>
    <style>
      body {
        font-family: Arial, sans-serif;
        max-width: 800px;
        margin: 0 auto;
        padding: 20px;
      }
      .grid {
        display: grid;
        grid-template-columns: repeat(4, 80px);
        grid-template-rows: repeat(4, 80px);
        gap: 2px;
        margin: 20px 0;
      }
      .cell {
        width: 80px;
        height: 80px;
        border: 1px solid #ccc;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
      }
      .agent {
        width: 30px;
        height: 30px;
        background-color: blue;
        border-radius: 50%;
        position: absolute;
      }
      .goal {
        background-color: green;
        color: white;
      }
      .obstacle {
        background-color: gray;
      }
      .controls {
        margin: 20px 0;
      }
      button {
        padding: 8px 16px;
        margin-right: 10px;
        cursor: pointer;
      }
      .info {
        margin: 20px 0;
        padding: 10px;
        background-color: #f0f0f0;
        border-radius: 5px;
      }
      .parameters {
        display: grid;
        grid-template-columns: auto 1fr auto;
        gap: 10px;
        align-items: center;
        margin-bottom: 10px;
      }
      table {
        border-collapse: collapse;
        margin-top: 20px;
        width: 100%;
      }
      th,
      td {
        border: 1px solid #ddd;
        padding: 8px;
        text-align: center;
      }
      .chart {
        width: 100%;
        height: 200px;
        margin-top: 20px;
      }
      .signature {
        text-align: center; /* Changed from 'right' to 'center' */
        font-style: italic;
        margin-top: 30px;
      }
    </style>
  </head>
  <body>
    <h1>Simple Q-Learning Grid World Simulation - Designed by Pejman</h1>

    <div class="info">
      <p>
        This simulation demonstrates Q-learning - a reinforcement learning
        algorithm where an agent learns to navigate a grid world to reach a goal
        while avoiding obstacles.
      </p>
    </div>

    <div class="parameters">
      <label for="alpha">Learning Rate (Ξ±):</label>
      <input type="range" id="alpha" min="0.1" max="1" step="0.1" value="0.5" />
      <span id="alpha-value">0.5</span>

      <label for="gamma">Discount Factor (Ξ³):</label>
      <input type="range" id="gamma" min="0.1" max="1" step="0.1" value="0.9" />
      <span id="gamma-value">0.9</span>

      <label for="epsilon">Exploration Rate (Ξ΅):</label>
      <input type="range" id="epsilon" min="0" max="1" step="0.1" value="0.3" />
      <span id="epsilon-value">0.3</span>
    </div>

    <div class="controls">
      <button id="step-btn">Step</button>
      <button id="train-btn">Train Episode</button>
      <button id="auto-btn">Auto Train</button>
      <button id="stop-btn" disabled>Stop</button>
      <button id="reset-btn">Reset</button>
    </div>

    <div class="info" id="status">Episode: 1 | Step: 0 | Total Reward: 0</div>

    <div class="grid" id="grid"></div>

    <h2>Q-Table</h2>
    <div id="q-table"></div>

    <h2>Learning Progress</h2>
    <canvas id="chart" class="chart"></canvas>

    <div class="signature">
      *Β© 2025 Pejman Ebrahimi - Basic Q-Learning Simulation*
    </div>

    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script>
      // Grid setup
      const grid = document.getElementById("grid");
      const gridSize = 4;
      let agentPos = { x: 0, y: 0 };
      const goalPos = { x: 3, y: 3 };
      const obstacles = [
        { x: 1, y: 1 },
        { x: 2, y: 1 },
        { x: 1, y: 2 },
      ];

      // Learning parameters
      let alpha = 0.5;
      let gamma = 0.9;
      let epsilon = 0.3;
      let qTable = {};

      // Training variables
      let episode = 1;
      let step = 0;
      let totalReward = 0;
      let rewards = [];
      let running = false;

      // Actions
      const actions = ["up", "right", "down", "left"];

      // Initialize grid
      function createGrid() {
        grid.innerHTML = "";
        for (let y = 0; y < gridSize; y++) {
          for (let x = 0; x < gridSize; x++) {
            const cell = document.createElement("div");
            cell.className = "cell";
            cell.id = `cell-${x}-${y}`;

            if (x === goalPos.x && y === goalPos.y) {
              cell.classList.add("goal");
              cell.textContent = "GOAL";
            } else if (obstacles.some((o) => o.x === x && o.y === y)) {
              cell.classList.add("obstacle");
            }

            grid.appendChild(cell);
          }
        }
        updateAgentPosition();
      }

      // Update agent position
      function updateAgentPosition() {
        const agent = document.querySelector(".agent");
        if (agent) agent.remove();

        const cell = document.getElementById(
          `cell-${agentPos.x}-${agentPos.y}`
        );
        const agentElement = document.createElement("div");
        agentElement.className = "agent";
        cell.appendChild(agentElement);
      }

      // Initialize Q-Table
      function initQTable() {
        qTable = {};
        for (let y = 0; y < gridSize; y++) {
          for (let x = 0; x < gridSize; x++) {
            if (obstacles.some((o) => o.x === x && o.y === y)) continue;
            qTable[`${x},${y}`] = {
              up: 0,
              right: 0,
              down: 0,
              left: 0,
            };
          }
        }
        updateQTableDisplay();
      }

      // Update Q-Table display
      function updateQTableDisplay() {
        const tableContainer = document.getElementById("q-table");
        tableContainer.innerHTML = "";

        const table = document.createElement("table");

        // Create header row
        const thead = document.createElement("thead");
        const headerRow = document.createElement("tr");
        headerRow.appendChild(document.createElement("th"));
        for (let x = 0; x < gridSize; x++) {
          const th = document.createElement("th");
          th.textContent = x;
          headerRow.appendChild(th);
        }
        thead.appendChild(headerRow);
        table.appendChild(thead);

        // Create table body
        const tbody = document.createElement("tbody");
        for (let y = 0; y < gridSize; y++) {
          const row = document.createElement("tr");

          const th = document.createElement("th");
          th.textContent = y;
          row.appendChild(th);

          for (let x = 0; x < gridSize; x++) {
            const cell = document.createElement("td");

            if (obstacles.some((o) => o.x === x && o.y === y)) {
              cell.textContent = "X";
              cell.style.backgroundColor = "lightgray";
            } else if (x === goalPos.x && y === goalPos.y) {
              cell.textContent = "GOAL";
              cell.style.backgroundColor = "lightgreen";
            } else {
              const state = `${x},${y}`;
              const stateQ = qTable[state];

              // Find best action
              let bestAction = actions[0];
              let bestValue = stateQ[bestAction];
              for (const action of actions) {
                if (stateQ[action] > bestValue) {
                  bestValue = stateQ[action];
                  bestAction = action;
                }
              }

              let actionSymbol = "";
              switch (bestAction) {
                case "up":
                  actionSymbol = "↑";
                  break;
                case "right":
                  actionSymbol = "β†’";
                  break;
                case "down":
                  actionSymbol = "↓";
                  break;
                case "left":
                  actionSymbol = "←";
                  break;
              }

              cell.textContent = `${actionSymbol} (${bestValue.toFixed(1)})`;

              // Color based on value
              const normalizedValue = Math.max(
                0,
                Math.min(1, (bestValue + 5) / 10)
              );
              cell.style.backgroundColor = `rgba(0, 128, 0, ${
                normalizedValue * 0.5
              })`;
            }

            row.appendChild(cell);
          }

          tbody.appendChild(row);
        }
        table.appendChild(tbody);
        tableContainer.appendChild(table);
      }

      // Choose action using epsilon-greedy policy
      function chooseAction() {
        const state = `${agentPos.x},${agentPos.y}`;
        const validActions = getValidActions();

        // Exploration
        if (Math.random() < epsilon) {
          return validActions[Math.floor(Math.random() * validActions.length)];
        }

        // Exploitation
        const stateQ = qTable[state];
        let bestAction = validActions[0];
        let bestValue = stateQ[bestAction];

        for (const action of validActions) {
          if (stateQ[action] > bestValue) {
            bestValue = stateQ[action];
            bestAction = action;
          }
        }

        return bestAction;
      }

      // Get valid actions for current state
      function getValidActions() {
        const validActions = [];

        // Check up
        if (agentPos.y > 0 && !isObstacle(agentPos.x, agentPos.y - 1)) {
          validActions.push("up");
        }

        // Check right
        if (
          agentPos.x < gridSize - 1 &&
          !isObstacle(agentPos.x + 1, agentPos.y)
        ) {
          validActions.push("right");
        }

        // Check down
        if (
          agentPos.y < gridSize - 1 &&
          !isObstacle(agentPos.x, agentPos.y + 1)
        ) {
          validActions.push("down");
        }

        // Check left
        if (agentPos.x > 0 && !isObstacle(agentPos.x - 1, agentPos.y)) {
          validActions.push("left");
        }

        return validActions;
      }

      // Check if position is an obstacle
      function isObstacle(x, y) {
        return obstacles.some((o) => o.x === x && o.y === y);
      }

      // Take action and get reward
      function takeAction(action) {
        const oldPos = { ...agentPos };

        // Update position based on action
        switch (action) {
          case "up":
            agentPos.y = Math.max(0, agentPos.y - 1);
            break;
          case "right":
            agentPos.x = Math.min(gridSize - 1, agentPos.x + 1);
            break;
          case "down":
            agentPos.y = Math.min(gridSize - 1, agentPos.y + 1);
            break;
          case "left":
            agentPos.x = Math.max(0, agentPos.x - 1);
            break;
        }

        // Check if position is valid
        if (isObstacle(agentPos.x, agentPos.y)) {
          agentPos = oldPos;
          return -10; // Hitting obstacle penalty
        }

        // Calculate reward
        if (agentPos.x === goalPos.x && agentPos.y === goalPos.y) {
          return 10; // Goal reward
        }

        return -1; // Step penalty
      }

      // Update Q-value for state-action pair
      function updateQValue(state, action, reward, nextState) {
        const currQ = qTable[state][action];

        // Find max Q-value for next state
        const nextStateQ = qTable[nextState];
        const maxNextQ = Math.max(...Object.values(nextStateQ));

        // Q-learning formula
        const newQ = currQ + alpha * (reward + gamma * maxNextQ - currQ);
        qTable[state][action] = newQ;
      }

      // Perform one training step
      function performStep() {
        const state = `${agentPos.x},${agentPos.y}`;
        const action = chooseAction();
        const reward = takeAction(action);
        updateAgentPosition();

        const nextState = `${agentPos.x},${agentPos.y}`;
        updateQValue(state, action, reward, nextState);

        step++;
        totalReward += reward;
        document.getElementById(
          "status"
        ).textContent = `Episode: ${episode} | Step: ${step} | Total Reward: ${totalReward}`;

        updateQTableDisplay();

        // Check if episode is done
        if (agentPos.x === goalPos.x && agentPos.y === goalPos.y) {
          rewards.push(totalReward);

          // Update chart
          chart.data.labels.push(episode);
          chart.data.datasets[0].data.push(totalReward);
          chart.update();

          // Start new episode
          episode++;
          resetAgentPosition();
          return true; // Episode completed
        }

        return false; // Episode not completed
      }

      // Train a complete episode
      function trainEpisode() {
        let episodeDone = false;
        while (!episodeDone) {
          episodeDone = performStep();
        }
      }

      // Auto-train function
      function autoTrain() {
        if (!running) return;

        const episodeDone = performStep();
        if (episodeDone) {
          setTimeout(autoTrain, 200);
        } else {
          requestAnimationFrame(autoTrain);
        }
      }

      // Reset agent position
      function resetAgentPosition() {
        agentPos = { x: 0, y: 0 };
        updateAgentPosition();
        step = 0;
        totalReward = 0;
        document.getElementById(
          "status"
        ).textContent = `Episode: ${episode} | Step: ${step} | Total Reward: ${totalReward}`;
      }

      // Reset environment
      function resetEnvironment() {
        agentPos = { x: 0, y: 0 };
        updateAgentPosition();
        initQTable();
        episode = 1;
        step = 0;
        totalReward = 0;
        rewards = [];

        document.getElementById(
          "status"
        ).textContent = `Episode: ${episode} | Step: ${step} | Total Reward: ${totalReward}`;

        // Reset chart
        chart.data.labels = [];
        chart.data.datasets[0].data = [];
        chart.update();
      }

      // Initialize chart
      const ctx = document.getElementById("chart").getContext("2d");
      const chart = new Chart(ctx, {
        type: "line",
        data: {
          labels: [],
          datasets: [
            {
              label: "Total Reward",
              data: [],
              borderColor: "blue",
              backgroundColor: "rgba(0, 0, 255, 0.1)",
              tension: 0.1,
              fill: true,
            },
          ],
        },
        options: {
          responsive: true,
          scales: {
            y: {
              beginAtZero: false,
            },
          },
        },
      });

      // Event listeners
      document
        .getElementById("step-btn")
        .addEventListener("click", performStep);
      document
        .getElementById("train-btn")
        .addEventListener("click", trainEpisode);

      document
        .getElementById("auto-btn")
        .addEventListener("click", function () {
          running = true;
          this.disabled = true;
          document.getElementById("stop-btn").disabled = false;
          autoTrain();
        });

      document
        .getElementById("stop-btn")
        .addEventListener("click", function () {
          running = false;
          this.disabled = true;
          document.getElementById("auto-btn").disabled = false;
        });

      document
        .getElementById("reset-btn")
        .addEventListener("click", resetEnvironment);

      document.getElementById("alpha").addEventListener("input", function () {
        alpha = parseFloat(this.value);
        document.getElementById("alpha-value").textContent = alpha.toFixed(1);
      });

      document.getElementById("gamma").addEventListener("input", function () {
        gamma = parseFloat(this.value);
        document.getElementById("gamma-value").textContent = gamma.toFixed(1);
      });

      document.getElementById("epsilon").addEventListener("input", function () {
        epsilon = parseFloat(this.value);
        document.getElementById("epsilon-value").textContent =
          epsilon.toFixed(1);
      });

      // Initialize environment
      createGrid();
      initQTable();
    </script>
  </body>
</html>