Writing and reading models and files

CPLEX supports reading models from files and writing models to files in a C++ application.

In example ilolpex1.cpp , one line is still 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. (That format is documented in the reference manual CPLEX File Formats.) Other formats supported for writing problems to a file are MPS and SAV (also documented in the reference manual CPLEX File Formats). 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 importModel 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

Modeling classes Description
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 arguments so that the newly created modeling objects will be returned to the caller. Example program ilolpex2.cpp shows how to use the method importModel.