A pure Python implementation of the Gomory cutting plane method for solving Integer Linear Programming (ILP) problems. Designed for educational purposes with exact fractional arithmetic.
- Pure Python - No external solver dependencies
- Exact arithmetic - Uses fractions instead of floating-point numbers
- Educational - Step-by-step display of simplex tableaux at each iteration
- Complete traceability - Follow every step of the algorithm
- Primal & Dual Simplex - Full implementation of both methods
- Gomory cuts generation - Automatic cutting plane generation
pip install gomorygit clone https://github.com/vleonel-junior/Gomory.git
cd Gomory
pip install -e .from gomory import Problem, GomorySolver
# Define an Integer Linear Programming problem
# max z = 6x₁ + 8x₂ + 7x₃
# subject to:
# 4x₁ + 6x₂ + 8x₃ ≤ 14
# x₁ ≤ 1, x₂ ≤ 1, x₃ ≤ 1
# xᵢ ∈ ℤ⁺ (integer variables)
problem = Problem(
objective=[6, 8, 7],
sense="max",
constraints=[
([4, 6, 8], "<=", 14),
([1, 0, 0], "<=", 1),
([0, 1, 0], "<=", 1),
([0, 0, 1], "<=", 1),
],
integer_vars=[0, 1, 2], # indices of integer variables
var_names=["x1", "x2", "x3"]
)
# Solve with verbose output
solver = GomorySolver(problem, verbose=True)
solution = solver.solve()
# Display solution
print(solution)Optimal integer solution found!
x1 = 0, x2 = 1, x3 = 1
z* = 15
The Gomory cutting plane method solves Integer Linear Programs through:
First, solve the linear program without integrality constraints using the primal simplex method.
If the solution is not integer, generate a Gomory cut from the row with the largest fractional part.
After adding the cut, the dual simplex method restores feasibility.
Repeat until an integer solution is found.
pytest tests/gomory/
├── gomory/
│ ├── __init__.py # Package exports
│ ├── fraction_utils.py # Fraction utilities
│ ├── problem.py # Problem modeling
│ ├── tableau.py # Simplex tableau
│ ├── simplex.py # Primal simplex
│ ├── dual_simplex.py # Dual simplex
│ ├── gomory_cut.py # Cut generation
│ ├── solver.py # Main solver
│ └── display.py # Formatted display
├── tests/ # Unit tests
├── examples/ # Usage examples
└── pyproject.toml # Package configuration
MIT License