主要内容

rlSimulinkEnv

使用实施的动态模型创建强化学习环境金宝app

描述

TherlSimulinkEnv函数从simulink创建强化学习环境对象金宝app®模型。环境对象作用一个界面,以便当您致电时sim或者train, these functions in turn call the Simulink model to generate experiences for the agents.

example

env= rl金宝appSimulinkenv(MDL,代理块)creates the reinforcement learning environment objectenvfor the Simulink modelMDL代理块contains the paths to one or more reinforcement learning agent blocks inMDL。如果you use this syntax, each agent block must reference an agent object already in the MATLAB®workspace.

example

env= rl金宝appSimulinkenv(MDL,代理块,obsInfo,actInfo)creates the reinforcement learning environment objectenv对于模型MDL。两个单元格数组obsInfoactInfomust contain the observation and action specifications for each agent block inMDL, in the same order as they appear in代理块

env= rl金宝appSimulinkenv(___,'UseFastRestart',fastRestartToggle)创建强化学习环境对象env和additionally enables fast restart. Use this syntax after any of the input arguments in the previous syntaxes.

例子

全部收缩

使用受过训练的代理金宝app和对应的simulink模型创建模拟环境创造Simulink Environment and Train Agent例子。

将代理加载在MATLAB®工作区中。

loadrlWaterTankDDPGAgent

创造an environment for therlwatertank模型,其中包含RL代理块。由于该块使用的代理已经在工作区中,因此您无需通过观察和操作规范来创建环境。

env = rl金宝appSimulinkenv('rlwatertank','rlwatertank/rl代理商')
env = 金宝appsimulinkenvwithagent带有属性:模型:rlwatertank agent block:rlwatertank/rl代理resetfcn:[] usefastrestart:on On

Validate the environment by performing a short simulation for two sample times.

验证环境(ENV)

You can now train and simulate the agent within the environment by usingtrainsim, respectively.

For this example, consider therlsimplependulummodel金宝appSimulink模型。该模型是一种简单的无摩擦摆,最初悬挂在向下的位置。

Open the model.

MDL='rlsimplependulummodel';Open_System(MDL)

创造rlnumericspecrlFiniteSetSpec观察和行动信息的对象。

obsinfo = rlnumericspec([3 1])3个观测值的矢量​​:sin(theta),cos(theta),d(theta)/dt
obsInfo = rlNumericSpec with properties: LowerLimit: -Inf UpperLimit: Inf Name: [0x0 string] Description: [0x0 string] Dimension: [3 1] DataType: "double"
actinfo = rlfinitesetsetspec([ -  2 0 2])% 3 possible values for torque: -2 Nm, 0 Nm and 2 Nm
actInfo = rlFiniteSetSpec with properties: Elements: [3x1 double] Name: [0x0 string] Description: [0x0 string] Dimension: [1 1] DataType: "double"

You can use dot notation to assign property values for therlnumericspecrlFiniteSetSpecobjects.

obsInfo.Name ='observations';actinfo.name ='torque';

Assign the agent block path information, and create the reinforcement learning environment for the Simulink model using the information extracted in the previous steps.

AgentBlk = [MDL'/RL Agent']; env = rlSimulinkEnv(mdl,agentBlk,obsInfo,actInfo)
env = 金宝appsimulinkenvwithagent具有属性:模型:rlsimplependulummodeldel agentBlock:rlsimplependulummodel/rl代理resetfcn:[] usefastrestart:on

You can also include a reset function using dot notation. For this example, randomly initializetheta0在模型工作区中。

env.ResetFcn = @(in) setVariable(in,'theta0',randn,“工作空间”,MDL)
env = 金宝appsimulinkenvwithagent具有属性:模型:rlsimplependulummodel agent block:rlSimplePendulummodel/rl代理resetfcn: @(in)setVariable(in)setVariable(in,'theta0','theta0',randn,randn,'workspace','workspace',mdl)usefastrestart on on on on on on:

从示例创建Simulink模型的环境金宝app培训多个代理商执行协作任务

将代理加载到MATLAB工作区中。

loadrlCollaborativeTaskAgents

创造an environment for therlCollaborativeTask模型,有两个代理块。由于两个块使用的代理(agentAagentB)已经在工作空间中了,您无需传递其观察和行动规范即可创建环境。

env = rl金宝appSimulinkenv('rlCollaborativeTask',[[“ rlcollaborativetask/agent a”,“ rlcollaborativetask/agent b”)))
env = 金宝appsimulinkenvwithagent带有属性:模型:rlCollaborativetask代理block:[rlcollaborativetask/agent a rlCollaborativetask/agent b] resetfcn:[]

You can now simulate or train the agents within the environment usingsim或者train, respectively.

Input Arguments

全部收缩

金宝appSimulink模型名称,指定为字符串或字符向量。该模型必须至少包含一个RL代理block.

代理阻止路径,指定为字符串,字符向量或字符串数​​组。

如果MDLcontains a singleRL代理block, specify代理块as a string or character vector containing the block path.

如果MDL包含多个RL代理blocks, specify代理块作为字符串数组,每个元素都包含一个代理块的路径。

MDLcan containRL代理未包含路径的块代理块。此类代理阻止作为环境的一部分行为,根据其当前政策选择行动。你打电话时sim或者train,这些代理商的经验未返回,其政策也没有更新。

对于MATLAB环境,不支持多代理模拟。金宝app

The agent blocks can be inside of a model reference. For more information on configuring an agent block for reinforcement learning, seeRL代理

Observation information, specified as a specification object, an array of specification objects, or a cell array.

如果MDL包含一个单个代理块,指定obsInfo作为一个rlnumericspec对象,一个rlFiniteSetSpecobject, or an array containing a mix of such objects.

如果MDL包含多个agent blocks, specifyobsInfoas a cell array, where each cell contains a specification object or array of specification objects for the corresponding block in代理块

有关更多信息,请参阅GetObservationinfo

Action information, specified as a specification object or a cell array.

如果MDL包含一个单个代理块,指定actInfo作为一个rlnumericspec或者rlFiniteSetSpec目的。

如果MDL包含多个agent blocks, specifyactInfo作为单元格数组,每个单元格包含一个针对相应块的规范对象代理块

有关更多信息,请参阅getActioninfo

选择快速重新启动的选项, specified as either'on'或者'离开'。快速重新启动允许您执行迭代模拟,而无需编译模型或每次终止模拟。

有关快速重新启动的更多信息,请参阅How Fast Restart Improves Iterative Simulations(Simulink)

Output Arguments

全部收缩

Reinforcement learning environment, returned as aSimulinkEnvWithAgent目的。

For more information on reinforcement learning environments, see创建Simul金宝appink增强学习环境

Version History

Introduced in R2019a