Implement the solution of Riccati algebraic equations in Python

What is Riccati Algebra Equation (ARE)?

The following formula. But $ A, Q, P \ in \ mathbb R ^ {n \ times n}, B \ in \ mathbb R ^ {n \ times m}, R \ in \ mathbb R ^ {m \ times m} $ , Q, R are definite matrix symmetric matrices, and $ A, B, Q, R $ are given to find $ P $.

A P + P A^T + Q - P B R^{-1} B^T P = 0

Scene field where ARE appears, example

It appears when finding the gain with the LQR of modern control theory. LQR is an optimal control theory, and the system $ \frac{dx}{dt} = Ax+Bu $ When can be expressed as, the control law that satisfies $ \ min J = \ int (x ^ TQx + u ^ TRu) dt $ uses $ P $ that satisfies the Riccati algebra equation above. $ u=-R^{-1}B^TPx $ The story that you can write.

solve

policy

We will implement and solve the Arimoto-Potter method (explained in the next section). At that time, the eigenvalue problem of the matrix will be solved, but this uses numpy or the like.

Arimoto / Potter method

The procedure will be explained. See books for proof.

1. Place a matrix

The matrix $ H \ in \ mathbb R ^ {2n \ times 2n} $ called the Hamilton matrix

H=\left[ \begin{array}{cc} A^T & -B R^{-1} B^T \newline -Q & -A \end{array} \right]

far.

2. Eigenvalue decomposition of Hamilton matrix

There are n eigenvalues of H whose real part is negative. Place this as $ \ lambda_1, \ lambda_2, ..., \ lambda_n $ and the corresponding eigenvectors are $ \ vec w_1, \ vec w_2, ..., \ vec w_n $.

3. Place a matrix that divides the eigenvectors side by side

Use this to get $ Y, Z \ in \ mathbb R ^ {n \ times n} $ $ \left[ \vec w_1 \vec w_2 ... \vec w_n \right] = \left[ \begin{array}{c} Y \newline Z \end{array} \right] $ far.

4. P can be found

P=ZY^{-1}

Implementation

import numpy as np
import numpy.linalg as LA

def solve_are(A, B, Q, R):
    # 1.Put the Hamilton Matrix
    H = np.block([[A.T, -B @ LA.inv(R) @ B.T],
                  [-Q , -A]])
    # 2.Eigenvalue decomposition
    eigenvalue, w = LA.eig(H)
    # 3.Put an auxiliary matrix
    Y_, Z_ = [], []
    n = len(w[0])//2
    for i in range(2*n):
        if eigenvalue[i].real < 0.0:
            Y_.append(w.T[i][:n])
            Z_.append(w.T[i][n:])
    Y = np.array(Y_).T
    Z = np.array(Z_).T
    # 4.P is sought
    return Z @ LA.inv(Y)

test

Test code

A = np.array([[3., 1.],[0., 1.]])
B = np.array([[1.2], [1.]])
Q = np.array([[1., 0.2], [0.2, 1.0]])
R = np.array([[1.]])
P = solve_are(A, B, Q, R)
print("P")
print(P)
print("Left side of Riccati algebraic equation")
print(A@P + [email protected] + Q - P@[email protected](R)@B.T@P)

result

P
[[ 69.20010326 -66.19334596]
 [-66.19334596  67.7487967 ]]
Left side of Riccati algebraic equation
[[-1.13686838e-13  5.11590770e-13]
 [ 2.55795385e-13 -5.68434189e-13]]

I solved it.

Recommended Posts

Implement the solution of Riccati algebraic equations in Python
Find the solution of the nth-order equation in python
Check the behavior of destructor in Python
Implement part of the process in C ++
The result of installing python in Anaconda
The basics of running NoxPlayer in Python
In search of the fastest FizzBuzz in Python
Output the number of CPU cores in Python
[Python] Sort the list of pathlib.Path in natural sort
Get the caller of a function in Python
Match the distribution of each group in Python
View the result of geometry processing in Python
Make a copy of the list in Python
Find the divisor of the value entered in python
The story of reading HSPICE data in Python
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Output in the form of a python array
Implement recommendations in Python
the zen of Python
Implement XENO in python
Implement sum in Python
Implement Traceroute in Python 3
Type in Python to implement algebraic extension (1) ~ Monoids, groups, rings, ring of integers ~
How to get the number of digits in Python
[python] Get the list of classes defined in the module
The story of FileNotFound in Python open () mode ='w'
Learn the design pattern "Chain of Responsibility" in Python
Get the size (number of elements) of UnionFind in Python
Not being aware of the contents of the data in python
Reproduce the execution example of Chapter 4 of Hajipata in Python
Let's use the open data of "Mamebus" in Python
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Heapsort)
[Python] Outputs all combinations of elements in the list
Get the URL of the HTTP redirect destination in Python
A reminder about the implementation of recommendations in Python
Reproduce the execution example of Chapter 5 of Hajipata in Python
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Check the asymptotic nature of the probability distribution in Python
Towards the retirement of Python2
Find the difference in Python
About the ease of Python
Implement naive bayes in Python 3.3
Implement ancient ciphers in python
Implement Redis Mutex in Python
Implement extension field in Python
Implement fast RPC in Python
Implement method chain in Python
Implement Dijkstra's Algorithm in python
Implement Slack chatbot in Python
Implementation of quicksort in Python
About the features of Python
The Power of Pandas: Python
Try scraping the data of COVID-19 in Tokyo with Python
Find out the apparent width of a string in python
I tried the accuracy of three Stirling's approximations in python
Measure the execution result of the program in C ++, Java, Python.
[Memo] The mystery of cumulative assignment statements in Python functions
The result of Java engineers learning machine learning in Python www
Calculate the square root of 2 in millions of digits with python