main content

simulate trained reinforcement learning agents within specified environment -凯发k8网页登录

simulate trained reinforcement learning agents within specified environment

since r2019a

description

example

experience = sim(env,agents) simulates one or more reinforcement learning agents within an environment, using default simulation options.

experience = sim(agents,env) performs the same simulation as the previous syntax.

env = sim(___,simopts) uses the simulation options object simopts. use simulation options to specify parameters such as the number of steps per simulation or the number of simulations to run. use this syntax after any of the input arguments in the previous syntaxes.

examples

simulate a reinforcement learning environment with an agent configured for that environment. for this example, load an environment and agent that are already configured. the environment is a discrete cart-pole environment created with rlpredefinedenv. the agent is a policy gradient (rlpgagent) agent. for more information about the environment and agent used in this example, see .

rng(0) % for reproducibility
load rlsimexample.mat
env
env = 
  cartpolediscreteaction with properties:
                  gravity: 9.8000
                 masscart: 1
                 masspole: 0.1000
                   length: 0.5000
                 maxforce: 10
                       ts: 0.0200
    thetathresholdradians: 0.2094
               xthreshold: 2.4000
      rewardfornotfalling: 1
        penaltyforfalling: -5
                    state: [4x1 double]
agent
agent = 
  rlpgagent with properties:
            agentoptions: [1x1 rl.option.rlpgagentoptions]
    useexplorationpolicy: 1
         observationinfo: [1x1 rl.util.rlnumericspec]
              actioninfo: [1x1 rl.util.rlfinitesetspec]
              sampletime: 0.1000

typically, you train the agent using train and simulate the environment to test the performance of the trained agent. for this example, simulate the environment using the agent you loaded. configure simulation options, specifying that the simulation run for 100 steps.

simopts = rlsimulationoptions(maxsteps=100);

for the predefined cart-pole environment used in this example. you can use plot to generate a visualization of the cart-pole system. when you simulate the environment, this plot updates automatically so that you can watch the system evolve during the simulation.

plot(env)

figure cart pole visualizer contains an axes object. the axes object contains 6 objects of type line, polygon.

simulate the environment.

experience = sim(env,agent,simopts)

figure cart pole visualizer contains an axes object. the axes object contains 6 objects of type line, polygon.

experience = struct with fields:
       observation: [1x1 struct]
            action: [1x1 struct]
            reward: [1x1 timeseries]
            isdone: [1x1 timeseries]
    simulationinfo: [1x1 struct]

the output structure experience records the observations collected from the environment, the action and reward, and other data collected during the simulation. each field contains a timeseries object or a structure of timeseries data objects. for instance, experience.action is a timeseries containing the action imposed on the cart-pole system by the agent at each step of the simulation.

experience.action
ans = struct with fields:
    cartpoleaction: [1x1 timeseries]

simulate an environment created for the simulink® model used in the example train multiple agents to perform collaborative task, using the agents trained in that example.

load the agents in the matlab® workspace.

load rlcollaborativetaskagents

create an environment for the rlcollaborativetask simulink® 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"]);

load the parameters that are needed by the rlcollaborativetask simulink® model to run.

rlcollaborativetaskparams

simulate the agents against the environment, saving the experiences in xpr.

xpr = sim(env,[agenta agentb]);

plot actions of both agents.

subplot(2,1,1); plot(xpr(1).action.forces)
subplot(2,1,2); plot(xpr(2).action.forces)

figure contains 2 axes objects. axes object 1 with title time series plot:forces, xlabel time (seconds), ylabel forces contains 2 objects of type stair. axes object 2 with title time series plot:forces, xlabel time (seconds), ylabel forces contains 2 objects of type stair.

input arguments

environment in which the agents act, specified as one of the following kinds of reinforcement learning environment object:

  • a predefined matlab® or simulink® environment created using rlpredefinedenv.

  • a custom matlab environment you create with functions such as rlfunctionenv or rlcreateenvtemplate. this kind of environment does not support training multiple agents at the same time.

  • a simulink environment you create using createintegratedenv. this kind of environment does not support training multiple agents at the same time.

  • a custom simulink environment you create using rlsimulinkenv. this kind of environment supports training multiple agents at the same time, and allows you to use multi-rate execution, so that each agent has its own execution rate.

  • a custom matlab environment you create using rlmultiagentfunctionenv or rlturnbasedfunctionenv. this kind of environment supports training multiple agents at the same time. in an rlmultiagentfunctionenv environment all agents execute in the same step, while in an rlmultiagentfunctionenv environment agents execute in turns.

for more information about creating and configuring environments, see:

when env is a simulink environment, the environment object acts an interface so that sim calls the (compiled) simulink model to generate experiences for the agents.

agents to simulate, specified as a reinforcement learning agent object, such as rlacagent or , or as an array of such objects.

if env is a multi-agent environment, specify agents as an array. the order of the agents in the array must match the agent order used to create env.

for more information about how to create and configure agents for reinforcement learning, see reinforcement learning agents.

simulation options, specified as an rlsimulationoptions object. use this argument to specify options such as:

  • number of steps per simulation

  • number of simulations to run

for details, see rlsimulationoptions.

output arguments

simulation results, returned as a structure or structure array. the number of rows in the array is equal to the number of simulations specified by the numsimulations option of rlsimulationoptions. the number of columns in the array is the number of agents. the fields of each experience structure are as follows.

observations collected from the environment, returned as a structure with fields corresponding to the observations specified in the environment. each field contains a of length n 1, where n is the number of simulation steps.

to obtain the current observation and the next observation for a given simulation step, use code such as the following, assuming one of the fields of observation is obs1.

obs = getsamples(experience.observation.obs1,1:n);
nextobs = getsamples(experience.observation.obs1,2:n 1);
these values can be useful if you are writing your own training algorithm using sim to generate experiences for training.

actions computed by the agent, returned as a structure with fields corresponding to the action signals specified in the environment. each field contains a of length n, where n is the number of simulation steps.

reward at each step in the simulation, returned as a of length n, where n is the number of simulation steps.

flag indicating termination of the episode, returned as a of a scalar logical signal. this flag is set at each step by the environment, according to conditions you specify for episode termination when you configure the environment. when the environment sets this flag to 1, simulation terminates.

information collected during simulation, returned as one of the following:

  • for matlab environments, a structure containing the field simulationerror. this structure contains any errors that occurred during simulation.

  • for simulink environments, a simulink.simulationoutput object containing simulation data. recorded data includes any signals and states that the model is configured to log, simulation metadata, and any errors that occurred.

version history

introduced in r2019a

网站地图