|
1 |
| -function ADNLProblem(ocp::OptimalControlModel, N::Integer) |
| 1 | +function ADNLProblem(ocp::OptimalControlModel, N::Integer, init=nothing) |
2 | 2 |
|
3 | 3 | # direct_infos
|
4 | 4 | t0, tf_, n_x, m, f, ξ, ψ, ϕ, dim_ξ, dim_ψ, dim_ϕ,
|
@@ -94,7 +94,7 @@ function ADNLProblem(ocp::OptimalControlModel, N::Integer)
|
94 | 94 | end
|
95 | 95 |
|
96 | 96 | # bounds for the constraints
|
97 |
| - function ipopt_l_u_b() |
| 97 | + function constraints_bounds() |
98 | 98 | lb = zeros(nc)
|
99 | 99 | ub = zeros(nc)
|
100 | 100 | index = 1 # counter for the constraints
|
@@ -134,17 +134,75 @@ function ADNLProblem(ocp::OptimalControlModel, N::Integer)
|
134 | 134 | return lb, ub
|
135 | 135 | end
|
136 | 136 |
|
137 |
| - # todo: init a changer |
138 |
| - xu0 = 1.1*ones(dim_xu) |
| 137 | + # todo: retrieve optional bounds from ocp parsed constraints |
| 138 | + function variables_bounds() |
| 139 | + # unbounded case |
| 140 | + l_var = -Inf*ones(dim_xu) |
| 141 | + u_var = Inf*ones(dim_xu) |
| 142 | + return l_var, u_var |
| 143 | + end |
| 144 | + |
| 145 | + # generate initial guess |
| 146 | + function set_state_at_time_step!(x, i, dim_x, N, xu) |
| 147 | + if i > N |
| 148 | + error("trying to set x(t_i) for i > N") |
| 149 | + else |
| 150 | + xu[1+i*dim_x:(i+1)*dim_x] = x[1:dim_x] |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + function set_control_at_time_step!(u, i, dim_x, N, m, xu) |
| 155 | + if i > N |
| 156 | + error("trying to set (t_i) for i > N") |
| 157 | + else |
| 158 | + xu[1+(N+1)*dim_x+i*m:m+(N+1)*dim_x+i*m] = u[1:m] |
| 159 | + end |
| 160 | + end |
| 161 | + |
| 162 | + function initial_guess() |
| 163 | + #println("Initialization: ", init) |
| 164 | + |
| 165 | + if init === nothing |
| 166 | + # default initialization |
| 167 | + xu0 = 1.1*ones(dim_xu) |
| 168 | + else |
| 169 | + if length(init) != (n_x + m) |
| 170 | + error("vector for initialization should be of size n+m",n_x+m) |
| 171 | + end |
| 172 | + # split state / control |
| 173 | + x_init = zeros(dim_x) |
| 174 | + x_init[1:n_x] = init[1:n_x] |
| 175 | + u_init = zeros(m) |
| 176 | + u_init[1:m] = init[n_x+1:n_x+m] |
| 177 | + |
| 178 | + # mayer -> lagrange additional state |
| 179 | + if hasLagrangeCost |
| 180 | + x_init[dim_x] = 0.1 |
| 181 | + end |
| 182 | + |
| 183 | + # constant initialization |
| 184 | + xu0 = zeros(dim_xu) |
| 185 | + for i in 0:N |
| 186 | + set_state_at_time_step!(x_init, i, dim_x, N, xu0) |
| 187 | + set_control_at_time_step!(u_init, i, dim_x, N, m, xu0) |
| 188 | + end |
| 189 | + end |
| 190 | + return xu0 |
| 191 | + end |
| 192 | + |
| 193 | + # variables bounds |
| 194 | + l_var, u_var = variables_bounds() |
| 195 | + |
| 196 | + # initial guess |
| 197 | + xu0 = initial_guess() |
139 | 198 |
|
140 |
| - l_var = -Inf*ones(dim_xu) |
141 |
| - u_var = Inf*ones(dim_xu) |
| 199 | + # free final time case |
142 | 200 | if has_free_final_time
|
143 | 201 | xu0[end] = 1.0
|
144 | 202 | l_var[end] = 1.e-3
|
145 | 203 | end
|
146 | 204 |
|
147 |
| - lb, ub = ipopt_l_u_b() |
| 205 | + lb, ub = constraints_bounds() |
148 | 206 |
|
149 | 207 | nlp = ADNLPModel(ipopt_objective, xu0, l_var, u_var, ipopt_constraint, lb, ub)
|
150 | 208 |
|
|
0 commit comments