ai-snake-lab 0.4.6__tar.gz → 0.4.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/PKG-INFO +1 -1
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/ai/ReplayMemory.py +31 -10
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/constants/DDef.py +1 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/constants/DDir.py +0 -1
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/constants/DFile.py +0 -1
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/ui/AISim.py +1 -1
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/pyproject.toml +1 -1
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/LICENSE +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/README.md +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/ai/AIAgent.py +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/ai/AITrainer.py +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/ai/EpsilonAlgo.py +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/ai/models/ModelL.py +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/ai/models/ModelRNN.py +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/constants/DDb4EPlot.py +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/constants/DEpsilon.py +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/constants/DFields.py +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/constants/DLabels.py +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/constants/DLayout.py +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/constants/DModelL.py +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/constants/DModelLRNN.py +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/constants/DReplayMemory.py +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/constants/DSim.py +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/constants/__init__.py +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/game/GameBoard.py +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/game/GameElements.py +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/game/SnakeGame.py +0 -0
- {ai_snake_lab-0.4.6/ai_snake_lab/utils → ai_snake_lab-0.4.8/ai_snake_lab/ui}/AISim.tcss +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/ui/Db4EPlot.py +0 -0
- {ai_snake_lab-0.4.6 → ai_snake_lab-0.4.8}/ai_snake_lab/utils/ConstGroup.py +0 -0
@@ -14,10 +14,11 @@ import os
|
|
14
14
|
from collections import deque
|
15
15
|
import random
|
16
16
|
import sqlite3, pickle
|
17
|
+
import tempfile
|
18
|
+
import shutil
|
17
19
|
|
18
20
|
from ai_snake_lab.constants.DReplayMemory import MEM_TYPE
|
19
|
-
from ai_snake_lab.constants.
|
20
|
-
from ai_snake_lab.constants.DDir import DDir
|
21
|
+
from ai_snake_lab.constants.DDef import DDef
|
21
22
|
|
22
23
|
|
23
24
|
class ReplayMemory:
|
@@ -31,11 +32,6 @@ class ReplayMemory:
|
|
31
32
|
self.max_states = 15000
|
32
33
|
self.max_shuffle_games = 40
|
33
34
|
self.max_games = 500
|
34
|
-
self.db_file = os.path.join(DDir.AI_SNAKE_LAB, DDir.DB, DFile.REPLAY_DB)
|
35
|
-
|
36
|
-
# Delete the replay memory file, if it exists
|
37
|
-
if os.path.exists(self.db_file):
|
38
|
-
os.remove(self.db_file)
|
39
35
|
|
40
36
|
if self._mem_type == MEM_TYPE.SHUFFLE:
|
41
37
|
# States are stored in a deque and a random sample will be returned
|
@@ -46,13 +42,33 @@ class ReplayMemory:
|
|
46
42
|
# A complete game will be returned
|
47
43
|
self.cur_memory = []
|
48
44
|
|
45
|
+
# Get a temporary directory for the DB file
|
46
|
+
self._tmpfile = tempfile.NamedTemporaryFile(suffix=DDef.DOT_DB, delete=False)
|
47
|
+
self.db_file = self._tmpfile.name
|
48
|
+
|
49
49
|
# Connect to SQLite
|
50
50
|
self.conn = sqlite3.connect(self.db_file, check_same_thread=False)
|
51
|
+
|
52
|
+
# Get a cursor
|
51
53
|
self.cursor = self.conn.cursor()
|
54
|
+
|
55
|
+
# We don't need the file handle anymore
|
56
|
+
self._tmpfile.close()
|
57
|
+
|
58
|
+
# Intialize the schema
|
52
59
|
self.init_db()
|
53
60
|
|
54
|
-
def
|
55
|
-
return
|
61
|
+
def __enter__(self):
|
62
|
+
return self
|
63
|
+
|
64
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
65
|
+
self.close()
|
66
|
+
|
67
|
+
def __del__(self):
|
68
|
+
try:
|
69
|
+
self.close()
|
70
|
+
except Exception:
|
71
|
+
pass # avoid errors on interpreter shutdown
|
56
72
|
|
57
73
|
def append(self, transition):
|
58
74
|
"""Add a transition to the current game."""
|
@@ -75,7 +91,12 @@ class ReplayMemory:
|
|
75
91
|
|
76
92
|
def close(self):
|
77
93
|
"""Close the database connection."""
|
78
|
-
self
|
94
|
+
if getattr(self, "conn", None):
|
95
|
+
self.conn.close()
|
96
|
+
self.conn = None
|
97
|
+
if getattr(self, "db_file", None) and os.path.exists(self.db_file):
|
98
|
+
os.remove(self.db_file)
|
99
|
+
self.db_file = None
|
79
100
|
|
80
101
|
def get_random_game(self):
|
81
102
|
"""Return a random full game from the database."""
|
@@ -63,7 +63,7 @@ class AISim(App):
|
|
63
63
|
"""A Textual app that has an AI Agent playing the Snake Game."""
|
64
64
|
|
65
65
|
TITLE = DDef.APP_TITLE
|
66
|
-
CSS_PATH =
|
66
|
+
CSS_PATH = DFile.CSS_FILE
|
67
67
|
|
68
68
|
## Runtime values
|
69
69
|
# Current epsilon value (degrades in real-time)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
[project]
|
2
2
|
name = "ai-snake-lab"
|
3
|
-
version = "0.4.
|
3
|
+
version = "0.4.8"
|
4
4
|
description = "Interactive reinforcement learning sandbox for experimenting with AI agents in a classic Snake Game environment."
|
5
5
|
authors = [{ name = "Nadim-Daniel Ghaznavi", email = "nghaznavi@gmail.com" }]
|
6
6
|
license = { text = "GPL-3.0" }
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|