main content

create environment object from a simulink model already containing agent and environment -凯发k8网页登录

create environment object from a simulink model already containing agent and environment

since r2019a

description

the rlsimulinkenv function creates an environment object from a simulink® model that already includes your agent block. the environment object acts as an interface so that when you call sim or train, these functions in turn call the (compiled) simulink model to generate experiences for the agents.

to create an environment object from a simulink model that does not include an agent block, use createintegratedenv instead. for more information on reinforcement learning environments, see create custom simulink environments.

example

env = rlsimulinkenv(mdl,agentblocks) creates the reinforcement learning environment object env for the simulink model mdl. agentblocks contains the paths to one or more reinforcement learning agent blocks in mdl. if you use this syntax, each agent block must reference an agent object already in the matlab® workspace.

example

env = rlsimulinkenv(mdl,agentblocks,observationinfo,actioninfo) creates the reinforcement learning environment object env for the model mdl. the two cell arrays observationinfo and actioninfo must contain the observation and action specifications for each agent block in mdl, in the same order as they appear in agentblocks.

env = rlsimulinkenv(___,'usefastrestart',fastrestarttoggle) creates a reinforcement learning environment object env and additionally enables fast restart. use this syntax after any of the input arguments in the previous syntaxes.

examples

create a simulink environment using the trained agent and corresponding simulink model from the create simulink environment and train agent example.

load the agent in the matlab® workspace.

load rlwatertankddpgagent

create an environment for the rlwatertank model, which contains an rl agent block. since the agent used by the block is already in the workspace, you do not need to pass the observation and action specifications to create the environment.

env = rlsimulinkenv("rlwatertank","rlwatertank/rl agent")
env = 
simulinkenvwithagent with properties:
           model : rlwatertank
      agentblock : rlwatertank/rl agent
        resetfcn : []
  usefastrestart : on

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

validateenvironment(env)

you can now train and simulate the agent within the environment by using train and sim, respectively.

for this example, consider the rlsimplependulummodel simulink® model. the model is a simple frictionless pendulum that initially hangs in a downward position.

open the model.

mdl = "rlsimplependulummodel";
open_system(mdl)

create rlnumericspec and rlfinitesetspec objects for the observation and action information, respectively.

the observation is a vector containing three signals: the sine, cosine, and time derivative of the angle.

obsinfo = rlnumericspec([3 1]) 
obsinfo = 
  rlnumericspec with properties:
     lowerlimit: -inf
     upperlimit: inf
           name: [0x0 string]
    description: [0x0 string]
      dimension: [3 1]
       datatype: "double"

the action is a scalar expressing the torque and can be one of three possible values, -2 nm, 0 nm and 2 nm.

actinfo = rlfinitesetspec([-2 0 2])
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 the rlnumericspec and rlfinitesetspec objects.

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 = 
simulinkenvwithagent with properties:
           model : rlsimplependulummodel
      agentblock : rlsimplependulummodel/rl agent
        resetfcn : []
  usefastrestart : on

you can also specify a reset function using dot notation. for this example, randomly initialize theta0 in the model workspace.

env.resetfcn = @(in) setvariable(in,"theta0",randn,"workspace",mdl)
env = 
simulinkenvwithagent with properties:
           model : rlsimplependulummodel
      agentblock : rlsimplependulummodel/rl agent
        resetfcn : @(in)setvariable(in,"theta0",randn,"workspace",mdl)
  usefastrestart : on

create an environment for the simulink model from the example train multiple agents to perform collaborative task.

load the agents in the matlab workspace.

load rlcollaborativetaskagents

create an environment for the rlcollaborativetask model, which has two agent blocks. since the agents used by the two blocks (agenta and agentb) are already in the workspace, you do not need to pass their observation and action specifications to create the environment.

env = rlsimulinkenv( ...
    "rlcollaborativetask", ...
    ["rlcollaborativetask/agent a","rlcollaborativetask/agent b"])
env = 
simulinkenvwithagent with properties:
           model : rlcollaborativetask
      agentblock : [
                     rlcollaborativetask/agent a
                     rlcollaborativetask/agent b
                   ]
        resetfcn : []
  usefastrestart : on

you can now simulate or train the agents within the environment using sim or train, respectively.

input arguments

simulink model name, specified as a string or character vector. the model must contain at least one rl agent block.

agent block paths, specified as a string, character vector, or string array.

if mdl contains a single rl agent block, specify agentblocks as a string or character vector containing the block path.

if mdl contains multiple rl agent blocks, specify agentblocks as a string array, where each element contains the path of one agent block.

mdl can contain rl agent blocks whose path is not included in agentblocks. such agent blocks behave as part of the environment, selecting actions based on their current policies. when you call sim or train, the experiences of these agents are not returned and their policies are not updated.

multi-agent simulation is not supported for matlab environments.

the agent blocks can be inside of a model reference. for more information on configuring an agent block for reinforcement learning, see rl agent.

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

if mdl contains a single agent block, specify observationinfo as an rlnumericspec object, an rlfinitesetspec object, or an array containing a mix of such objects.

if mdl contains multiple agent blocks, specify observationinfo as a cell array, where each cell contains a specification object or array of specification objects for the corresponding block in agentblocks.

for more information, see getobservationinfo.

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

if mdl contains a single agent block, specify actioninfo as an rlnumericspec or rlfinitesetspec object.

if mdl contains multiple agent blocks, specify actioninfo as a cell array, where each cell contains a specification object for the corresponding block in agentblocks.

for more information, see getactioninfo.

option to toggle fast restart, specified as either 'on' or 'off'. fast restart allows you to perform iterative simulations without compiling a model or terminating the simulation each time.

for more information on fast restart, see (simulink).

output arguments

reinforcement learning environment, returned as a simulinkenvwithagent object.

for more information on reinforcement learning environments, see create custom simulink environments.

version history

introduced in r2019a

网站地图