-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontrol.jl
More file actions
182 lines (140 loc) · 4.13 KB
/
control.jl
File metadata and controls
182 lines (140 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"""
$(TYPEDSIGNATURES)
Define the control input for a given optimal control problem model.
This function sets the control dimension and optionally allows specifying the control name and the names of its components.
!!! note
This function should be called only once per model. Calling it again will raise an error.
# Arguments
- `ocp::PreModel`: The model to which the control will be added.
- `m::Dimension`: The control input dimension (must be greater than 0).
- `name::Union{String,Symbol}` (optional): The name of the control variable (default: `"u"`).
- `components_names::Vector{<:Union{String,Symbol}}` (optional): Names of the control components (default: automatically generated).
# Examples
```julia-repl
julia> control!(ocp, 1)
julia> control_dimension(ocp)
1
julia> control_components(ocp)
["u"]
julia> control!(ocp, 1, "v")
julia> control_components(ocp)
["v"]
julia> control!(ocp, 2)
julia> control_components(ocp)
["u₁", "u₂"]
julia> control!(ocp, 2, :v)
julia> control_components(ocp)
["v₁", "v₂"]
julia> control!(ocp, 2, "v", ["a", "b"])
julia> control_components(ocp)
["a", "b"]
```
"""
function control!(
ocp::PreModel,
m::Dimension,
name::T1=__control_name(),
components_names::Vector{T2}=__control_components(m, string(name)),
)::Nothing where {T1<:Union{String,Symbol},T2<:Union{String,Symbol}}
# checks using @ensure
@ensure !__is_control_set(ocp) CTBase.UnauthorizedCall(
"the control has already been set."
)
@ensure m > 0 CTBase.IncorrectArgument("the control dimension must be greater than 0")
@ensure size(components_names, 1) == m CTBase.IncorrectArgument(
"the number of control names must be equal to the control dimension"
)
# set the control
ocp.control = ControlModel(string(name), string.(components_names))
return nothing
end
# ------------------------------------------------------------------------------ #
# GETTERS
# ------------------------------------------------------------------------------ #
"""
$(TYPEDSIGNATURES)
Get the name of the control variable.
# Arguments
- `model::ControlModel`: The control model.
# Returns
- `String`: The name of the control.
# Example
```julia-repl
julia> name(controlmodel)
"u"
```
"""
function name(model::ControlModel)::String
return model.name
end
"""
$(TYPEDSIGNATURES)
Get the name of the control variable from the solution.
# Arguments
- `model::ControlModelSolution`: The control model solution.
# Returns
- `String`: The name of the control.
"""
function name(model::ControlModelSolution)::String
return model.name
end
"""
$(TYPEDSIGNATURES)
Get the names of the control components.
# Arguments
- `model::ControlModel`: The control model.
# Returns
- `Vector{String}`: A list of control component names.
# Example
```julia-repl
julia> components(controlmodel)
["u₁", "u₂"]
```
"""
function components(model::ControlModel)::Vector{String}
return model.components
end
"""
$(TYPEDSIGNATURES)
Get the names of the control components from the solution.
# Arguments
- `model::ControlModelSolution`: The control model solution.
# Returns
- `Vector{String}`: A list of control component names.
"""
function components(model::ControlModelSolution)::Vector{String}
return model.components
end
"""
$(TYPEDSIGNATURES)
Get the control input dimension.
# Arguments
- `model::ControlModel`: The control model.
# Returns
- `Dimension`: The number of control components.
"""
function dimension(model::ControlModel)::Dimension
return length(components(model))
end
"""
$(TYPEDSIGNATURES)
Get the control input dimension from the solution.
# Arguments
- `model::ControlModelSolution`: The control model solution.
# Returns
- `Dimension`: The number of control components.
"""
function dimension(model::ControlModelSolution)::Dimension
return length(components(model))
end
"""
$(TYPEDSIGNATURES)
Get the control function associated with the solution.
# Arguments
- `model::ControlModelSolution{TS}`: The control model solution.
# Returns
- `TS`: A function giving the control value at a given time or state.
"""
function value(model::ControlModelSolution{TS})::TS where {TS<:Function}
return model.value
end