lp_solve Frequently Asked Questions

- Where can I find the latest version of lp_solve?
There is a yahoo group (http://groups.yahoo.com/group/lp_solve/) that bundles all activities of the lp_solve community. You have to register but this is for free. You will find there the latest sources, examples and manuals.

- Can I use lp_solve in commercial code?
Yes you can. However read the GNU LESSER GENERAL PUBLIC LICENSE which you can find in LGPL.txt

- How should lp_solve be cited when I use it?

lpsolve citation data
Description     : Open source (Mixed-Integer) Linear Programming system
Language        : Multi-platform, pure ANSI C / POSIX source code, Lex/Yacc based parsing
Official name   : lp_solve (alternatively lpsolve)
Release data    : Version dated 1 May 2004
Co-developers   : Michel Berkelaar, Kjell Eikland, Peter Notebaert
Licence terms   : GNU LGPL (Lesser General Public Licence)
Citation policy : General references as per LGPL
                  Module specific references as specified therein
This text is also in citations.txt

- What are the default bounds on variables?
As all (at least, simplex) lp solvers, the default lower bound on a variable is 0 and the default upper bound is unlimited.

- Is it possible to set negative bounds on variables?
Yes, this is perfectly possible.

- Is it possible to set a minus infinite lower bound on a variable?
Yes, this is possible. In the lp format, use as lower bound -Inf. In the mps format, use -1e30
If you use the API interface, use get_infinite(lp)
Note that lp_solve splits this variable in 2 if there is a positive or no upper bound. This is done automatically by lp_solve. Because of this, the model becomes larger and thus can take some more time to solve, so only put a minus infinity lower bound on a variable when it is needed.

- What is the layout of the lp/mps/CPLEX format?
See the help file

- lp_solve fails to solve my model. What can I do?
lp_solve has a lot of options. See lp_solve -h
Especially consider to use scaling via the -s options

- Can lp_solve handle non-linear equations?
No, lp_solve can only handle linear constraints. However it also supports integer and semi-continuous variables and special ordered sets. This can be a help to simulate non-linearity.

- Can lp_solve handle ratios?
Ratios can be converted to linear equations. See the help file.

- lp_solve prints the output on screen. Can I have the output also in a file?
Yes, you can use standard redirection for that. Use the > before the filename to do that.
For example:
lp_solve input.lp >output.txt

- I have a constraint saying that x < 2, but lp_solve comes with a solution of 2, how comes?
< is the same as as <=
also > is the same as >=
This makes more than sense. Mathematically, 1.99999999999999999999999999999999999999999999999999 is smaller than 2, but what is the practical difference with 2? You always have to consider rounding errors also... If the variable is defined as integer then just say x <= 1

- When I solve my model, I get a stack overflow error/core dump/protection error. What is wrong?
If your model has integer/binary/semi-cont/SOS variables then version 5 can solve your problem. In version 4, the implemented branch-and-bound algorithm puts its branches on the stack and that can result in a stack overflow when it is a tough model. Version 5 doesn't put the branches on the stack anymore and because of that, this error is solved.

- Version 4 solves my models slower than version 3 did. What can I do?
Some models are indeed slower with version 4. Version 5 has several enhancements and is faster and more stable. Also version 4 always calculates the sensitivity information while older versions did not have this feature. This also takes some time. Version 5 only calculates the sensitivity when this information is specifically asked that makes it again faster.

- It takes a long time to build the model via the API interface. Especially add_constraint, add_constraintex, str_add_constraint seems to be slow. How can it be made faster?
Version 5 has a new API call set_add_rowmode that makes add_constraint, str_add_constraint spectacular faster.
If the model is build column by column, then it is strongly suggested to use add_columnex instead of add_column because add_columnex gives the possibility to only supply the non-zero elements and that speeds up building the model considerably, especially if the matrix is sparse (a lot of zero elements).
If the model is build row by row, then it is strongly suggested to use add_constraintex instead of add_constraint because add_constraintex gives the possibility to only supply the non-zero elements and that speeds up building the model considerably, especially if the matrix is sparse (a lot of zero elements).

- Can lp_solve give me the 'simplex tableau' of the model?
Version 5 has a new routine (print_tableau) to give you this information.

- Is there documentation about the API interface?
Yes, there are html (help) files. See the Files section.

- What is the maximum number of rows/columns that lp_solve can handle?
There is no fixed limit. Only available memory is a limit.

- Can lp_solve be called from .NET?
Yes it can. There is a VB.NET and a C# example in the Files section.

- Can lp_solve be called from Delphi?
Yes it can. There is a Delphi library the Files section. Thanks to Butch Dulaney for providing this module.

- Can lp_solve be called from Java?
Yes it can. There is a Java wrapper library called lpsolve4j in the files section that enables calls to lp_solve via the Java Native Interface (JNI) mechanism. Thanks to Juergen Ebert for providing this library.

- I use a programming language that is not listed as supported or for which there are no examples. Can lp_solve be called from this language?
If this language allows calling functions from a C-dll, then it is possible. It is not because there is no example provided (yet) that it is not possible. The lp_solve community is always interested in your interface to call the lp_solve library. It doesn't have to be perfect.

- The Windows examples don't work. I get an error running them. Some dll cannot be found.
The examples call a dll that contains the lp_solve routines. This dll is called lpsolve.dll and must be on the system either in the directory of the application or somewhere in the path.
This dll is contained in the windows binaries archive in the files section.

- Does lp_solve supports the mps free format?
No, not at this time. There is however a utility LPx2D that allows to convert this format. See the Files section.

- Does the lp_solve lp format support comments?
Yes it does. Put the comments between /* */
It can be anywhere in the text, even over multiple lines. Just like in C
lp_solve and newer also supports the C++ line comment //

- I want to compile lp_solve myself, but I get link errors. Also what should I do with lp.y, lex.l, lp_rlp.y, lp_rlp.l, lp_rlpt.y, lp_rlpt.l?
The .y files are yacc files. They can be translated to C via the bison or yacc commands.
The .l files are lex files. They can be translated to C via the flex or lex commands.
lp.y/lex.l were used in version 4. Version 5 use the names lp_rlp.y/lp_rlp.l and lp_rlpt.y/lp_rlpt.l All unix environments have these commands. There are also windows versions available.
For example at http://unxutils.sourceforge.net/
Also only include lp.c in your compile project, not lex.c. lex.c is automatically #included by lp.c
Version 5 doesn't use the .c extension for the translated lex file. Instead it uses the .h extension. That way there is no confusion. So you don't have to (and may not) include it yourself again in the project.
To compile lp_solve, you must use make or gmake and a Makefile. There are several example makefiles in the source included like Makefile.linux, Makefile.msc. Enter make or gmake -f makefile. Note that for WINDOWS, you must use gnu make because MS make hasn't enough functionality. You can find gmake at http://unxutils.sourceforge.net/

- I compile lp_solve myself, but I get link errors saying that main (or _main) is already defined.
demo.c, mps2lp.c, lp2mps.c, lp_solve.c may never be in the same project. You must use one of these with the other C source files (except lex.c, see above) to make either demo.exe, mps2lp.exe, lp2mps.exe, lp_solve.exe

- I want to use the lpsolve dll, but when I compile my C/C++ program, I get link errors. He doesn't find any of the lpsolve API routines. What am I doing wrong?
Each function that you use in a C/C++ application must be defined. The compiler gives a warning if he doesn't find a definition of a function and the linker gives an error if he doesn't find an implementation for each used function. The implementation of a function can be in your source code, in another source file or in a library. If the function is in the same source code and defined before it is called, then you get no compiler warning because it is declared implicitly. If the function is after you call it, in another source file or in a library, then your source code must have a definition of this function or the compiler will give a warning. This is because the compiler wants to check if the function is correctly called (correct number of arguments, correct return code, should there be done some implicit conversion, ...). It is common to define these declarations in a header (.h) file. That is the reason why you include stdio.h, stdlib.h, string.h, windows.h and so on. For lpsolve, this library is lp_lib.h (lpkit.h in version 4 and before). Because lp_lib.h and lpkit.h also include other header files, you need these also.  Including these source files do not guarantee that the linker will not give an error. This because the linker not only wants the definition of the functions, it also needs the implementation of it. So all routine calls that you do must be found somewhere by the linker. These can be in one of your source files and if they are not static defined then it is ok for the linker, or they are in a library. For example the function printf is a function from a library. This library can be linked statically or dynamically with your program. In Visual C, for the standard libraries this is defined via one of the compiler options /MT, /MD, /ML (in .NET via project, properties, C/C++, Code generation, Runtime Library. In VC6 via Project, settings, C/C++, category code generation, use runtime library). This results in using one of the libraries LIBC*.lib or MSVCRT*.lib. If you use static linking, then these routines are linked at link time with your program so that this program runs without the need of any other file. So these routines are then in LIBC.lib (the static libraries). If you use dynamic linking (MSVCRT.lib), then this library contains minimal code (not the implementation of this function) to call a library (a dll) at runtime. For example MSVCRT.DLL. So this dll must be on the system when you *run* the application. You will see that the resulting exe is smaller when dynamically linked compared to statically linked. This is logical because the implementation of the routine is in the DLL. The advantage of a DLL is that you can have a new implementation of it and use it in your application without having to rebuild your application. This because the DLL is linked at runtime with your program. Off course, the interface should stay the same for that. Also see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_c_run.2d.time_libraries.asp for an explanation on the VC runtime libraries.
Now besides the standard libraries there are also additional libraries. For example odbc32.lib is one that provides an API to access ODBC, or ws2_32.lib that contains the winsock/TCP/IP API calls and so on. These are all dynamic linked libraries, so the implementation is in a DLL. You must specify the library to the linker so that he knows how to call the implemented DLL (how it is called, ...).

Now if you want to use the lpsolve dll, you must also specify the lpsolve library to the linker so that you don't get the link errors. For version 5, this library is called lpsolve5.lib. You must specify it via Project, Properties, Linker, Input, Additional Dependencies (.NET) or Project, Settings, Link, Category General, Object Library Modules (VC6). There you add lpsolve5.lib (if there are already other libraries, just add at the end, separated via a space). You can specify the path where this file is located or you can specify via Project, Properies, Linker, General, Additional Library Directories (.NET) or Project, Settings, Link, Category Input, Additional library path, the paths where libraries can be found (separated by ,) . Basically that is it.

There is one additional thing.

- lp.c and lex.c that are generated on my system are different from the versions that can be found in the support folder in the files section. Are the latest versions of these files on your site?
Depending on the platform, the command used and the version of the command, these files are indeed different. That is not a problem. You can compare this with a compiler. The same source code generates totally different binaries, depending on the compiler used and its version, but the programs react the same (at least, they should ...).

- When I start the lp_solve program, nothing happens. I just get a blinking cursor. If I enter a command like lp1 = make_lp(0,4), I get a parse error. What is wrong here? How do I use the program?
lp_solve is a library with a set of routines with purpose to solve a MIP model. These routines are sometimes refered as the API (Application Programming Interface). These routines can be used in a C-program to solve MIP models. This is for example demonstrated in the demo program. Under Windows there is also a dll available that can be used by other programming languages to call the API functions. There are demos in VB and .NET available, but the principle is the same. The lp_solve program is basically another 'demo' program of this api. This program reads the MIP model from standard input or from a file and outputs the result back to the console or to a file. There are two possible input formats: the 'lp' format and the 'mps' format. This has nothing to do with the API. So lp1 = make_lp(0,4) is a totally wrong input for the lp_solve program. The lp-format is a 'readable' format of a MIP model.
For example:

max: -x1 + 2 x2;
C1: 2x1 + x2 <= 5;
-4 x1 + 4 x2 <= 5;

int x2,x1;

This lp-format is the default format of the lp_solve program.

The other format is mps format. This is a totally different format and used by many MIP solvers. This format is not very readable for us humans. The same example as above in mps format looks like this:

 N  r_0
 L  C1
 L  r_2
    MARK0000  'MARKER'                 'INTORG'
    x1        r_0                  1   C1                   2
    x1        r_2                 -4
    x2        r_0                 -2   C1                   1
    x2        r_2                  4
    MARK0001  'MARKER'                 'INTEND'
    RHS       C1                   5   r_2                  5

To enable this input format in the lp_solve program, use the -mps option.

The lp_solve program has many other options to control many other things like the amount of data to output, if scaling must be used and so on.

There are two other 'demo' programs: lp2mps and mps2lp. They convert model files from one format to the other.

The lp_solve, lp2mps and mps2lp programs all use API calls of the lp_solve library to perform their actions.

If lp_solve is started without an input file, then it gives a blinking cursor and waits for input. It is now possible to enter the model in the specified format (lp by default). But this is not very handy. You must enter the whole model each time you want to solve it and must be careful that you don't type any errors. To stop input mode, press Ctrl Z.
However it is much better to write your model in a disk file with your favourite editor (make sure it is in text format) and then provide this file to lp_solve. For example:

lp_solve input.lp

It is also possible to use input redirection:

lp_solve <input.lp

Or more sophisticated constructs like:

gen_model | lp_solve

gen_model is here a user written program that outputs the model to standard output and via the pipe character (|) this output is redirected to lp_solve. So no intermediate file is needed in this case.