saving and reloading optimizer objects

409 views
Skip to first unread message

Drecalde

unread,
Apr 14, 2015, 12:40:11 AM4/14/15
to yal...@googlegroups.com
I am having trouble saving and reloading the optimizer objects

When i run the optimizer code the model is solved without problems.

When i restart matlab I cant get the

im calling and saving the optimizer as:

parameters_in = {a,b,c,d,e};

solutions_out
= {out1,out2};

ops
= sdpsettings('verbose','2', 'solver', 'gurobi');

controller
= optimizer(Constraints,objective,ops,parameters_in,solutions_out);

save saved controller
;


to reload i do:


load saved
;

loadobj
(controller);

[solutions,diagnostics] = controller{{a,b,c,d,e}};



When i run the second time i get this error


Error using reshape
To RESHAPE the number of elements must not change.
Error in optimizer/subsref (line 140)
                u
= [u reshape(output.Primal(1+self.map),self.dimout)];


I realized that the code is running on the other side of the if/else statement on subsref code when i reload the model:

....  (line 133)      
       
if isempty(self.output.z)
           
if ~isempty(output.Primal)
               
% Make sure we map index 0 to Nans
               
% 0 corresponds to variables which werent visible in
               
% problem
                output
.Primal = [nan;output.Primal];
                u
= [u reshape(output.Primal(1+self.map),self.dimout)];
           
else
                u
= [u reshape(0*self.map+nan,self.dimout)];
           
end
       
else
           
if ~isempty(output.Primal)
               
% Make sure we map index 0 to Nans
               
% 0 corresponds to variables which werent visible in
               
% problem
                output
.Primal = [nan;output.Primal];
                assign
(self.output.z,output.Primal(1+self.map));
                assign
(self.input.expression,thisData);
                u
= [u reshape(double(self.output.expression),self.dimout)];
           
end
       
end
....

The problem is on the (self.output.z) but I am not sure how this is created


Johan Löfberg

unread,
Apr 14, 2015, 1:28:06 AM4/14/15
to yal...@googlegroups.com
You would have to supply an example which I can run

I think the output model is too complex for it to run as a stand-alone optimizer object. When the output expression is complex, the algebraic manipulations to compute the requested output is done using YALMIPs symbolic machinery (i.e, sdpvars), which doesn't survive a save/load sequence.

Johan Löfberg

unread,
Apr 14, 2015, 1:40:03 AM4/14/15
to yal...@googlegroups.com
Yes, as expected, it happens when the output expression is a complex sdpvar expression (which is expected). However, I note that your code signals another line, so I think you are running an old version

>> yalmip('clear');clear all
>> sdpvar a b x y
>> P = optimizer([x;y]<=a+b,-x-y,[],{a,b},{x^2+y});
>> save dummy P
>> load dummy
>> P{{1,2}}

Error using reshape
To RESHAPE the number of elements must not change.

Error in optimizer/subsref (line 157)
                u
= [u reshape(output.Primal(1+self.map),self.dimout)];


Drecalde

unread,
Apr 14, 2015, 1:58:56 AM4/14/15
to yal...@googlegroups.com
I am now running the last version. Is there any other way of saving the optimizer model?

Johan Löfberg

unread,
Apr 14, 2015, 2:02:10 AM4/14/15
to yal...@googlegroups.com
No. To have an optimizer object entirely independent from YALMIP and thus survive save/load, it has to have a simple output function (basically just variables, not any kind of expression).

Drecalde

unread,
Apr 14, 2015, 10:48:57 AM4/14/15
to yal...@googlegroups.com
is there any way to store the object properties, e.g.
fieldnames(controller)

store them as a separate structure and then restore it when loading the model?

Johan Löfberg

unread,
Apr 14, 2015, 2:44:39 PM4/14/15
to yal...@googlegroups.com
Don't understand why you would want to do that ( fieldnames(struct(controller))). This is precisely what MATLAB does when you save a class object.

Your problem is simply the fact that you shouldn't define an output expression. Instead of save/load with
P = optimizer([x;y]<=a+b,-x-y,[],{a,b},{x^2+y});
...
P{{1,2}}

you should do
P = optimizer([x;y]<=a+b,-x-y,[],{a,b},{x,y});
...
sol = P{{1,2}};
sol
{1}^2+sol{2}





Drecalde

unread,
Apr 15, 2015, 9:44:28 PM4/15/15
to yal...@googlegroups.com
What i want to do is to create the optimizer object.

P = optimizer([x;y]<=a+b,-x-y,[],{a,b},{x^2+y});

store the object "P", the reason to store P is because it takes a long time to create the problem.

I have multiple scenarios I want to compute, the data for the problem is changing but the structure doesnt.

I want to be able to save P and reload it from the hard disk instead of computing the entire model again.

When saving and reloading the model, the fields inside the optimizer object "P" are reinitialized. e.g. matrices set to '[ ]' and scalars to '0'

What i was suggesting is if there is any way of storing the fields in "P"

names=fieldnames(P);

names
=
   
'recover'
   
'model'
   
'dimin'
   
'dimout'
   
'diminOrig'
   
'dimoutOrig'
   
'complexInput'
   
'complexOutput'
   
'mask'
   
'map'
   
'input'
   
'output'
   
'parameters'
   
'nonlinear'
   
'F'
   
'h'
   
'ops'
   
'complicatedEvalMap'

Save these fields as a structure and then restore them when the model is reloaded

Im new to YALMIP and wanted to know if this is possible. Thanks for your work developing this tool and also for answering all the questions.







Johan Löfberg

unread,
Apr 16, 2015, 1:26:09 AM4/16/15
to yal...@googlegroups.com
Still don't understand. Saving fields manually somehow will not help you in any sense with the fact that the optimizer object requires you to use simple output functions if you want to save/load it. You cannot extract the fields, save them, load them, and then get back a suitable object which you can call.

Create an output function which only returns variables, and then create the desired expression after having solved the problem through the optimizer call

P1 = optimizer([x;a*y]<=a+b,-x-y,[],{a,b},{x,y});
P2 = optimizer([b*x;y]<=2*a+b,-2*x-3*y,[],{a,b},{x,y});
save myfile P1 P2
clear all
load myfile
sol1 = P1{{1,2}};
sol2 = P2{{1,2}};
sol
1{1}^2+sol1{2}
sol2{1}^4-sol2{2}^2


Reply all
Reply to author
Forward
0 new messages