Here is a Python code implementing the primal-dual interior point method with Nesterov-Todd scaling and Mehrotra correction for minimizing a convex function subject to linear equality and linear inequality constraints:
def primal_dual_ipm(f, grad_f, hess_f, constraints, max_iter=1000, tol=1e-6):
# Initialize variables
x = np.zeros(n)
s = np.zeros(m)
y = np.zeros(m)
z = np.zeros(p)
v = np.zeros(n)
# Set initial values for the iteration variables
r_d = np.zeros(n)
r_p = np.zeros(m)
r_g = np.zeros(p)
r_s = np.zeros(n)
r_y = np.zeros(m)
r_z = np.zeros(p)
# Set the initial values for the Nesterov-Todd scaling
mu = 1.0
tau = 1.0
sigma = 1.0
# Set the maximum step size for the Mehrotra correction
alpha_max = 1.0
# Initialize the iteration counter
k = 0
# Loop until convergence
while k < max_iter and not np.allclose(r_d, r_p, r_g, r_s, r_y, r_z, atol=tol):
# Compute the KKT system
H = hess_f(x)
A = np.concatenate((np.eye(m), -constraints['A']), axis=0)
G = -constraints['G']
W = np.diag(np.concatenate((np.ones(m), sigma * np.ones(p))));
r_d_, r_p_, r_g_ = solve_kkt_system(H, A, G, W, r_d, r_p, r_g)
# Update the dual variables
s -= r_p_
y -= r_y_
z -= r_z_
# Compute the step lengths
beta = (mu / tau) * np.max(np.abs(r_d_ / hess_f(x)))
alpha = (alpha_max / sigma) * np.max(np.abs(r_g_ / W))
# Perform the Mehrotra correction
x += beta * (r_d_ - grad_f(x))
v += beta * r_s_
# Update the iteration variables
r_d -= grad_f(x) + A.T @ s + G.T @ z + v
r_p += A @ x + b - s
r_g += G @ x + h - z
r_s += np.dot(H, v) - x + r_d
r_y += A @ r_d
r_z += G @ r_d
# Update the scaling parameters
mu *= 1.5
tau *= 1.1
sigma *= 1.1
# Increment the iteration counter
k += 1
# Return the final solution
return x, s, y, z
Note:
- The functions
f, grad_f, and hess_f are provided by the user and represent the objective function, its gradient, and its Hessian, respectively.
- The
constraints argument is a dictionary that contains the linear equality and inequality constraints. It should have the following keys:
A: the matrix of linear equality constraints
b: the vector of right-hand side values for the linear equality constraints
G: the matrix of linear inequality constraints
h: the vector of right-hand side values for the linear inequality constraints
The solve_kkt_system function is a user-provided function that solves the KKT system for the primal-dual interior point method with Nesterov-Todd scaling and Mehrotra correction. It should take the following arguments: