Re: [Gurobi] writing & reading lp file from Matlab

1,506 views
Skip to first unread message

Christopher Maes

unread,
Feb 18, 2013, 10:11:06 AM2/18/13
to gur...@googlegroups.com
> I am looking a command how to read and write lp problem, MipSart file from
> Matlab. anyone can help me please?

Hi Sothea,

You can write an LP file from MATLAB by setting the parameter
ResultFile=myfilename.lp. You can load a MIP start into MATLAB by
specifying a vector for model.start. For example, suppose you solve a
MIP and want to use its solution to specify the start for another
model. Do:

results = gurobi(model1);
model2.start = results.x;

The start attribute is described in detail in the MATLAB documentation:
http://www.gurobi.com/documentation/5.1/reference-manual/node656#sec:MATLAB
See the section titled: "The optimization model." Or type help gurobi
from the MATLAB prompt.

You can also write out a MIP start file by specifying the parameter
ResultFile=mymipstart.mst.
You can read a MIP start file into MATLAB with pure MATLAB code by doing:
fid = fopen('mystart.mst', 'r');
C = textscan(fid, '%s %f', 'HeaderLines', 1);
varnames = C{1};
varvalues = C{2};
fclose(fid);

There is no mechanism for reading a .lp file into MATLAB. If you can
explain what you are trying to accomplish by reading in a .lp file
into MATLAB I might be able to propose a workaround.

Thanks,
Chris

Christopher Maes

unread,
Feb 18, 2013, 12:20:34 PM2/18/13
to gur...@googlegroups.com
> Thanks for your helpful answer. For the reason i want to read lp file into
> Matlab, in fact i want to use lp file generated from Cplex which containt
> the real varaible name, for example teta(i,n), alpha(i,n),... so i can get
> the solution with thses variable names. if i use the matrix formulation, i
> get the solution with C(i) names. It is hard to compare solution with Cplex
> solution. For another reason, i want to run Gurobi-cplex. i want first to
> run Gurobi and then to run Cplex with MipStart from Gurobi. I wonder that
> Cplex perform better solution than Gurobi. But Gurobi find feasible solution
> faster than Cplex.
>
> If you can propose the method how to do it would be great for me.

Is there a reason why you need to use the MATLAB interface? What you
want to do can easily be accomplished with the command line
(gurobi_cl) or Python interface.

For example, to solve a model stored in .lp format and write out a MIP
start using gurobi_cl do:
gurobi_cl ResultFile=mystart.mst mymodel.lp

In Python this is:
from gurobipy import *
model = read('mymodel.lp')
model.optimize()
model.write('mystart.mst')

Chris

Christopher Maes

unread,
Feb 18, 2013, 12:57:52 PM2/18/13
to gur...@googlegroups.com
> Thanks again for your help. I also know how to run from command line but if
> i do it i have to do two time where 1 for running Gurobi and another for
> Cplex. That is why i would like to run the both from Matlab.

Sorry, I must still be missing something. If you are solving both
problems from within MATLAB, can't you convert them inside of MATLAB?
Why do you need to use a .lp file as a go between?

Christopher Maes

unread,
Feb 18, 2013, 4:13:31 PM2/18/13
to gur...@googlegroups.com
> ok it is not clear from me. The problem is variable name. As i already named
> them via cplex method. my lp fille contain variable name, teta(i,n), etc.
> that is why i want to keep them and need Gurobi do the same. If not i can
> work around others ways as you show above.

My guess is that you should be able to do what you want by writing a
program that maintains a mapping between variables names and variable
indices in MATLAB. But we will definitely consider adding support for
variable and constraint names in a future version of the MATLAB
interface.

Thanks,
Chris

sothea hong

unread,
Feb 18, 2013, 4:22:23 PM2/18/13
to gur...@googlegroups.com
Thanks Chris,

exactly what i mean. I am waiting for it.


--

---
You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Christopher Maes

unread,
Feb 19, 2013, 10:37:41 AM2/19/13
to gur...@googlegroups.com
> Hi Chris,
>
> I cannot write lp and mst file at same time. if i define code like this
>
> params.ResultFile='myfilename.lp';
> params.ResultFile='mymipstart.mst';
>
> i get only mymipstart.mst file.
>
> It is normal?

Yes it's normal: you overwrote the value for the ResultFile parameter.
>> params.ResultFile = 'myfilename.lp'

params =

ResultFile: 'myfilename.lp'

>> params.ResultFile = 'mymipstart.mst'

params =

ResultFile: 'mymipstart.mst'


If you need to output both the lp file and the MIP start, you'll need
to call gurobi twice. The ResultFile is written at the end of the
solve, so you might want to set a small time limit when writing the
.lp file.

clear params;
params.TimeLimit = 10;
params.ResultFile = 'mymodel.lp';
gurobi(model, params);
clear params;
params.ResultFile = 'mystart.mst';
results = gurobi(model, params);

Please take a look at the MATLAB examples and documentation:
http://www.gurobi.com/documentation/5.1/example-tour/node155
http://www.gurobi.com/documentation/5.1/reference-manual/node656#sec:MATLAB

Chris
Reply all
Reply to author
Forward
0 new messages