Beispiel #1
0
    def __init__(self, patient_number=0):
        """
        Initializing the simulation environment.
        """
        np.random.seed(1) ### Fixing seed


        self.previous_action = 0

        ## Loading variable parameters
        reward_flag, bg_init_flag = self._update_parameters()


        # Initialize bolus history
        self.bolusHistoryIndex = 0
        self.bolusHistoryValue = []
        self.bolusHistoryTime = []
        self.insulinOnBoard = np.zeros(1)

        # Initialize sensor model
        self.CGMlambda = 15.96    # Johnson parameter of recalibrated and synchronized sensor error.
        self.CGMepsilon = -5.471  # Johnson parameter of recalibrated and synchronized sensor error.
        self.CGMdelta = 1.6898    # Johnson parameter of recalibrated and synchronized sensor error.
        self.CGMgamma = -0.5444   # Johnson parameter of recalibrated and synchronized sensor error.
        self.CGMerror = 0
        self.sensor_noise = np.random.randn(1)
        # self.CGMaux = []
        self.sensorNoiseValue = 0.07 # Set a value

        # =======================
        # Anas patient parameters
        # =======================
        P, init_basal, carb_factor, _ = matlab_to_python(patient_number)
        init_basal_optimal = init_basal
        self.P = P
        self.init_basal_optimal = init_basal_optimal
        self.bolus = carb_factor

        self.action_space = spaces.Box(0, 2*self.init_basal_optimal[0], (1,), dtype=np.float32)


        if bg_init_flag == 'random':
            self.init_basal = np.random.choice(np.linspace(init_basal_optimal-2, init_basal_optimal, 10))
        elif bg_init_flag == 'fixed':
            self.init_basal = init_basal_optimal

        # Flag for manually resetting the init
        self.reset_basal_manually = None

        self._seed()
        self.viewer = None

        # ==========================================
        # Setting up the Hovorka simulator
        # ==========================================

        # Initial values for parameters
        initial_pars = (self.init_basal, 0, P)

        # Initial value
        X0 = fsolve(hovorka_model_tuple, np.zeros(11), args=initial_pars)
        self.X0 = X0

        # Simulation setup
        self.integrator = ode(hovorka_model)
        self.integrator.set_integrator('vode', method='bdf', order=5)

        self.integrator.set_initial_value(X0, 0)

        # Simulation time in minutes
        self.simulation_time = 30
        self.n_solver_steps = 1
        self.stepsize = int(self.simulation_time/self.n_solver_steps)
        self.observation_space = spaces.Box(0, 500, (int(self.stepsize + 4 + 2),), dtype=np.float32)

        # State is BG, simulation_state is parameters of hovorka model
        initial_bg = X0[-1] * 18
        initial_insulin = np.ones(4) * self.init_basal_optimal
        initial_iob = np.zeros(1)
        self.state = np.concatenate([np.repeat(initial_bg, self.simulation_time), initial_insulin, initial_iob, np.zeros(1)])

        self.simulation_state = X0

        # Keeping track of entire blood glucose level for each episode
        self.bg_history = []
        self.insulin_history = initial_insulin

        # ====================
        # Meal setup
        # ====================

        eating_time = 1
        meals, meal_indicator = meal_generator(eating_time=eating_time, premeal_bolus_time=0)

        self.meals = meals
        self.meal_indicator = meal_indicator
        self.eating_time = eating_time

        # Counter for number of iterations
        self.num_iters = 0

        # If blood glucose is less than zero, the simulator is out of bounds.
        self.bg_threshold_low = 0
        self.bg_threshold_high = 500

        # TODO: This number is arbitrary
        self.max_iter = 2160

        # Reward flag
        self.reward_flag = reward_flag

        self.steps_beyond_done = None
Beispiel #2
0
    def __init__(self, patient_number=None):
        """
        Initializing the simulation environment.
        """

        # Action space
        self.previous_action = 0

        # State space
        self.observation_space = spaces.Box(0, 500, (34,), dtype=np.float32)
        # self.observation_space = spaces.Box(0, 500, 1)

        self.bolus = 6

        ## Loading variable parameters
        # meal_times, meal_amounts, reward_flag, bg_init_flag = self._update_parameters()
        reward_flag, bg_init_flag = self._update_parameters()

        # Action space
        # ====================================
        # Normalized action space!!
        # ====================================
        self.action_space = spaces.Box(0, 50, (1,), dtype=np.float32)
        # self.action_space = spaces.Box(-1, 1, (1,))



        # self.seed()
        self.viewer = None

        # ==========================================
        # Setting up the cambridge simulator
        # ==========================================


        # Selecting the virtual patient parameter
        if patient_number==None:
            patient_number = 0
        self.patient_number = patient_number


        P = pars[:, patient_number]
        self.init_basal = init_basal_rates[patient_number]

        # Patient parameters -- sub_1() means virtual patient #1
        # P = subject(1)
        # P = subject(2)
        # P = subject(3)
        # P = subject(4)
        # P = subject(5)
        # P = subject(6)

        self.P = P

        # Initial basal -- this rate dictates the initial BG value

        if bg_init_flag == 'random':
            self.init_basal = np.random.choice(np.linspace(init_basal_rates[self.patient_number]-2, init_basal_rates[self.patient_number], 10))

        # Flag for manually resetting the init
        self.reset_basal_manually = None

        # Initial values for parameters
        initial_pars = (self.init_basal, 0, P)

        # Initial value
        X0 = fsolve(cambridge_model_tuple, np.zeros(11), args=initial_pars)
        self.X0 = X0

        # Simulation setup
        self.integrator = ode(cambridge_model)
        self.integrator.set_integrator('vode', method='bdf', order=5)
        self.integrator.set_initial_value(X0, 0)

        # Simulation time in minutes
        self.simulation_time = 30

        # State is BG, simulation_state is parameters of hovorka model
        initial_bg = X0[-1] * 18
        initial_insulin = np.zeros(4)
        self.state = np.concatenate([np.repeat(initial_bg, self.simulation_time), initial_insulin])

        self.simulation_state = X0

        # Keeping track of entire blood glucose level for each episode
        self.bg_history = []
        self.insulin_history = initial_insulin

        # ====================
        # Meal setup
        # ====================

        eating_time = 30
        meals, meal_indicator = meal_generator(eating_time=eating_time)

        # TODO: Clean up these
        self.meals = meals
        self.meal_indicator = meal_indicator
        self.eating_time = eating_time

        # Counter for number of iterations
        self.num_iters = 0

        # If blood glucose is less than zero, the simulator is out of bounds.
        self.bg_threshold_low = 0
        self.bg_threshold_high = 500

        # TODO: This number is arbitrary
        self.max_iter = 1440

        # Reward flag
        self.reward_flag = reward_flag

        self.steps_beyond_done = None
Beispiel #3
0
    def __init__(self):
        """
        Initializing the simulation environment.
        """

        # Fixing the random seed -- for reproducible experiments
        np.random.seed(1)

        # Miguel: Why is this needed?
        self.previous_action = 0

        # Bolus carb factor -- [g/U]
        # self.bolus = 30
        self.bolus = 25

        ## Loading variable parameters -- used if environment is extended
        reward_flag, bg_init_flag = self._update_parameters()


        # Miguel: CamelCase is not used in rest of code

        # Initialize bolus history -- used for insulin on board
        self.bolusHistoryIndex = 0
        self.bolusHistoryValue = []
        self.bolusHistoryTime = []
        self.insulinOnBoard = np.zeros(1)

        # Initialize sensor model -- Miguel: do we need all of this?
        # self.CGMlambda = 15.96    # Johnson parameter of recalibrated and synchronized sensor error.
        # self.CGMepsilon = -5.471  # Johnson parameter of recalibrated and synchronized sensor error.
        # self.CGMdelta = 1.6898    # Johnson parameter of recalibrated and synchronized sensor error.
        # self.CGMgamma = -0.5444   # Johnson parameter of recalibrated and synchronized sensor error.
        # self.CGMerror = 0
        self.sensor_noise = np.random.randn(1)
        # self.CGMaux = []
        # self.sensorNoiseValue = 0.07 # Set a value


        # Model parameters
        P = hovorka_parameters(70)
        init_basal_optimal = 6.43
        self.P = P
        self.init_basal_optimal = init_basal_optimal

        # Initializing the action space
        self.action_space = spaces.Box(0, 2*self.init_basal_optimal, (1,), dtype=np.float32)

        # Initial basal rate -- used for init and reset. Either randomly initialized or by a fixed value.

        if bg_init_flag == 'random':
            self.init_basal = np.random.choice(np.linspace(init_basal_optimal-2, init_basal_optimal, 10))
        elif bg_init_flag == 'fixed':
            self.init_basal = init_basal_optimal

        # Flag for manually resetting the init when the episode restarts
        self.reset_basal_manually = None

        self._seed()
        self.viewer = None

        # ==========================================
        # Setting up the Hovorka simulator
        # ==========================================

        # Initial values for parameters
        initial_pars = (self.init_basal, 0, P)

        # Initial value
        X0 = fsolve(hovorka_model_tuple, np.zeros(11), args=initial_pars)
        self.X0 = X0

        # Simulation setup
        self.integrator = ode(hovorka_model)
        self.integrator.set_integrator('vode', method='bdf', order=5)
        self.integrator.set_initial_value(X0, 0)

        # Simulation time in minutes -- the default is solving the simulator 30 minutes at a time
        # with a solver time of one minute.
        self.simulation_time = 30
        self.n_solver_steps = 1
        self.stepsize = int(self.simulation_time/self.n_solver_steps)

        # State space for the environment -- [30 min of BG, last 4 insulin action, bolus (if given) during last 30 mins]
        self.observation_space = spaces.Box(0, 500, (int(self.stepsize + 4 + 2),), dtype=np.float32)

        # State is BG, simulation_state is parameters of hovorka model
        initial_bg = X0[-1] * 18
        initial_insulin = np.ones(4) * self.init_basal_optimal
        initial_iob = np.zeros(1)

        # Initial state
        self.state = np.concatenate([np.repeat(initial_bg, self.stepsize), initial_insulin, initial_iob, np.zeros(1)])

        self.simulation_state = X0

        # Keeping track of entire blood glucose level and insulin for each episode
        self.bg_history = []
        self.insulin_history = initial_insulin

        # ====================
        # Meal setup
        # ====================

        # Default eating time is considered one minute -- empirically the simulations are not too sensitive to this choice.
        eating_time = 1

        # Meals are carb intake and meal_indicator is the counted carbs by the patient
        meals, meal_indicator = meal_generator(eating_time=eating_time, premeal_bolus_time=0)

        self.meals = meals
        self.meal_indicator = meal_indicator
        self.eating_time = eating_time

        # Counter for number of iterations
        self.num_iters = 0

        # If blood glucose is less than zero or above 500, the simulator is considered out of bounds.
        self.bg_threshold_low = 0
        self.bg_threshold_high = 500

        # The max episode lenght is 36 hours
        self.max_iter = 2160

        # Reward flag
        self.reward_flag = reward_flag

        self.steps_beyond_done = None