-
-
Couldn't load subscription status.
- Fork 233
add input-affine equation decomposition #3912
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
Open
baggepinnen
wants to merge
6
commits into
master
Choose a base branch
from
add-input-affine-form
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+189
−0
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6b60b1a
add input-affine equation factorization
baggepinnen bb55cb5
format
baggepinnen 457bf96
use `fast_substitute`
baggepinnen f7d06d5
use linear_expansion
baggepinnen 656aec2
operate on equations
baggepinnen 9cbca5f
error on alg equations
baggepinnen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| """ | ||
| input_affine_form(eqs, inputs) | ||
|
|
||
| Extract control-affine (input-affine) form from symbolic equations. | ||
|
|
||
| Given a system of equations of the form `ẋ = F(x, u)` where `x` is the state | ||
| and `u` are the inputs, this function extracts the control-affine representation: | ||
| `ẋ = f(x) + g(x)*u` | ||
|
|
||
| where: | ||
| - `f(x)` is the drift term (dynamics without inputs) | ||
| - `g(x)` is the input matrix | ||
|
|
||
| # Arguments | ||
| - `eqs`: Vector of symbolic equations representing the system dynamics | ||
| - `inputs`: Vector of input/control variables | ||
|
|
||
| # Returns | ||
| - `f`: The drift vector f(x) | ||
| - `g`: The input matrix g(x) | ||
|
|
||
| # Example | ||
| ```julia | ||
| using ModelingToolkit, Symbolics | ||
|
|
||
| @variables x1 x2 u1 u2 | ||
| state = [x1, x2] | ||
| inputs = [u1, u2] | ||
|
|
||
| # Define system equations: ẋ = F(x, u) | ||
| eqs = [ | ||
| -x1 + 2*x2 + u1, | ||
| x1*x2 - x2 + u1 + 2*u2 | ||
| ] | ||
|
|
||
| # Extract control-affine form | ||
| f, g = input_affine_form(eqs, inputs) | ||
| ``` | ||
|
|
||
| # Notes | ||
| The function assumes that the equations are affine in the inputs. If the equations | ||
| are nonlinear in the inputs, the result may not be meaningful. | ||
| """ | ||
| function input_affine_form(eqs, inputs) | ||
| # Extract the input matrix g(x) by taking coefficients of each input | ||
| g = [Symbolics.coeff(Symbolics.simplify(eq, expand = true), u) | ||
| for eq in eqs, u in inputs] | ||
| g = Symbolics.simplify.(g, expand = true) | ||
|
|
||
| # Extract the drift term f(x) by substituting inputs = 0 | ||
| f = Symbolics.substitute.(eqs, Ref(Dict(inputs .=> 0))) | ||
baggepinnen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| f = Symbolics.simplify.(f, expand = true) | ||
|
|
||
| return f, g | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| using ModelingToolkit, Test | ||
| using Symbolics | ||
| using StaticArrays | ||
|
|
||
| @testset "input_affine_form" begin | ||
| # Test with simple linear system | ||
| @testset "Simple linear system" begin | ||
| @variables x1 x2 u1 u2 | ||
| state = [x1, x2] | ||
| inputs = [u1, u2] | ||
|
|
||
| eqs = [ | ||
| -x1 + 2*x2 + u1, | ||
| x1*x2 - x2 + u1 + 2*u2 | ||
| ] | ||
|
|
||
| f, g = input_affine_form(eqs, inputs) | ||
|
|
||
| # Verify reconstruction | ||
| eqs_reconstructed = f + g * inputs | ||
| @test isequal(Symbolics.simplify.(eqs_reconstructed), Symbolics.simplify.(eqs)) | ||
|
|
||
| # Check dimensions | ||
| @test length(f) == length(eqs) | ||
| @test size(g) == (length(eqs), length(inputs)) | ||
| end | ||
|
|
||
| # Test with Segway dynamics example | ||
| @testset "Segway dynamics" begin | ||
| # Segway parameters | ||
| grav = 9.81 | ||
| R = 0.195 | ||
| M = 2 * 2.485 | ||
| Jc = 2 * 0.0559 | ||
| L = 0.169 | ||
| m = 44.798 | ||
| Jg = 3.836 | ||
| m0 = 52.710 | ||
| J0 = 5.108 | ||
| Km = 2 * 1.262 | ||
| bt = 2 * 1.225 | ||
|
|
||
| # Dynamics of Segway in Euler-Lagrange form | ||
| D(q) = [m0 m*L*cos(q[2]); m*L*cos(q[2]) J0] | ||
| function H(q, q̇) | ||
| return SA[ | ||
| -m * L * sin(q[2]) * q̇[2] + bt * (q̇[1] - R * q̇[2]) / R, | ||
| -m * grav * L * sin(q[2]) - bt * (q̇[1] - R * q̇[2]) | ||
| ] | ||
| end | ||
| B(q) = SA[Km / R, -Km] | ||
|
|
||
| # Convert to control affine form | ||
| function f_seg(x) | ||
| q, q̇ = x[SA[1, 2]], x[SA[3, 4]] | ||
| return [q̇; -D(q) \ H(q, q̇)] | ||
| end | ||
| function g_seg(x) | ||
| q, q̇ = x[SA[1, 2]], x[SA[3, 4]] | ||
| return [SA[0, 0]; D(q) \ B(q)] | ||
| end | ||
|
|
||
| # Trace dynamics symbolically | ||
| @variables q1 q2 qd1 qd2 u | ||
| x = [q1; q2; qd1; qd2] | ||
| inputs = [u] | ||
| eqs = f_seg(x) + g_seg(x) * u | ||
|
|
||
| # Extract control-affine form | ||
| fe, ge = input_affine_form(eqs, inputs) | ||
|
|
||
| # Test reconstruction | ||
| eqs2 = fe + ge * inputs | ||
| diff = Symbolics.simplify.(eqs2 - eqs, expand = true) | ||
|
|
||
| # The difference should be zero or very close to zero symbolically | ||
| # We test numerically since symbolic simplification might not be perfect | ||
| f2, _ = build_function(fe, x, expression = false) | ||
| g2, _ = build_function(ge, x, expression = false) | ||
|
|
||
| for i in 1:10 | ||
| x_val = rand(length(x)) | ||
| @test f2(x_val) ≈ f_seg(x_val) rtol=1e-10 | ||
| @test g2(x_val) ≈ g_seg(x_val) rtol=1e-10 | ||
| end | ||
| end | ||
|
|
||
| # Test with multiple inputs | ||
| @testset "Multiple inputs" begin | ||
| @variables x1 x2 x3 u1 u2 | ||
| state = [x1, x2, x3] | ||
| inputs = [u1, u2] | ||
|
|
||
| eqs = [ | ||
| x2, | ||
| x3, | ||
| -x1 - 2*x2 - x3 + u1 + 3*u2 | ||
| ] | ||
|
|
||
| f, g = input_affine_form(eqs, inputs) | ||
|
|
||
| # Expected results | ||
| f_expected = [x2, x3, -x1 - 2*x2 - x3] | ||
| g_expected = [0 0; 0 0; 1 3] | ||
|
|
||
| @test isequal(Symbolics.simplify.(f), Symbolics.simplify.(f_expected)) | ||
|
|
||
| # Test g matrix elements | ||
| for i in 1:size(g, 1), j in 1:size(g, 2) | ||
|
|
||
| @test isequal(Symbolics.simplify(g[i, j]), g_expected[i, j]) | ||
| end | ||
| end | ||
|
|
||
| # Test with nonlinear state dynamics | ||
| @testset "Nonlinear state dynamics" begin | ||
| @variables x1 x2 u | ||
| state = [x1, x2] | ||
| inputs = [u] | ||
|
|
||
| eqs = [ | ||
| x2, | ||
| -sin(x1) - x2 + u | ||
| ] | ||
|
|
||
| f, g = input_affine_form(eqs, inputs) | ||
|
|
||
| # Expected results | ||
| f_expected = [x2, -sin(x1) - x2] | ||
| g_expected = reshape([0, 1], 2, 1) | ||
|
|
||
| @test isequal(Symbolics.simplify.(f), Symbolics.simplify.(f_expected)) | ||
| @test isequal(g, g_expected) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
simplifyand especiallyexpandare pretty expensive. This should ideally be done withsemilinear_formor repeated application ofSymbolics.linear_expansion.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.
semilinear_formappeared to be buggy and not doing what I wantedhttps://juliahub.slack.com/archives/C03UEC0P6G0/p1756812154880999
linear_expansionhas this issueand it has failed to work for me in similar situations in the past JuliaSymbolics/Symbolics.jl#550
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.
I can look into
semilinear_form. The linkedlinear_expansionissue is simply that the expression was not affine. That isn't the case if this function requires the system to be affine in the inputs.