-
Notifications
You must be signed in to change notification settings - Fork 438
Improved speed of ctrb and obsv functions #941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…xponentiation following the MATLAB implimentation of obsv
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution!
A couple of comments:
- Have you benchmarked this to confirm it's actually faster? Also, I am not aware of a use case where the obsv or ctrb matrix must be repeatedly evaluated - is there one?
- can you add a unit test for obsv and ctrb for the t < n case?
- can you define n in the docstrings?
Co-authored-by: Sawyer Fuller <58706249+sawyerbfuller@users.noreply.github.com>
Co-authored-by: Sawyer Fuller <58706249+sawyerbfuller@users.noreply.github.com>
Dear Prof. Fuller and others, Thank you for the prompt reply. Concerning: “Have you benchmarked this to confirm it's actually faster? Also, I am not aware of a use case where the obsv or ctrb matrix must be repeatedly evaluated - is there one?” I have benchmarked the speed of these two methods in this Google Colab notebook which is available here: https://colab.research.google.com/drive/1nDQgmJuQE-Q6H2OZfi4QUstJB44JikHN?usp=sharing. This appears to create a improvement in speed (see the plot below). The notebook creates two functions I agree that there are not many use cases where we need to repeatedly create the controllability or observability matrices, but I have lately been working on a problem of biological sensor selection where I find it beneficial to create many observability matrices. In particular, I am applying several sensor selection algorithms to design my output matrix Regarding: “can you add a unit test for obsv and ctrb for the t < n case?” I have added the test cases Concerning: “can you define n in the docstrings?” I have removed The addition of this parameter may seem unnecessary, but it is relevant to a method I am currently using. Since it doesn't break anything to add Please let me know what else! Thank you very much. Best, |
This is actually not only for performance but for accuracy reasons, the right way to go. Hence it is an improvement anyways. It is basically a Krylov space argument that leads to repeated powers of However, this test is fundamentally broken for production code; i) it is only a theoretical tool since the powers blow up regardless ii) rank decision is a very tricky thing to nail it down without false positives and false negatives iii) as if it is not enough, the answer is a binary yes or no. It does not give any indication of how close we are dancing to uncontrollability/unobservability. A typical and best bang for buck candidate is D. Boley's test if you are really looking for a better test which computes the cancellation distance in our context (but originally checks for the rank deficiency metric) Then you can put a limit on the distance and you can tie it to the matrix norms and so on. In my opinion a much better candidate instead of a lower semi-continuous function. I have the regular version here in case you want to check it out. The article is in the docstring. I hope academia emphasizes a bit more that the Kalman test is just a theoretical tool and we use it just on the homework problems. |
Dear Ilhan, Thank you. I agree there are several issues with utilizing the Kalman test for determining controllability or observability, and thank you for sharing the test by Boley. My particular motivation for this change is to use Gramians to determine observability. This is advantageous to me because in addition to providing a Yes/No test, the Gramian provides information on the principal directions of controllability/observability. Since discrete, finite horizon Gramians can be computed in terms of While there are these limitations that you have noted, it seems to me that we are in agreement this is a beneficial argument to add for the Thank you. Best, |
Dear all, It appears that this is failing with the below error related to Thank you. Best,
|
It's failing in parts of the code unaffected by your change, so don't worry about in this PR. |
I tried re-running the failing test once or twice but it kept failing. I agree that it has nothing to do with this PR, so I'll merge it. |
Current implementation of ctrb and obsv functions utilize
np.linalg.matrix_power
inside of afor i in range(1,n)
loop. I modified these functions to reduce the number of matrix multiplications byamat
essentially by utilizing the precomputed matrix exponentiation from thei-1
the iteration of the loop when computing thei
th iteration. This is similar to the MATLAB implementation of the similar functions.Additionally, I added an optional parameter
t
to both functions that defaults toNone
for the case where the reduced observability and controllability matrices are needed. By default, the full observability and controllability matrices are created.This appears to pass all test cases.