lp_solve

Mixed Integer Linear Programming (MILP) solver.

History

It was developed at the Eindhoven University of Technology.

FAQ

What is it?

Linear interger programming library, based on the revised simplex method and branch-and-bound methed for the integers.

How cool is it?

Reasonable.

Competitors?

GlPK, Clp, Cgl, Coin-or

When to use it?

When you want to embed your linear programming solver in a program.

Is it dead?

Nope, but there is not much development either.

Where is it?

http://lpsolve.sourceforge.net/

How to use it

The documentation is extensive but hard to read.

Download

file: 1_download.sh

#!/bin/bash

mkdir -p tmp
cd ./tmp
wget -q -O lp_solve_5.5.2.5_source.tar.gz https://downloads.sourceforge.net/project/lpsolve/lpsolve/5.5.2.5/lp_solve_5.5.2.5_source.tar.gz
tar -xaf lp_solve_5.5.2.5_source.tar.gz
cd lp_solve_5.5/lpsolve55
chmod +x ccc
./ccc

Init

file: main.c

#include <stdio.h>

#include "lp_lib.h"

void simple_report(const lprec *lp, const int d) {
    if (d == 0 ) {
        printf("optimal solution found.\n");
        print_objective(lp);
        print_solution(lp, 1);
        print_constraints(lp, 1);
        print_duals(lp);
    }
}

int main(void) {
    lprec *lp;
    int d;
    int majorversion, minorversion, release, build;

    lp_solve_version(&majorversion, &minorversion, &release, &build);
    printf("lp_solve %d.%d.%d.%d demo\n\n", majorversion, minorversion, release, build);

    //new problem with 4 variables and 0 constraints
    if ((lp = make_lp(0, 4)) != NULL) {
        //add some constraints
        double row[] = {0, 3, 2, 2, 1};
        if (add_constraint(lp, row, LE, 4)) {
            //add_constraintex is now used to add a row.
            //only the npn-zero values must be specfied with this call
            int colno[] = {2, 3, 4};
            double row1[] = {4, 3, 1};
            if (add_constraintex(lp, sizeof(colno) / sizeof(*colno), row1, colno, GE, 3)) {
                //set the objective function
                double row2[] = {0, 2, 3, -2, 3};
                if (set_obj_fn(lp, row2)) {
                    //name variable and constraints
                    if (set_row_name(lp, 1, "speed")) {
                        if (set_col_name(lp, 2, "money")) {
                            //solve the problem
                            d = solve(lp);
                            simple_report(lp, d);
                            //change a single element in the matrix, maximise the objective and solve
                            if (set_mat(lp,2,1,0.5)) {
                                set_maxim(lp);
                                d = solve(lp);
                                simple_report(lp, d);
                            }
                            //change the value of a rhs element with set_rh(lp,1,7.45)\n");
                            if ( set_rh(lp, 1, 7.45) ) {
                                d = solve(lp);
                                simple_report(lp, d);
                            }
                            //change the integer type, set bounds remove a constraint
                            set_int(lp, 4, TRUE);
                            set_lowbo(lp, 2, 2);
                            set_upbo(lp, 4, 5.3);
                            del_constraint(lp, 1);
                            d = solve(lp);
                            simple_report(lp, d);
                            //Add an equality constraint and add one column
                            double row3[] = {0, 1, 2, 1, 4};
                            double col[] = {3, 2, 2};
                            if (add_constraint(lp, row3, EQ, 8)) {
                                if(add_column(lp, col)) {
                                    del_column(lp,3);
                                }
                            }
                            //remove one column, set scaling
                            set_scaling(lp, SCALE_MEAN);
                            //get one matrix element
                            printf("%f %f\n", (double)get_mat(lp,2,3), (double)get_mat(lp,1,1));
                            d = solve(lp);
                            simple_report(lp, d);
                            //unscale and reset the problem to the initial basis
                            unscale(lp);
                            default_basis(lp);
                        }
                    }
                }
            }
        }
    }

    return(0);
}

file: 2_compile_and_run.sh

#!/bin/bash

gcc -o ./tmp/main main.c -I./tmp/lp_solve_5.5 ./tmp/lp_solve_5.5/lpsolve55/bin/ux64/liblpsolve55.a -lm -ldl
./tmp/main

Further reading:

We only tipped the top of the iceberg here. There is much more:

https://en.wikipedia.org/wiki/List_of_optimization_software