学习并应用PID控制器的约束GydF4y2Ba
此示例显示了如何从数据中学习约束并将这些约束应用于PID控制应用程序。首先,您使用深神经网络学习约束功能,这需要深度学习Toolbox™软件。然后,您使用约束执行块将约束应用于PID控制操作。GydF4y2Ba
在此示例中,植物动力学由以下方程式[1]描述。GydF4y2Ba
The goal for the plant is to track the following trajectories.
对于将已知约束函数应用于同一PID控制应用程序的示例,请参见GydF4y2Ba对PID控制器执行限制GydF4y2Ba。GydF4y2Ba
Set the random seed and configure model parameters and initial conditions.
rng(0);% Random seedr = 1.5;GydF4y2Ba所需轨迹的%半径GydF4y2BaTS = 0.1;GydF4y2Ba% Sample timeTf = 22;% 期间GydF4y2Bax0_1 = -r;GydF4y2BaX1初始条件的%GydF4y2BaX0_2 = 0;X2初始条件的%GydF4y2BamaxSteps = Tf/Ts;%模拟步骤GydF4y2Ba
Design PID Controllers
Before learning and applying constraints, design PID controllers for tracking the reference trajectories. ThetrackingWithPIDs
model contains two PID controllers with gains tuned using thePID Tuner应用程序。有关Simulink模型中调整PID控制器的更多信息,请参见金宝appGydF4y2BaIntroduction to Model-Based PID Tuning in Simulink。GydF4y2Ba
mdl =GydF4y2Ba'trackingWithPIDs'; open_system(mdl)
Simulate the PID controllers and plot their tracking performance.
%模拟模型。GydF4y2Baout = sim(mdl);GydF4y2Ba% Extract trajectories.logdata = out.logsout;x1_traj = logdata {3} .values.data;x2_traj = logdata {4} .values.data;x1_des = logdata {1} .values.data;x2_des = logdata {2} .values.data;GydF4y2Ba%情节轨迹。GydF4y2BaFigure('姓名'GydF4y2Ba,,,,GydF4y2Ba'Tracking')xlim([ - 2,2])ylim([ - 2,2])图(x1_des,x2_des,GydF4y2Ba'r'GydF4y2Ba)Xlabel('x1')ylabel('x2')holdon绘图(x1_traj,x2_traj,GydF4y2Ba'b:',,,,GydF4y2Ba'行宽'GydF4y2Ba,2)保持GydF4y2Baon情节(x1_traj(1),x2_traj(1),'G*'GydF4y2Ba)holdon情节(x1_traj(end),x2_traj(end),'去'GydF4y2Ba) 传奇(GydF4y2Ba'Desired',,,,GydF4y2Ba'Trajectory',,,,GydF4y2Ba'开始'GydF4y2Ba,,,,GydF4y2Ba'End')GydF4y2Ba
约束功能GydF4y2Ba
In this example, you learn application constraints and modify the control actions of the PID controllers to satisfy these constraints.
The feasible region for the plant is given by the constraints 。Therefore, the trajectories must satisfy 。GydF4y2Ba
You can approximate the plant dynamics by the following equation.
将约束应用于此方程会产生以下约束函数。GydF4y2Ba
约束执行块接受表格的约束GydF4y2Ba 。For this application, the coefficients of this constraint function are as follows.
Learn Constraint Function
在此示例中,约束函数的系数未知。因此,您必须从培训数据中得出它们。要收集培训数据,请使用GydF4y2BaRLCOLLECTDAPIDGydF4y2Ba
模型。This model allows you to pass either zero or random inputs to the plant model and record the resulting plant outputs.
mdl =GydF4y2Ba'rlcollectDatapid'GydF4y2Ba; open_system(mdl)
学习未知矩阵GydF4y2Ba ,将植物输入设置为零。当您这样做时,植物动力就会变成GydF4y2Ba 。GydF4y2Ba
mdl =GydF4y2Ba'rlcollectDatapid'GydF4y2Ba; open_system(mdl)
使用GydF4y2BaCollectDataPID
助手功能。此功能多次模拟模型并提取输入/输出数据。该功能还将训练数据格式化为具有六列的数组:GydF4y2Ba
,,,,GydF4y2Ba
,,,,GydF4y2Ba
,,,,GydF4y2Ba
,,,,GydF4y2Ba
,,,,and
。GydF4y2Ba
numsamples = 1000;data = collectDatapid(MDL,numsamples);GydF4y2Ba
找到最小二乘解决方案GydF4y2Ba 你罪Gthe input/output data.
inputData = data(:,1:2);outputData = data(:,5:6);a = inputdata \ outputdata;GydF4y2Ba
收集培训数据。学习未知功能GydF4y2Ba ,配置模型以使用遵循正态分布的随机输入值。GydF4y2Ba
% Configure model to use random input data.blk = [mdl'/Manual Switch'];set_param(blk,GydF4y2Ba'SW'GydF4y2Ba,,,,GydF4y2Ba'0'GydF4y2Ba);GydF4y2Ba% Collect data.data = collectDatapid(MDL,numsamples);GydF4y2Ba
训练深层神经网络以近似GydF4y2Ba
F你nction using the火车构成GydF4y2Ba
助手功能。This function formats the data for training then creates and trains a deep neural network. Training a deep neural network requires Deep Learning Toolbox software.
深神经网络的输入是植物状态。通过提取收集的状态信息来创建输入培训数据。GydF4y2Ba
inputData = data(:,1:2);GydF4y2Ba
由于深神经网络的输出对应于GydF4y2Ba ,,,,you must derive output training data using the collected input/output data and the computed 矩阵。GydF4y2Ba
你=data(:,3:4); x_next = data(:,5:6); fx = (A*inputData')'; outputData = (x_next - fx)./u;
对于此示例,为了确保可重复性,请加载验证的网络。自己训练网络,设置GydF4y2BatrainConstraint
至GydF4y2Batrue
。GydF4y2Ba
trainconstraint = false;GydF4y2BaiftrainConstraint network = trainConstraintPID(inputData,outputData);else加载GydF4y2BatrainedNetworkPIDnetwork结尾GydF4y2Ba
The following figure shows an example of the training progress.
Simulate PID Controllers with Constraint Enforcement
要使用约束执行模拟PID控制器,请使用GydF4y2BatrackingWithLearnedConstraintPID
模型。该模型在将其应用于工厂之前会限制控制器的输出。GydF4y2Ba
mdl =GydF4y2Ba'trackingWithLearnedConstraintPID'; open_system(mdl)
要查看约束实现,请打开约束子系统。在这里,受过训练的深神经网络近似GydF4y2Ba based on the current plant state, and the Constraint Enforcement block enforces the constraint function.
模拟模型并绘制结果。GydF4y2Ba
%模拟模型。GydF4y2Baout = sim(mdl);GydF4y2Ba% Extract trajectories.logdata = out.logsout;X1_traj = zeros(size(out.tout)); x2_traj = zeros(size(out.tout));为了GydF4y2Bact = 1:size(out.tout,1)x1_traj(ct)= logdata {4} .values.data(::,:,:,ct);x2_traj(ct)= logdata {5} .values.data(:,:,:,ct);GydF4y2Ba结尾GydF4y2Bax1_des = logdata {2} .values.data;x2_des = logdata {3} .values.data;GydF4y2Ba%情节轨迹。GydF4y2BaFigure('姓名'GydF4y2Ba,,,,GydF4y2Ba'Tracking with Constraint');绘图(x1_des,x2_des,GydF4y2Ba'r'GydF4y2Ba)Xlabel('x1')ylabel('x2')holdon绘图(x1_traj,x2_traj,GydF4y2Ba'b:',,,,GydF4y2Ba'行宽'GydF4y2Ba,2)保持GydF4y2Baon情节(x1_traj(1),x2_traj(1),'G*'GydF4y2Ba)holdon情节(x1_traj(end),x2_traj(end),'去'GydF4y2Ba) 传奇(GydF4y2Ba'Desired',,,,GydF4y2Ba'Trajectory',,,,GydF4y2Ba'开始'GydF4y2Ba,,,,GydF4y2Ba'End',,,,GydF4y2Ba'Location',,,,GydF4y2Ba'最好的'GydF4y2Ba)GydF4y2Ba
The Constraint Enforcement block successfully constrains the control actions such that the plant states remain less than one.
bdclose(GydF4y2Ba'rlcollectDatapid'GydF4y2Ba)bdclose(GydF4y2Ba'trackingWithLearnedConstraintPID')GydF4y2Ba
References
[[1这是给予的Robey, Alexander, Haimin Hu, Lars Lindemann, Hanwen Zhang, Dimos V. Dimarogonas, Stephen Tu, and Nikolai Matni. "Learning Control Barrier Functions from Expert Demonstrations." Preprint, submitted April 7, 2020.https://arxiv.org/abs/2004.03315GydF4y2Ba