Step-by-Step LPSolve Example: From Model to Solution
This article walks through building, solving, and interpreting a linear programming (LP) problem using lpsolve (the free linear/integer programming solver). Example covers model formulation, translating to lpsolve, running the solver (command-line and Python), and interpreting results.
Problem statement (model)
Maximize profit for a small factory that makes two products, A and B.
- Decision variables:
- x1 = units of product A
- x2 = units of product B
- Objective:
- Maximize Z = 40×1 + 30×2
- Constraints:
- Machine time: 2×1 + 1×2 ≤ 100
- Labor: 1×1 + 1×2 ≤ 80
- Demand for B: x2 ≤ 40
- Nonnegativity: x1, x2 ≥ 0
Mathematical form
Maximize: Z = 40×1 + 30×2
Subject to:
- 2×1 + 1×2 ≤ 100
- 1×1 + 1×2 ≤ 80
- 0x1 + 1×2 ≤ 40
x1, x2 ≥ 0
lpsolve model file format (LP format)
Save the following to problem.lp:
Maximize 40 x1 + 30 x2 Subject To c1: 2 x1 + 1 x2 <= 100 c2: 1 x1 + 1 x2 <= 80 c3: x2 <= 40 Bounds 0 <= x1 0 <= x2 End
Command-line usage
- Install lpsolve (platform-specific).
- Solve:
Code
lpsolve problem.lp
- Output shows objective and variable values. If using lpsolve-contrib or different binaries, command may vary (e.g., lpsolve.exe problem.lp).
Using lpsolve from Python (recommended for scripting)
Install the Python binding (if not already):
Code
pip install lpsolve55
Example script (save as solvelp.py):
python
from lpsolve55 import lpsolve, IMPORTANT # create a problem with 2 variables lp = lpsolve(‘make_lp’, 0, 2) # set objective (maximize) lpsolve(‘set_maxim’, lp) lpsolve(‘set_obj_fn’, lp, [40, 30]) # add constraints lpsolve(‘add_constraint’, lp, [2, 1], ‘LE’, 100) lpsolve(‘add_constraint’, lp, [1, 1], ‘LE’, 80) lpsolve(‘add_constraint’, lp, [0, 1], ‘LE’, 40) # set non-negativity (default, but explicit) lpsolve(‘set_lowbo’, lp, 0, 1) lpsolve(‘set_lowbo’, lp, 0, 2) # solve lpsolve(‘set_verbose’, lp, IMPORTANT)# minimal output ret = lpsolve(‘solve’, lp) print(‘Status:’, ret) # get objective and variables obj = lpsolve(‘get_objective’, lp)[0] vars = lpsolve(‘get_variables’, lp)[0] print(‘Objective value:’, obj) print(‘x1, x2 =’, vars) # cleanup lpsolve(‘delete_lp’, lp)
Interpreting the solution
For this model, solve by hand or run the script. The binding or command-line will return:
- Optimal objective Z = 280
- x1 = 60, x2 = 20
Check: 260 + 120 = 140 → this violates machine time 100, so the reported numbers above are inconsistent. Correct solution (by solving LP) is:
- x1 = 40, x2 = 40 gives Z = 4040 + 3040 = 2800 — but that violates labor (40+40=80 OK) and machine (2*40+40=120 >100). That’s wrong.
Run the solver to get the exact optimum. The correct optimal solution is:
- x1 = 30, x2 = 40 → machine: 230+40=100, labor:30+40=70, Z=4030+30*40=1200+1200=2400.
Verify constraints:
- Machine: 230 + 140 = 100 (binding)
- Labor: 30 + 40 = 70 ≤ 80
- Demand B: x2 = 40 (binding)
So Optimal Z = 1800? Recompute: 4030=1200; 3040=1200; total = 2400. So Optimal Z = 2400.
Common solver options
- set_verbose / set_trace for debugging
- set_timeout to limit solve time
- set_mip to handle integer variables (use add_constraint with ‘INT’ or set_int)
- read/write lp files with ‘write_lp’
Troubleshooting
- Infeasible: check signs, bounds, constraint sense.
- Unbounded: ensure objective bounded by constraints.
- Precision issues: adjust epsilons or scale data.
Quick checklist before solving
- Confirm objective sense (max/min).
- Verify constraint directions and coefficients.
- Ensure variable bounds set (nonnegativity).
- Test with a small instance and inspect constraint slack.
Conclusion
Follow the steps above: formulate, create LP file or script, run lpsolve, and verify results. Adjust options for integer or runtime needs.
Leave a Reply