在openai健身房或rllab中作为强化学习环境的1型糖尿病模拟器(uva/padova模拟器的python实现)
simglucose的Python项目详细描述
葡萄糖
用python实现的1型糖尿病模拟器,用于强化学习目的
这个模拟器是fda批准的一个python实现,仅用于研究目的。该模拟器包括30名虚拟病人、10名青少年、10名成人、10名儿童。
- 注:simglucose只支持python3。
主要功能
- 模拟环境遵循OpenAI Gym和RLLab API。它在每一步都返回观察、奖励、完成、信息,这意味着模拟器"强化学习准备就绪"。
- 支持自定义奖励功能。奖赏函数是最后一小时血糖测量值的函数。默认情况下,每一步的奖励是
风险[t-1]-风险[t]
。风险[t]
是本文件中定义的时间t
风险指数。- 支持并行计算。模拟器使用pathos多处理包并行模拟多个患者(您可以通过设置
parallel=false
)来关闭并行功能。- 模拟器提供一个随机场景生成器(
来自simglucose.simulation.scenario\u gen import randomsenario
)和一个自定义场景生成器(来自simglucose.simulation.scenario import customscenario
)。命令行用户界面将引导您完成方案设置。- 模拟器提供了目前最基本的基本弹丸控制器。它提供了非常简单的语法来实现自己的控制器,如模型预测控制、PID控制、强化学习控制等。
- 您可以指定随机种子,以防重复实验。
- 模拟器将在模拟后生成多个性能分析图。这些图包括血糖跟踪图、控制变异性网格分析图、不同区域血糖统计图、危险指数统计图。
- 注意:
动画
和并行
不能在MacOS中同时设置为真
。macos中matplotlib的大多数后端不是线程安全的。Windows尚未测试。如果有人测试过,请告诉我结果。 - 支持并行计算。模拟器使用pathos多处理包并行模拟多个患者(您可以通过设置
安装
强烈建议使用pip
到installsimglucose
,按照以下链接安装PIP。
自动安装:
pip install simglucose
手动安装:
git clone https://github.com/jxx123/simglucose.git
cd simglucose
如果安装了pip,则
pip install -e .
如果您没有pip
,则
python setup.py install
如果安装了rllab(可选),包将利用rllab中的一些功能。
注意:自动安装版本和手动安装版本之间可能有一些细微的差异。使用git clone
和手动安装获取最新版本。
快速启动
使用simglucose作为模拟器并测试控制器
运行模拟器用户界面
fromsimglucose.simulation.user_interfaceimportsimulatesimulate()
您可以自由实现自己的控制器,并在模拟器中进行测试。例如,
fromsimglucose.simulation.user_interfaceimportsimulatefromsimglucose.controller.baseimportController,ActionclassMyController(Controller):def__init__(self,init_state):self.init_state=init_stateself.state=init_statedefpolicy(self,observation,reward,done,**info):''' Every controller must have this implementation! ---- Inputs: observation - a namedtuple defined in simglucose.simulation.env. For now, it only has one entry: blood glucose level measured by CGM sensor. reward - current reward returned by environment done - True, game over. False, game continues info - additional information as key word arguments, simglucose.simulation.env.T1DSimEnv returns patient_name and sample_time ---- Output: action - a namedtuple defined at the beginning of this file. The controller action contains two entries: basal, bolus '''self.state=observationaction=Action(basal=0,bolus=0)returnactiondefreset(self):''' Reset the controller state to inital state, must be implemented '''self.state=self.init_statectrller=MyController(0)simulate(controller=ctrller)
这两个示例也可以在examples\folder中找到。
实际上,您可以通过 您可以创建仿真对象,并运行批处理仿真。例如, 脱机运行分析 通过创建问题来解决任何错误、增强功能甚至讨论。
以下说明最初来自sklearn的贡献说明 促进simglucose的首选工作流是
主存储库on
github,在分支上克隆和开发。步骤: 从项目存储库转移
点击页面右上角的"fork"按钮。这创造
github用户帐户下的代码副本。有关的详细信息
如何分叉存储库请参见本指南。
将simglucose repo的fork从github帐户克隆到本地磁盘: 创建 始终使用 在功能分支上开发功能。使用 要在git中记录更改,请将更改推送到github帐户: 遵循这些说明
从分叉创建拉取请求。这将向提交者发送电子邮件。 (如果上面的任何一个对你来说都是魔法的话你,请查一下
git文档在Web上,或向朋友或其他贡献者寻求帮助。模拟指定更多的模拟参数
simulate(sim_time=my_sim_time,scenario=my_scenario,controller=my_controller,start_time=my_start_time,save_path=my_save_path,animate=False,parallel=True)
OpenAI健身房使用
importgym# Register gym environment. By specifying kwargs,# you are able to choose which patient to simulate.# patient_name must be 'adolescent#001' to 'adolescent#010',# or 'adult#001' to 'adult#010', or 'child#001' to 'child#010'fromgym.envs.registrationimportregisterregister(id='simglucose-adolescent2-v0',entry_point='simglucose.envs:T1DSimEnv',kwargs={'patient_name':'adolescent#002'})env=gym.make('simglucose-adolescent2-v0')observation=env.reset()fortinrange(100):env.render(mode='human')print(observation)# Action in the gym environment is a scalar# representing the basal insulin, which differs from# the regular controller action outside the gym# environment (a tuple (basal, bolus)).# In the perfect situation, the agent should be able# to control the glucose only through basal instead# of asking patient to take bolusaction=env.action_space.sample()observation,reward,done,info=env.step(action)ifdone:print("Episode finished after {} timesteps".format(t+1))break
importgymfromgym.envs.registrationimportregisterdefcustom_reward(BG_last_hour):ifBG_last_hour[-1]>180:return-1elifBG_last_hour[-1]<70:return-2else:return1register(id='simglucose-adolescent2-v0',entry_point='simglucose.envs:T1DSimEnv',kwargs={'patient_name':'adolescent#002','reward_fun':custom_reward})env=gym.make('simglucose-adolescent2-v0')reward=1done=Falseobservation=env.reset()fortinrange(200):env.render(mode='human')action=env.action_space.sample()observation,reward,done,info=env.step(action)print(observation)print("Reward = {}".format(reward))ifdone:print("Episode finished after {} timesteps".format(t+1))break
RLLab用法
pip install simglucose
0
高级用法
pip install simglucose
1
pip install simglucose
2
发行说明
2018年10月9日
policy
方法通过info['patient\u state']
访问所有当前患者状态
2018年2月26日
2018年10月1日
2018年7月1日
gym.make('simglucose-v0')
创建环境。simglucose.envs.t1dsimenv的构造函数中硬编码
报告问题
如何贡献
pip install simglucose
3
功能
分支以保存您的开发更改:pip install simglucose
4
功能
分支。最好不要在
master
分支上工作!git add
添加更改的文件,然后使用git commit
文件:pip install simglucose
5
pip install simglucose
6
推荐PyPI第三方库