Saving & and loading


The most efficient and safe way to save and communicate YALMIP models is to work with the MATLAB script, i.e. the YALMIP code. In some cases however, it might be necessary to use binary or other formats. Additionally, there might be cases where a YALMIP model needs to be converted to a solver specific numerical model.

Loading & saving in binary MATLAB format

The objects in YALMIP all support loading and saving, hence it is possible to save a model in a binary MATLAB format.

A = randn(3);
P = sdpvar(3,3);
F = set(A'*P+P*A < -eye(3));
obj = trace(P);
save mymodel
clear all;
load mymodel

F
+++++++++++++++++++++++++++++++++++++++++++++++++
|   ID|      Constraint|                    Type|
+++++++++++++++++++++++++++++++++++++++++++++++++
|   #1|   Numeric value|   Matrix inequality 3x3|
+++++++++++++++++++++++++++++++++++++++++++++++++
obj
Linear scalar (real, 3 variables)

Note that loading a YALMIP model will destroy any sdpvar and set object in the current workspace. The binary data format is not recommended, and the reason is three-fold. The first reason is incompatibility between different MATLAB version, the second reason is possible incompatibility between different YALMIP versions, and finally that the binary format in YALMIP currently is inefficient for large-scale problems.

Loading & saving in SDPA sparse ASCII format

The leading format to communicate standard linear SDP problems in a platform and application independent way is the sparse SDPA format. YALMIP can both save and load models in this format.

A = randn(3);
P = sdpvar(3,3);
F = set(A'*P+P*A < -eye(3));
obj = trace(P);
savesdpafile(F,obj,'mymodel.dat-s');
clear all;
[F,obj] = loadsdpafile('mymodel.dat-s');

F
+++++++++++++++++++++++++++++++++++++++++++++++++
|   ID|      Constraint|                    Type|
+++++++++++++++++++++++++++++++++++++++++++++++++
|   #1|   Numeric value|   Matrix inequality 3x3|
+++++++++++++++++++++++++++++++++++++++++++++++++
obj
Linear scalar (real, 3 variables)

The SDPA format is limited to standard linear SDP problems (without equality constraints), hence many YALMIP models cannot be saved in this format.

Exporting solver specific models

YALMIP can be used to extract the numerical model in various solver specific formats. This can be done along during a call to the solver, but also without explicitly invoking the solver. To export the solver while solving the problem, we use the savesolverinput option.
A = randn(3);
P = sdpvar(3,3);
F = set(A'*P+P*A < -eye(3));
obj = trace(P);
sol = solvesdp(F,obj,sdpsettings('solver','sedumi','savesolverinput',1));
sol.solverinput
ans = 
       A: [9x6 double]
       c: [9x1 double]
       b: [6x1 double]
       K: [1x1 struct]
    pars: [1x1 struct]

To export the model with actually calling the solver, use export.

A = randn(3);
P = sdpvar(3,3);
F = set(A'*P+P*A < -eye(3));
obj = trace(P);
mdl = export(F,obj,sdpsettings('solver','sedumi'));
mdl

All solvers are currently not supported in export. If you miss support for some solver, please make a feature request.

Saving AMPL models

A rudimentary support for exporting models in AMPL format is available. This functionality is mainly intended for small nonlinear or mixed integer programs, and was only implemented in order to save one particular problem. If you are interested in a better support, make a feature request.
sdpvar a b c
F = set(a+b^2 < c*pi) + set(integer(b)) + set(a+b==3);
obj = a*b*c;
saveampl(F,obj,'mymodel.mod')
type mymodel
var x {1..2};
var z {1..1} integer ;
minimize obj:  x[1]*z[1]*x[2];
subject to constr1: 0 <= -x[1]+x[2]-z[1]^2;
subject to constr2: 0 == 3-x[1]-z[1];
solve;
display x;
display z;
display obj;