YALMIP-MPT3 explicit MPC mix issue

305 views
Skip to first unread message

lhlctrl

unread,
Jun 16, 2021, 4:48:34 PM6/16/21
to YALMIP

Dear Professor Johan Löfberg,

I used the YALMIP and MPT toolboxes together earlier, which worked fine earlier (with MPT2.6.2), because something is easier in MPT and something is easier in YALMIP.
I have installed the newest MPT3 recently which installed the newest YALMIP (yalmipversion: '20210331') to my computer.
I have a YALMIP-MPT mix issue. The topic is: explicit MPC calculation.
Earlier the communication was between the interfaces YALMIP and MPT easy:
...
[mpsol, diag] = solvemp(F, obj, [], x{1}, u{1})
ctrl = mpt_mpsol2ctrl(mpsol, ..., ...);
[...] = mpt_getInput(ctrl, x0);
figure;
plot(slice(ctrl.Pn, [1 2], [0 0]))
xlabel('x_3'); ylabel('x_4');
...
etc.

This old syntax is already obsolete and not supported.

I experience that the some code lines above do not not work with the new syntaxes.
I found 2 possibilities in the User Guides for the implementation but both have problems, but different problems:

1.) Assume I defined the constraints (F) and the objective function (obj) with sdpvars according to the YALMIP formulation.

[mpsol,diag,aux,Valuefunction,Optimizer] = solvemp(F,obj,[],x{1},u{1})
ctrlYalmip = EMPCController(mpt_mpsol2pu(mpsol));

The structure ctrlYalmip will be filled in incomplete. For example
ctrlYalmip.N = N;
ctrlYalmip.nu = nu;
are missing also. But this is not such a big problem because I can fill in them manually later on although not nice.

The problem/bug comes here (MPT):
[uYalmip,feasibleYalmip,openloopYalmip] = ctrlYalmip.evaluate(x0);

function [u, feasible, openloop] = evaluate(obj, xinit, varargin)
...
[U, feasible, idx, J] = obj.optimizer.feval(xinit, ...
'primal', 'tiebreak', 'obj');
...
will return only with one element in U despite of the fact that for example N = 3
(The open-loop u{1}, u{2}u{3} is required to get working EMPCController evaluate(x0))

therefore this error will come:
Error using EMPCController/evaluate (line 216)
Number of optimizers is inconsistent with "N" and/or "nu".

- How could I reach with solvemp to get a correct form in mpsol which is good for MPT and to get a MPT consistent ctrl object?
- How can I get the open loop solution from solvemp in YALMIP at all?
- Does it exist in YALMIP similar method to this: [uYalmip,feasibleYalmip,openloopYalmip] = ctrlYalmip.evaluate(x0);?

2.) If I use a different, second method which is written in the MPT User guide:
ctrlMptYal = MPCController(sys,N);
Y = ctrlMptYal.toYALMIP();
...

Y.constraints = F;  % F in YALMIP formulation
Y.objective = obj;  % obj in YALMIP formulation

ctrlMptYal.fromYALMIP(Y);
ctrlMptYalExplct = EMPCController(ctrlMptYal);

figure;
subplot(2,1,1);
ctrlMptYalExplct.partition.plot();
title('MPT\_YALMIP mix');
subplot(2,1,2);
ctrlMptYalExplct.feedback.fplot();

[uMptYal,feasibleMptYal,openloopMptYal] = ctrlMptYalExplct.evaluate(x0)

then MPT ignores the complete YALMIP constraints completely.
- Why?
--------------------------------------------------------------------------------------------------------------------------
- So I can not realize the original, with the older MPT worked communication between the MPT and the YALMIP.
- How could I resolve this issue?

And a last, little general question:
-Can explicit MPC polihedron/polytope slices plot in YALMIP also (for example in case of 4 state variables) or only with MPT toolbox with

slice12 = ctrl.partition.slice([1,2],[0,0]);

figure;
slice12.plot()
xlabel('x_3'); ylabel('x_4');

Johan Löfberg

unread,
Jun 16, 2021, 5:36:34 PM6/16/21
to YALMIP

you would have to supply reproducible examples

lhlctrl

unread,
Jun 18, 2021, 2:11:24 PM6/18/21
to YALMIP
  Dear Professor Johan Löfberg,

I attached the reproducible examples:
testMPT.m => ok, only as a reference
testYALMIP.m => Issue 1.)
testMPT_YALMIPmix.m => Issue 2.)
----
And a last, little general question:
-Can explicit MPC polihedron/polytope slices plot in YALMIP also (for example in case of 4 state variables) or only with MPT toolbox with
slice12 = ctrl.partition.slice([1,2],[0,0]);
---
Thank you.


MPT_YALMIPissue.zip

Johan Löfberg

unread,
Jun 18, 2021, 2:24:04 PM6/18/21
to YALMIP

Note sure what you are doing here as you are doing some hacks which I do not what they are doing

 

This create something which given a vector x0 returns a scalar

 

[mpsol,diag,aux,Valuefunction,Optimizer] = solvemp(F,obj,[],x{1},u{1})

 

Then you use some MPT functions to map to some MPT object, and then manually edit properties

ctrlYalmip.N = N;

ctrlYalmip.nu = nu;

 

then you call a method and it fails

 

ctrlYalmip.evaluate(x0)

 

and if you debug that failure, it is because the code in the class assumes that you have an MPC solution with an explicit solution for the whole trajectory, not just the first one

 

I don’t see how that ever can have worked since it only can work if the output from the optimizer which you have converted returns the full control sequence

lhlctrl

unread,
Jun 19, 2021, 7:13:31 PM6/19/21
to YALMIP
Dear Professor Johan Löfberg,

Yes, Professor see right the ctrlYalmip = EMPCController(mpt_mpsol2pu(mpsol)); does not fill in the ctrlYalmip object correctly. It does not correspond to a working MPT ctrl object. I thought that only these attributes are missing which the MPT Toolbox indicated error at first therefore I filled in:
ctrlYalmip.N = N;
ctrlYalmip.nu = nu;
but then it turned out the scalar problem as you also noticed, mentioned here:
[uYalmip,feasibleYalmip,openloopYalmip] = ctrlYalmip.evaluate(x0)

Maybe you have not checked but issue 2 (testMPT_YALMIPmix.m => Issue 2.) has another problem: it does not consider the YALMIP constraints at all.

The correct MPT object (here ctrl below) generating from YALMIP solution worked with MPT2.6.2 with the following commands:
[mpsol, diag] = solvemp(F, obj, [], x{1}, u{1})
ctrl = mpt_mpsol2ctrl(mpsol, nu, ny);
[uopt_mp, d1, d2, obj_opt_mp] = mpt_getInput(ctrl, x0);

But the MPT2.6.2 was reworked in MPT3 conceptually and the commands mpt_mpsol2ctrl, mpt_getInput were removed.

So currently there is no method to use MPT3 and YALMIP together for solving explicit MPC problems because the MPT3 has some bugs for creating the MPT ctrl object from the YALMIP solution. This is my experience. :(
************************************************************************************************************************************************************************************************************************


Ok, but maybe I can solve everything only by using YALMIP and then I can leave out the MPT3 completely.

I would like to solve the following problem:

1.) MPC problem formulation in YALMIP. It is ok.
(I have 4 state variables problem with special constraints, a MIQP problem will be created.)

2.) I would like to get the explicit solution:
[mpsol,diag,aux,Valuefunction,Optimizer] = solvemp(F,obj,[],x{1},u{1})
It is ok also.

3.) I would like to calculate the control action at a given x0 (from here https://yalmip.github.io/tutorial/multiparametricprogramming/):
[i,j] = isinside(mpsol{1}.Pn,x0)
mpsol{1}.Fi{j}*x0 + mpsol{1}.Gi{j}

or with

assign(x{1}, x0);
value(Optimizer)

Am I right?

4.) Which method is used in case of isinside to find the place of the x0 (e.g. binary search)? Can I have the possibility to influence the method?

5.) How could I get the open loop 3 solutions for a given x0 for example in case of horizon N=3?

6.) I would like to plot slices of the explicit solution. I experienced that the following methods work only for 2D case (2 state variables):
plot(Valuefunction);
figure
plot(Optimizer);
How can I plot slices of the explicit solution for example for 4 state variables (for example the explicit MPC regions from a point of view x1-x3).
7.) It can occur that overlapping regions are created.
Is that control action chosen in the overlapping areas by the solvemp which has the smaller performance value? If not then can I influence it?
-----------------------------------------------------
Thank you.
(My plan is that I try to do everything with YALMIP because the MPT3 has the issues/bugs above.)

lhlctrl

unread,
Jun 23, 2021, 11:40:14 AM6/23/21
to YALMIP
  Dear Professor Johan Löfberg,

Could you help me to answer my concrete YALMIP questions (3-7) below, please?

Thank you in advance.

Johan Löfberg

unread,
Jun 23, 2021, 12:43:25 PM6/23/21
to YALMIP
3: yes

4: no idea, you would have to debug, but my guess is they run a simple sequential search by default, and you will have to do something else to setup a tree etc

5 yalmip does not care if you declare U(1) or U as the output from the object, so you use exactly the same strategy

6/7 those are MPT concepts, so to figure out that you would have to look at mpt/debug the call

lhlctrl

unread,
Jun 23, 2021, 6:28:54 PM6/23/21
to YALMIP
  Dear Professor Johan Löfberg,

> yalmip does not care if you declare U(1) or U as the output from the object, so you use exactly the same strategy
Yes, I also thought, expected that it should work in such a way but it does not work for me. I attached a very simple example.

Could you tell me, please why?
I would like to get the 3 open-loop control value in case of horizon N = 3. See my very simple example, please.
testYALMIPfinal1.m

Johan Löfberg

unread,
Jun 23, 2021, 7:13:55 PM6/23/21
to YALMIP
you have cells

solvemp(F,obj,[],x{1},[u{:}])

note that you have unnecessarily complicated code to create u and x cell arrays

lhlctrl

unread,
Jul 2, 2021, 3:13:58 AM7/2/21
to YALMIP
  Dear Professor Johan Löfberg,

> note that you have unnecessarily complicated code to create u and x cell arrays
I read this very good tutorial, I know it.

----
But what do you think of it?
You mean this?
x = {}; u = {}; y = {};
for i = 1:N+1
    x{i} = sdpvar(nx, 1);
end

for i = 1:N
    u{i} = sdpvar(nu, 1);
end

for i = 1:N
    y{i} = sdpvar(ny, 1);
end

I read it in your tutorial also:

"In some situations, coding is simplified with a multi-dimensional variable. This is supported in YALMIP with two different constructs, cell arrays and multi-dimensional sdpvar objects.

The cell array format is nothing but an abstraction of the following code

for i = 1:5 X{i} = sdpvar(2,3); end "
---

I tried to define it in another way but it was not simpler.
Could you tell me what you think of it?  How could I do it better?
Thank you.


Best regards

Johan Löfberg

unread,
Jul 2, 2021, 3:25:54 AM7/2/21
to YALMIP
x=sdpvar(ones(1,N+1)*nx,ones(1,N+1))

or simply skip cells

x=sdpvar(nx,1,N)

Reply all
Reply to author
Forward
0 new messages