https://github.com/rystrauss/simplex
Implementation of the Simplex algorithm for solving linear programs.
https://github.com/rystrauss/simplex
constrained-optimization linear-programming optimization simplex-algorithm
Last synced: 24 days ago
JSON representation
Implementation of the Simplex algorithm for solving linear programs.
- Host: GitHub
- URL: https://github.com/rystrauss/simplex
- Owner: rystrauss
- License: mit
- Created: 2020-03-17T14:17:12.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-06-22T01:54:08.000Z (over 3 years ago)
- Last Synced: 2024-01-30T00:43:01.880Z (almost 2 years ago)
- Topics: constrained-optimization, linear-programming, optimization, simplex-algorithm
- Language: Python
- Homepage:
- Size: 22.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simplex Algorithm
The `simplex` package contained in this repository provides an interface for constructing linear programs
and solving them with the [Simplex algorithm](https://en.wikipedia.org/wiki/Simplex_algorithm).
## Usage
Consider the following LP:
**Maximize 3x + 4y subject to the following constraints:**
x + 2y ≤ 14
3x - y ≥ 0
x - y ≤ 2
The code below demonstrates how to construct and solve this LP using the `simplex` package.
For simplicity, this solver assumes that the objective is to be maximized and does not provide the option to set
explicit lower bounds on variables.
```python
from simplex import Solver
# Create the solver
solver = Solver()
# Create variables
x = solver.add_variable('x', Solver.INFINITY)
y = solver.add_variable('y', Solver.INFINITY)
# Constraint 0: x + 2y <= 14.
constraint0 = solver.add_constraint(-Solver.INFINITY, 14)
constraint0.set_coefficient(x, 1)
constraint0.set_coefficient(y, 2)
# Constraint 1: 3x - y >= 0.
constraint1 = solver.add_constraint(0, Solver.INFINITY)
constraint1.set_coefficient(x, 3)
constraint1.set_coefficient(y, -1)
# Constraint 2: x - y <= 2.
constraint2 = solver.add_constraint(-Solver.INFINITY, 2)
constraint2.set_coefficient(x, 1)
constraint2.set_coefficient(y, -1)
# Objective function: 3x + 4y.
objective = solver.objective()
objective.set_coefficient(x, 3)
objective.set_coefficient(y, 4)
# Solve the linear program
status = solver.solve()
print('Solution status:', status)
print('Objective value:', objective.solution_value)
print('x value:', x.solution_value)
print('y value:', y.solution_value)
```