How to save an optimizer object

1,201 views
Skip to first unread message

Pantelis Sopasakis

unread,
Nov 20, 2013, 10:32:16 AM11/20/13
to yal...@googlegroups.com
Dear all,

I have created some YALMIP object using the command 'optimizer' (and it took a few hours to construct them). I would like to know if it is possible to store them as .mat or in any other format. I tried to use MATLAB's save, but I got a warning from YALMIP which reads as follows:

> In lmi.saveobj at 3
Warning: YALMIP objects cannot be saved in binary format. You will run into troubles if you try to load this file
later. You should clear all YALMIP objects first, or avoid having any YALMIP objects in the list of variables which
you save.


So, what happened is that YALMIP stored my objects as structures. Is it possible to reconstruct the original YALMIP objects from these structures? How should I store my optimizers? Is it possible to apply the command 'export' on objects that are returned from 'optimizer'?

Thanks a lot in advance,
Best regards,
Pantelis Sopasakis

Johan Löfberg

unread,
Nov 20, 2013, 3:52:06 PM11/20/13
to yal...@googlegroups.com
No, saving objects is not possible. The code is the model. The excessive definition time should be attacked. What does your code look like?

Export cannot be used on optimizer objects (what would it be and what would you use it for? Sure, save it as a struct if you want to for some reason. P = optimizer(...);P=struct(P);save P, but I don't see why)


Johan Löfberg

unread,
Nov 20, 2013, 4:19:01 PM11/20/13
to yal...@googlegroups.com
Having said that, I could possibly add save/load support for optimizer objects. I see the use-case now, and the problems which led me to remove save/load for sdpvar objects will not appear.

Pantelis Sopasakis

unread,
Nov 25, 2013, 12:03:05 PM11/25/13
to yal...@googlegroups.com
Hi Johan,

Thank you very much for your answer. I think there is need for a load-method for optimizer objects. Can you give me some instructions, let's say an example, on how to store and reload an optimizer object?

There are certain use cases when it is necessary to do so. For instance, when you need to share an MPC controller designed in YALMIP without the need that the recipient of the controller needs to re-run the YALMIP code to generate it.

I would appreciate your help.

Best regards,
Pantelis

Johan Löfberg

unread,
Nov 25, 2013, 12:34:51 PM11/25/13
to yal...@googlegroups.com
I'll try to add it soon, it shouldn't be hard.

Johan Löfberg

unread,
Nov 26, 2013, 3:00:27 PM11/26/13
to yal...@googlegroups.com
Simply add two files loadobj.m and saveobj.m in extras\@optimizer, with the following content (yes, no code in the files, only headers necessary)

function P = loadobj(P)

and

function P = loadobj(P)


Johan Löfberg

unread,
Nov 27, 2013, 2:09:47 AM11/27/13
to yal...@googlegroups.com
and by the way, the important question remains: Why does it take several hours to define the problem. You are encouraged to contact me to try to pin-point some weird bottle-neck.

Pantelis Sopasakis

unread,
Nov 27, 2013, 4:07:40 AM11/27/13
to yal...@googlegroups.com
Thank you very much for the solution! Indeed, it works perfectly!

First off, my problem is a strictly convex QP. The piece of code that takes hours to complete is exactly here (I'm pasting it exactly as it appears in my code):

controller{iMode} = optimizer(F, obj, sdpsettings('verbose',1, 'solver', 'cplex'),x{1},u{1});

The size of the problem can be rather big, e.g. F counts around 2,500 linear inequalities involving usually 12,000~20,000 variables. The vector x is of dimension 12,000 and the size of u is around 2,000.

Do you want me to send you some additional information? I can make a MAT file with the objective and the constraints of the problem and send it to you.


Johan Löfberg

unread,
Nov 27, 2013, 4:44:03 AM11/27/13
to yal...@googlegroups.com
OK, that is a pretty large problem then. You are free to send me the model to see if anything can be done (once managed to get down a 1 million variable QP from hours to a couple of seconds. requires structure though)

Pantelis Sopasakis

unread,
Nov 27, 2013, 5:57:14 AM11/27/13
to yal...@googlegroups.com
If you wish to take a look, here's a problem which is large - but not enormously huge - but yet the command 'optimizer' takes hours to complete. I stored the problem using:

[model,recoverymodel,diagnostic,internalmodel] = export(F,obj,yalmip_struct{iMode}.settings);

You can find the variables in the MAT file at https://www.dropbox.com/s/04b9liujqxg2uyz/yalmip_benchmark.mat
As you see the problem has approx. 28,000 variables, 143,000 inequality constraints and 25,000 equalities.

Johan Löfberg

unread,
Nov 27, 2013, 6:00:47 AM11/27/13
to yal...@googlegroups.com
I need the model description and data to run it. At this compiled numerical format, it is too late to see anything

Pantelis Sopasakis

unread,
Nov 27, 2013, 6:13:07 AM11/27/13
to yal...@googlegroups.com
You can still see the structure of the problem since you have the Hessian and matrices of the constraints. For instance spy(model.H) reveals a block-diagonal structure with very high sparsity (density: 0.0294%). Also, the matrix Aineq is very sparse (density: 0.0185%) and the same for Aeq (density: 0.0431%).

How can I store the constraints and the objective of the problem so as to send them to you? You mean that the origin problem can be possibly formulated in another way?

Do you mean you need the code that generates the problem?

Johan Löfberg

unread,
Nov 27, 2013, 6:14:09 AM11/27/13
to yal...@googlegroups.com
Yes, the code, so I can profile etc

Pantelis Sopasakis

unread,
Nov 27, 2013, 4:45:11 PM11/27/13
to yal...@googlegroups.com
I created a MATLAB script that generates the constraints and the objective of my problem and you can find it at https://gist.github.com/alphaville/5b2389242cb6cca189b6. You will also need a file called  yalmip_benchmark which you can find at https://www.dropbox.com/s/o65aiyrkbruqbr7/yalmip_benchmark_big.mat.

What takes a long time to complete is the formulation of the QP problem (i.e., the invocation to `optimizer`). The time needed for the solution of the problem is no higher than 1.2s.

Johan Löfberg

unread,
Nov 28, 2013, 3:09:53 AM11/28/13
to yal...@googlegroups.com
Turned out to be a simple fix (to test convexity of quadratic I computed eigenvalues, which is a bad idea on a sparse 14000x14000 almost diagonal matrix. I now perform a Dulmage-Mendelsohn block-diagonalization first and check each individual block). 

tic
CTRL = optimizer(F,obj,sdpsettings('solver', 'cplex'),x{1},u{1});
toc
Elapsed time is 2.788538 seconds.

BTW, SET is obsolete so remove that from your code. Also, I guess (hope!) the code is autogenerated, but why not a simple for-loop?
optimizer.m

Pantelis Sopasakis

unread,
Nov 28, 2013, 6:31:32 PM11/28/13
to yal...@googlegroups.com
This saved me from a lot of trouble and lots of hours of waiting! Thanks a million! Indeed, it now runs before I could say Jack Robinson.
By the way, there is an alternative way to determine if a matrix is positive definite; you can do:

[~,p]=chol(Q);
problematicQP
=1-p;


The code is of course auto-generated! I used some fprintf in my loops to store these commands in a file so as to share it with you.

Johan Löfberg

unread,
Nov 29, 2013, 1:50:13 AM11/29/13
to yal...@googlegroups.com
Unfortunately, I have to test for positive semidefinite which chol will fail for. What I normally do is some initial simple tests, and then an chol-test, which applies to sparse matrices, and if this test fails (possibly due to positive semidefiniteness), I cast the matrix as full and check smallest eigenvalue. My quick hack posted here has already been extended slightly to include some of these things.

Your code also revealed some performance issues when profiling the whole file. It took almost 30 seconds here. I am confident I can reduce to 15s with some pretty minor changes.

Pantelis Sopasakis

unread,
Nov 30, 2013, 4:20:26 AM11/30/13
to yal...@googlegroups.com
I ran across another problem trying to store optimizer objects. I added the two functions `loadobj` and `saveobj` as you suggested. When after I construct my optimizer I do:

c = optimizer(…)
save my_file c

and then I do:

clear all
load my_file

Indeed, c is reloaded as an instance of the class optimizer.
But, if I do:

clear classes

or if I simply restart MATLAB, then c is loaded as a structure and not as an optimizer!

Pantelis Sopasakis

unread,
Nov 30, 2013, 4:34:25 AM11/30/13
to yal...@googlegroups.com
Just an idea regarding loading optimizer objects...

Maybe it would be fine to do something like:

function P = loadobj(P)

 class_P
= class(P);
 
if strcmp(class_P,'optimizer'),
   
%Do nothing - return the object as it is
   
return;
 
else if strcmp(class_P,'struct'),
   
%Use the information in the struct to reconstruct
   
%the optimizer object.

 
else
   
%Error - invalid input type.
 
end

Pantelis Sopasakis

unread,
Nov 30, 2013, 4:39:17 AM11/30/13
to yal...@googlegroups.com
Sorry for spamming the list with emails, but I think I found a solution:

function P = loadobj(P)
if isstruct(P),
    P
=class(P,'optimizer');
elseif isa
(P,'optimizer')
   
return;
else
    error
('Wrong type!');
end
end


Johan Löfberg

unread,
Nov 30, 2013, 7:21:47 AM11/30/13
to yal...@googlegroups.com
Great, thank you for alerting me on this

Simon

unread,
Jul 8, 2016, 1:28:59 AM7/8/16
to YALMIP
Dear Johan,
 I'm a freshman of Yalmip and I met a problem recently. I got a complex model with millions of constrains. Since part of the constrains are not sensitive with the parameters, I want to save them in .mat file and load them afterwards to save the model loading time. However, I got the warning that "yalmip objects cannot be saved in binary format. You'll run into trouble... " and the .mat file is empty. I don't know how it happens and how can I save part of my constrains.
Thank you

Johan Löfberg

unread,
Jul 8, 2016, 2:05:57 PM7/8/16
to YALMIP
An optimizer object can be saved (optimizer is a special command in yalmip, look it up). A standard model, i.e., constraints and objective, cannot be saved.

Simon

unread,
Mar 6, 2017, 8:35:52 AM3/6/17
to YALMIP
Dear Johan
  Fully sorry for the late feedback. Thank you for the perfect answers.
Reply all
Reply to author
Forward
0 new messages