Figure 2.5:

Closed-loop economic MPC versus tracking MPC starting at x=(-8,8) with optimal steady state (8,4). Both controllers asymptotically stabilize the steady state. Dashed contours show cost functions for each controller.

Code for Figure 2.5

Text of the GNU GPL.

main.m


  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
% Compare economic vs. tracking mpc.



mpc = import_mpctools();
N = struct('x', 2, 'u', 1, 't', 50);

% Linear model.
A = [0.5, 1; 0, 0.75];
B = [0; 1];
f = mpc.getCasadiFunc(@(x,u) A*x + B*u, [N.x, N.u], {'x', 'u'}, {'F'});

% Objective function.
q = [-2; 2];
r = -10;
lecon = mpc.getCasadiFunc(@(x, u) q'*x + r'*u, [N.x, N.u], {'x', 'u'}, {'lecon'});

Q = [10,0; 0,10];
R = 1;
ltrack = mpc.getCasadiFunc(@(x,u,xs,us) (x - xs)'*Q*(x - xs) + (u - us)'*R*(u - us), ...
                           [N.x, N.u, N.x, N.u], {'x', 'u', 'xs', 'us'}, {'ltrack'});

% Bounds.
lb = struct('u', -1, 'x', [-10; -10]);
ub = struct('u', 1, 'x', [10; 10]);

% Target finder.
target = mpc.sstarg('l', lecon, 'f', f, 'lb', lb, 'ub', ub, 'N', N, ...
                    'verbosity', 0);
target.solve();
xs = target.var.x;
us = target.var.u;
ls = target.obj;

% Set up controllers.
par = struct('xs', xs, 'us', us);
econ = mpc.nmpc('l', lecon, 'f', f, 'lb', lb, 'ub', ub, 'N', N, 'xf', xs, ...
                'verbosity', 0);
track = mpc.nmpc('l', ltrack, 'f', f, 'lb', lb, 'ub', ub, 'par', par, 'N', N, ...
                 'xf', xs, 'verbosity', 0);

% Simulate both.
Nsim = 25;
x0 = [-8; 8];
controllers = {econ, track};
names = {'econ', 'track'};
data = struct();
for i = 1:length(controllers)
    fprintf('Simulating %s MPC\n', names{i});
    x = NaN(N.x, Nsim + 1);
    x(:,1) = x0; % Initial condition.
    u = NaN(N.u, Nsim);
    controller = controllers{i};
    for t = 1:Nsim
        controller.fixvar('x', 1, x(:,t));
        controller.solve();
        u(:,t) = controller.var.u(:,1);
        x(:,t + 1) = controller.var.x(:,2);
        controller.saveguess();
    end
    data.(names{i}) = struct('x', x, 'u', u);
end

% Get data for objective function contours.
x1 = linspace(-10, 15, 251);
x2 = linspace(0, 10, 101);
[xx1, xx2] = meshgrid(x1, x2);
dx1 = xx1 - track.par.xs(1); % Deviation variables.
dx2 = xx2 - track.par.xs(2);
Vecon = q(1)*dx1 + q(2)*dx2;
Vtrack = Q(1,1)*dx1.^2 + 2*Q(2,1)*dx1.*dx2 + Q(2,2)*dx2.^2;
data.contours = struct('x1', x1, 'x2', x2, 'Vecon', Vecon, 'Vtrack', Vtrack);

% Make plots.
figure();
hold('on');
contour(xx1, xx2, Vecon, 10, '-c');
contour(xx1, xx2, Vtrack, 10, '-m');
colors = {'blue', 'red'};
for i = 1:length(names)
    n = names{i};
    plot(data.(n).x(1,:), data.(n).x(2,:), '-o', 'color', colors{i});
end
xlabel('x_1');
ylabel('x_2', 'rotation', 0);

% Find rotated cost function.
mu = -(eye(N.x) - A)'\q;
alpha = 0.01;
Mu = dlyap(A', alpha*eye(N.x));
lam = @(x) mu'*(x - xs) + (x - xs)'*Mu*(x - xs);

lrot = @(x, u) q'*(x - xs) + r'*(u - us) + lam(x) - lam(A*x + B*u) ...
               - alpha*(x - xs)'*(x - xs);
[x1, x2, u] = meshgrid(linspace(lb.x(1), ub.x(1), 21), ...
                       linspace(lb.x(2), ub.x(2), 21), ...
                       linspace(lb.u, ub.u, 21));
l = arrayfun(@(x1, x2, u) lrot([x1; x2], u), x1, x2, u);
fprintf('Minimum of dissipation inequality: %g\n', min(l(:)));

% Simulate multiple economic MPC trajectories.
econ.reset();
Nx0 = 16;
colors = jet(Nx0);
theta = linspace(0, 2*pi(), Nx0 + 1);
theta = theta(2:end);
x0 = [8*cos(theta); 8*sin(theta)];
Nsim = 21;
x = NaN(N.x, Nsim, Nx0);
u = NaN(N.u, Nsim, Nx0);
V = NaN(Nsim, Nx0);
plotopts = struct('fig', figure(), 'unames', {{'u', 'V'}}, 'marker', '');
fprintf('Simulating more economic MPC');
for j = 1:Nx0
    x(:,1,j) = x0(:,j);
    for i = 1:Nsim
        econ.fixvar('x', 1, x(:,i,j));
        econ.solve();
        if ~isequal(econ.status, 'Solve_Succeeded')
            warning('%s: failure at time %d!', names{i}, t);
            break
        end
        u(:,i,j) = econ.var.u(:,1);
        V(i,j) = econ.obj + lam(x(:,i,j)) - N.t*ls;
        if i < Nsim
            x(:,i + 1,j) = econ.var.x(:,2);
        end
    end
    fprintf('.');
    mpc.mpcplot(x(:,:,j), [u(:,:,j); V(:,j)'], 'color', colors(j,:), ...
                '**', plotopts);
end
fprintf('\n');
data.phase = struct('x', x, 'u', u, 'V', V);