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
|
%
% MIT No Attribution
%
% Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl, KU Leuven.
%
% Permission is hereby granted, free of charge, to any person obtaining a copy of this
% software and associated documentation files (the "Software"), to deal in the Software
% without restriction, including without limitation the rights to use, copy, modify,
% merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
% permit persons to whom the Software is furnished to do so.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
% INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
% PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
% HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
% OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
% SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%
%
% An implementation of direct collocation
% Joel Andersson, 2016
import casadi.*
% Degree of interpolating polynomial
d = 3;
% Get collocation points
tau_root = [0 collocation_points(d, 'legendre')];
% Coefficients of the collocation equation
C = zeros(d+1,d+1);
% Coefficients of the continuity equation
D = zeros(d+1, 1);
% Coefficients of the quadrature function
B = zeros(d+1, 1);
% Construct polynomial basis
for j=1:d+1
% Construct Lagrange polynomials to get the polynomial basis at the collocation point
coeff = 1;
for r=1:d+1
if r ~= j
coeff = conv(coeff, [1, -tau_root(r)]);
coeff = coeff / (tau_root(j)-tau_root(r));
end
end
% Evaluate the polynomial at the final time to get the coefficients of the continuity equation
D(j) = polyval(coeff, 1.0);
% Evaluate the time derivative of the polynomial at all collocation points to get the coefficients of the continuity equation
pder = polyder(coeff);
for r=1:d+1
C(j,r) = polyval(pder, tau_root(r));
end
% Evaluate the integral of the polynomial to get the coefficients of the quadrature function
pint = polyint(coeff);
B(j) = polyval(pint, 1.0);
end
% Time horizon
T = 10;
% Declare model variables
x1 = SX.sym('x1');
x2 = SX.sym('x2');
x = [x1; x2];
u = SX.sym('u');
% Model equations
xdot = [(1-x2^2)*x1 - x2 + u; x1];
% Objective term
L = x1^2 + x2^2 + u^2;
% Continuous time dynamics
f = Function('f', {x, u}, {xdot, L});
% Control discretization
N = 20; % number of control intervals
h = T/N;
% Start with an empty NLP
w={};
w0 = [];
lbw = [];
ubw = [];
J = 0;
g={};
lbg = [];
ubg = [];
% "Lift" initial conditions
Xk = MX.sym('X0', 2);
w = {w{:}, Xk};
lbw = [lbw; 0; 1];
ubw = [ubw; 0; 1];
w0 = [w0; 0; 1];
% Formulate the NLP
for k=0:N-1
% New NLP variable for the control
Uk = MX.sym(['U_' num2str(k)]);
w = {w{:}, Uk};
lbw = [lbw; -1];
ubw = [ubw; 1];
w0 = [w0; 0];
% State at collocation points
Xkj = {};
for j=1:d
Xkj{j} = MX.sym(['X_' num2str(k) '_' num2str(j)], 2);
w = {w{:}, Xkj{j}};
lbw = [lbw; -0.25; -inf];
ubw = [ubw; inf; inf];
w0 = [w0; 0; 0];
end
% Loop over collocation points
Xk_end = D(1)*Xk;
for j=1:d
% Expression for the state derivative at the collocation point
xp = C(1,j+1)*Xk;
for r=1:d
xp = xp + C(r+1,j+1)*Xkj{r};
end
% Append collocation equations
[fj, qj] = f(Xkj{j},Uk);
g = {g{:}, h*fj - xp};
lbg = [lbg; 0; 0];
ubg = [ubg; 0; 0];
% Add contribution to the end state
Xk_end = Xk_end + D(j+1)*Xkj{j};
% Add contribution to quadrature function
J = J + B(j+1)*qj*h;
end
% New NLP variable for state at end of interval
Xk = MX.sym(['X_' num2str(k+1)], 2);
w = {w{:}, Xk};
lbw = [lbw; -0.25; -inf];
ubw = [ubw; inf; inf];
w0 = [w0; 0; 0];
% Add equality constraint
g = {g{:}, Xk_end-Xk};
lbg = [lbg; 0; 0];
ubg = [ubg; 0; 0];
end
% Create an NLP solver
prob = struct('f', J, 'x', vertcat(w{:}), 'g', vertcat(g{:}));
solver = nlpsol('solver', 'ipopt', prob);
% Solve the NLP
sol = solver('x0', w0, 'lbx', lbw, 'ubx', ubw,...
'lbg', lbg, 'ubg', ubg);
w_opt = full(sol.x);
% Plot the solution
x1_opt = w_opt(1:3+2*d:end);
x2_opt = w_opt(2:3+2*d:end);
u_opt = w_opt(3:3+2*d:end);
tgrid = linspace(0, T, N+1);
clf;
hold on
plot(tgrid, x1_opt, '--')
plot(tgrid, x2_opt, '-')
stairs(tgrid, [u_opt; nan], '-.')
xlabel('t')
legend('x1','x2','u')
|