主要内容

Custom Agents

要实现自己的自定义加强学习算法,可以通过创建自定义代理类的子类来创建自定义代理。然后,您可以在Matlab中培训和模拟此代理®和Simulink®环境。有关在MATLAB中创建类的更多信息,请参阅用户定义的类

创建模板类

要定义自定义代理,请先创建一个是子类的类rl.agent.customagent.class. As an example, this topic describes the custom LQR agent trained inTrain Custom LQR Agent。作为您自己代理的起点,您可以打开和修改此自定义代理类。要将示例文件添加到MATLAB路径并在MATLAB命令行处打开文件,请键入以下代码。

addpath(fullfile(matlabroot,'例子''rl''主要'));编辑lqrcustomagent.m

一种fter saving the class to your own working folder, you can remove the example files from the path.

rmpath(fullfile(matlabroot,'例子''rl''主要'));

此类具有以下类定义,它表示代理类名和关联的抽象代理。

classdeflqrcustomagent 
              

要定义您的代理,您必须指定以下内容:

  • 代理属性

  • 构造函数

  • 评论家representation that estimates the discounted long-term reward (if required for learning)

  • 演员表示,即根据当前观察选择动作(如果需要学习)

  • R.equired agent methods

  • 可选的代理方法

代理属性

在里面属性类文件的部分,指定创建和培训代理所需的任何参数。这些参数可以包括:

  • 折扣未来奖励的折扣因素

  • 用于探索模型的配置参数,例如噪声模型或epsilon - 贪婪探索

  • 使用重放内存的缓冲区

  • Mini-batch sizes for sampling from the experience buffer

  • N你mber of steps to look ahead during training

For more information on potential agent properties, see the option objects for the built-in Reinforcement Learning Toolbox™ agents.

rl.agent.customagent.类已经包括代理示例时间的属性(SampleTime)和行动和观察规范(一种ctionInfo观察税收那respectively).

自定义LQR代理定义以下代理属性。

属性% Q问:% RR.%反馈收益K.% Discount factor伽玛= 0.95%评论家评论家k%缓冲器K.Bufferk%的更新数kupdate = 1% Number for estimator update估计= 10.end属性(Access = private) Counter = 1 YBuffer HBufferend

构造函数

To create your custom agent, you must define a constructor function that:

例如,lqrcustomagent.构造函数定义了连续动作和观察空间,并创造了批评者表示。该疯狂的功能is an optional helper function that defines the critic representation.

功能obj = lqrcustomagent(q,r,italyk)%检查输入参数的数量narginchk(3,3);%调用抽象类构造函数obj = obj@rl.agent.customagent();%设置Q和R矩阵obj.q = q;obj.r = r;%定义了观察和动作空间obj.observationinfo = rlnumericspec([size(q,1),1]);obj.actioninfo = rlnumericspec([size(r,1),1]);%创造批评批评者表示obj.critic = createcitic(obj);% Initialize the gain matrixobj.k = italyk;% Initialize the experience buffersobj.ybuffer = zeros(obj.estimateNum,1);num =尺寸(q,1)+大小(r,1);obj.hbuffer = zeros(obj.estimateNum,0.5 * num *(num + 1));obj.kbuffer = cell(1,1000);obj.kbuffer {1} = obj.k;endend

演员和批评者表示

如果您的学习算法使用批读表示来估计长期奖励,则要选择动作或两者的演员,您必须将其添加为代理属性。创建代理时,您必须创建这些表示;也就是说,在构造函数中。有关创建演员和批评者的更多信息,请参阅Create Policy and Value Function Representations

例如,自定义LQR代理使用批评者表示,存储在其中评论家财产,没有演员。批评者的创作是实施的疯狂的辅助功能,从中调用lqrcustomagent.构造函数。

功能批评=疯狂(obj)nq = size(obj.q,1);nr = size(obj.r,1);n = nq + nr;W0 = 0.1 *α(0.5 *(n + 1)* n,1);批评= rlqvaluerepresentation({@(x,u)computequadraticbasis(x,u,n),w0},......getobservationInfo(obj),getActionInfo(OBJ));批评.Options.gradientThreshold = 1;end

在这种情况下,评论家是一个rlqvalueerepresentationobject. To create such a representation, you must specify the handle to a custom basis function, in this case thecomputequadraticbasis.功能。有关此评论家的更多信息,请参阅Train Custom LQR Agent

R.equired Functions

To create a custom reinforcement learning agent you must define the following implementation functions. To call these functions in your own code, use the wrapper methods from the abstract base class. For example, to callgetActionimpl., 采用努力。包装器方法具有与实现方法相同的输入和输出参数。

功能 Description
getActionimpl. 选择s an action by evaluating the agent policy for a given observation
努力WithExplorationImpl 使用代理的探索模型选择一个操作
学习中 从当前的经历中了解并恢复探索的动作

Within your implementation functions, to evaluate your actor and critic representations, you can use thegetValue努力,和getMaxQValue职能。

  • To evaluate anrlvalueerepresentationcritic with only observation input signals, obtain the state value functionV.使用以下语法。

    v = getValue(评论家,观察);
  • To evaluate anrlqvalueerepresentation批判与观察和动作输入信号,获得状态动作值函数问:使用以下语法。

    q = getValue(评论家,[观察,动作]);
  • To evaluate anrlqvalueerepresentation批评只有观察输入信号,获取状态动作值函数问:对于所有可能的离散操作,使用以下语法。

    q = getValue(评论家,观察);
  • 离散的动作空间rlqvalueerepresentationcritic, obtain the maximum Q state-action value function问:对于所有可能的离散操作,使用以下语法。

    [maxq,maxactindex] = getmaxqvalue(批评者,观察);
  • 评估演员代表(rlstochastorrepresentation要么rlDeterministicActorRepresentation),获得行动一种使用以下语法。

    一种= getAction(Actor,Observation);

对于这些情况中的每一个,如果您的演员或批评网络使用经常性神经网络,则在获得相应的网络输出之后,该功能也可以返回网络状态的当前值。

getActionimpl.功能

getActionimpl.功能is evaluates the policy of your agent and selects an action. This function must have the following signature, whereobjis the agent object,Observation是目前的观察,还有actionis the selected action.

功能action = getActionimpl(obj,观察)

For the custom LQR agent, you select an action by applying the= -K.X控制法。

功能action = getActionimpl(obj,观察)%给定系统的当前状态,返回一个动作action = -obj.K*Observation{:};end

努力WithExplorationImpl功能

努力WithExplorationImpl函数使用代理商的探索模型选择一个操作。使用此功能,您可以实现epsilon-greedy探索等算法。此功能必须具有以下签名,在哪里objis the agent object,Observation是目前的观察,还有actionis the selected action.

功能action = getActionWithExplorationImpl(obj,Observation)

For the custom LQR agent, the努力WithExplorationImpl功能将随机白噪声添加到使用当前代理策略选择的操作。

功能action = getActionWithExplorationImpl(obj,Observation)赋予当前观察的%,选择一个动作action = getAction(obj,Observation);% Add random noise to the actionnum = size(obj.r,1);动作=动作+ 0.1 * RANDN(NUM,1);end

学习中功能

学习中函数定义代理商如何从当前的经验中学到。此函数通过更新策略参数并使用探索选择操作来实现您的代理的自定义学习算法。此功能必须具有以下签名,在哪里objis the agent object,exp.是目前的代理经验,还有actionis the selected action.

功能Action = Seathimpl(OBJ,EXP)

该agent experience is the cell arrayexp.= {state,action,reward,nextstate,isdone}

  • is the current observation.

  • action是目前的行动。

  • 奖励是目前的奖励。

  • nextState是下一个观察。

  • 已经完成了是一个逻辑标志,表明训练集是完整的。

对于自定义LQR代理,批评参数将更新Nsteps.

功能Action = Seathimpl(OBJ,EXP)%解析体验输入x = exp {1} {1};u = exp {2} {1};dx = exp {4} {1};y =(x'* obj.q * x + u'* obj.r * u);num = size(obj.q,1)+大小(obj.r,1);% Wait N steps before updating the critic parametersN= obj.EstimateNum; h1 = computeQuadraticBasis(x,u,num); h2 = computeQuadraticBasis(dx,-obj.K*dx,num); H = h1 - obj.Gamma* h2;ifobj.Counter<=N obj.YBuffer(obj.Counter) = y; obj.HBuffer(obj.Counter,:) = H; obj.Counter = obj.Counter + 1;else% Update the critic parameters based on the batch of%经验H_buf = obj.HBuffer; y_buf = obj.YBuffer; theta = (H_buf'*H_buf)\H_buf'*y_buf; obj.Critic = setLearnableParameters(obj.Critic,{theta});% Derive a new gain matrix based on the new critic parametersobj.k = getnewk(obj);%重置体验缓冲区obj.counter = 1;obj.ybuffer = zeros(n,1);obj.hbuffer = zeros(n,0.5 * num *(num + 1));obj.kupdate = obj.kupdate + 1;obj.kbuffer {obj.kupdate} = obj.k;end%查找并通过探索返回行动action = getActionWithExploration(obj,exp{4});end

可选功能

Optionally, you can define how your agent is reset at the start of training by specifying aresetimpl.使用以下功能签名,其中objis the agent object. Using this function, you can set the agent into a known or random condition before training.

功能resetImpl (ob)

一种lso, you can define any other helper functions in your custom agent class as required. For example, the custom LQR agent defines a疯狂的功能for creating the critic representation and agetNewK功能that derives the feedback gain matrix from the trained critic parameters.

Create Custom Agent

一种fter you define your custom agent class, create an instance of it in the MATLAB workspace. For example, to create the custom LQR agent, define the问:R.,和itsialk.values and call the constructor function.

Q = [10,3,1; 3,5,4; 1,4,9];r = 0.5 *眼睛(3);k0 =地方(a,b,[0.4,0.8,0.5]);代理= lqrcustomagent(q,r,k0);

在验证环境对象后,您可以使用它来培训钢筋学习代理。有关列车定制LQR代理的示例,请参阅Train Custom LQR Agent

See Also

相关话题