A hands-on collection of solved exercises and examples of Linear Programming using Python and R — exploring the mathematics behind resource allocation, the Simplex method, duality, and sensitivity analysis.
A complete learning path for Linear Programming, blending mathematical foundations with computational practice. Each notebook is a self-contained lesson that models real-world problems — from diet optimization to portfolio allocation — and solves them using industrial-grade solvers.
- 📖 Rich Documentation — Theory of LP, Simplex, duality, shadow prices, and sensitivity.
- 🐍 Python Labs — Using PuLP, NumPy, Matplotlib, and Pandas.
- 📊 R Labs — Using the
linprogpackage for matrix-based optimization. - 🧮 Native Simplex Implementation — A from-scratch Simplex algorithm in Python (NumPy) and in R (base matrices).
Target audience: Students, economists, data scientists, and operations research enthusiasts who want to master optimization fundamentals.
If you already have uv installed, you can start exploring in two simple steps:
# 1. Clone the repository
git clone https://github.com/hiltonmbr/linear-programming.git
cd linear-programming
# 2. Set up the Python environment and launch the labs
make setup-env
make jupyter-labYour browser will open automatically with the interactive exercise notebooks in the src/ folder. Select Python (.venv) as the kernel when opening the first notebook.
💡 VS Code users: Install the "Jupyter" extension (
ms-toolsai.jupyter), open a.ipynbfile, and select.venvas the kernel.
| Requirement | Details |
|---|---|
| uv | Fast Python package installer and resolver. Essential for make setup-env (Installation) |
| R (optional) | Only needed for R notebooks — install from cran.r-project.org |
| GLPK (optional) | Required for sensitivity analysis reports — brew install glpk (macOS) or sudo apt install glpk-utils (Ubuntu) |
Verify the installation:
uv --versionRead the documentation in the docs/ folder before diving into the hands-on practice.
| # | Module | What you will learn | Link |
|---|---|---|---|
| 1 | Theory & Foundations | LP model, standard matrix form, geometric interpretation, slack/surplus variables. | 📖 Read |
| 2 | Simplex Method & Duality | The Simplex algorithm, primal-dual relationship, shadow prices, sensitivity analysis. | 📖 Read |
| 3 | Python Guide (PuLP) | Modeling LP problems with PuLP — step-by-step with code snippets. | 📖 Read |
| 4 | R Guide (linprog) | Matrix-based LP solving with R's linprog package. | 📖 Read |
| 5 | Interpreting Results | Solver status, slacks, shadow prices, and sensitivity analysis explained. | 📖 Read |
Our labs are inside the src/ folder. Each notebook combines theory, code, and economic interpretation.
| # | Topic | Description | Python | R |
|---|---|---|---|---|
| 1 | 🥗 Minimum-Cost Diet | Minimize diet cost subject to nutritional constraints. | 🐍 | 📊 |
| 2 | 🏭 Optimal Production | Maximize factory profit with departmental capacity constraints. | 🐍 | 📊 |
| 3 | 🌾 Farmer's Resource Allocation | Maximize farm revenue with water and labor constraints (4 crops). | 🐍 | 📊 |
| 4 | 🐇 Minimum-Cost Feed Mix | Minimize feed cost while meeting nutritional requirements for rabbits. | 🐍 | 📊 |
| 5 | ⚙️ Simplex from Scratch | Native Simplex implementation (matrix tableau, no solvers). | 🐍 | 📊 |
| 6 | 💼 Portfolio Optimization with ESG | Maximize returns with risk, ESG, liquidity, and diversification constraints. | 🐍 | 📊 |
| 7 | 🌽 Multi-Crop Farm Planning | Maximize farm profit across 6 crops with 5 resource constraints. | 🐍 | 📊 |
| 8 | 🏭 Multi-Plant Production | Optimize production across 2 factories and 3 products with market rules. | 🐍 | 📊 |
| 9 | 💧 Water Resources Allocation | Allocate river basin water among 4 competing uses. | 🐍 | 📊 |
| 10 | 🚚 Multi-Product Distribution | Minimize transport cost for 2 products across 3 warehouses and 4 stores. | 🐍 | 📊 |
| Library | Purpose |
|---|---|
| PuLP | Mathematical modeling and linear optimization API |
| NumPy | Linear algebra and vector operations |
| Matplotlib | Graphical visualization of feasible regions |
| Pandas | Structured data manipulation |
| Package | Purpose |
|---|---|
linprog |
Solving linear programs using the Simplex algorithm |
lpSolve |
Mixed-integer LP solver with support for equality and bound constraints |
# ── Environment ──
make setup-env # 🐍 Sets up the Python environment with uv + registers Jupyter kernel
make install-r # 📊 Installs R dependencies (linprog)
# ── Running Notebooks ──
make jupyter-lab # 📓🚀 Launches JupyterLab
make jupyter # 📔 Launches classic Jupyter Notebook
# ── Code Quality ──
make lint # ✅ Checks Python code style with ruff
make format # 🎨 Auto-formats Python code with ruff
# ── Maintenance ──
make clean # 🧹 Removes temp files and cache
make strip # 🧽 Strips outputs from all notebooks (fresh start)📁 linear-programming/
├── src/ → Interactive Jupyter notebooks (exercises)
├── docs/ → Theoretical documentation (5 modules)
├── pyproject.toml → Python project configuration (uv)
├── Makefile → Terminal shortcuts (the fun way)
└── .venv/ → Virtual environment (created by setup-env)
This project is made available under the MIT License.
Open educational material. Created for the Mathematical Economics course. Developed by Hilton Ramalho.
Repository built for study purposes and application of quantitative methods and operations research.