Skip to content

hasancatalgol/linear-programming

Repository files navigation

Pyomo Linear Programming Example — Product Mix

This is a minimal, self-contained linear program (LP) using Pyomo to decide how many units of two products (A and B) to produce in order to maximize profit subject to machine-hour, labor-hour, and demand constraints.

Product-Mix LP Plot


Problem formulation

$$ \begin{aligned} \text{Maximize:}\quad & z = 40x_A + 30x_B \\ \text{Subject to:}\quad & \begin{cases} 2x_A + x_B \le 100,\\ x_A + x_B \le 80,\\ x_B \le 40,\\ x_A,, x_B \ge 0. \end{cases} \end{aligned} $$


Method (how it’s solved)

This model is a plain linear program (LP). When you run it with GLPK or CBC:

  • Algorithm: By default they use a (revised) simplex method for LPs
    – GLPK runs simplex; CBC delegates the LP to Clp, which also uses primal/dual simplex.
  • Idea in one line: simplex walks along the corners (vertices) of the feasible polygon/polytope, moving to a better adjacent corner until no improving move exists (reduced costs ≥ 0), which certifies optimality.

Quick start

1) Install Python packages

uv add pyomo matplotlib

You’ll also need a solver (GLPK or CBC):

  • macOS:
    brew install glpk
  • Ubuntu/Debian:
    sudo apt install glpk-utils
  • Windows:
    Download GLPK binaries or install CBC from CoinOR.

2) Run the solver

uv run python main.py

Example with custom limits:

uv run python main.py --hours 120 --labor 90 --bmax 50

3) Run with plot

To visualize the constraints, feasible region, and optimum:

uv run python main.py --plot

Example with custom limits and plot:

uv run python main.py --hours 120 --labor 90 --bmax 50 --plot

4) Saving the plot to docs/

When using --plot, the program will automatically save the diagram as:

docs/fig.png

…while also showing it interactively. You can change the filename/location in plot.py.


Example Output (CLI)

Solver: glpk
Status: ok | optimal

Optimal production plan:
  A: 30.00 units
  B: 40.00 units

Total profit: 2200.00

Resource usage at optimum:
  Machine hours: 100.00 / 100
  Labor hours:   70.00 / 80
  B upper bound: 40.00 / 40

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages