1 | function mpt_pwa_2 |
---|
2 | |
---|
3 | % Data |
---|
4 | A = [2 -1;1 0];nx = 2; |
---|
5 | B = [1;0];nu = 1; |
---|
6 | C = [0.5 0.5]; |
---|
7 | |
---|
8 | % Prediction horizon |
---|
9 | N = 4; |
---|
10 | |
---|
11 | % States x(k), ..., x(k+N) |
---|
12 | x = sdpvar(repmat(nx,1,N),repmat(1,1,N)); |
---|
13 | |
---|
14 | % Inputs u(k), ..., u(k+N) (last one not used) |
---|
15 | u = sdpvar(repmat(nu,1,N),repmat(1,1,N)); |
---|
16 | |
---|
17 | % Binary for PWA selection |
---|
18 | d = binvar(2,1); |
---|
19 | |
---|
20 | % Compare |
---|
21 | sysStruct.A{1} = A; |
---|
22 | sysStruct.B{1} = B; |
---|
23 | sysStruct.C{1} = C; |
---|
24 | sysStruct.D{1} = [0]; |
---|
25 | sysStruct.A{2} = A; |
---|
26 | sysStruct.B{2} = B*pi; |
---|
27 | sysStruct.C{2} = C; |
---|
28 | sysStruct.D{2} = [0]; |
---|
29 | sysStruct.guardX{1} = [-1 0]; |
---|
30 | sysStruct.guardU{1} = [0]; |
---|
31 | sysStruct.guardC{1} = [0]; |
---|
32 | sysStruct.guardX{2} = [1 0]; |
---|
33 | sysStruct.guardU{2} = [0]; |
---|
34 | sysStruct.guardC{2} = [0]; |
---|
35 | |
---|
36 | %set constraints on output |
---|
37 | sysStruct.ymin = -1; |
---|
38 | sysStruct.ymax = 1; |
---|
39 | |
---|
40 | %set constraints on input |
---|
41 | sysStruct.umin = -1; |
---|
42 | sysStruct.umax = 1; |
---|
43 | |
---|
44 | sysStruct.xmin = [-5;-5]; |
---|
45 | sysStruct.xmax = [5;5]; |
---|
46 | |
---|
47 | probStruct.norm=1; |
---|
48 | probStruct.Q=eye(2); |
---|
49 | probStruct.R=1; |
---|
50 | probStruct.N=N-1; |
---|
51 | probStruct.P_N=zeros(2); |
---|
52 | probStruct.subopt_lev=0; |
---|
53 | probStruct.y0bounds=1; |
---|
54 | probStruct.Tconstraint=0; |
---|
55 | ctrl=mpt_control(sysStruct,probStruct) |
---|
56 | |
---|
57 | % Online |
---|
58 | obj = 0; |
---|
59 | F = set([]); |
---|
60 | dd = []; |
---|
61 | for k = N-1:-1:1 |
---|
62 | |
---|
63 | bounds(x{k},-5,5); |
---|
64 | bounds(u{k},-1,1); |
---|
65 | bounds(x{k+1},-5,5); |
---|
66 | |
---|
67 | % Feasible region |
---|
68 | F = F + set(-1 < u{k} < 1); |
---|
69 | F = F + set(-1 < C*x{k} < 1); |
---|
70 | F = F + set(-5 < x{k} < 5); |
---|
71 | F = F + set(-1 < C*x{k+1} < 1); |
---|
72 | F = F + set(-5 < x{k+1} < 5); |
---|
73 | |
---|
74 | % PWA Dynamics |
---|
75 | d = binvar(2,1);dd = [dd;d]; |
---|
76 | F = F + set(implies(d(1),x{k+1} == (A*x{k}+B*u{k}))); |
---|
77 | F = F + set(implies(d(2),x{k+1} == (A*x{k}+pi*B*u{k}))); |
---|
78 | F = F + set(implies(d(1),x{k}(1) > 0)); |
---|
79 | F = F + set(implies(d(2),x{k}(1) < 0)); |
---|
80 | F = F + set(sum(d) == 1); |
---|
81 | obj = obj + norm([x{k};u{k}],1); |
---|
82 | end |
---|
83 | mpsol{k} = solvemp(F,obj,sdpsettings('debug',1),x{k},u{k}); |
---|
84 | mpsol{1} = mpt_removeOverlaps(mpsol{1}) |
---|
85 | mbg_asserttolequal(mpt_isPWAbigger(mpsol{1},ctrl),0); |
---|