Tuesday, November 4, 2014

Using LPSOLVE for linear programming problems.



Although Gnumeric and LibreOffice CALC spreadsheet softwares can solve linear programming problems, these do not include postoptimality or sensitivity reports. But not to worry.

The free standing LP program LPSOLVE can be used to solve linear programming problems and is capable of producting sensitity reports.
Let us illustrate how to use this software on a simple program.

Create a file, simple.py with the following contents:

max: 5000 X+ 3000 Y;


C1: X + Y   <=  8;
C2: 2 X + Y <= 10;
Note that LPSOLVE will do the automatic conversion of the above problem to a standard from internally, the user need not explicity rewrite the model to include slack, surplus or artificial variables. From the command line, issue lp_solve -S7 simple.py . This will tell the program to solve the lp problem specification and solve for the optimum values of the variables and to produce a complete report :
Model name: 
                 X        Y 
Maximize      5000     3000 
C1               1        1 <=        8
C2               2        1 <=       10
Type          Real     Real 
upbo           Inf      Inf 
lowbo            0        0 

Scale factors:
R0                   scaled at 0.000237841
C1                   scaled at 0.840896
C2                   scaled at 0.594604
X                    scaled at 0.840896
Y                    scaled at 1.18921

Value of objective function: 28000

Actual values of the variables:
X                               2
Y                               6

Actual values of the constraints:
C1                              8
C2                             10

Objective function limits:
                                 From            Till       FromValue
X                                3000            6000          -1e+30
Y                                2500            5000          -1e+30

Dual values with from - till limits:
                           Dual value            From            Till
C1                               1000               5              10
C2                               2000               8              16
X                                   0          -1e+30           1e+30
Y                                   0          -1e+30           1e+30

Tableau at iter 2:
             -3             -4
  2      2.0000000     -1.4142136      5.0453785
  1     -1.4142136      2.0000000      2.3784142
        -0.2828427     -0.8000000      6.6595598


The output contains the model formulation, scaling factors used, value of objective function(28000), value of the variables,X=2 ad Y=6 constraint values (value of slacks), and the final final tableu at termination If there were errors in the specification file, the software may not output the reusults. Thee flag -S7 gives the most detailed output. Here are other flag setttings:
-S0: Print nothing
         -S1: Only objective value
         -S2: Obj value+variables (default)
         -S3: Obj value+variables+constraints
         -S4: Obj value+variables+constraints+duals
         -S5: Obj value+variables+constraints+duals+lp model
         -S6: Obj value+variables+constraints+duals+lp model+scales
         -S7: Obj value+variables+constraints+duals+lp model+scales+lp tableau

It is beyond the scope of this article to explain or interpret the output values retuned by LPSOLVE. That might be explained in another artcle in our other blogs.