Skip to content

Commit 25b1bab

Browse files
committed
Fix random init state bugs and turn on random init state by default for gym interface
1 parent 3b920ce commit 25b1bab

File tree

5 files changed

+30
-34
lines changed

5 files changed

+30
-34
lines changed

examples/run_gymnasium.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
register(
55
id="simglucose/adolescent2-v0",
66
entry_point="simglucose.envs:T1DSimGymnaisumEnv",
7-
max_episode_steps=10,
7+
max_episode_steps=1000,
88
kwargs={"patient_name": "adolescent#002"},
99
)
1010

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="simglucose",
5-
version="0.2.9",
5+
version="0.2.10",
66
description="A Type-1 Diabetes Simulator as a Reinforcement Learning Environment in OpenAI gym or rllab (python implementation of UVa/Padova Simulator)",
77
url="https://github.com/jxx123/simglucose",
88
author="Jinyu Xie",

simglucose/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +0,0 @@
1-
from gym.envs.registration import register
2-
3-
register(
4-
id='simglucose-v0',
5-
entry_point='simglucose.envs:T1DSimEnv',
6-
)

simglucose/envs/simglucose_gym_env.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
from datetime import datetime
1313
import gymnasium
1414

15-
1615
PATIENT_PARA_FILE = pkg_resources.resource_filename(
17-
"simglucose", "params/vpatient_params.csv"
18-
)
16+
"simglucose", "params/vpatient_params.csv")
1917

2018

2119
class T1DSimEnv(gym.Env):
@@ -28,9 +26,11 @@ class T1DSimEnv(gym.Env):
2826
SENSOR_HARDWARE = "Dexcom"
2927
INSULIN_PUMP_HARDWARE = "Insulet"
3028

31-
def __init__(
32-
self, patient_name=None, custom_scenario=None, reward_fun=None, seed=None
33-
):
29+
def __init__(self,
30+
patient_name=None,
31+
custom_scenario=None,
32+
reward_fun=None,
33+
seed=None):
3434
"""
3535
patient_name must be 'adolescent#001' to 'adolescent#010',
3636
or 'adult#001' to 'adult#010', or 'child#001' to 'child#010'
@@ -79,20 +79,19 @@ def _create_env(self):
7979

8080
if isinstance(self.patient_name, list):
8181
patient_name = self.np_random.choice(self.patient_name)
82-
patient = T1DPatient.withName(patient_name, random_init_bg=True, seed=seed4)
82+
patient = T1DPatient.withName(patient_name,
83+
random_init_bg=True,
84+
seed=seed4)
8385
else:
84-
patient = T1DPatient.withName(
85-
self.patient_name, random_init_bg=True, seed=seed4
86-
)
86+
patient = T1DPatient.withName(self.patient_name,
87+
random_init_bg=True,
88+
seed=seed4)
8789

8890
if isinstance(self.custom_scenario, list):
8991
scenario = self.np_random.choice(self.custom_scenario)
9092
else:
91-
scenario = (
92-
RandomScenario(start_time=start_time, seed=seed3)
93-
if self.custom_scenario is None
94-
else self.custom_scenario
95-
)
93+
scenario = (RandomScenario(start_time=start_time, seed=seed3) if
94+
self.custom_scenario is None else self.custom_scenario)
9695

9796
sensor = CGMSensor.withName(self.SENSOR_HARDWARE, seed=seed2)
9897
pump = InsulinPump.withName(self.INSULIN_PUMP_HARDWARE)
@@ -109,19 +108,19 @@ def _close(self):
109108
@property
110109
def action_space(self):
111110
ub = self.env.pump._params["max_basal"]
112-
return spaces.Box(low=0, high=ub, shape=(1,))
111+
return spaces.Box(low=0, high=ub, shape=(1, ))
113112

114113
@property
115114
def observation_space(self):
116-
return spaces.Box(low=0, high=1000, shape=(1,))
115+
return spaces.Box(low=0, high=1000, shape=(1, ))
117116

118117
@property
119118
def max_basal(self):
120119
return self.env.pump._params["max_basal"]
121120

122121

123122
class T1DSimGymnaisumEnv(gymnasium.Env):
124-
metadata = {"render_modes": ["human"]}
123+
metadata = {"render_modes": ["human"], "render_fps": 60}
125124
MAX_BG = 1000
126125

127126
def __init__(
@@ -140,12 +139,14 @@ def __init__(
140139
reward_fun=reward_fun,
141140
seed=seed,
142141
)
143-
self.observation_space = gymnasium.spaces.Box(
144-
low=0, high=self.MAX_BG, shape=(1,), dtype=np.float32
145-
)
146-
self.action_space = gymnasium.spaces.Box(
147-
low=0, high=self.env.max_basal, shape=(1,), dtype=np.float32
148-
)
142+
self.observation_space = gymnasium.spaces.Box(low=0,
143+
high=self.MAX_BG,
144+
shape=(1, ),
145+
dtype=np.float32)
146+
self.action_space = gymnasium.spaces.Box(low=0,
147+
high=self.env.max_basal,
148+
shape=(1, ),
149+
dtype=np.float32)
149150

150151
def step(self, action):
151152
obs, reward, done, info = self.env.step(action)
@@ -159,7 +160,8 @@ def step(self, action):
159160
# )
160161
# Once the max_episode_steps is set, the truncated value will be overridden.
161162
truncated = False
162-
return np.array([obs.CGM], dtype=np.float32), reward, done, truncated, info
163+
return np.array([obs.CGM],
164+
dtype=np.float32), reward, done, truncated, info
163165

164166
def reset(self, seed=None, options=None):
165167
super().reset(seed=seed)

simglucose/patient/t1dpatient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def reset(self):
246246
Reset the patient state to default intial state
247247
'''
248248
if self._init_state is None:
249-
self.init_state = self._params.iloc[2:15]
249+
self.init_state = np.copy(self._params.iloc[2:15].values)
250250
else:
251251
self.init_state = self._init_state
252252

0 commit comments

Comments
 (0)