Skip to content

Commit 26b5f75

Browse files
committed
simplex and IPA choice
1 parent 8097aaf commit 26b5f75

File tree

4 files changed

+61
-23
lines changed

4 files changed

+61
-23
lines changed
477 Bytes
Binary file not shown.

interior_point.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def interior_point_iteration(A, c, x_initial, alpha):
2424
x = D @ x_tilde
2525
return x
2626

27-
def interior_point(A, c, x_initial, alpha=0.5, optimal_tol=10e-5):
28-
iterations = 0
27+
def interior_point(A, c, x_initial, alpha=0.5, optimal_tol=10e-6):
28+
iterations_count = 0
2929
path = [x_initial]
3030

3131
x_prev = -10 * np.ones(c.size)
@@ -35,8 +35,8 @@ def interior_point(A, c, x_initial, alpha=0.5, optimal_tol=10e-5):
3535
x = interior_point_iteration(A, c, x, alpha)
3636

3737
path.append(x)
38-
iterations += 1
38+
iterations_count += 1
3939

40-
return x, path, iterations
40+
return x, path, iterations_count
4141

4242

linear_programming.py

+46-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import matplotlib.pyplot as plt
77

88
import interior_point
9-
from simplex import simplex
9+
import simplex
1010

1111

1212
GREATER_EQUAL = -1
@@ -105,6 +105,10 @@ def copy(self) -> "LinearProgrammingProblem":
105105
lp.is_maximizing = self.is_maximizing
106106
lp.objective = self.objective.copy()
107107

108+
lp._A_updated = False
109+
lp._b_updated = False
110+
lp._c_updated = False
111+
108112
return lp
109113

110114

@@ -220,22 +224,54 @@ def compute_feasible_basis(self):
220224
def is_feasible(lp: "LinearProgrammingProblem", x: np.array, tol=10e-5) -> bool:
221225
return all(np.isclose(lp.A @ x, lp.b, atol=tol))
222226

223-
def solve(self, method=SOLVER_SIMPLEX):
224-
if (method == SOLVER_SIMPLEX):
225-
lp_slacked = self.slacken_problem()
227+
def solve(self, method=None):
228+
lp_slacked = self.slacken_problem()
229+
# Decide which solver to use
230+
# Simplex?
231+
if (method == None):
232+
x_zero = simplex.zero_point_solution(lp_slacked.A, lp_slacked.b, lp_slacked.c)
233+
if (simplex.is_feasible(lp_slacked.A, x_zero, lp_slacked.b)):
234+
# If zero point solution is feasible, use simplex
235+
method = SOLVER_SIMPLEX
236+
237+
if (method == None):
238+
# Solve using interior point algorithm
239+
x_ones = np.ones(len(self.variables))
240+
x_initial = np.hstack((x_ones, self.b - (self.A @ x_ones)))
241+
if (self.is_feasible(self, x_initial)):
242+
# IPA is feasible
243+
method = SOLVER_INTERIOR_POINT_METHOD
226244

227-
x_sol = simplex(lp_slacked.A, lp_slacked.b, lp_slacked.c, lp_slacked.vars_slack_amount)
245+
246+
x_sol = None
247+
iterations_count = None
248+
249+
if (method == SOLVER_SIMPLEX):
250+
# Solve using simplex
251+
x_sol, iterations_count = simplex.simplex(lp_slacked.A, lp_slacked.b, lp_slacked.c, lp_slacked.vars_slack_amount)
228252

229253
# Cut slack variables
230254
x_sol = x_sol[:lp_slacked.c.size - lp_slacked.vars_slack_amount]
231-
print(x_sol)
232255

233-
return x_sol
256+
elif (method == SOLVER_INTERIOR_POINT_METHOD):
257+
# Solve using interior point algorithm
258+
x_ones = np.ones(len(self.variables))
259+
x_initial = np.hstack((x_ones, self.b - (self.A @ x_ones)))
260+
261+
#x_initial = np.array([1 for _ in range(len(lp_slacked.variables))])
262+
x_sol, path, iterations_count = interior_point.interior_point(lp_slacked.A, lp_slacked.c, x_initial)
263+
264+
# Cut slack variables
265+
x_sol = x_sol[:lp_slacked.c.size - lp_slacked.vars_slack_amount]
266+
267+
else:
268+
print("No solution found")
269+
return
234270

235-
if (method == SOLVER_INTERIOR_POINT_METHOD):
236-
pass
271+
print(f"Solving using {method}")
272+
print(f"Solved in {iterations_count} iterations")
237273

238-
return "x"
274+
return x_sol
239275

240276

241277
def plot_solution_path(self):

simplex.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
"""
1313

1414

15-
def simplex(A: np.array, b: np.array, c: np.array, slacks_amt: int) -> np.array:
15+
def simplex(A: np.array, b: np.array, c: np.array, slacks_amt: int) -> "tuple[np.array, int]":
1616
# https://sdu.itslearning.com/ContentArea/ContentArea.aspx?LocationID=17727&LocationType=1&ElementID=589350
1717
#x_current = basic_feasible_solution(A, b, c)
1818
#N = [i for i in range(c.size - slacks_amt)] # Not In basis
1919
B = [i + (c.size - slacks_amt) for i in range(slacks_amt)] # In basis
20+
# Zero basic solution
21+
22+
iterations_count = 0
2023

2124
while True:
22-
tableau(A, b, c)
25+
iterations_count += 1
2326
## Choosing a pivot
2427
# Pick a non-negative column TODO
2528
positives = np.where(c > 0)
@@ -73,19 +76,19 @@ def simplex(A: np.array, b: np.array, c: np.array, slacks_amt: int) -> np.array:
7376
x_sol = np.zeros(c.size)
7477
for i in range(len(B)):
7578
x_sol[B[i]] = b[i]
76-
return np.array(x_sol)
79+
return np.array(x_sol), iterations_count
7780

7881
def row_mult(A: np.array, row_index: int, scalar: int) -> np.array:
79-
# Row operation: R1 = scalar * R1
80-
# Inspiration: (TODO - optimize)
82+
# Row operation: R1 = scalar * R1 (TODO - optimize)
83+
# Inspiration:
8184
# https://personal.math.ubc.ca/~pwalls/math-python/linear-algebra/solving-linear-systems/
8285
I = np.eye(A.shape[0])
8386
I[row_index, row_index] = scalar
8487
return I @ A
8588

8689
def row_add(A: np.array, row_main_index: int, row_other_index: int, scalar: int) -> np.array:
87-
# Row operation: R1 = R1 + scalar * R2
88-
# Inspiration: (TODO - optimize)
90+
# Row operation: R1 = R1 + scalar * R2 (TODO - optimize)
91+
# Inspiration:
8992
# https://personal.math.ubc.ca/~pwalls/math-python/linear-algebra/solving-linear-systems/
9093

9194
I = np.eye(A.shape[0])
@@ -109,8 +112,7 @@ def zero_point_solution(A: np.array, b: np.array, c: np.array) -> np.array:
109112
slack_sign = [A[i][num_decision+i] for i in range(len(b))]
110113
slack_values = [b[i]/slack_sign[i] for i in range(b.size)]
111114
zero_point = np.hstack((np.zeros(num_decision), slack_values))
112-
print("zero_point:", zero_point)
113-
print("Zero point solution is feasible:", is_feasible(A, zero_point, b))
115+
114116
return zero_point
115117

116118

0 commit comments

Comments
 (0)