External Language Interfaces To facilitate the development of other model language interfaces, a
quite simple "eXternal Language Interface" (XLI) has been defined, which allows lp_solve
to be dynamically configured (at run-time) to use alternative means to read and
write the MIP model. lp_solve has several build-in interfaces to models: mps, lp,
CPLEX lp. XLI allows implementing a custom reader and writer for other model layouts.
Under Windows, an XLI is provided as DLL (xli_*.dll), under UNIX/LINUX as a dynamic linked
library (libxli_*.so). Under Windows, the following search order is used:
Under Unix/Linux, following search order is used:
Under Unix/Linux it is standard that a library has the lib prefix and a .so postfix.
Under Windows there is no prefix and a .dll postfix.
lp_solve -rxli xli_MathProg input.mod The latter makes sure that the XLI is searched in the current directory, especially for Unix/Linux. With the lp_solve program, to set the XLI to write the model, use the option -wxli xliname filename. Via the API, use set_XLI to set the XLI library and write_XLI to write the model. xliname is the name of the dynamic linked library. It is possible to provide a path to this name to be sure that the XLI from the specified location is used. If no path is given, then it depends on the OS where it will be searched.Under Windows, the following search order is used:
Under Unix/Linux, following search order is used:
Under Unix/Linux it is standard that a library has the lib prefix and a .so postfix.
Under Windows there is no prefix and a .dll postfix.
lp_solve -wxli xli_MathProg output.mod The latter makes sure that the XLI is searched in the current directory, especially for Unix/Linux. Creating an XLITo create your own XLI, you have to create a DLL/shared library that implements the following routines:
lp_XLI1.c must be included at the beginning of the source file. This to include an extra routine xli_compatible needed by the lpsolve library to check the compatibility. If you want to create XLIs yourself, make sure that under Windows,
you use 8 byte alignments. This is needed for the XLIs to work correctly with the general
distribution of lp_solve and also to make sharing XLIs as uncomplicated as possible. If not, it will likely crash.
It doesn't matter which calling convention is used to compile the library. The XLI_CALLMODEL directive
makes sure that the calling convention of the needed routines is always ok independent of the
calling convention specified in the project. EXPORTS xli_compatible @1 xli_name @2 xli_readmodel @3 xli_writemodel @4
The definition file must be added to the project. How to do this depends on the version. XLI prototype/* Generic include libraries */ #include <malloc.h> #include <string.h> #include "lp_lib.h" #ifdef FORTIFY # include "lp_fortify.h" #endif /* Include routines common to language interface implementations */ #include "lp_XLI1.c" char * XLI_CALLMODEL xli_name(void) { return("XLI_xxx v1.0" ); /* return the name and version */ } MYBOOL XLI_CALLMODEL xli_readmodel(lprec *lp, char *model, char *data, char *options, int verbose) { MYBOOL status = FALSE; /* implement the code here to read the model */ return(status); /* status says if the model could be read or not. TRUE is ok, FALSE is not ok */ } MYBOOL XLI_CALLMODEL xli_writemodel(lprec *lp, char *filename, char *options, MYBOOL results) { MYBOOL status = FALSE; /* implement the code here to write the model */ return( status ); /* status says if the model could be written or not. TRUE is ok, FALSE is not ok */ } Working example:demo.c:/* Modularized external language interface module - w/interface for lp_solve v5.0+ ---------------------------------------------------------------------------------- Author: Peter Notebaert Contact: lpsolve@peno.be License terms: LGPL. Template used: Requires: Release notes: v1.0.0 28 June 2004 First implementation. ---------------------------------------------------------------------------------- */ /* Generic include libraries */ #include <malloc.h> #include <string.h> #include "lp_lib.h" /* Include libraries for this language system */ #include <math.h> #ifdef FORTIFY # include "lp_fortify.h" #endif /* Include routines common to language interface implementations */ #include "lp_XLI1.c" MYBOOL XLI_CALLMODEL xli_name(void) { return( "xli_demo v1.0" ); } MYBOOL XLI_CALLMODEL xli_readmodel(lprec *lp, char *model, char *data, char *options, int verbose) { MYBOOL status = FALSE; REAL row[1+2]; /* must be 1 more then number of columns ! */ lp->add_columnex(lp, 0, NULL, NULL); /* add empty column */ lp->add_columnex(lp, 0, NULL, NULL); /* add empty column */ lp->set_add_rowmode(lp, TRUE); row[1] = 1.0; row[2] = 2.0; lp->add_constraint(lp, row, GE, 3.0); /* constructs the row: +v_1 +2 v_2 >= 3 */ lp->set_add_rowmode(lp, FALSE); status = TRUE; return(status); } MYBOOL XLI_CALLMODEL xli_writemodel(lprec *lp, char *filename, char *options, MYBOOL results) { return(lp->write_lp(lp, filename)); }demo.def: EXPORTS xli_compatible @1 xli_name @2 xli_readmodel @3 xli_writemodel @4 This example is not very practical. It is only useful for demonstration purposes.
Let's assume that the name of this library is xli_demo. This XLI can be called from lp_solve via the following syntax: lp_solve -rxli xli_demo "" -wxli xli_demo output.txt An empty ("") filename is provided because it isn't used by this demo. This command will create a file output.txt with contents: /* Objective function */ min: ; /* Constraints */ +C1 +2 C2 >= 3; /* Variable bounds */ Because we didn't provide the option -parse_only, the model is also solved and the result is shown on screen: Value of objective function: 0 Actual values of the variables: C1 3 C2 0 |