Introduction

Traditionally control design is based on a mathematical model of the system.
Using Reinforcement Learning, controllers can be designed without a mathematical model. This is done by interacting with a simulation model or maybe even the physical system.
In this approach, inputs are applied to the system, evaluated, and the control or policy updated to minimize a quality function Q.

Model

The model of the system (or environment) is unknown but is assumed to be discrete-time linear time-invariant (LTI). ​​
x(k+1)=a.x(k)+b.u(k)
(1)​Here
x∈
n

is the state vector and
u∈
m

is the input vector.
Define the stage cost to be quadratic
s
k
=x(k).q.x(k)+u(k).r.u(k)
.
Define the performance index as the infinite-horizon sum
J
k
=
∞
∑
i=k
s
k
​​
J
k
=
∞
∑
i=k
x(k).q.x(k)+u(k).r.u(k)
(2)
A discrete-time LTI is a Markov Decision Processes (MDP) where the set of states is
X=
n

, the set of actions is
U=
m

, the transition probabilities are 1, and the cost function is
s
k
.

Value of a policy

Let’s assume a stationary optimal policy
u(k)=-g.x(k)
. This is the constant state feedback optimal control law.
Then from (1)
x(k+1)=(a-b.g).x(k),​​x(k+2)=(a-b.g).x(k+1)=
2
(a-b.g)
.x(k),​​⋮
​
x(k+n)=
n
(a-b.g)
.x(k)
(3)
The value of a policy is the cost
J
k
accrued by following a policy. Denote by
V
g
(x(k))
the value of the policy
u(k)=-g.x(k)
. It is also denoted by
g
J
k
.
From (2)​
V
g
(x(k))=
∞
∑
i=k
x(i).q.x(i)+x(i)g.r.g.x(i)
(4)​​
V
g
(x(k))=
∞
∑
i=k
x(i).(q+g.r.g).x(i)
​​
V
g
(x(k))=x(k).(q+g.r.g).x(k)+x(k+1).(q+g.r.g).x(k+1)+…
​Using (3)​
V
g
(x(k))=x(k).(q+g.r.g).x(k)+x(k)(a-b.g).(q+g.r.g).(a-b.g).x(k)+…
​​
V
g
(x(k))=x(k).I+(a-b.g)+
2
(a-b.g)
+….(q+g.r.g).I+(a-b.g)+
2
(a-b.g)
+….x(k)
​​​
V
g
(x(k))=x(k).
P
g
.x(k)
(5)
The value
V
g
is quadratic and positive definite. From any initial value the policy
u(k)=-g.x(k)
drives the system to the origin along the
V
g
surface.
It can be shown that the solution
P
g
of the discrete-time algebraic Riccati equation (DARE) for the linear quadratic regulation (LQR) problem to minimize
∞
∑
i=0
x(k).q.x(k)+u(k).r.u(k)
for
x(k+1)=a.x(k)+b.u(k)
corresponds to the optimal policy
u(k)=-g.x(k)

The Q function of a policy

Define the quality function Q to be the cost to start in state
x(k)
and use an arbitrary input signal
u(k)
at instant k, and
u(i)=-g.x(i),i>k
for all subsequent instants.
Q(x(k),u(k))=x(k).q.x(k)+u(k).r.u(k)+
∞
∑
i=k+1
x(i).q.x(i)+x(i)g.r.g.x(i)
​Note that
V
ℊ
(x(k))=Q(x(k),-ℊ.x(k))
. (6)​Using (4)​
Q(x(k),u(k))=x(k).q.x(k)+u(k).r.u(k)+
V
g
(x(k+1))
(7)​​​Using(5)​
Q(x(k),u(k))=x(k).q.x(k)+u(k).r.u(k)+x(k+1).
P
g
.x(k+1)
​Using (1)​
Q(x(k),u(k))=x(k).q.x(k)+u(k).r.u(k)+(x(k)a+u(k).b).
P
g
.(a.x(k)+b.u(k))
​​
Q(x(k),u(k))=

x(k)
u(k)
.
q+a.
P
g
.a
a.
P
g
.b
b.
P
g
.a
r+b.
P
g
.b
.
x(k)
u(k)
It can be shown that computing the input
u(k)
that minimizes
Q(x(k),u(k))
using
∂Q(x(k),u(k))
∂u(k)
==0
gives the solution of the LQR problem
u(k)=
-1
(r+b.
P
g
.b)
.b.
P
g
.a.x(k)
However it cannot be implemented the above way because the system’s dynamics is unknown.
Let’s assume that
Q(x(k),u(k))=

x(k)
u(k)
.
s
xx
s
xu
s
ux
s
uu
.
x(k)
u(k)
and set
∂Q(x(k),u(k))
∂u(k)
==0
. ​This gives ​
s
ux
.x(k)+
s
uu
.u(k)==0
​​and​
u(k)==-
-1
s
uu
.
s
ux
.x(k)
Thus we can compute the gain of the policy at instant
k
as
-1
s
uu
.
s
ux
, provided we know the matrix
s
.

Q learning

Or how to compute the Q function and hence the optimal policy.
From (7)​
Q(x(k),u(k))=x(k).q.x(k)+u(k).r.u(k)+
V
g
(x(k+1))
​​
Q(x(k),u(k))-
V
g
(x(k+1))=x(k).q.x(k)+u(k).r.u(k)
​From (6)​
Q(x(k),u(k))-Q(x(k+1),-g.x(k+1))=x(k).q.x(k)+u(k).r.u(k)
Since s is a symmetric matrix it has
d=1+2+…+(n+m)
unique entries
s
1
,…,
s
d
and
Q(x(k),u(k))
can be expanded out linearly as ​
Q(x(k),u(k))=

x(k)
u(k)
.
s
xx
s
xu
s
ux
s
uu
.
x(k)
u(k)
​​
Q(x(k),u(k))=ϕ(x(k),u(k)).(
s
1
…
s
d
)
Thus
​
(ϕ(x(k),u(k))-ϕ(x(k+1),-g.x(k+1))).(
s
1
…
s
d
)=x(k).q.x(k)+u(k).r.u(k)
All this has been implemented as a resource function QLearningLQRegulator.

An example

Consider the following system.
Let’s assume that the system starts at 2.
Let’s define the the state and input weights to be unity, set the initial gain to 0, and compute the controller using 25 simulations.
We can get a slew of property values from the data object .
This is the computed gain.
It agrees with the result from LQRegulatorGains which uses the model to compute the gain.
Plotting the gain values, we see how it converged to the final value from the initial gain 0.
The plot above shows that gain was updated after every 5 simulations. This can be also be obtained as a property.
We can also see the inputs that were used to excite the system. For every 5 simulations the input gain remained the same, but the input values were different.
We can also plot the state values which shows the system being regulated to 0 from the initial value 2.

Summary

Starting from the very basics I have shown the derivation of the equations to compute an optimal controller for a system without full knowledge of its underlying dynamics. The controller is computed using a trial-and error approach and it converges to the optimal solution while simultaneously regulating the system to the origin. The approach seems to work well when the underlying model is that of a linear discrete-time system, provided the process is started with a stabilizing controller. This can be extended to handle continuous and nonlinear systems and also to solve tracking problems.

References

◼
  • F. L. Lewis, D. Vrabie and K. G. Vamvoudakis, “Reinforcement Learning and Feedback Control: Using Natural Decision Methods to Design Optimal Adaptive Controllers,” in IEEE Control Systems Magazine, vol. 32, no. 6, pp. 76-105, Dec. 2012, doi: 10.1109/MCS.2012.2214134.
  • CITE THIS NOTEBOOK

    Reinforcement learning applied feedback control​
    by Suba Thomas​
    Wolfram Community, STAFF PICKS, January 15, 2025
    ​https://community.wolfram.com/groups/-/m/t/3356797