Using lpsolve from MATLAB

MATLAB?

MATLAB® is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation. Typical uses include

MATLAB is an interactive system whose basic data element is an array that does not require dimensioning. This allows you to solve many technical computing problems, especially those with matrix and vector formulations, in a fraction of the time it would take to write a program in a scalar non-interactive language such as C or Fortran.

The name MATLAB stands for matrix laboratory. MATLAB was originally written to provide easy access to matrix software developed by the LINPACK and EISPACK projects. Today, MATLAB engines incorporate the LAPACK and BLAS libraries, embedding the state of the art in software for matrix computation.

MATLAB has evolved over a period of years with input from many users. In university environments, it is the standard instructional tool for introductory and advanced courses in mathematics, engineering, and science. In industry, MATLAB is the tool of choice for high-productivity research, development, and analysis.

MATLAB features a family of add-on application-specific solutions called toolboxes. Very important to most users of MATLAB, toolboxes allow you to learn and apply specialized technology. Toolboxes are comprehensive collections of MATLAB functions (M-files) that extend the MATLAB environment to solve particular classes of problems. Areas in which toolboxes are available include signal processing, control systems, neural networks, fuzzy logic, wavelets, simulation, and many others.

We will not discuss the specifics of MATLAB here but instead refer the reader to the MATLAB website and documentation.

MATLAB and lpsolve

lpsolve is callable from MATLAB via an external interface or MEX-function. As such, it looks like lpsolve is fully integrated with MATLAB. Matrices can directly be transferred between MATLAB and lpsolve in both directions. The complete interface is written in C so it has maximum performance. The whole lpsolve API is implemented with some extra's specific for MATLAB (especially for matrix support). So you have full control to the complete lpsolve functionality via the mxlpsolve MATLAB driver. If you find that this involves too much work to solve an lp model then you can also work via higher-level M-files that can make things a lot easier. See further in this article.

MATLAB is ideally suited to handle linear programming problems. These are problems in which you have a quantity, depending linearly on several variables, that you want to maximize or minimize subject to several constraints that are expressed as linear inequalities in the same variables. If the number of variables and the number of constraints are small, then there are numerous mathematical techniques for solving a linear programming problem. Indeed these techniques are often taught in high school or university level courses in finite mathematics. But sometimes these numbers are high, or even if low, the constants in the linear inequalities or the object expression for the quantity to be optimized may be numerically complicated in which case a software package like MATLAB is required to effect a solution.

Installation

To make this possible, a driver program is needed: mxlpsolve (mxlpsolve.dll or mxlpsolve.mexw32 or mxlpsolve.mexw64 under Windows). This driver must be put in a directory known to MATLAB (specified via File, Set Path or via the MATLAB path command) and MATLAB can call the mxlpsolve solver.
Note that under Windows, the original extension was .dll. Newer versions of MATLAB require extension .mexw32. You can just rename the .dll to .mexw32 for that.

This driver calls lpsolve via the lpsolve shared library (lpsolve55.dll under Windows and liblpsolve55.so under Unix/Linux) (archive lp_solve_5.5.2.11_dev.zip/lp_solve_5.5.2.11_dev.tar.gz). This has the advantage that the mxlpsolve driver doesn't have to be recompiled when an update of lpsolve is provided. The shared library must be somewhere in the Windows path.

So note the difference between the MATLAB lpsolve driver that is called mxlpsolve and the lpsolve library that implements the API that is called lpsolve55.

There are also some MATLAB script files (.m) as a quick start.

To test if everything is installed correctly, enter mxlpsolve in the MATLAB command window. If it gives the following, then everything is ok:

mxlpsolve MATLAB Interface version 5.5.0.6
using lpsolve version 5.5.2.11

Usage: [ret1, ret2, ...] = mxlpsolve('functionname', arg1, arg2, ...)

However, if you get the following:

mxlpsolve driver not found !!!
Check if mxlpsolve.dll (or mxlpsolve.mexw32 or mxlpsolve.mexw64) is on your system and in a directory known to MATLAB.
Press enter to see the paths where MATLAB looks for the driver.

Then MATLAB can find the mxlpsolve.m file, but not the mxlpsolve.dll, mxlpsolve.mexw32 or mxlpsolve.mexw64 file. This dll should be in the same directory as the .m file.
Note that under Windows, the original extension was .dll. Newer versions of MATLAB require extension .mexw32. You can just rename the .dll to .mexw32 for that.

If you get the following:

??? Undefined function or variable 'mxlpsolve'.

Then MATLAB cannot find the mxlpsolve.* files. Enter path in the command line to see the MATLAB search path for its files. You can modify this path via File, Set Path. Specify the path where the mxlpsolve.* files are located on your system.

If you get the following (Windows):

??? Failed to initialise lpsolve library. Error

in == > ...\mxlpsolve.dll

Or (Unix/Linux):

liblpsolve55.so: cannot open shared object file: No such file or directory.

Then MATLAB can find the mxlpsolve driver program, but the driver program cannot find the lpsolve library that contains the lpsolve implementation. This library is called lpsolve55.dll under Windows and liblpsolve55.so under Unix/Linux.
Under Windows, the lpsolve55.dll file must be in a directory that in the PATH environment variable. This path can be shown via the following command in MATLAB: !PATH
It is common to place this in the WINDOWS\system32 folder.

Under Unix/Linux, the liblpsolve55.so shared library must be either in the directories /lib or /usr/lib or in a directory specified by the LD_LIBRARY_PATH environment variable.

Note that it may also be necessary to restart MATLAB after having put the files in the specified directory. It was noted that MATLAB sometimes doesn't see the newly added files in folders until it is restarted.

All this is developed and tested with MATLAB version 6.0.0.88 Release 12.

Solve an lp model from MATLAB via mxlpsolve

In the following text, >> before the MATLAB commands is the MATLAB prompt. Only the text after >> must be entered.

To call an lpsolve function, the following syntax must be used:

>> [ret1, ret2, ...] = mxlpsolve('functionname', arg1, arg2, ...)

The return values are optional and depend on the function called. functionname must always be enclosed between single quotes to make it alphanumerical and it is case sensitive. The number and type of arguments depend on the function called. Some functions even have a variable number of arguments and a different behaviour occurs depending on the type of the argument. functionname can be (almost) any of the lpsolve API routines (see lp_solve API reference) plus some extra MATLAB specific functions. Most of the lpsolve API routines use or return an lprec structure. To make things more robust in MATLAB, this structure is replaced by a handle or the model name. The lprec structures are maintained internally by the lpsolve driver. The handle is an incrementing number starting from 0. Starting from driver version 5.5.0.2 on, it is also possible to use the model name instead of the handle. This can of course only be done if a name is given to the model. This is done via lpsolve routine set_lp_name or by specifying the model name in routine read_lp. See Using model name instead of handle.

Almost all callable functions can be found in the lp_solve API reference. Some are exactly as described in the reference guide, others have a slightly different syntax to make maximum use of the MATLAB functionality. For example make_lp is used identical as described. But get_variables is slightly different. In the API reference, this function has two arguments. The first the lp handle and the second the resulting variables and this array must already be dimensioned. When lpsolve is used from MATLAB, nothing must be dimensioned in advance. The mxlpsolve driver takes care of dimensioning all return variables and they are always returned as return value of the call to mxlpsolve. Never as argument to the routine. This can be a single value as for get_objective (although MATLAB stores this in a 1x1 matrix) or a matrix or vector as in get_variables. In this case, get_variables returns a 4x1 matrix (vector) with the result of the 4 variables of the lp model.

Note that you can get an overview of the available functionnames and their arguments by entering the following in MATLAB:

>> help mxlpsolve

An example

(Note that you can execute this example by entering command per command as shown below or by just entering example1. This will execute example1.m. You can see its contents by entering type example1.m)

>> lp=mxlpsolve('make_lp', 0, 4);
>> mxlpsolve('set_verbose', lp, 3);
>> mxlpsolve('set_obj_fn', lp, [1, 3, 6.24, 0.1]);
>> mxlpsolve('add_constraint', lp, [0, 78.26, 0, 2.9], 2, 92.3);
>> mxlpsolve('add_constraint', lp, [0.24, 0, 11.31, 0], 1, 14.8);
>> mxlpsolve('add_constraint', lp, [12.68, 0, 0.08, 0.9], 2, 4);
>> mxlpsolve('set_lowbo', lp, 1, 28.6);
>> mxlpsolve('set_lowbo', lp, 4, 18);
>> mxlpsolve('set_upbo', lp, 4, 48.98);
>> mxlpsolve('set_col_name', lp, 1, 'COLONE');
>> mxlpsolve('set_col_name', lp, 2, 'COLTWO');
>> mxlpsolve('set_col_name', lp, 3, 'COLTHREE');
>> mxlpsolve('set_col_name', lp, 4, 'COLFOUR');
>> mxlpsolve('set_row_name', lp, 1, 'THISROW');
>> mxlpsolve('set_row_name', lp, 2, 'THATROW');
>> mxlpsolve('set_row_name', lp, 3, 'LASTROW');
>> mxlpsolve('write_lp', lp, 'a.lp');
>> mxlpsolve('get_mat', lp, 1, 2)

ans =

   78.2600

>> mxlpsolve('solve', lp)

ans =

     0

>> mxlpsolve('get_objective', lp)

ans =

   31.7828

>> mxlpsolve('get_variables', lp)

ans =

   28.6000
         0
         0
   31.8276

>> mxlpsolve('get_constraints', lp)

ans =

   92.3000
    6.8640
  391.2928

Note that there are some commands that return an answer. To see the answer, the command was not terminated with a semicolon (;). If the semicolon is put at the end of a command, the answer is not shown. However it is also possible to write the answer in a variable. For example:

>> obj=mxlpsolve('get_objective', lp)

obj =

   31.7828

Or without echoing on screen:

>> obj=mxlpsolve('get_objective', lp);

The last command will only write the result in variable obj without showing anything on screen. get_variables and get_constraints return a vector with the result. This can also be put in a variable:

>> x=mxlpsolve('get_variables', lp);
>> b=mxlpsolve('get_constraints', lp);

It is always possible to show the contents of a variable by just giving it as command:

>> x

x =
  28.6000
        0
        0
  31.8276

Don't forget to free the handle and its associated memory when you are done:

>> mxlpsolve('delete_lp', lp);

Using model name instead of handle

From driver version 5.5.0.2 on, it is possible to use the model name instead of the handle. From the moment the model has a name, you can use this name instead of the handle. This is best shown by an example. Above example would look like this:
>> lp=mxlpsolve('make_lp', 0, 4);
>> mxlpsolve('set_lp_name', lp, 'mymodel');
>> mxlpsolve('set_verbose', 'mymodel', 3);
>> mxlpsolve('set_obj_fn', 'mymodel', [1, 3, 6.24, 0.1]);
>> mxlpsolve('add_constraint', 'mymodel', [0, 78.26, 0, 2.9], 2, 92.3);
>> mxlpsolve('add_constraint', 'mymodel', [0.24, 0, 11.31, 0], 1, 14.8);
>> mxlpsolve('add_constraint', 'mymodel', [12.68, 0, 0.08, 0.9], 2, 4);
>> mxlpsolve('set_lowbo', 'mymodel', 1, 28.6);
>> mxlpsolve('set_lowbo', 'mymodel', 4, 18);
>> mxlpsolve('set_upbo', 'mymodel', 4, 48.98);
>> mxlpsolve('set_col_name', 'mymodel', 1, 'COLONE');
>> mxlpsolve('set_col_name', 'mymodel', 2, 'COLTWO');
>> mxlpsolve('set_col_name', 'mymodel', 3, 'COLTHREE');
>> mxlpsolve('set_col_name', 'mymodel', 4, 'COLFOUR');
>> mxlpsolve('set_row_name', 'mymodel', 1, 'THISROW');
>> mxlpsolve('set_row_name', 'mymodel', 2, 'THATROW');
>> mxlpsolve('set_row_name', 'mymodel', 3, 'LASTROW');
>> mxlpsolve('write_lp', 'mymodel', 'a.lp');
>> mxlpsolve('get_mat', 'mymodel', 1, 2)

ans =

   78.2600

>> mxlpsolve('solve', 'mymodel')

ans =

     0

>> mxlpsolve('get_objective', 'mymodel')

ans =

   31.7828

>> mxlpsolve('get_variables', 'mymodel')

ans =

   28.6000
         0
         0
   31.8276

>> mxlpsolve('get_constraints', 'mymodel')

ans =

   92.3000
    6.8640
  391.2928

So everywhere a handle is needed, you can also use the model name. You can even mix the two methods. There is also a specific MATLAB routine to get the handle from the model name: get_handle.
For example:

>> mxlpsolve('get_handle', 'mymodel')
0

Don't forget to free the handle and its associated memory when you are done:

>> mxlpsolve('delete_lp', 'mymodel')

In the next part of this documentation, the handle is used. But if you name the model, the name could thus also be used.

Matrices

In MATLAB, all numerical data is stored in matrices; even a scalar variable. MATLAB also supports complex numbers (a + b * i with i=SQRT(-1)). mxlpsolve can only work with real numbers. MATLAB also supports sparse matrices. Sparse matrices are matrices where only the non-zero elements are provided and stored. This results in both less storage and faster calculation if there are a sufficient number of zero values in the matrix and there usually are. The mxlpsolve driver supports both dense and sparse matrices and their use is totally transparent to the user. Everywhere a matrix can be provided, it can be dense or sparse. In the above example all matrices were dense. For example:
>> mxlpsolve('add_constraint', lp, [0.24, 0, 11.31, 0], 1, 14.8);

In sparse matrix notation, this can be written:

>> mxlpsolve('add_constraint', lp, sparse([0.24, 0, 11.31, 0]), 1, 14.8);

Most of the time, variables are used to provide the data:

>> mxlpsolve('add_constraint', lp, a1, 1, 14.8);

Where a1 is a matrix variable that can be dense or sparse.

The mxlpsolve driver sees all provided matrices as sparse matrices. mxlpsolve also uses sparse matrices internally and data can be provided sparse via the ex routines. For example add_constraintex. The mxlpsolve driver always uses the ex routines to provide the data to lpsolve. Even if you call from MATLAB the routine names that would require a dense matrix (for example add_constraint), the mxlpsolve driver will always call the sparse version of the routine (for example add_constraintex). This results in the most performing behaviour. Note that if a dense matrix is provided, the dimension must exactly match the dimension that is expected by mxlpsolve. Matrices with too few or too much elements gives an 'invalid vector.' error. Sparse matrices can off course provide less elements (the non provided elements are seen as zero). However if too many elements are provided or an element with a too large index, again an 'invalid vector.' error is raised.

Most of the time, mxlpsolve needs vectors (rows or columns). In all situations, it doesn't matter if the vectors are row or column vectors. The driver accepts them both. For example:

>> mxlpsolve('add_constraint', lp, [0.24; 0; 11.31; 0], 1, 14.8);

Which is a column vector, but it is also accepted.

An important final note. Several lp_solve API routines accept a vector where the first element (element 0) is not used. Other lp_solve API calls do use the first element. In the MATLAB interface, there is never an unused element in the matrices. So if the lp_solve API specifies that the first element is not used, then this element is not in the MATLAB matrix.

Sets

All numerical data is stored in matrices. Alphanumerical data, however, is more difficult to store in matrices. Matrices require that each element has the same size (length) and that is difficult and unpractical for alphanumerical data. In a limited number of lpsolve routines, alphanumerical data is required or returned and in some also multiple elements. An example is set_col_name. For this, MATLAB sets are used. To specify a set of alphanumerical elements, the following notation is used: { 'element1', 'element2', ... }. Note the { and } symbols instead of [ and ] that are used with matrices.

Maximum usage of matrices/sets with mxlpsolve

Because MATLAB is all about matrices, all lpsolve API routines that need a column or row number to get/set information for that column/row are extended in the mxlpsolve MATLAB driver to also work with matrices. For example set_int in the API can only set the integer status for one column. If the status for several integer variables must be set, then set_int must be called multiple times. The mxlpsolve MATLAB driver however also allows specifying a vector to set the integer status of all variables at once. The API call is: return = mxlpsolve('set_int', lp, column, must_be_int). The matrix version of this call is: return = mxlpsolve('set_int', lp, [must_be_int]). The API call to return the integer status of a variable is: return = mxlpsolve('is_int', lp, column). The matrix version of this call is: [is_int] = mxlpsolve('is_int', lp)
Also note the get_mat and set_mat routines. In MATLAB these are extended to return/set the complete constraint matrix. See following example.

Above example can thus also be done as follows:
(Note that you can execute this example by entering command per command as shown below or by just entering example2. This will execute example2.m. You can see its contents by entering type example2.m)

>> lp=mxlpsolve('make_lp', 0, 4);
>> mxlpsolve('set_verbose', lp, 3);
>> mxlpsolve('set_obj_fn', lp, [1, 3, 6.24, 0.1]);
>> mxlpsolve('add_constraint', lp, [0, 78.26, 0, 2.9], 2, 92.3);
>> mxlpsolve('add_constraint', lp, [0.24, 0, 11.31, 0], 1, 14.8);
>> mxlpsolve('add_constraint', lp, [12.68, 0, 0.08, 0.9], 2, 4);
>> mxlpsolve('set_lowbo', lp, [28.6, 0, 0, 18]);
>> mxlpsolve('set_upbo', lp, [Inf, Inf, Inf, 48.98]);
>> mxlpsolve('set_col_name', lp, {'COLONE', 'COLTWO', 'COLTHREE', 'COLFOUR'});
>> mxlpsolve('set_row_name', lp, {'THISROW', 'THATROW', 'LASTROW'});
>> mxlpsolve('write_lp', lp, 'a.lp');
>> mxlpsolve('get_mat', lp)

ans =

         0   78.2600         0    2.9000
    0.2400         0   11.3100         0
   12.6800         0    0.0800    0.9000

>> mxlpsolve('solve', lp)

ans =

     0

>> mxlpsolve('get_objective', lp)

ans =

   31.7828

>> mxlpsolve('get_variables', lp)

ans =

   28.6000
         0
         0
   31.8276

>> mxlpsolve('get_constraints', lp)

ans =

   92.3000
    6.8640
  391.2928

Note the usage of Inf in set_upbo. This stands for 'infinity'. Meaning an infinite upper bound. It is also possible to use -Inf to express minus infinity. This can for example be used to create a free variable.

Starting from driver version 5.5.0.3 in, get_mat can also return the matrix in sparse format. By default the function returns it in dense format for backwards compatibility. However if a 3rd argument is provided that is non-zero, the returned matrix is sparse:

>> mxlpsolve('get_mat', lp, 1)

ans =

   (2,1)       0.2400
   (3,1)      12.6800
   (1,2)      78.2600
   (2,3)      11.3100
   (3,3)       0.0800
   (1,4)       2.9000
   (3,4)       0.9000

To show the full power of the matrices, let's now do some matrix calculations to check the solution. It works further on above example:

>> A=mxlpsolve('get_mat', lp);
>> X=mxlpsolve('get_variables', lp);
>> B = A * X

B =
   92.3000
    6.8640
  391.2928

So what we have done here is calculate the values of the constraints (RHS) by multiplying the constraint matrix with the solution vector. Now take a look at the values of the constraints that lpsolve has found:

>> mxlpsolve('get_constraints', lp)

ans =

   92.3000
    6.8640
  391.2928

Exactly the same as the calculated B vector, as expected.

Also the value of the objective can be calculated in a same way:

>> C=mxlpsolve('get_obj_fn', lp);
>> X=mxlpsolve('get_variables', lp);
>> obj = C * X

obj =

   31.7828

So what we have done here is calculate the value of the objective by multiplying the objective vector with the solution vector. Now take a look at the value of the objective that lpsolve has found:

>> mxlpsolve('get_objective', lp)

ans =

   31.7828

Again exactly the same as the calculated obj value, as expected.

Using string constants

From driver version 5.5.2.11 on, it is possible to use string constants everywhere an lp_solve constant is needed or returned. This is best shown by an example. In the above code we had:
>> lp=mxlpsolve('make_lp', 0, 4);
>> mxlpsolve('set_verbose', lp, 3);
>> mxlpsolve('add_constraint', lp, [0, 78.26, 0, 2.9], 2, 92.3);
>> mxlpsolve('add_constraint', lp, [0.24, 0, 11.31, 0], 1, 14.8);
>> mxlpsolve('add_constraint', lp, [12.68, 0, 0.08, 0.9], 2, 4);

Note the 3rd parameter on set_verbose and the 4th on add_constraint. These are lp_solve constants. One could define all the possible constants in MATLAB and then use them in the calls, but that has several disadvantages. First there stays the possibility to provide a constant that is not intended for that particular call. Another issue is that calls that return a constant are still returning it numerical.

Both issues can now be handled by string constants. The above code can be done as following with string constants:

>> lp=mxlpsolve('make_lp', 0, 4);
>> mxlpsolve('set_verbose', lp, 'IMPORTANT');
>> mxlpsolve('add_constraint', lp, [0, 78.26, 0, 2.9], 'GE', 92.3);
>> mxlpsolve('add_constraint', lp, [0.24, 0, 11.31, 0], 'LE', 14.8);
>> mxlpsolve('add_constraint', lp, [12.68, 0, 0.08, 0.9], 'GE', 4);

This is not only more readable, there is much lesser chance that mistakes are being made. The calling routine knows which constants are possible and only allows these. So unknown constants or constants that are intended for other calls are not accepted. For example:

>> mxlpsolve('set_verbose', lp, 'blabla');
??? BLABLA: Unknown.

>> mxlpsolve('set_verbose', lp, 'GE');
??? GE: Not allowed here.

Note the difference between the two error messages. The first says that the constant is not known, the second that the constant cannot be used at that place.

Constants are case insensitive. Internally they are always translated to upper case. Also when returned they will always be in upper case.

The constant names are the ones as specified in the documentation of each API routine. There are only 3 exceptions, extensions actually. 'LE', 'GE' and 'EQ' in add_constraint and is_constr_type can also be '<', '<=', '>', '>=', '='. When returned however, 'GE', 'LE', 'EQ' will be used.

Also in the matrix version of calls, string constants are possible. For example:

>> mxlpsolve('set_constr_type', lp, {'LE', 'EQ', 'GE'});

Some constants can be a combination of multiple constants. For example set_scaling:

>> mxlpsolve('set_scaling', lp, 3+128);

With the string version of constants this can be done as following:

>> mxlpsolve('set_scaling', lp, 'SCALE_MEAN|SCALE_INTEGERS');

| is the OR operator used to combine multiple constants. There may optinally be spaces before and after the |.

Not all OR combinations are legal. For example in set_scaling, a choice must be made between SCALE_EXTREME, SCALE_RANGE, SCALE_MEAN, SCALE_GEOMETRIC or SCALE_CURTISREID. They may not be combined with each other. This is also tested:

>> mxlpsolve('set_scaling', lp, 'SCALE_MEAN|SCALE_RANGE');
??? SCALE_RANGE cannot be combined with SCALE_MEAN

Everywhere constants must be provided, numeric or string values may be provided. The routine automatically interpretes them.

Returning constants is a different story. The user must let lp_solve know how to return it. Numerical or as string. The default is numerical:

>> mxlpsolve('get_scaling', lp)

ans =

   131

To let lp_solve return a constant as string, a call to a new function must be made: return_constants

>> mxlpsolve('return_constants', 1);

From now on, all returned constants are returned as string:

>> mxlpsolve('get_scaling', lp)

ans =

SCALE_MEAN|SCALE_INTEGERS

Also when an array of constants is returned, they are returned as string when return_constants is set:

>> mxlpsolve('get_constr_type', lp)

ans =

    'LE'    'EQ'    'GE'

This for all routines until return_constants is again called with 0:

>> mxlpsolve('return_constants', 0);

The (new) current setting of return_constants is always returned by the call. Even when set:

>> mxlpsolve('return_constants', 1)

ans =

     1

To get the value without setting it, don't provide the second argument:

>> mxlpsolve('return_constants')

ans =

     1

In the next part of this documentation, return_constants is the default, 0, so all constants are returned numerical and provided constants are also numerical. This to keep the documentation as compatible as possible with older versions. But don't let you hold that back to use string constants in your code.

M-files

MATLAB can execute a sequence of statements stored in diskfiles. Such files are called "M-files" because they must have the file type of ".m" as the last part of their filename (extension). Much of your work with MATLAB will be in creating and refining M-files. M-files are usually created using your local editor.

M-files can be compared with batch files or scripts. You can put MATLAB commands in them and execute them at any time. The M-file is executed like any other command, by entering its name (without the .m extension).

The mxlpsolve MATLAB distribution contains some example M-files to demonstrate this.

To see the contents of such a file, enter the command 'type filename'. You can also edit these files with your favourite text editor (or notepad).

example1.m

Contains the commands as shown in the first example of this article.

example2.m

Contains the commands as shown in the second example of this article.

example3.m

Contains the commands of a practical example. See further in this article.

example4.m

Contains the commands of a practical example. See further in this article.

example5.m

Contains the commands of a practical example. See further in this article.

example6.m

Contains the commands of a practical example. See further in this article.

lp_solve.m

This script uses the API to create a higher-level function called lp_solve. This function accepts as arguments some matrices and options to create and solve an lp model. See the beginning of the file or type help lp_solve or just lp_solve to see its usage:

 >> help lp_solve
 LP_SOLVE  Solves mixed integer linear programming problems.

   SYNOPSIS: [obj,x,duals] = lp_solve(f,a,b,e,vlb,vub,xint,scalemode,keep)

      solves the MILP problem

              max v = f'*x
                a*x <> b
                  vlb <= x <= vub
                  x(int) are integer

   ARGUMENTS: The first four arguments are required:

            f: n vector of coefficients for a linear objective function.
            a: m by n matrix representing linear constraints.
            b: m vector of right sides for the inequality constraints.
            e: m vector that determines the sense of the inequalities:
                      e(i) = -1  ==> Less Than
                      e(i) =  0  ==> Equals
                      e(i) =  1  ==> Greater Than
          vlb: n vector of lower bounds. If empty or omitted,
               then the lower bounds are set to zero.
          vub: n vector of upper bounds. May be omitted or empty.
         xint: vector of integer variables. May be omitted or empty.
    scalemode: scale flag. Off when 0 or omitted.
         keep: Flag for keeping the lp problem after it's been solved.
               If omitted, the lp will be deleted when solved.

   OUTPUT: A nonempty output is returned if a solution is found:

          obj: Optimal value of the objective function.
            x: Optimal value of the decision variables.
        duals: solution of the dual problem.

Example of usage. To create and solve following lp-model:

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

int x2,x1;

The following command can be used:

>> [obj, x]=lp_solve([-1, 2], [2, 1; -4, 4], [5, 5], [-1, -1], [], [], [1, 2])

obj =

     3


x =

     1
     2

lp_maker.m

This script is analog to the lp_solve script and also uses the API to create a higher-level function called lp_maker. This function accepts as arguments some matrices and options to create an lp model. Note that this scripts only creates a model and returns a handle. See the beginning of the file or type help lp_maker or just lp_maker to see its usage:

 >> help lp_maker

 LP_MAKER  Makes mixed integer linear programming problems.

   SYNOPSIS: lp_handle = lp_maker(f,a,b,e,vlb,vub,xint,scalemode,setminim)
      make the MILP problem
        max v = f'*x
          a*x <> b
            vlb <= x <= vub
            x(int) are integer

   ARGUMENTS: The first four arguments are required:
            f: n vector of coefficients for a linear objective function.
            a: m by n matrix representing linear constraints.
            b: m vector of right sides for the inequality constraints.
            e: m vector that determines the sense of the inequalities:
                      e(i) < 0  ==> Less Than
                      e(i) = 0  ==> Equals
                      e(i) > 0  ==> Greater Than
          vlb: n vector of non-negative lower bounds. If empty or omitted,
               then the lower bounds are set to zero.
          vub: n vector of upper bounds. May be omitted or empty.
         xint: vector of integer variables. May be omitted or empty.
    scalemode: Autoscale flag. Off when 0 or omitted.
     setminim: Set maximum lp when this flag equals 0 or omitted.

   OUTPUT: lp_handle is an integer handle to the lp created.

Example of usage. To create following lp-model:

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

int x2,x1;

The following command can be used:

>> lp=lp_maker([-1, 2], [2, 1; -4, 4], [5, 5], [-1, -1], [], [], [1, 2])

lp =

     0

To solve the model and get the solution:

>> mxlpsolve('solve', lp)

ans =

     0

>> mxlpsolve('get_objective', lp)

ans =

     3

>> mxlpsolve('get_variables', lp)

ans =

     1
     2

Don't forget to free the handle and its associated memory when you are done:

>> mxlpsolve('delete_lp', lp);

lpdemo.m

Contains several examples to build and solve lp models.

ex.m

Contains several examples to build and solve lp models. Also solves the lp_examples from the lp_solve distribution.

A practical example

We shall illustrate the method of linear programming by means of a simple example, giving a combination graphical/numerical solution, and then solve both a slightly as well as a substantially more complicated problem.

Suppose a farmer has 75 acres on which to plant two crops: wheat and barley. To produce these crops, it costs the farmer (for seed, fertilizer, etc.) $120 per acre for the wheat and  $210 per acre for the barley. The farmer has $15000 available for expenses. But after the harvest, the farmer must store the crops while awaiting favourable market conditions. The farmer has storage space for 4000 bushels. Each acre yields an average of 110 bushels of wheat or 30 bushels of barley.  If the net profit per bushel of wheat (after all expenses have been subtracted) is $1.30 and for barley is $2.00, how should the farmer plant the 75 acres to maximize profit?

We begin by formulating the problem mathematically.  First we express the objective, that is the profit, and the constraints algebraically, then we graph them, and lastly we arrive at the solution by graphical inspection and a minor arithmetic calculation.

Let x denote the number of acres allotted to wheat and y the number of acres allotted to barley. Then the expression to be maximized, that is the profit, is clearly

P = (110)(1.30)x + (30)(2.00)y = 143x + 60y.

There are three constraint inequalities, specified by the limits on expenses, storage and acreage. They are respectively:

120x + 210y <= 15000
110x + 30y <= 4000
x + y <= 75

Strictly speaking there are two more constraint inequalities forced by the fact that the farmer cannot plant a negative number of acres, namely:

x >= 0, y >= 0.

Next we graph the regions specified by the constraints. The last two say that we only need to consider the first quadrant in the x-y plane. Here's a graph delineating the triangular region in the first quadrant determined by the first inequality.

>> X = 0:125;
>> Y1 = (15000 - 120.*X)./210;
>> area(X, Y1)

Source

Now let's put in the other two constraint inequalities.

>> Y2 = max((4000 - 110.*X)./30, 0);
>> Y3 = max(75 - X, 0);
>> Ytop = min([Y1; Y2; Y3]);
>> area(X, Ytop)
>> axis([0 40 0 75])

Source

The blue area is the solution space that holds valid solutions. This means that any point in this area fulfils the constraints.

Now let's superimpose on top of this picture a contour plot of the objective function P.

>> hold on
>> [U V] = meshgrid(0:40, 0:75);
>> contour(U, V, 143.*U + 60.*V);
>> hold off

Source

The lines give a picture of the objective function. All solutions that intersect with the blue area are valid solutions, meaning that this result also fulfils the set constraints. The more the lines go to the right, the higher the objective value is. The optimal solution or best objective is a line that is still in the blue area, but with an as large as possible value.

It seems apparent that the maximum value of P will occur on the level curve (that is, level line) that passes through the vertex of the polygon that lies near (22,53).
It is the intersection of x + y = 75 and 110*x + 30*y = 4000
This is a corner point of the diagram. This is not a coincidence. The simplex algorithm, which is used by lp_solve, starts from a theorem that the optimal solution is such a corner point.
In fact we can compute the result:

>> x = [1 1; 110 30] \ [75; 4000]

x =

   21.8750
   53.1250

The acreage that results in the maximum profit is 21.875 for wheat and 53.125 for barley. In that case the profit is:

>> format bank
>> P = [143 60] * x

P =
       6315.63

That is, $6315.63.

Note that these command are in script example3.m

Now, lp_solve comes into the picture to solve this linear programming problem more generally. After that we will use it to solve two more complicated problems involving more variables and constraints.

For this example, we use the higher-level script lp_maker to build the model and then some lp_solve API calls to retrieve the solution. Here is again the usage of lp_maker:

>> help lp_maker

 LP_MAKER  Makes mixed integer linear programming problems.

   SYNOPSIS: lp_handle = lp_maker(f,a,b,e,vlb,vub,xint,scalemode,setminim)
      make the MILP problem
        max v = f'*x
          a*x <> b
            vlb <= x <= vub
            x(int) are integer

   ARGUMENTS: The first four arguments are required:
            f: n vector of coefficients for a linear objective function.
            a: m by n matrix representing linear constraints.
            b: m vector of right sides for the inequality constraints.
            e: m vector that determines the sense of the inequalities:
                      e(i) < 0  ==> Less Than
                      e(i) = 0  ==> Equals
                      e(i) > 0  ==> Greater Than
          vlb: n vector of non-negative lower bounds. If empty or omitted,
               then the lower bounds are set to zero.
          vub: n vector of upper bounds. May be omitted or empty.
         xint: vector of integer variables. May be omitted or empty.
    scalemode: Autoscale flag. Off when 0 or omitted.
     setminim: Set maximum lp when this flag equals 0 or omitted.

   OUTPUT: lp_handle is an integer handle to the lp created.

Now let's formulate this model with lp_solve:

>> f = [143 60];
>> A = [120 210; 110 30; 1 1];
>> b = [15000; 4000; 75];
>> lp = lp_maker(f, A, b, [-1; -1; -1], [], [], [], 1, 0);
>> solvestat = mxlpsolve('solve', lp)

solvestat =

             0

>> format bank
>> obj = mxlpsolve('get_objective', lp)

obj =

       6315.63

>> format short
>> x = mxlpsolve('get_variables', lp)

x =

   21.8750
   53.1250

>> mxlpsolve('delete_lp', lp);

Note that these command are in script example4.m

With the higher-level script lp_maker, we provide all data to lp_solve. lp_solve returns a handle (lp) to the created model. Then the API call 'solve' is used to calculate the optimal solution of the model. The value of the objective function is retrieved via the API call 'get_objective' and the values of the variables are retrieved via the API call 'get_variables'. At last, the model is removed from memory via a call to 'delete_lp'. Don't forget this to free all memory allocated by lp_solve.

The solution is the same answer we obtained before.  Note that the non-negativity constraints are accounted implicitly because variables are by default non-negative in lp_solve.

Well, we could have done this problem by hand (as shown in the introduction) because it is very small and it can be graphically presented.
Now suppose that the farmer is dealing with a third crop, say corn, and that the corresponding data is:

cost per acre$150.75
yield per acre125 bushels
profit per bushel$1.56

With three variables it is already a lot more difficult to show this model graphically. Adding more variables makes it even impossible because we can't imagine anymore how to represent this. We only have a practical understanding of 3 dimentions, but beyound that it is all very theorethical.

If we denote the number of acres allotted to corn by z, then the objective function becomes:

P = (110)(1.30)x + (30)(2.00)y + (125)(1.56) = 143x + 60y + 195z

And the constraint inequalities are:

120x + 210y + 150.75z <= 15000
110x + 30y + 125z <= 4000
x + y + z <= 75
x >= 0, y >= 0, z >= 0

The problem is solved with lp_solve as follows:

>> f = [143 60 195];
>> A = [120 210 150.75; 110 30 125; 1 1 1];
>> b = [15000; 4000; 75];
>> lp = lp_maker(f, A, b, [-1; -1; -1], [], [], [], 1, 0);
>> solvestat = mxlpsolve('solve', lp)

solvestat =

     0

>> format bank
>> obj = mxlpsolve('get_objective', lp)

obj =

       6986.84

>> format short
>> x = mxlpsolve('get_variables', lp)

x =

         0
   56.5789
   18.4211

>> mxlpsolve('delete_lp', lp);

Note that these command are in script example5.m

So the farmer should ditch the wheat and plant 56.5789 acres of barley and 18.4211 acres of corn.

There is no practical limit on the number of variables and constraints that MATLAB can handle. Certainly none that the relatively unsophisticated user will encounter. Indeed, in many true applications of the technique of linear programming, one needs to deal with many variables and constraints. The solution of such a problem by hand is not feasible, and software like MATLAB is crucial to success. For example, in the farming problem with which we have been working, one could have more crops than two or three. Think agribusiness instead of family farmer. And one could have constraints that arise from other things beside expenses, storage and acreage limitations. For example:

Below is a sequence of commands that solves exactly such a problem. You should be able to recognize the objective expression and the constraints from the data that is entered.  But as an aid, you might answer the following questions:

>> f = [110*1.3 30*2.0 125*1.56 75*1.8 95*.95 100*2.25 50*1.35];
>> A = [120 210 150.75 115 186 140 85;
        110 30 125 75 95 100 50;
        1 1 1 1 1 1 1;
        1 -1 0 0 0 0 0;
        0 0 1 0 -2 0 0;
        0 0 0 -1 0 -1 1];

>> b = [55000;40000;400;0;0;0];
>> lp = lp_maker(f, A, b, [-1; -1; -1; -1; -1; -1], [10 10 10 10 20 20 20], [100 Inf 50 Inf Inf 250 Inf], [], 1, 0);
>> solvestat = mxlpsolve('solve', lp)

solvestat =

     0

>> format bank
>> obj = mxlpsolve('get_objective', lp)

obj =

      75398.04

>> format short
>> x = mxlpsolve('get_variables', lp)

x =

   10.0000
   10.0000
   40.0000
   45.6522
   20.0000
  250.0000
   20.0000

>> mxlpsolve('delete_lp', lp);

Note that these command are in script example6.m

Note that we have used in this formulation the vlb and vub arguments of lp_maker. This to set lower and upper bounds on variables. This could have been done via extra constraints, but it is more performant to set bounds on variables. Also note that Inf is used for variables that have no upper limit. This stands for Infinity.

Note that despite the complexity of the problem, lp_solve solves it almost instantaneously. It seems the farmer should bet the farm on crop number 6. We strongly suggest you alter the expense and/or the storage limit in the problem and see what effect that has on the answer.

Another, more theoretical, example

Suppose we want to solve the following linear program using MATLAB:

max 4x1 + 2x2 + x3
s. t. 2x1 + x2 <= 1
x1 + 2x3 <= 2
x1 + x2 + x3 = 1
x1 >= 0
x1 <= 1
x2 >= 0
x2 <= 1
x3 >= 0
x3 <= 2

Convert the LP into MATLAB format we get:

f = [4 2 1]
A = [2 1 0; 1 0 2; 1 1 1]
b = [1; 2; 1]

Note that constraints on single variables are not put in the constraint matrix. lp_solve can set bounds on individual variables and this is more performant than creating additional constraints. These bounds are:

l = [ 0 0 0]
u = [ 1 1 2]

Now lets enter this in MATLAB:

>> f = [4 2 1];
>> A = [2 1 0; 1 0 2; 1 1 1];
>> b = [1; 2; 1];
>> l = [ 0 0 0];
>> u = [ 1 1 2];

Now solve the linear program using MATLAB: Type the commands

>> lp = lp_maker(f, A, b, [-1; -1; -1], l, u, [], 1, 0);
>> solvestat = mxlpsolve('solve', lp)

solvestat =

     0

>> obj = mxlpsolve('get_objective', lp)

obj =

    2.5000

>> x = mxlpsolve('get_variables', lp)

x =

    0.5000
         0
    0.5000

>> mxlpsolve('delete_lp', lp)

What to do when some of the variables are missing ?
For example, suppose there are no lower bounds on the variables. In this case define l to be the empty set using the MATLAB command:

>> l = [];

This has the same effect as before, because lp_solve has as default lower bound for variables 0.

But what if you want that variables may also become negative?
Then you can use -Inf as lower bounds:

>> l = [-Inf -Inf -Inf];

Solve this and you get a different result:

>> lp = lp_maker(f, A, b, [-1; -1; -1], l, u, [], 1, 0);
>> solvestat = mxlpsolve('solve', lp)

solvestat =

     0

>> obj = mxlpsolve('get_objective', lp)

obj =

    2.6667

>> x = mxlpsolve('get_variables', lp)

x =

    0.6667
   -0.3333
    0.6667

>> mxlpsolve('delete_lp', lp)

Overview of API routines

Note again that the MATLAB command 'help mxlpsolve' gives an overview of all functions that can be called via mxlpsolve with their arguments and return values.

Note that everwhere where lp is used as argument that this can be a handle (lp_handle) or the models name.

Extra MATLAB routines

These routines are not part of the lpsolve API, but are added for backwards compatibility. Most of them exist in the lpsolve API with another name.

Compile the mxlpsolve driver

Windows

Under Windows, the mxlpsolve MATLAB driver is a dll: mxlpsolve.dll or mxlpsolve.mexw32 or mxlpsolve.mexw64
This dll is an interface to the lpsolve55.dll lpsolve dll that contains the implementation of lp_solve. lpsolve55.dll is distributed with the lp_solve package (archive lp_solve_5.5.2.11_dev.zip). The mxlpsolve MATLAB driver dll is just a wrapper between MATLAB and lp_solve to translate the input/output to/from MATLAB and the lp_solve library.
Note that under Windows, the original extension was .dll. Newer versions of MATLAB require extension .mexw32. You can just rename the .dll to .mexw32 for that.
See also External Interfaces/API, MATLAB®

Also the library lp_explicit.lib is needed. The lp_explicit.lib file is a wrapper library between the lp_solve dll and the compiled application. This wrapper is needed because the lp_solve dll uses __stdcall calling convention. There seems to be a problem in MATLAB to call __stdcall functions directly (unexpected crashes). The lp_explicit library uses __cdecl calling convention and this then calls the lp_solve dll which uses __stdcall calling convention. Looks a bit complex, but it works without any problem.

There appears to be another way to compile MATLAB mex files with gcc for Windows. This is not tested. See Compiling Matlab mex files with gcc for Windows.

Unix/Linux

Under Unix/Linux, the mxlpsolve MATLAB driver is a shared library. The resulting file has a platform-dependent extension, as shown in the table below:

    sol2, SunOS 5.x - .mexsol
    hpux            - .mexhpux
    hp700           - .mexhp7
    ibm_rs          - .mexrs6
    sgi             - .mexsg
    alpha           - .mexaxp
    glnx86          - .mexglx
    Mac OS X        - .mexmac

This shared library is an interface to the lpsolve55.so lpsolve shared library that contains the implementation of lp_solve. lpsolve55.so is distributed with the lp_solve package (archive lp_solve_5.5.2.11_dev.tar.gz). The mxlpsolve MATLAB driver library is just a wrapper between MATLAB and lp_solve to translate the input/output to/from MATLAB and the lp_solve library.

All platforms

The mxlpsolve MATLAB driver is written in C. To compile this code, the MATLAB compiler is needed (mex). This compiler must be called from MATLAB. MATLAB may also need to be configured to find the C compiler. For this, enter the following in the MATLAB command windows and follow the instructions: mex -setup

To make the compilation process easier, a makefile can be used: Makefile.m
Enter 'help Makefile' from the MATLAB command window to see a list of options.
It may be necessary to edit this file first to change the path where lp_solve is installed. Change at the beginning lpsolvepath.
To make for release, just enter Makefile and everything is build.
This compiles three source files: lpsolve.c, matlab.c and hash.c
The optional arguments to Makefile are used for development. The first argument allows specifying a filename so that only this file is build. For example hash.c should only be compiled once while developing. So specifying 'lpsolve.c' as first argument will only compile this file and then link everything. This makes the build process a bit faster. The second argument is by default 0. When set to 1, then extra argument checking is done and while executing, some debug information is printed. Should only be used for debugging purposes. When released, this parameter should be 0. The third argument is by default 1. When set to 0, the makefile will not ask to press enter to start building.

See also Using lpsolve from O-Matrix, Using lpsolve from Sysquake, Using lpsolve from Scilab, Using lpsolve from Octave, Using lpsolve from FreeMat, Using lpsolve from Euler, Using lpsolve from Python, Using lpsolve from Sage, Using lpsolve from PHP, Using lpsolve from R, Using lpsolve from Microsoft Solver Foundation