-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d105d30
commit b6d6179
Showing
5 changed files
with
165 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from os import PathLike | ||
import sqlite3 | ||
from typing import Any | ||
|
||
import gymnasium as gym | ||
|
||
from pokemonred_puffer.environment import RedGymEnv | ||
|
||
|
||
class SqliteStateResetWrapper(gym.Wrapper): | ||
def __init__( | ||
self, | ||
env: RedGymEnv, | ||
database: str | bytes | PathLike[str] | PathLike[bytes], | ||
): | ||
super().__init__(env) | ||
self.conn = sqlite3.connect(database) | ||
self.cur = self.conn.cursor() | ||
self.cur.execute( | ||
""" | ||
INSERT INTO states(env_id, pyboy_state, reset) | ||
VALUES(?, ?, ?) | ||
""", | ||
(self.env.unwrapped.env_id, b"", False), | ||
) | ||
|
||
def reset(self, seed: int | None = None, options: dict[str, Any] | None = None): | ||
reset, pyboy_state = self.cur.execute( | ||
""" | ||
SELECT reset, pyboy_state | ||
FROM states | ||
WHERE env_id = ? | ||
""", | ||
(self.env.unwrapped.env_id,), | ||
).fetchone() | ||
if reset: | ||
if options: | ||
options["state"] = pyboy_state | ||
else: | ||
options = {"state": pyboy_state} | ||
res = self.env.reset(seed=seed, options=options) | ||
self.cur.execute( | ||
""" | ||
UPDATE states | ||
SET reset = False | ||
WHERE env_id = ? | ||
""", | ||
(self.env.unwrapped.env_id,), | ||
) | ||
return res |