1 | yalmip('clear') |
---|
2 | clc |
---|
3 | echo on |
---|
4 | %********************************************************* |
---|
5 | % |
---|
6 | % Multi-parametric programing |
---|
7 | % |
---|
8 | %********************************************************* |
---|
9 | % |
---|
10 | % This example requires the MPT-Toolbox |
---|
11 | % |
---|
12 | % We return to the MPC-problem. |
---|
13 | % This time however, we seek the explicit solution as a |
---|
14 | % function of the current state x |
---|
15 | pause |
---|
16 | |
---|
17 | % Create numerics for a discretized double integrator |
---|
18 | N = 5; |
---|
19 | A = [2 -1;1 0]; |
---|
20 | B = [1;0]; |
---|
21 | C = [0.5 0.5]; |
---|
22 | [H,S] = create_CHS(A,B,C,N); |
---|
23 | pause |
---|
24 | |
---|
25 | % Variables |
---|
26 | x = sdpvar(2,1); |
---|
27 | U = sdpvar(N,1); |
---|
28 | pause |
---|
29 | |
---|
30 | % Parameterized predictions |
---|
31 | Y = H*x+S*U; |
---|
32 | |
---|
33 | %Control constraints |
---|
34 | F = set(-1 < U < 1); |
---|
35 | |
---|
36 | % Terminal constraint |
---|
37 | F = F+set(-1 < Y(N) < 1); |
---|
38 | pause |
---|
39 | |
---|
40 | % Exploration space |
---|
41 | F = F + set(-5 < x < 5); |
---|
42 | pause |
---|
43 | |
---|
44 | % Call solvemp (same syntax as solvesdp, except additional |
---|
45 | % fourth argument to specify the parametric variable) |
---|
46 | mpsol = solvemp(F,Y'*Y+U'*U,[],x); |
---|
47 | pause |
---|
48 | |
---|
49 | % We can, e.g., plot the solution |
---|
50 | plot(mpsol{1}.Pn) |
---|
51 | pause |
---|
52 | |
---|
53 | % Solve the problem with a L1 cost |
---|
54 | mpsol = solvemp(F,norm([Y;U],1),[],x); |
---|
55 | pause |
---|
56 | |
---|
57 | % Plot the value function |
---|
58 | clf |
---|
59 | mpt_plotPWA(mpsol{1}.Pn,mpsol{1}.Bi,mpsol{1}.Ci) |
---|
60 | pause |
---|
61 | |
---|
62 | |
---|
63 | % Solve the problem with a L1 cost, but this time |
---|
64 | % only request U(1) in the returned solution |
---|
65 | mpsol = solvemp(F,norm([Y;U],1),[],x,U(1)); |
---|
66 | pause |
---|
67 | |
---|
68 | % Plot the optimizer |
---|
69 | clf |
---|
70 | mpt_plotPWA(mpsol{1}.Pn,mpsol{1}.Fi,mpsol{1}.Gi) |
---|
71 | pause |
---|
72 | |
---|
73 | % Extract value of optimizer at point [0.1;0.2] |
---|
74 | [ii,jj] = isinside(mpsol{1}.Pn,[0.1;0.3]); |
---|
75 | mpsol{1}.Fi{jj}*[0.1;0.3] + mpsol{1}.Gi{jj} |
---|
76 | pause |
---|
77 | |
---|
78 | % To avoid learning MPT commands, simply use |
---|
79 | % some more outputs from SOLVEMP, and YALMIP PWA |
---|
80 | % functions will be generated automatically |
---|
81 | [mpsol,diagnost,Z,Valuefunction,Optimizer] = solvemp(F,norm([Y;U],1),[],x,U(1)); |
---|
82 | clf |
---|
83 | plot(Valuefunction) |
---|
84 | pause |
---|
85 | |
---|
86 | clf |
---|
87 | plot(Optimizer) |
---|
88 | pause |
---|
89 | |
---|
90 | assign(x,[0.1;0.3]); |
---|
91 | double(Optimizer) |
---|
92 | pause |
---|
93 | |
---|
94 | clc |
---|
95 | % The Valuefunction and Optmizer are standard |
---|
96 | % variables in YALMIP (so called nonlinear operators) |
---|
97 | % which we can use as variables when setting up other |
---|
98 | % optmimization problem |
---|
99 | % |
---|
100 | % Here we solve the silly problem of finding the state |
---|
101 | % with maximal x(1) coordinate, while having an optmial |
---|
102 | % cost less than 10. |
---|
103 | % |
---|
104 | % Since the value function for a simple parametric LP |
---|
105 | % is convex and can be written as the maximum of a set |
---|
106 | % of linear functions, the problem will be an LP, and |
---|
107 | % YALMIP keeps track of convexity etc, since the value |
---|
108 | % function behind the scenes is described with a so |
---|
109 | % called convexity-aware nonlinear operator. |
---|
110 | pause |
---|
111 | solvesdp(set(Valuefunction < 10),-x(1)), |
---|
112 | double(x) |
---|
113 | double(Valuefunction) |
---|
114 | pause |
---|
115 | |
---|
116 | % Why not solve a nonconvex problem and find the maximum |
---|
117 | % of the convex value function. Note that this will generate |
---|
118 | % a binary LP problem, so you need to have an MILP solver |
---|
119 | pause |
---|
120 | solvesdp([],-Valuefunction) |
---|
121 | double(x) |
---|
122 | double(Valuefunction) |
---|
123 | pause |
---|
124 | |
---|
125 | % Note that the transparant use of the value functions and the |
---|
126 | % optimizer as standard piecewise YALMIP variables still is |
---|
127 | % under development. Quadratic functions are for instance |
---|
128 | % not fully supported yet. |
---|
129 | % |
---|
130 | % Learn a lot more in the HTML manual... |
---|
131 | |
---|
132 | pause |
---|
133 | echo off |
---|