Linear Programming

Linear programming refers to optimizing a linear objective function subject to linear equality and inequality constraints. It has applications across operations research, logistics, scheduling, etc.

The general form is:

Maximize:

c1x1 + c2x2 + … + cn*xn

Subject to:

a11x1 + a12x2 + … + a1n*xn (<=, =, >=) b1

a21x1 + a22x2 + … + a2n*xn (<=, =, >=) b2

am1x1 + am2x2 + … + amn*xn (<=, =, >=) bm

Java example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public double solveLP(double[] obj, double[][] constraints) {

  LinearProgram lp = new LinearProgram(obj);

  for (double[] row : constraints) {
    LinearConstraint lc = new LinearConstraint(row, Relationship.GEQ, 0.0);
    lp.addConstraint(lc);
  }

  return lp.solve();
}

C++ example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
double solveLP(vector<double> obj, vector<vector<double>> constraints) {

  LinearProgram lp(obj);

  for (vector<double> row : constraints) {
    LinearConstraint lc(row, GEQ, 0.0); 
    lp.addConstraint(lc);
  }

  return lp.solve();
} 

Python example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from pulp import *

def solve_lp(obj, constraints):

  prob = LpProblem("Linear Program", LpMaximize)

  # Define decision variables 
  x = {str(i): LpVariable(...) for i in range(len(obj))}
  
  prob += lpDot(obj, x)

  for row in constraints:
    prob += lpDot(row, x) >= 0

  status = prob.solve()

  return LpStatus[status] # Optimized successfully?

Powerful technique for optimizing allocation problems subject to linear constraints.

Linear Programming (LP) is a method to achieve the best outcome in a mathematical model whose requirements are represented by linear relationships. In simpler terms, it’s about finding the best (highest or lowest) value that a linear function can take based on linear equality and inequality constraints.

Algorithm

  1. Objective Function: Create a linear function that you want to maximize or minimize.
  2. Constraints: These are the linear equations or inequalities that bound the decision variables.
  3. Feasible Region: Identify the region where all constraints overlap.
  4. Optimal Solution: The point(s) in the feasible region where the objective function takes its optimal value.

Java Code

Java does not have built-in support for LP, but you can use third-party libraries like Apache Commons Math.

1
// Use Apache Commons Math to implement LP.

C++ Code

C++ does not have native support for LP. Libraries like GLPK can be used.

1
// Use GLPK or another LP solver library

Python Code

Python has the SciPy library which provides a convenient function to solve LP problems.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from scipy.optimize import linprog

# Objective function coefficients
c = [-1, -2]

# Inequality constraints matrix
A = [[1, 1], [2, 1]]
b = [2, 3]

# Solve linear programming problem
result = linprog(c, A_ub=A, b_ub=b, method='simplex')

# Output result
print('Optimal value:', -result.fun, '\nX:', result.x)

In the Python code, we’re minimizing -x - 2y subject to x + y <= 2 and 2x + y <= 3. The negative sign in -result.fun flips the minimized value to a maximized value, as we intended to maximize x + 2y.

This covers the basic idea of Linear Programming and how to implement it in different programming languages using third-party libraries.