Demonstration for using Opt interface
mpt_demo_opt1
f = randn(8,1);
A = randn(25,8);
b = 5*rand(25,1);Formulate LP
problem1 = Opt('f',f,'A',A,'b',b)
------------------------------------------------- Linear program Num variables: 8 Num inequality constraints: 25 Num equality constraints: 0 Solver: LCP -------------------------------------------------Solve LP
res1 = problem1.solve
res1 = xopt: [8x1 double] lambda: [1x1 struct] obj: -7.22865124839414 how: 'ok' exitflag: 1
problem2 = Opt('f',f,'A',A,'b',b,'pB',ones(25,1),'Ath',[-1;1],'bth',[1;1])
------------------------------------------------- Parametric linear program Num variables: 8 Num inequality constraints: 25 Num equality constraints: 0 Num parameters: 1 Solver: PLCP -------------------------------------------------Solve MPLP
res2 = problem2.solve
mpt_plcp: 6 regions res2 = xopt: [1x1 PolyUnion] exitflag: 1 how: 'ok' stats: [1x1 struct]Solution is stored as "primal", we can plot it
res2.xopt.fplot('primal','show_set',true,'LineWidth',3), title('Primal solution')
problem3 = Opt('f',f,'A',A,'b',b,'pB',ones(25,1),'Ath',[-1;1],'bth',[1;1],'solver','MPLP')
------------------------------------------------- Parametric linear program Num variables: 8 Num inequality constraints: 25 Num equality constraints: 0 Num parameters: 1 Solver: MPLP -------------------------------------------------Solve MPLP using MPT2
res3 = problem3.solve
Calling mpt_mplp_26 with default options... mpt_mplp: 6 regions res3 = xopt: [1x1 PolyUnion] mplpsol: [1x1 struct] exitflag: 1 how: 'ok' stats: [1x1 struct]Plot the objective value
res3.xopt.fplot('obj','show_set',true,'LineWidth',3), title('Objective value')
A = [0.5 -1;1 0]; B = [1;0]; nu = 1;MPC data
Q = eye(2); R = 1; N = 4;Initial state
x0 = sdpvar(2,1);Setup the problem
u = sdpvar(nu,N);
constraints = [];
objective = 0;
x = x0;
for k = 1:N x = A*x + B*u(k); objective = objective + norm(Q*x,1) + norm(R*u(k),1); constraints = [constraints, -1 <= u(k) <= 1, -5 <= x <= 5]; endFormulate the problem
problem4 = Opt(constraints, objective, x0, u)
------------------------------------------------- Parametric linear program Num variables: 24 Num inequality constraints: 56 Num equality constraints: 0 Num lower bounds 24 Num upper bounds 24 Num parameters: 2 Solver: PLCP -------------------------------------------------Solve the problem
res4 = problem4.solve
regions: 8, unexplored: 11 regions: 24, unexplored: 12 mpt_plcp: 52 regions res4 = xopt: [1x1 PolyUnion] exitflag: 1 how: 'ok' stats: [1x1 struct]Plot the partition
res4.xopt.plot
sysStruct.A = [1 1; 0 1]; sysStruct.B = [1; 0.5]; sysStruct.C = [1 0; 0 1]; sysStruct.D = [0;0]; sysStruct.ymin = [-5; -5]; sysStruct.ymax = [5; 5]; sysStruct.umin = -1; sysStruct.umax = 1;Problem formulation
probStruct.norm=2; probStruct.Q=eye(2); probStruct.R=1; probStruct.N=5; probStruct.subopt_lev=0;Generate matrices of the appropriate formulation of parametric problem.
Matrices = mpt_constructMatrices(sysStruct,probStruct);
Function mpt_constructMatrices is obsolete and will be removed in a future MPT version. Iteration 1... Iteration 2... Iteration 3... Iteration 4...Define the problem using Opt class.
problem5 = Opt(Matrices)
------------------------------------------------- Parametric quadratic program Num variables: 5 Num inequality constraints: 20 Num equality constraints: 0 Num parameters: 2 Solver: PLCP -------------------------------------------------Solve the problem
res5 = problem5.solve
mpt_plcp: 25 regions res5 = xopt: [1x1 PolyUnion] exitflag: 1 how: 'ok' stats: [1x1 struct]Plot he objective function
res5.xopt.fplot('obj')
◀ | mpt_demo_lti5 | mpt_demo_sets1 | ▶ |
© 2010-2013 Martin Herceg: ETH Zurich, herceg@control.ee.ethz.ch