Using lpsolve from Sage

Sage?

Sage is free, open-source math software that supports research and teaching in algebra, geometry, number theory, cryptography, numerical computation, and related areas. Both the Sage development model and the technology in Sage itself are distinguished by an extremely strong emphasis on openness, community, cooperation, and collaboration: we are building the car, not reinventing the wheel. The overall goal of Sage is to create a viable, free, open-source alternative to Maple, Mathematica, Magma, and MATLAB.

We will not discuss the specifics of Sage here but instead refer the reader to the Sage website. Also see Sage Tutorial, Sage Tutorial (pdf), Sage Feature Tour and Sage For Newbies.

Sage and lpsolve

The primary implementation language of Sage is Python. Sage is build on top of Python. See Sage afterword.
Because there is already a link from lpsolve to Python (see Using lpsolve from Python), the link from Sage to lpsolve is in fact quite easy and ready available. Nothing special must be done except for installing the lpsolve Python driver.
lpsolve is callable from Sage via this Python extension or module. As such, it looks like lpsolve is fully integrated with Sage. Matrices can directly be transferred between Sage 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 Sage (especially for matrix support). So you have full control to the complete lpsolve functionality via the lpsolve Sage driver. If you find that this involves too much work to solve an lp model then you can also work via higher-level script files that can make things a lot easier. See further in this article.

Sage 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 optimised may be numerically complicated in which case a software package like Sage is required to effect a solution.

Installation

To make this possible, the Python driver program is needed. This driver must be put in a directory known to Sage and Sage can call the lpsolve solver.
How this can be done can be read in the Python documentation: Using lpsolve from Python.

There is only one thing that must be considered: Sage comes with its own version of Python. So not the Python version that is already installed on the system (/usr/bin/python and /usr/lib/pythonx.y/*) is used, but a Python version in a subdirectory of the sage installation. For example /sage-3.4-linux-Ubuntu_8.10-i686-Linux/local/bin and /sage-3.4-linux-Ubuntu_8.10-i686-Linux/local/lib/pythonx.y/*

The consequence is that the lpsolve Python driver must also be installed on that place. The same installation files as for Python can be used, but they must be installed on the sage directory.
For example /sage-3.4-linux-Ubuntu_8.10-i686-Linux/local/lib/python2.5/site-packages. There lpsolve55.so, lp_solve.py, lp_solve.pyc, lp_maker.py, lp_maker.pyc and lpsolve55-5.5.0.6-py2.5.egg-info must reside after the installation.
See also Install the lpsolve driver for the installation of these files.

Solve an lp model from Sage via lpsolve

See Using lpsolve from Python. Everything that is written there also applies to sage.
Note however that the prompt is different.

Under Python the prompt is >>>
Under Sage the prompt is sage:

That is the only difference.

Sage also has a web interface called Sage Notebook. Also in this application lpsolve can be called in the same way.

Also note that you can enter the command load "filename" with filename a .py Python file with commands. The lpsolve examples (example1.py, ...) can be loaded and executed from the sage prompt as such.

See also Using lpsolve from MATLAB, 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 PHP, Using lpsolve from R, Using lpsolve from Microsoft Solver Foundation