Take the Master Linear Programming in Python with PuLP course
In algorithm design, some seemingly simple problems often evade simple solutions. For such problems, the solution requires the help of specific mathematical tools. Once these tools are employed, there's often a direct path to the solution. Such problems offer a unique opportunity to understand complex and difficult mathematical ideas.
This blog discusses a Computational Geometry problem that evades simple solutions. In solving this problem, the required mathematical ideas will become clear. The problem statement is below.
Problem statement: Given a set of
As shown in the figure below, each line segment is described by the following three values:
x-coordinate
upper y-coordinate
lower y-coordinate
The transversal line of
Find a specific transversal line. For example, the line with the lowest/highest slope.
Identify all transversal lines of
This blog will also solve the variants of the problem.
Let us try to find a transversal line of
For
Each line segment in
The conditions are linear constraints in the unknowns
Mathematical optimization is the abstraction of the problem of finding the best solution among all possible solutions. A mathematical optimization problem has the form
Where
The difficulty of solving an optimization problem depends on the nature of the objective and the constraint functions. The simplest form of an optimization problem is called a linear program, where the objective and constraint functions are linear.
In the objective function
Luckily, the vertical line segment transversal problem maps to a linear program.
The following linear program checks for the existence of a transversal line.
Here, the optimization variable is
The objective function
can be used to indicate our preference. For example, to find the transversal line with the maximum gradient, set .
The optimize.linprog
function of Python's SciPy library can be used to solve the problem. The code below returns the optimal value of the optimization variable in case the linear program is feasible. Otherwise, it prints the message The problem is infeasible
.
import numpy as npfrom scipy.optimize import linprog# Problem instance: Feasible solution existsn = 3x = np.array([-3, -1, 2])y_plus = np.array([2, 1, 2])y_minus = np.array([-2, -1, -3])# Problem instance: Feasible solution does not exist# n = 3# x = np.array([-2.5, 0, 2])# y_plus = np.array([2, 0, 4])# y_minus = np.array([-1, -2, 2])# Objective function for line with maximum gradientg = np.array([-1, 0])# Define the inequality constraint matrix A and vector bones = np.ones(n)A_1 = np.vstack((-x, ones)).TA_2 = np.vstack((x, -ones)).TA = np.vstack((A_1,A_2))b = np.hstack((-y_minus, y_plus)).T# # Define the bounds for the decision variablesbounds = [(None, None), (None, None)]# # Solve the linear programming problemresult = linprog(g, A_ub=A, b_ub=b, bounds=bounds)# Print resultif result.status == 2:print("The problem is infeasible")else:# Print the solutionprint(result.x)
Let us visualize the solution of the problem instance given in the code above. In the figure on the left, the three vertical line segments clearly admit several transversal lines. In the figure on the right, the set of transversal lines are visualized by plotting the feasible set of the constraint functions in the
The plot on the right depicts what is known as the dual plane. A line
The problem instance in lines 11–14 is shown below, where there is no transversal line and the green strips don't have a common region of intersection. For this case, the linear program is infeasible and hence the program prints The problem is infeasible
.
Visualize the set of transversal lines by projecting the feasible region in the dual plane back to the primal plane.
The feasible region is a convex polygon. Each vertex of the convex polygon represents a line in the primal plane. These vertices are projected back to the primal plane as dashed lines and the region in between all pairs of lines is filled to represent the set of transversal lines. There are some examples in the slides below.
The number of dashed lines on the left is equal to the number of vertices of the blue feasible polygon on the right.
Free Resources