All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
How to add file readers Mainly, file readers are called to parse an input file and generate a constraint integer programming model. They create constraints and variables and activate variable pricers if necessary. However, they can also be called, for example, to parse an input file containing information about a primal solution or fixing of variables. Besides that it is possible to use some of them for writing (exporting) the problem in a specific format. Since a file reader is also responsible for writing a file, the user may ask why the readers have not the name "filehandler". This name would represent this plugin much better than the used one. We now explain how users can add their own file readers. Take the file reader for MIPs in IBM's Mathematical Programming System format (src/scip/reader_mps.c) as an example. As all other default plugins, it is written in C. C++ users can easily adapt the code by using the scip::ObjReader wrapper base class and implement the scip_...() virtual methods instead of the SCIP_DECL_READER... callback methods. Additional documentation for the callback methods of a file reader can be found in the file type_reader.h. Here is what you have to do to implement a file reader named "myreader" in C:
Properties of a File ReaderAt the top of the new file "reader_myreader.c" you can find the file reader properties. These are given as compiler defines. In the C++ wrapper class, you have to provide the file reader properties by calling the constructor of the abstract base class scip::ObjReader from within your constructor. The properties you have to set have the following meaning:
File Reader DataBelow the header "Data structures" you can find a struct which is called "struct SCIP_ReaderData". In this data structure, you can store the data of your file reader. For example, you should store the adjustable parameters of the file reader in this data structure. If you are using C++, you can add file reader data as usual as object variables to your class. Interface MethodsAt the bottom of "reader_myreader.c", you can find the interface method SCIPincludeReaderMyreader(), which also appears in "reader_myreader.h". SCIPincludeReaderMyreader() is called by the user, if (s)he wants to include the reader, i.e., if (s)he wants to use the reader in his/her application. This method only has to be adjusted slightly. It is responsible for notifying SCIP of the presence of the reader. For this, you can either call SCIPincludeReader(), or SCIPincludeReaderBasic() since SCIP version 3.0. In the latter variant, additional callbacks must be added via setter functions as, e.g., SCIPsetReaderCopy(). We recommend this latter variant because it is more stable towards future SCIP versions which might have more callbacks, whereas source code using the first variant must be manually adjusted with every SCIP release containing new callbacks for readers in order to compile. If you are using file reader data, you have to allocate the memory for the data at this point. You can do this by calling: SCIP_CALL( SCIPallocMemory(scip, &readerdata) );
You also have to initialize the fields in struct SCIP_ReaderData afterwards. You may also add user parameters for your file reader, see the method SCIPincludeReaderLp() in src/scip/reader_lp.c for an example. Fundamental Callback Methods of a File ReaderFile reader plugins have no fundamental callback methods. This is due to the fact that a file reader can be used for reading and/or writing a file. A file reader is only useful if the reader method READERREAD and/or the writing method READERWRITE is implemented. One of these methods should be implemented for every file reader; the other callback methods READERCOPY and READERFREE are optional. In the C++ wrapper class scip::ObjReader, the scip_read() and scip_write() methods (which corresponds to the READERREAD and READERWRITE callback) are virtual member functions. At least one of them should be implemented. Additional documentation for the callback methods can be found in type_reader.h. Additional Callback Methods of a File ReaderAdditional callbacks can either be passed directly with SCIPincludeReader() to SCIP or via specific setter functions after a call of SCIPincludeReaderBasic(), see also Interface Methods. File reader plugins contain only additional callback methods, namely the methods READERREAD, READERWRITE, READERFREE, and READERCOPY. Therefore, these are not needed to be implemented. However, at least READERREAD and/or READERWRITE should be implemented (see notes above). READERREADThe READERREAD callback is called when the user invokes SCIP to read in a file with file name extension corresponding to the READER_EXTENSION property of the file reader. This is usually triggered by a call to the method SCIPreadProb() or by an interactive shell command. The READERREAD callback should parse the input file and perform the desired action, which usually means generating a constraint integer programming model, adding a primal solution, fixing variables in an existing model.
Primal solutions can only be created for the transformed problem. Therefore, the user has to call SCIPtransformProb() before (s)he reads in the file containing the solution and adds it to the solution pool via the method SCIPreadSol(). READERWRITEThe READERWRITE callback is called when the user invokes SCIP to write a problem (original or transformed) in the format the reader supports. This is only possible if this callback is implemented. To write the problem all necessary information is given through the parameters of this callback method (see type_reader.h). This information should be used to output the problem in the requested format. This callback method is usually triggered by the call of the methods SCIPwriteOrigProblem(), SCIPwriteTransProblem(), SCIPprintOrigProblem(), or SCIPprintTransProblem(). READERCOPYThe READERCOPY callback is executed when a SCIP instance is copied, e.g. to solve a sub-SCIP. By defining this callback as static
SCIP_DECL_READERCOPY(readerCopyMyreader)
{
assert(scip != NULL);
assert(reader != NULL);
assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
SCIP_CALL( SCIPincludeReaderMyreader(scip) );
return SCIP_OKAY;
}
READERFREEIf you are using file reader data, you have to implement this method in order to free the file reader data. This can be done by the following procedure: static
SCIP_DECL_READERFREE(readerFreeMyreader)
{
SCIP_READERDATA* readerdata;
readerdata = SCIPreaderGetData(reader);
assert(readerdata != NULL);
SCIPfreeMemory(scip, &readerdata);
SCIPreaderSetData(reader, NULL);
return SCIP_OKAY;
}
If you have allocated memory for fields in your file reader data, remember to free this memory before freeing the file reader data itself. If you are using the C++ wrapper class, this method is not available. Instead, just use the destructor of your class to free the member variables of your class. |