Skip to content

Commit

Permalink
modified: CHANGELOG.md
Browse files Browse the repository at this point in the history
	modified:   src/main/main_systems.jl
	modified:   src/sub/design_systems.jl
	modified:   test/systems_test.jl
  • Loading branch information
Pierre BLAUD committed Mar 9, 2023
1 parent 3d604ab commit e1c50f5
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## v0.1.4

* Modification of proceed_system for linear and non-linear model.
* Modification of proceed_system (linear and non-linear model).
* Add system without constraints (linear and non-linear model).
* Improve tests.

## v0.1.3
Expand Down
36 changes: 32 additions & 4 deletions src/main/main_systems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ function proceed_system(A, B, nbr_state, nbr_input, variation; kws_...)

elseif haskey(kws, :input_constraint) == false &&
haskey(kws, :state_constraint) == false
# Issues
@error "There are no input nor state constraints, at least input constaint is mandatory"
return system = _controller_system_design(model)
else
# Issues
@error "There are no input nor state constraints, at least input constaint is mandatory"
Expand Down Expand Up @@ -89,8 +88,7 @@ function proceed_system(f, nbr_state, nbr_input, variation; kws_...)

elseif haskey(kws, :input_constraint) == false &&
haskey(kws, :state_constraint) == false
# Issues
@error "There are no input nor state constraints, at least input constraint is mandatory"
return system = _controller_system_design(model)
else
# Issues
@error "There are no input nor state constraints, at least input constraint is mandatory"
Expand All @@ -107,6 +105,36 @@ The function uses ForwardDiff package and the jacobian function.
* `state`: references state point.
* `input`: references input point.
"""
function proceed_system_linearization(
system::MathematicalSystems.BlackBoxControlContinuousSystem,
state::Vector{Float64},
input::Vector{Float64},
)

#Linearization to get A and B at references values (state and input)
VectorXU = vcat(state, input)
JacobianMatrix = ForwardDiff.jacobian(system.f, VectorXU)
A_sys = JacobianMatrix[1:system.statedim, 1:system.statedim]
B_sys = JacobianMatrix[1:system.statedim, system.statedim+1:end]

return MathematicalSystems.@system x' = A_sys * x + B_sys * u
end

function proceed_system_linearization(
system::MathematicalSystems.BlackBoxControlDiscreteSystem,
state::Vector{Float64},
input::Vector{Float64},
)

#Linearization to get A and B at references values (state and input)
VectorXU = vcat(state, input)
JacobianMatrix = ForwardDiff.jacobian(system.f, VectorXU)
A_sys = JacobianMatrix[1:system.statedim, 1:system.statedim]
B_sys = JacobianMatrix[1:system.statedim, system.statedim+1:end]

return MathematicalSystems.@system x' = A_sys * x + B_sys * u
end

function proceed_system_linearization(
system::MathematicalSystems.ConstrainedBlackBoxControlContinuousSystem,
state::Vector{Float64},
Expand Down
48 changes: 48 additions & 0 deletions src/sub/design_systems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
# You can obtain one at https://mozilla.org/MPL/2.0/. #
########################################################

"""
_controller_system_design
A function for design the system (model and constraints) with MathematicalSystems from non linear model.
"""
function _controller_system_design(model::DiscreteNonLinearModel)

# Set the system
return MathematicalSystems.BlackBoxControlDiscreteSystem(
model.f,
model.nbr_state,
model.nbr_input,
)
end

"""
_controller_system_design
A function for design the system (model and constraints) with MathematicalSystems from non linear model.
Expand Down Expand Up @@ -72,6 +86,16 @@ function _controller_system_design(
)
end

"""
_controller_system_design
A function for design the system (model and constraints) with MathematicalSystems from linear model.
"""
function _controller_system_design(model::DiscreteLinearModel)

# Set the system
return MathematicalSystems.@system x⁺ = model.A * x + model.B * u
end

"""
_controller_system_design
A function for design the system (model and constraints) with MathematicalSystems from linear model.
Expand Down Expand Up @@ -127,6 +151,20 @@ function _controller_system_design(
#MathematicalSystems.ConstrainedLinearControlDiscreteSystem(model.A, model.B, X, U)
end

"""
_controller_system_design
A function for design the system (model and constraints) with MathematicalSystems from non linear model.
"""
function _controller_system_design(model::ContinuousNonLinearModel)

# Set the system
return MathematicalSystems.BlackBoxControlContinuousSystem(
model.f,
model.nbr_state,
model.nbr_input,
)
end

"""
_controller_system_design
A function for design the system (model and constraints) with MathematicalSystems from non linear model.
Expand Down Expand Up @@ -193,6 +231,16 @@ function _controller_system_design(
)
end

"""
_controller_system_design
A function for design the system (model and constraints) with MathematicalSystems from linear model.
"""
function _controller_system_design(model::ContinuousLinearModel)

# Set the system
return MathematicalSystems.@system x' = model.A * x + model.B * u
end

"""
_controller_system_design
A function for design the system (model and constraints) with MathematicalSystems from linear model.
Expand Down
105 changes: 105 additions & 0 deletions test/systems_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,35 @@ end
@test LazySets.high(sys.U) == [1.0]
end

@testset "Linear user system without constraints" begin

A = [
1 1
0 0.9
]
B = [1; 0.5]

nbr_state = 2
nbr_input = 1

sys = proceed_system(
A,
B,
nbr_state,
nbr_input,
"discrete";
)

@test typeof(sys) == LinearControlDiscreteSystem{
Float64,
Matrix{Float64},
Matrix{Float64},
}

@test sys.A == A
@test sys.B == [1.0; 0.5;;]
end

@testset "Non linear user system with input and state constraints" begin

model_origin = "user"
Expand Down Expand Up @@ -159,6 +188,30 @@ end
@test LazySets.high(sys.U) == [1.0]
end

@testset "Non linear user system without constraints" begin

f = function in(x)
cos(x)
end

nbr_state = 1
nbr_input = 1

sys = proceed_system(
f,
nbr_state,
nbr_input,
"discrete";
)

@test typeof(sys) == BlackBoxControlDiscreteSystem{
typeof(in),
}

@test sys.f == f

end

### Continuous ###

@testset "Linear user system with state and input constraints" begin
Expand Down Expand Up @@ -237,6 +290,35 @@ end
@test LazySets.high(sys.U) == [1.0]
end

@testset "Linear user system continuous without constraints" begin

A = [
1 1
0 0.9
]
B = [1; 0.5]
nbr_state = 2
nbr_input = 1


sys = proceed_system(
A,
B,
nbr_state,
nbr_input,
"continuous";
)

@test typeof(sys) == LinearControlContinuousSystem{
Float64,
Matrix{Float64},
Matrix{Float64},
}

@test sys.A == A
@test sys.B == [1.0; 0.5;;]
end

@testset "Non linear user system with input and state constraints" begin

model_origin = "user"
Expand Down Expand Up @@ -303,6 +385,29 @@ end
@test LazySets.high(sys.U) == [1.0]
end

@testset "Non linear user system without constraints" begin

f = function in(x)
cos(x)
end

nbr_state = 1
nbr_input = 1

sys = proceed_system(
f,
nbr_state,
nbr_input,
"continuous",
)

@test typeof(sys) == BlackBoxControlContinuousSystem{
typeof(in),
}

@test sys.f == f
end

### Model from identification ###
# Not yet necessarely #

Expand Down

0 comments on commit e1c50f5

Please sign in to comment.