Skip to content

Commit

Permalink
Fix random init state bugs and turn on random init state by default f…
Browse files Browse the repository at this point in the history
…or gym interface
  • Loading branch information
jxx123 committed May 16, 2024
1 parent 3b920ce commit 25b1bab
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 34 deletions.
2 changes: 1 addition & 1 deletion examples/run_gymnasium.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
register(
id="simglucose/adolescent2-v0",
entry_point="simglucose.envs:T1DSimGymnaisumEnv",
max_episode_steps=10,
max_episode_steps=1000,
kwargs={"patient_name": "adolescent#002"},
)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="simglucose",
version="0.2.9",
version="0.2.10",
description="A Type-1 Diabetes Simulator as a Reinforcement Learning Environment in OpenAI gym or rllab (python implementation of UVa/Padova Simulator)",
url="https://github.com/jxx123/simglucose",
author="Jinyu Xie",
Expand Down
6 changes: 0 additions & 6 deletions simglucose/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
from gym.envs.registration import register

register(
id='simglucose-v0',
entry_point='simglucose.envs:T1DSimEnv',
)
52 changes: 27 additions & 25 deletions simglucose/envs/simglucose_gym_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
from datetime import datetime
import gymnasium


PATIENT_PARA_FILE = pkg_resources.resource_filename(
"simglucose", "params/vpatient_params.csv"
)
"simglucose", "params/vpatient_params.csv")


class T1DSimEnv(gym.Env):
Expand All @@ -28,9 +26,11 @@ class T1DSimEnv(gym.Env):
SENSOR_HARDWARE = "Dexcom"
INSULIN_PUMP_HARDWARE = "Insulet"

def __init__(
self, patient_name=None, custom_scenario=None, reward_fun=None, seed=None
):
def __init__(self,
patient_name=None,
custom_scenario=None,
reward_fun=None,
seed=None):
"""
patient_name must be 'adolescent#001' to 'adolescent#010',
or 'adult#001' to 'adult#010', or 'child#001' to 'child#010'
Expand Down Expand Up @@ -79,20 +79,19 @@ def _create_env(self):

if isinstance(self.patient_name, list):
patient_name = self.np_random.choice(self.patient_name)
patient = T1DPatient.withName(patient_name, random_init_bg=True, seed=seed4)
patient = T1DPatient.withName(patient_name,
random_init_bg=True,
seed=seed4)
else:
patient = T1DPatient.withName(
self.patient_name, random_init_bg=True, seed=seed4
)
patient = T1DPatient.withName(self.patient_name,
random_init_bg=True,
seed=seed4)

if isinstance(self.custom_scenario, list):
scenario = self.np_random.choice(self.custom_scenario)
else:
scenario = (
RandomScenario(start_time=start_time, seed=seed3)
if self.custom_scenario is None
else self.custom_scenario
)
scenario = (RandomScenario(start_time=start_time, seed=seed3) if
self.custom_scenario is None else self.custom_scenario)

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

@property
def observation_space(self):
return spaces.Box(low=0, high=1000, shape=(1,))
return spaces.Box(low=0, high=1000, shape=(1, ))

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


class T1DSimGymnaisumEnv(gymnasium.Env):
metadata = {"render_modes": ["human"]}
metadata = {"render_modes": ["human"], "render_fps": 60}
MAX_BG = 1000

def __init__(
Expand All @@ -140,12 +139,14 @@ def __init__(
reward_fun=reward_fun,
seed=seed,
)
self.observation_space = gymnasium.spaces.Box(
low=0, high=self.MAX_BG, shape=(1,), dtype=np.float32
)
self.action_space = gymnasium.spaces.Box(
low=0, high=self.env.max_basal, shape=(1,), dtype=np.float32
)
self.observation_space = gymnasium.spaces.Box(low=0,
high=self.MAX_BG,
shape=(1, ),
dtype=np.float32)
self.action_space = gymnasium.spaces.Box(low=0,
high=self.env.max_basal,
shape=(1, ),
dtype=np.float32)

def step(self, action):
obs, reward, done, info = self.env.step(action)
Expand All @@ -159,7 +160,8 @@ def step(self, action):
# )
# Once the max_episode_steps is set, the truncated value will be overridden.
truncated = False
return np.array([obs.CGM], dtype=np.float32), reward, done, truncated, info
return np.array([obs.CGM],
dtype=np.float32), reward, done, truncated, info

def reset(self, seed=None, options=None):
super().reset(seed=seed)
Expand Down
2 changes: 1 addition & 1 deletion simglucose/patient/t1dpatient.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def reset(self):
Reset the patient state to default intial state
'''
if self._init_state is None:
self.init_state = self._params.iloc[2:15]
self.init_state = np.copy(self._params.iloc[2:15].values)
else:
self.init_state = self._init_state

Expand Down

0 comments on commit 25b1bab

Please sign in to comment.