Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📈 Linear Programming

Mastering Optimization: From Theory to Code

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.

Python R Jupyter PuLP License uv


🎯 What is this repository?

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 linprog package 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.


⚡ Quick Start (2 minutes)

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-lab

Your 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 .ipynb file, and select .venv as the kernel.


⚙️ Prerequisites

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 --version

🗺️ Learning Map

📖 Theory: The Mathematical Foundations

Read 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

🧪 Hands-on Labs: Code Your Solutions

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. 🐍 📊

🛠️ Technologies and Libraries

Python

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

R

Package Purpose
linprog Solving linear programs using the Simplex algorithm
lpSolve Mixed-integer LP solver with support for equality and bound constraints

📝 Lab Administration Cheatsheet

# ── 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)

🏗️ Project Structure

📁 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)

📄 License and References

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.

About

Introduction to Linear Programming with R and Python

Topics

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages