Write python code to minimize a convex function subject to linear equality and linear inequality constraints using an primal-dual interior point method with Nesterov-Todd scaling and Mehrotra correction. Assume that a function already exists to solve the KKT system with matrix [H, A', G'; A 0 0; G 0 -W'W] where H is the hessian of the non-linear convex function being minimized. Please give an implementation of each step. Assume that the function f, its gradient, its hessian as well as a function solve_kkt_system(H, A, G, W, r_d, r_p, r_g) are provided externally.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here is an implementation of a primal-dual interior point method with Nesterov-Todd scaling and Mehrotra correction to minimize a convex function f(x) subject to linear equality constraints Ax = b and linear inequality constraints Gx <= h:
python
import numpy as np
def ipm(f, grad_f, hess_f, A, b, G, h, x0, s0, z0, tol=1e-8):
m, n = G.shape
x = x0
s = s0
z = z0
mu = (s.dot(z)) / (m+n)
tau = mu*10
max_iters = 100
alpha = 0.99995
beta = 0.1
for i in range(max_iters):
# Form KKT matrix
H = hess_f(x)
W = np.diag(z)
r_p = grad_f(x) - A.T@s - G.T@z
r_d = Ax - b
r_g = s.dot(z) - mu*m
dx, ds, dz = solve_kkt_system(H, A, G, W, r_d, r_p, r_g)
# Compute step length
sx = -x.dot(dx) / dx.dot(dx)
ss = -s.dot(ds) / ds.dot(ds)
sz = -z.dot(dz) / dz.dot(dz)
# Centrality parameter
sigma = (sx + ss + sz) / 3
# Compute affine step
x_aff = x + alpha*dx
s_aff = s + alpha*ds
z_aff = z + alpha*dz
# Evaluate centering parameter
mu_aff = (s_aff.dot(z_aff)) / (m + n)
# Mehrotra correction
dx = dx + (mu_aff/sigma - mu)*dx
ds = ds + (mu_aff/sigma - mu)*ds
dz = dz + (mu_aff/sigma - mu)*dz
# Compute maximum step length
sx = -x.dot(dx) / dx.dot(dx)
ss = -s.dot(ds) / ds.dot(ds)
sz = -z.dot(dz) / dz.dot(dz)
step = np.min([1.0, sx, ss, sz])
# Update iterates
x = x + step*beta*dx
s = s + step*beta*ds
z = z + step*beta*dz
# Update barrier parameter
mu = mu*np.max([1 - step, alpha])
# Check convergence
if np.max([np.linalg.norm(r_p), np.linalg.norm(r_d), np.linalg.norm(r_g)]) < tol:
break
return x, s, z
This implements the major steps of the algorithm, including forming the KKT system, computing the affine step, applying Mehrotra correction, line search, and updating the iterates. The key components like Nesterov-Todd scaling (W matrix) and Mehrotra correction are included.