Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1c1e2af

Browse files
committedJul 28, 2024·
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent b6ad841 commit 1c1e2af

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed
 

‎linear_programming/interior_point_method.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import numpy as np
1111

12+
1213
class InteriorPointMethod:
1314
"""
1415
Operate on linear programming problems using the Primal-Dual Interior-Point Method.
@@ -21,7 +22,14 @@ class InteriorPointMethod:
2122
max_iter (int): Maximum number of iterations.
2223
"""
2324

24-
def __init__(self, c: np.ndarray, a: np.ndarray, b: np.ndarray, tol: float = 1e-8, max_iter: int = 100) -> None:
25+
def __init__(
26+
self,
27+
c: np.ndarray,
28+
a: np.ndarray,
29+
b: np.ndarray,
30+
tol: float = 1e-8,
31+
max_iter: int = 100,
32+
) -> None:
2533
self.c = c
2634
self.a = a
2735
self.b = b
@@ -33,7 +41,9 @@ def __init__(self, c: np.ndarray, a: np.ndarray, b: np.ndarray, tol: float = 1e-
3341

3442
def _is_valid_input(self) -> bool:
3543
"""Validate the input for the linear programming problem."""
36-
return (self.a.shape[0] == self.b.shape[0]) and (self.a.shape[1] == self.c.shape[0])
44+
return (self.a.shape[0] == self.b.shape[0]) and (
45+
self.a.shape[1] == self.c.shape[0]
46+
)
3747

3848
def _convert_to_standard_form(self):
3949
"""Convert constraints to standard form by adding slack and surplus variables."""
@@ -65,17 +75,23 @@ def solve(self) -> tuple[np.ndarray, float]:
6575
r2 = a.T @ y + s - c
6676
r3 = x * s
6777

68-
if np.linalg.norm(r1) < self.tol and np.linalg.norm(r2) < self.tol and np.linalg.norm(r3) < self.tol:
78+
if (
79+
np.linalg.norm(r1) < self.tol
80+
and np.linalg.norm(r2) < self.tol
81+
and np.linalg.norm(r3) < self.tol
82+
):
6983
break
7084

7185
mu = np.dot(x, s) / n
7286

7387
# Form the KKT matrix
74-
kkt = np.block([
75-
[np.zeros((n, n)), a.T, np.eye(n)],
76-
[a, np.zeros((m, m)), np.zeros((m, n))],
77-
[s_diag, np.zeros((n, m)), x_diag]
78-
])
88+
kkt = np.block(
89+
[
90+
[np.zeros((n, n)), a.T, np.eye(n)],
91+
[a, np.zeros((m, m)), np.zeros((m, n))],
92+
[s_diag, np.zeros((n, m)), x_diag],
93+
]
94+
)
7995

8096
# Right-hand side
8197
r = np.hstack([-r2, -r1, -r3 + mu * np.ones(n)])
@@ -84,8 +100,8 @@ def solve(self) -> tuple[np.ndarray, float]:
84100
delta = np.linalg.solve(kkt, r)
85101

86102
dx = delta[:n]
87-
dy = delta[n:n+m]
88-
ds = delta[n+m:]
103+
dy = delta[n : n + m]
104+
ds = delta[n + m :]
89105

90106
# Step size
91107
alpha_primal = min(1, 0.99 * min(-x[dx < 0] / dx[dx < 0], default=1))

0 commit comments

Comments
 (0)
Please sign in to comment.