dfa-gym 0.1.0__py3-none-any.whl

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.
dfa_gym/__init__.py ADDED
@@ -0,0 +1,16 @@
1
+ from dfa_gym.dfa_env import *
2
+ from dfa_gym.dfa_wrapper import *
3
+
4
+ from dfa_samplers import RADSampler
5
+ from gymnasium.envs.registration import register
6
+
7
+ register(
8
+ id='DFAEnv-v0',
9
+ entry_point='dfa_gym.dfa_env:DFAEnv',
10
+ kwargs = {"sampler": RADSampler(n_tokens=12), "timeout": 75}
11
+ )
12
+
13
+ register(
14
+ id='DFAEnv-v1',
15
+ entry_point='dfa_gym.dfa_env:DFAEnv'
16
+ )
dfa_gym/dfa_env.py ADDED
@@ -0,0 +1,45 @@
1
+ import numpy as np
2
+ import gymnasium as gym
3
+ from gymnasium import spaces
4
+ from dfa_samplers import DFASampler, RADSampler
5
+
6
+ from typing import Any
7
+
8
+ __all__ = ["DFAEnv"]
9
+
10
+ class DFAEnv(gym.Env):
11
+ def __init__(
12
+ self,
13
+ sampler: DFASampler | None = None,
14
+ timeout: int = 100
15
+ ):
16
+ super().__init__()
17
+ self.sampler = sampler if sampler is not None else RADSampler()
18
+ self.size_bound = self.sampler.get_size_bound()
19
+ self.action_space = spaces.Discrete(self.sampler.n_tokens)
20
+ self.observation_space = spaces.Box(low=0, high=9, shape=(self.size_bound,), dtype=np.int64)
21
+ self.dfa = None
22
+ self.timeout = timeout
23
+ self.t = None
24
+
25
+ def reset(self, seed: int | None = None, options: dict[str, Any] | None = None) -> tuple[np.ndarray, dict[str, Any]]:
26
+ np.random.seed(seed)
27
+ self.dfa = self.sampler.sample()
28
+ self.t = 0
29
+ return self._get_dfa_obs(), {}
30
+
31
+ def step(self, action: int) -> tuple[np.ndarray, int, bool, bool, dict[str, Any]]:
32
+ self.dfa = self.dfa.advance([action]).minimize()
33
+ reward = 0
34
+ if self.dfa._label(self.dfa.start):
35
+ reward = 1
36
+ elif self.dfa.find_word() is None:
37
+ reward = -1
38
+ self.t += 1
39
+ done = reward != 0 or self.t > self.timeout
40
+ return self._get_dfa_obs(), reward, done, False, {}
41
+
42
+ def _get_dfa_obs(self) -> np.ndarray:
43
+ dfa_obs = np.array([int(i) for i in str(self.dfa.to_int())])
44
+ obs = np.pad(dfa_obs, (self.size_bound - dfa_obs.shape[0], 0), constant_values=0)
45
+ return obs
dfa_gym/dfa_wrapper.py ADDED
@@ -0,0 +1,57 @@
1
+ import numpy as np
2
+ import gymnasium as gym
3
+ from gymnasium import spaces
4
+ from dfa_samplers import DFASampler, RADSampler
5
+
6
+ from typing import Any
7
+
8
+ __all__ = ["DFAWrapper"]
9
+
10
+ class DFAWrapper(gym.Wrapper):
11
+ def __init__(
12
+ self,
13
+ env_id: str,
14
+ sampler: DFASampler | None = None,
15
+ label_f: callable = None,
16
+ r_agg_f: callable = None
17
+ ):
18
+ super().__init__(gym.make(env_id))
19
+ self.sampler = sampler if sampler is not None else RADSampler()
20
+ self.label_f = label_f if label_f is not None else lambda obs: np.random.choice(self.sampler.n_tokens)
21
+ self.r_agg_f = r_agg_f if r_agg_f is not None else lambda _, dfa_reward: dfa_reward
22
+ self.size_bound = self.sampler.get_size_bound()
23
+ self.action_space = self.env.action_space
24
+ self.observation_space = spaces.Dict({
25
+ "obs": self.env.observation_space,
26
+ "dfa_obs": spaces.Box(low=0, high=9, shape=(self.size_bound,), dtype=np.int64),
27
+ })
28
+ self.dfa = None
29
+
30
+ def reset(self, seed: int | None = None, options: dict[str, Any] | None = None) -> tuple[np.ndarray, dict[str, Any]]:
31
+ np.random.seed(seed)
32
+ obs, info = self.env.reset(seed=seed, options=options)
33
+ self.dfa = self.sampler.sample()
34
+ dfa_obs = self._get_dfa_obs()
35
+ obs = {"obs": obs, "dfa_obs": dfa_obs}
36
+ return obs, info
37
+
38
+ def step(self, action: int) -> tuple[np.ndarray, int, bool, bool, dict[str, Any]]:
39
+ obs, reward, done, truncated, info = self.env.step(action)
40
+ symbol = self.label_f(obs)
41
+ if symbol is not None:
42
+ self.dfa = self.dfa.advance([symbol]).minimize()
43
+ dfa_obs = self._get_dfa_obs()
44
+ obs = {"obs": obs, "dfa_obs": dfa_obs}
45
+ dfa_reward = 0
46
+ if self.dfa._label(self.dfa.start):
47
+ dfa_reward = 1
48
+ elif self.dfa.find_word() is None:
49
+ dfa_reward = -1
50
+ reward = self.r_agg_f(reward, dfa_reward)
51
+ done = done or dfa_reward != 0
52
+ return obs, reward, done, truncated, info
53
+
54
+ def _get_dfa_obs(self) -> np.ndarray:
55
+ dfa_arr = np.array([int(i) for i in str(self.dfa.to_int())])
56
+ dfa_obs = np.pad(dfa_arr, (self.size_bound - dfa_arr.shape[0], 0), constant_values=0)
57
+ return dfa_obs
@@ -0,0 +1,11 @@
1
+ Metadata-Version: 2.4
2
+ Name: dfa-gym
3
+ Version: 0.1.0
4
+ Summary: Gymnasium environment for solving DFAs and wrapping other environments with DFA goals
5
+ License-File: LICENSE
6
+ Requires-Python: >=3.12
7
+ Requires-Dist: dfa-samplers>=0.1.0
8
+ Requires-Dist: gymnasium>=1.0.0
9
+ Description-Content-Type: text/markdown
10
+
11
+ # dfa-gym
@@ -0,0 +1,7 @@
1
+ dfa_gym/__init__.py,sha256=tLY48NluNVv66znFnlR7j9o-pRW5caaO766W724__HY,364
2
+ dfa_gym/dfa_env.py,sha256=u-mOCPhXRljp2t-VmvDfsHbKIdHdn59UGomHxUa_BxQ,1601
3
+ dfa_gym/dfa_wrapper.py,sha256=11eqfyl6g2v-wILGMWLg9L2sMJYnwl5rMn11p9YbQF0,2283
4
+ dfa_gym-0.1.0.dist-info/METADATA,sha256=W_S2r-zMFEX9oeVJa3kCVCStnhQ91rNj-tA_FO6oBm8,309
5
+ dfa_gym-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
6
+ dfa_gym-0.1.0.dist-info/licenses/LICENSE,sha256=Cvu0BZqt3rcFFv70hcFDgD_y8ryOKW85F-qGRfYI4iM,1071
7
+ dfa_gym-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.27.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 RAD-Embeddings
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.