Writing and Reading Models and Files

In example ilolpex1.cpp we left one line unexplained:

  cplex.exportModel("lpex1.lp");

This statement causes cplex to write the model it has currently extracted to the file called lpex1.lp. In this case, the file will be written in LP format (whose usage is described in detail in the ILOG CPLEX Reference Manual). Other formats supported for writing problems to a file are MPS and SAV (also described in the ILOG CPLEX Reference Manual). IloCplex decides which file format to write based on the extension of the file name.

IloCplex also supports reading of files through one of its importModel methods. A call to cplex.importModel(model, "file.lp") causes CPLEX to read a problem from the file file.lp and add all the data in it to model as new objects. (Again, MPS and SAV format files are also supported.) In particular, CPLEX creates an instance of

IloObjective 
for the objective function found in file.lp,
IloNumVar 
for each variable found in file.lp, except
IloSemiContVar 
for each semi-continuous or semi-integer variable found in file.lp,
IloRange 
for each row found in file.lp,
IloSOS1 
for each SOS of type 1 found in file.lp, and
IloSOS2 
for each SOS of type 2 found in file.lp.

If you also need access to the modeling objects created by importModel(), two additional signatures are provided:

  void IloCplex::importModel(IloModel& m,
                             const char* filename,
                             IloObjective& obj,
                             IloNumVarArray vars,
                             IloRangeArray  rngs) const;

and

  void IloCplex::importModel(IloModel& m,
                             const char* filename,
                             IloObjective& obj,
                             IloNumVarArray vars,
                             IloRangeArray rngs,
                             IloSOS1Array sos1,
                             IloSOS2Array sos2) const;

They provide additional parameters so that the newly created modeling objects will be returned to the caller. Example program ilolpex2.cpp gives an example of how to use method importModel().


Previous Page: Building and Solving a Small LP Model in C++  Return to Top Next Page: Selecting an Optimizer