Skip to content
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

Handle arbitrary order of set / rhs term specifcation in @system #176

Merged
merged 13 commits into from
Feb 21, 2020
1 change: 1 addition & 0 deletions docs/src/lib/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ _corresponding_type
_capture_dim
extract_dyn_equation_parameters
add_asterisk
sort
```

## Querying expressions
Expand Down
42 changes: 39 additions & 3 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,42 @@ function extract_set_parameter(expr, state, input, noise) # input => to check se
throw(ArgumentError("the set entry $(expr) does not have the correct form `x_ ∈ X_`"))
end

# Sort the parameter `parameters` such that it has the order `order` (e.g. (:X, :U, :W))
# allowing for arbitrary order of parameters which is a array of tuple, where the second
# element of each tuple is the relevant value for the ordering. If a value of `order`
# is not contained in `parameters` it will be ignored.
schillic marked this conversation as resolved.
Show resolved Hide resolved

"""
sort(parameters::Vector, order::Tuple)
mforets marked this conversation as resolved.
Show resolved Hide resolved

Returns a sorted vector `parameters` according to `order`.
ueliwechsler marked this conversation as resolved.
Show resolved Hide resolved
`parameters` is a vector that contains tuples where the second element of each
`Tuple` is considered for the sorting according to `order`.

If a value of `order` is not contained in `parameters` the corresponding entry of
`order` will be omitted.

### Input

- `parameters` -- vector of tuples
- `order` -- tuple of symbols
ueliwechsler marked this conversation as resolved.
Show resolved Hide resolved

### Output

Sorted `parameters` vector according to `order`.
ueliwechsler marked this conversation as resolved.
Show resolved Hide resolved
"""
mforets marked this conversation as resolved.
Show resolved Hide resolved
function sort(parameters::Vector, order::Tuple)
order_parameters = []
for ordered_element in order
for tuple in parameters
if tuple[2] == ordered_element
push!(order_parameters, tuple)
end
end
end
return order_parameters
end

"""
system(expr...)

Expand Down Expand Up @@ -779,9 +815,9 @@ macro system(expr...)
dyn_eq, AT, constr, state, input, noise, dim, x0 = parse_system(expr)
end
lhs, rhs = extract_dyn_equation_parameters(dyn_eq, state, input, noise, dim, AT)
set = extract_set_parameter.(constr, state, input, noise)
# TODO: order set variables such that the order is X, U, W
field_names, var_names = constructor_input(lhs, rhs, set)
ordered_rhs = sort(rhs, (:A, :B, :c, :D, :f, :statedim, :inputdim, :noisedim))
ordered_set = sort(extract_set_parameter.(constr, state, input, noise), (:X, :U, :W))
field_names, var_names = constructor_input(lhs, ordered_rhs, ordered_set)
sys_type = _corresponding_type(AT, field_names)
sys = Expr(:call, :($sys_type), :($(var_names...)))
if x0 == nothing
Expand Down
21 changes: 17 additions & 4 deletions test/@system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,29 @@ f1(x, u, w) = x'*x + u'*u + w'*w


# ===================
# Test to fix in next PR
# Error Handling
# ===================
@testset "@system miscellaneous" begin
@testset "@system for arbitrary order of set specification" begin
sys = @system(x' = Ax + Bu, u∈U, x∈X)
@test sys == ConstrainedLinearControlContinuousSystem(A,B,X,U)
sys = @system(x' = Ax + Bu + Dw, u∈U, w∈W, x∈X)
@test sys == NoisyConstrainedLinearControlContinuousSystem(A,B,D,X,U,W)
end
@testset "@system for arbitrary order of rhs terms" begin
sys = @system(x' = Bu + Ax, u∈U, x∈X)
@test sys == ConstrainedLinearControlContinuousSystem(A,B,X,U)
sys = @system(x' = Dw + Ax + Bu , u∈U, w∈W, x∈X)
@test sys == NoisyConstrainedLinearControlContinuousSystem(A,B,D,X,U,W)
sys = @system(x' = c + Ax + Bu , u∈U, x∈X)
@test sys == ConstrainedAffineControlContinuousSystem(A,B,c,X,U)
end
end

# sys = @system(x' = Ax + Bu, u∈U, x∈X)
# sys == ConstrainedLinearControlContinuousSystem(A,B,X,U)

# ===================
# Continuous systems
# ===================

@testset "@system for continous identity systems" begin
@test @system(x' = 0, dim: 2) == ContinuousIdentitySystem(2)
sys = @system x' = 0 dim: 2
Expand Down