A basic implementation of the Simplex algorithm, written in Swift

Simplex-Swift

This package contains a basic implementation of the Simplex algorithm.

Usage

1. Solving maximization problem

For example, given a function with constraints:

F = 7X1 + 8X2 + 10X3

2X1 + 3X2 + 2X3 ≤ 1000

X1 + X2 + 2X3 ≤ 800

We need to maximize F(X1 X2 X3).

let function    : [Double] = [7, 8, 10]
let constraint1 : [Double] = [2, 3, 2, 1000]
let constraint2 : [Double] = [1, 1, 2, 800]

if let solution = SimplexSolver.maximize(function: function, constraints: [constraint1, constraint2], maximumIterationsCount: 10) {
    let x1 = solution[0]  // 200
    let x2 = solution[1]  // 0
    let x3 = solution[2]  // 300
    let f  = solution[3]  // 4400
}

The maximum value of the function F(X1 X2 X3) is 4400 when X1 = 200, X2 = 0, X3 = 300.

2. Solving minimization problem

Another example, given a function with constraints:

F = 3X1 + 9X2

2X1 + X2 ≥ 8

X1 + 2X2 ≥ 8

Minimize F(X1 X2).

let function    : [Double] = [3, 9]
let constraint1 : [Double] = [2, 1, 8]
let constraint2 : [Double] = [1, 2, 8]

if let solution = SimplexSolver.minimize(function: function, constraints: [constraint1, constraint2], maximumIterationsCount: 10) {
    let x1 = solution[0]  // 8
    let x2 = solution[1]  // 0
    let f  = solution[2]  // 24
}

The minimum value of the function F(X1 X2) is 24 when X1 = 8, X2 = 0.

GitHub

View Github