Linear programming with the simplex method
In this recipe, we consider a special case of optimization problems, problems with linear constraints. These tasks imply that you need to optimize (maximize or minimize) a linear combination of positive variables, taking into account a set of linear constraints. Linear programming doesn't have well-known and direct applications in computer vision, but you may encounter it somewhere down the road. So, let's see how you can deal with linear programming problems using OpenCV.
Getting ready
Before you proceed with this recipe, you need to install the OpenCV 3.0 (or greater) Python API package.
How to do it...
You need to complete the following steps:
- Import the modules:
import cv2 import numpy as np
- Create a linear constraint matrix and weights for the function, which we're going to optimize:
m = 10 n = 10 constraints_mat = np.random.randn(m, n+1) weights = np.random.randn(1, n)
- Apply the simplex method to the task by invoking
cv2.solveLP
. Then, parse the result...