#!/usr/bin/env python # coding: utf-8 # # Linear program # A linear program is an optimization problem with a linear objective and affine inequality constraints. A common standard form is the following: # $$ # \begin{array}{ll} # \mbox{minimize} & c^Tx \\ # \mbox{subject to} & Ax \leq b. # \end{array} # $$ # Here $A \in \mathcal{R}^{m \times n}$, $b \in \mathcal{R}^m$, and $c \in \mathcal{R}^n$ are problem data and $x \in \mathcal{R}^{n}$ is the optimization variable. The inequality constraint $Ax \leq b$ is elementwise. # # For example, we might have $n$ different products, each constructed out of $m$ components. Each entry $A_{ij}$ is the amount of component $i$ required to build one unit of product $j$. Each entry $b_i$ is the total amount of component $i$ available. We lose $c_j$ for each unit of product $j$ ($c_j < 0$ indicates profit). Our goal then is to choose how many units of each product $j$ to make, $x_j$, in order to minimize loss without exceeding our budget for any component. # # In addition to a solution $x^\star$, we obtain a dual solution $\lambda^\star$. A positive entry $\lambda^\star_i$ indicates that the constraint $a_i^Tx \leq b_i$ holds with equality for $x^\star$ and suggests that changing $b_i$ would change the optimal value. # ## Example # In the following code, we solve a linear program with CVXPY. # In[36]: # Import packages. import cvxpy as cp import numpy as np # Generate a random non-trivial linear program. m = 15 n = 10 np.random.seed(1) s0 = np.random.randn(m) lamb0 = np.maximum(-s0, 0) s0 = np.maximum(s0, 0) x0 = np.random.randn(n) A = np.random.randn(m, n) b = A@x0 + s0 c = -A.T@lamb0 # Define and solve the CVXPY problem. x = cp.Variable(n) prob = cp.Problem(cp.Minimize(c.T@x), [A@x <= b]) prob.solve() # Print result. print("\nThe optimal value is", prob.value) print("A solution x is") print(x.value) print("A dual solution is") print(prob.constraints[0].dual_value)