Compare commits
10
Commits
4dce78c370
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a76dbc1ea9 | ||
|
|
8566bc1e3b | ||
|
|
3472cda172 | ||
|
|
6468387814 | ||
|
|
68081a4287 | ||
|
|
84eb70638e | ||
|
|
15654b84c0 | ||
|
|
1b783e663a | ||
|
|
9fb3634e9b | ||
|
|
393a02feba |
@@ -14,20 +14,22 @@ class Result:
|
|||||||
objective_function_value: Optional[np.float64]
|
objective_function_value: Optional[np.float64]
|
||||||
solution: Optional[np.array]
|
solution: Optional[np.array]
|
||||||
maximize: bool
|
maximize: bool
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
state: State,
|
state: State,
|
||||||
objective_function_value: Optional[np.array] = None,
|
objective_function_value: Optional[np.array] = None,
|
||||||
solution: np.float64 = None, maximize:bool = True):
|
solution: np.float64 = None, maximize: bool = True):
|
||||||
self.state = state
|
self.state = state
|
||||||
self.objective_function_value = objective_function_value
|
self.objective_function_value = objective_function_value
|
||||||
self.solution = solution
|
self.solution = solution
|
||||||
self.maximize = maximize
|
self.maximize = maximize
|
||||||
#def print_initial_inputs(Vector &C, Matrix &A, Vector &b, double eps, bool maximize)
|
|
||||||
|
|
||||||
def print_initial_inputs(
|
def print_initial_inputs(
|
||||||
C: np.array, # Vector of objective function coefficients
|
C: np.array, # Vector of objective function coefficients
|
||||||
A: np.array, # Matrix of constraint coefficients
|
A: np.array, # Matrix of constraint coefficients
|
||||||
x_0: np.array, # Initial point (vector)
|
|
||||||
b: np.array, # Vector of right-hand side values of constraints
|
b: np.array, # Vector of right-hand side values of constraints
|
||||||
|
x_0: np.array, # Initial point (vector)
|
||||||
eps: np.float64 = 0.01, # Solution accuracy
|
eps: np.float64 = 0.01, # Solution accuracy
|
||||||
alpha: np.float64 = 0.5, # Step coefficient
|
alpha: np.float64 = 0.5, # Step coefficient
|
||||||
maximize: bool = True):
|
maximize: bool = True):
|
||||||
@@ -44,15 +46,14 @@ def print_initial_inputs(
|
|||||||
|
|
||||||
print("Minimize")
|
print("Minimize")
|
||||||
|
|
||||||
|
|
||||||
z_str = "z = "
|
z_str = "z = "
|
||||||
previousIsZero = True
|
previousIsZero = True
|
||||||
lastNonZero = False
|
lastNonZero = False
|
||||||
for i in range(len(C)):
|
for i in range(C.shape[0]):
|
||||||
|
|
||||||
isNegative = False;
|
isNegative = False
|
||||||
|
|
||||||
for k in range(len(C)):
|
for k in range(C.shape[0]):
|
||||||
|
|
||||||
if (C[k] == 0):
|
if (C[k] == 0):
|
||||||
|
|
||||||
@@ -61,11 +62,8 @@ def print_initial_inputs(
|
|||||||
lastNonZero = False
|
lastNonZero = False
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (not previousIsZero and not lastNonZero):
|
if (not previousIsZero and not lastNonZero):
|
||||||
z_str += " + ";
|
z_str += " + "
|
||||||
|
|
||||||
|
|
||||||
if (C[i] != 0):
|
if (C[i] != 0):
|
||||||
if (C[i] != 1):
|
if (C[i] != 1):
|
||||||
@@ -83,16 +81,14 @@ def print_initial_inputs(
|
|||||||
previousIsZero = True
|
previousIsZero = True
|
||||||
|
|
||||||
print(z_str)
|
print(z_str)
|
||||||
print("\nsubject to the constrains:\n");
|
print("\nsubject to the constrains:\n")
|
||||||
for i in range(len(b)):
|
for i in range(b.shape[0]):
|
||||||
c_str = ""
|
c_str = ""
|
||||||
previousIsZero = True
|
previousIsZero = True
|
||||||
lastNonZero = False
|
lastNonZero = False
|
||||||
for j in range(len(A[i])):
|
for j in range(A.shape[1]):
|
||||||
#for (int j = 0; j < A.getColumns(); j++)
|
|
||||||
isNegative = False
|
isNegative = False
|
||||||
for k in range(j, len(A[i])):
|
for k in range(j, A.shape[1]):
|
||||||
#for (int k = j; k < A[i].size(); k++)
|
|
||||||
|
|
||||||
if (A[i][k] == 0):
|
if (A[i][k] == 0):
|
||||||
|
|
||||||
@@ -104,7 +100,6 @@ def print_initial_inputs(
|
|||||||
if (not previousIsZero and not lastNonZero):
|
if (not previousIsZero and not lastNonZero):
|
||||||
c_str += " + "
|
c_str += " + "
|
||||||
|
|
||||||
|
|
||||||
if (A[i][j] != 0):
|
if (A[i][j] != 0):
|
||||||
if (A[i][j] != 1):
|
if (A[i][j] != 1):
|
||||||
if (A[i][j] < 0):
|
if (A[i][j] < 0):
|
||||||
@@ -121,23 +116,21 @@ def print_initial_inputs(
|
|||||||
else:
|
else:
|
||||||
previousIsZero = True
|
previousIsZero = True
|
||||||
|
|
||||||
|
|
||||||
c_str += " <= " + str(b[i])
|
c_str += " <= " + str(b[i])
|
||||||
print(c_str)
|
print(c_str)
|
||||||
|
|
||||||
|
|
||||||
def print_result(result:Result):
|
def print_result(result: Result):
|
||||||
|
|
||||||
if (result.state == State.INAPPLICABLE):
|
if (result.state == State.INAPPLICABLE):
|
||||||
print("The method is not applicable!")
|
print("The method is not applicable!")
|
||||||
elif (result.state == State.UNSOLVED ):
|
elif (result.state == State.UNSOLVED):
|
||||||
print("Unsolved problem!")
|
print("Unsolved problem!")
|
||||||
else:
|
else:
|
||||||
print("SOLVED!")
|
print("SOLVED!")
|
||||||
decVar_str = ""
|
decVar_str = ""
|
||||||
decVar_str +="Decision variables: ["
|
decVar_str += "Decision variables: ["
|
||||||
for i in range(len(result.solution)):
|
for i in range(len(result.solution)):
|
||||||
#for (int i = 0; i < result.solution.size(); i++)
|
|
||||||
decVar_str += str(result.solution[i])
|
decVar_str += str(result.solution[i])
|
||||||
if (i != len(result.solution) - 1):
|
if (i != len(result.solution) - 1):
|
||||||
|
|
||||||
@@ -147,11 +140,8 @@ def print_result(result:Result):
|
|||||||
print(decVar_str)
|
print(decVar_str)
|
||||||
res_str = ""
|
res_str = ""
|
||||||
if (result.maximize):
|
if (result.maximize):
|
||||||
|
|
||||||
res_str += "Maximum "
|
res_str += "Maximum "
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
res_str += "Minimum "
|
res_str += "Minimum "
|
||||||
|
|
||||||
res_str += f"objective function value: {result.objective_function_value}"
|
res_str += f"objective function value: {result.objective_function_value}"
|
||||||
@@ -164,39 +154,35 @@ def interior_point(
|
|||||||
A: np.array, # Matrix of constraint coefficients
|
A: np.array, # Matrix of constraint coefficients
|
||||||
b: np.array, # Vector of right-hand side values of constraints
|
b: np.array, # Vector of right-hand side values of constraints
|
||||||
x_0: np.array, # Initial point (vector)
|
x_0: np.array, # Initial point (vector)
|
||||||
eps: np.float64 = 0.01, # Solution accuracy
|
eps: np.float64 = 1e-6, # Solution accuracy
|
||||||
alpha: np.float64 = 0.5, # Step coefficient
|
alpha: np.float64 = 0.5, # Step coefficient
|
||||||
maximizing: bool = True) -> Result: # Flag for maximization or minimization
|
maximizing: bool = True) -> Result: # Flag for maximization or minimization
|
||||||
|
|
||||||
|
# number of non-slack variables
|
||||||
|
k = C.shape[0]
|
||||||
|
|
||||||
# Check if the method is applicable: the initial point must satisfy the constraints
|
# Check if the method is applicable: the initial point must satisfy the constraints
|
||||||
if (not np.all(np.dot(A, x_0) >= b) or np.any(x_0 == 0)):
|
if (not np.all(np.dot(A, x_0) <= b) or np.any(x_0 <= 0)):
|
||||||
return Result(State.INAPPLICABLE, maximize=maximizing)
|
return Result(State.INAPPLICABLE, maximize=maximizing)
|
||||||
# If the problem is a minimization, invert the coefficients of the objective function
|
# If the problem is a minimization, invert the coefficients of the objective function
|
||||||
if (not maximizing):
|
if (not maximizing):
|
||||||
C = -C
|
C = -C
|
||||||
|
|
||||||
m = len(A) # Number of constraints
|
m = A.shape[0] # Number of constraints
|
||||||
n = len(A[0]) # Number of variables
|
n = A.shape[1] # Number of variables
|
||||||
|
|
||||||
# Initialize variables for the initial iteration
|
# Initialize variables for the initial iteration
|
||||||
x = np.ones(n) # Solution vector
|
x_0 = np.concatenate((x_0, b-np.dot(A, x_0)))
|
||||||
s = np.ones(m) # Slack variables vector
|
x = x_0 # Solution vector
|
||||||
|
|
||||||
|
A = np.concatenate((A, np.eye(m)), axis=1)
|
||||||
|
C = np.concatenate((C, np.zeros(m)))
|
||||||
|
|
||||||
iteration = 0 # Iteration counter
|
iteration = 0 # Iteration counter
|
||||||
|
|
||||||
while(True):
|
while (True):
|
||||||
# Calculate slack variables for each constraint
|
|
||||||
for i in range(m):
|
|
||||||
slack = b[i]
|
|
||||||
for j in range(n):
|
|
||||||
slack -= A[i][j] * x[j]
|
|
||||||
s[i] = slack
|
|
||||||
|
|
||||||
# Update solution variables
|
|
||||||
for i in range(min(m, n)):
|
|
||||||
x[i] = s[i]
|
|
||||||
|
|
||||||
# Create a diagonal matrix from the slack variables vector
|
# Create a diagonal matrix from the slack variables vector
|
||||||
D = np.diag(s)
|
D = np.diag(x)
|
||||||
|
|
||||||
# Solve the system of equations to find x*
|
# Solve the system of equations to find x*
|
||||||
x_star = np.dot(np.linalg.inv(D), x)
|
x_star = np.dot(np.linalg.inv(D), x)
|
||||||
@@ -204,19 +190,25 @@ def interior_point(
|
|||||||
C_star = np.dot(D, C)
|
C_star = np.dot(D, C)
|
||||||
|
|
||||||
# Form the projection matrix
|
# Form the projection matrix
|
||||||
I = np.eye(n)
|
|
||||||
A_star_transpose = np.transpose(A_star)
|
A_star_transpose = np.transpose(A_star)
|
||||||
P = I - np.dot(A_star_transpose, np.linalg.inv(np.dot(A_star, A_star_transpose)))
|
P = np.eye(n+m) - np.dot(np.dot(A_star_transpose, np.linalg.inv(np.dot(A_star, A_star_transpose))), A_star)
|
||||||
P = np.dot(P, A_star)
|
|
||||||
|
|
||||||
# Calculate the gradient of the objective function
|
# Calculate the gradient of the objective function
|
||||||
C_p = np.dot(P, C_star)
|
C_p = np.dot(P, C_star)
|
||||||
Mu = np.max(np.absolute(C_p))
|
Mu = np.min(C_p)
|
||||||
|
if (Mu > 0):
|
||||||
|
return Result(State.INAPPLICABLE)
|
||||||
|
|
||||||
|
# Update the value of x* considering the step size and gradient
|
||||||
|
x_star = np.ones(n+m) + alpha / np.abs(Mu) * C_p
|
||||||
|
x_new = np.dot(D, x_star)
|
||||||
# Check the stopping criterion based on accuracy
|
# Check the stopping criterion based on accuracy
|
||||||
if Mu < eps:
|
if np.linalg.norm(x_new - x) <= eps:
|
||||||
result = np.dot(C, x)
|
result = np.dot(C, x) if (maximizing) else -np.dot(C, x)
|
||||||
return Result(State.SOLVED, objective_function_value=result, solution=x, maximize=maximizing)
|
x = x[:k]
|
||||||
|
return Result(
|
||||||
|
State.SOLVED, objective_function_value=np.round(result, 3), solution=np.round(x, 3), maximize=maximizing
|
||||||
|
)
|
||||||
|
|
||||||
iteration += 1
|
iteration += 1
|
||||||
|
|
||||||
@@ -224,17 +216,13 @@ def interior_point(
|
|||||||
if iteration >= 1000:
|
if iteration >= 1000:
|
||||||
return Result(State.UNSOLVED, maximize=maximizing)
|
return Result(State.UNSOLVED, maximize=maximizing)
|
||||||
|
|
||||||
# Update the value of x* considering the step size and gradient
|
x = x_new
|
||||||
x_star += (alpha / Mu) * C_p
|
|
||||||
x = np.dot(D, x_star)
|
|
||||||
|
|
||||||
|
|
||||||
# TODO 5 tests (from assignment 1) and comparison with simplex and alpha = 0.9
|
# TODO 5 tests (from assignment 1) and comparison with simplex and alpha = 0.9
|
||||||
def TEST_CASE_GENERAL():
|
def TEST_CASE_GENERAL_A05():
|
||||||
|
|
||||||
print("----------------------------RUNNING_TEST_GENERAL_CASE----------------------------")
|
|
||||||
|
|
||||||
|
|
||||||
|
print("----------------------RUNNING_TEST_GENERAL_CASE----------------------")
|
||||||
C = np.array([5, 4])
|
C = np.array([5, 4])
|
||||||
A = np.array([
|
A = np.array([
|
||||||
[6, 4],
|
[6, 4],
|
||||||
@@ -243,16 +231,17 @@ def TEST_CASE_GENERAL():
|
|||||||
[0, 1]])
|
[0, 1]])
|
||||||
b = np.array([24, 6, 1, 2])
|
b = np.array([24, 6, 1, 2])
|
||||||
x_0 = np.array([1, 1])
|
x_0 = np.array([1, 1])
|
||||||
eps = 0.01
|
eps = 1e-4
|
||||||
alpha = 0.5
|
alpha = 0.5
|
||||||
maximize = True
|
maximize = True
|
||||||
|
|
||||||
print_initial_inputs(C, A, b, x_0, eps, alpha, maximize);
|
print_initial_inputs(C, A, b, x_0, eps, alpha, maximize)
|
||||||
result = interior_point(C, A, b, x_0, eps, alpha, maximize);
|
result = interior_point(C, A, b, x_0, eps, alpha, maximize)
|
||||||
|
|
||||||
expected_state = State.SOLVED
|
expected_state = State.SOLVED
|
||||||
if result.state == expected_state:
|
if result.state == expected_state:
|
||||||
print_result(result)
|
print_result(result)
|
||||||
|
return 1
|
||||||
else:
|
else:
|
||||||
state_name = ""
|
state_name = ""
|
||||||
if result.state == State.UNSOLVED:
|
if result.state == State.UNSOLVED:
|
||||||
@@ -263,27 +252,60 @@ def TEST_CASE_GENERAL():
|
|||||||
state_name = "SOLVED"
|
state_name = "SOLVED"
|
||||||
print(f"incorrect state type. expected SOLVED, got {state_name}.")
|
print(f"incorrect state type. expected SOLVED, got {state_name}.")
|
||||||
|
|
||||||
def TEST_MINIMIZE_CASE():
|
|
||||||
|
|
||||||
print("----------------------------RUNNING_TEST_MINIMIZE_CASE----------------------------")
|
def TEST_CASE_GENERAL_A09():
|
||||||
|
|
||||||
|
print("----------------------RUNNING_TEST_GENERAL_CASE----------------------")
|
||||||
|
C = np.array([5, 4])
|
||||||
|
A = np.array([
|
||||||
|
[6, 4],
|
||||||
|
[1, 2],
|
||||||
|
[-1, 1],
|
||||||
|
[0, 1]])
|
||||||
|
b = np.array([24, 6, 1, 2])
|
||||||
|
x_0 = np.array([1, 1])
|
||||||
|
eps = 1e-4
|
||||||
|
alpha = 0.9
|
||||||
|
maximize = True
|
||||||
|
|
||||||
|
print_initial_inputs(C, A, b, x_0, eps, alpha, maximize)
|
||||||
|
result = interior_point(C, A, b, x_0, eps, alpha, maximize)
|
||||||
|
|
||||||
|
expected_state = State.SOLVED
|
||||||
|
if result.state == expected_state:
|
||||||
|
print_result(result)
|
||||||
|
return 1
|
||||||
|
else:
|
||||||
|
state_name = ""
|
||||||
|
if result.state == State.UNSOLVED:
|
||||||
|
state_name = "UNSOLVED"
|
||||||
|
elif result.state == State.INAPPLICABLE:
|
||||||
|
state_name = "INAPPLICABLE"
|
||||||
|
elif result.state == State.SOLVED:
|
||||||
|
state_name = "SOLVED"
|
||||||
|
print(f"incorrect state type. expected SOLVED, got {state_name}.")
|
||||||
|
|
||||||
|
|
||||||
|
def TEST_MINIMIZE_CASE_A05():
|
||||||
|
print("----------------------RUNNING_TEST_MINIMIZE_CASE----------------------")
|
||||||
C = np.array([-2, 2, -6])
|
C = np.array([-2, 2, -6])
|
||||||
A = np.array([
|
A = np.array([
|
||||||
[2, 1, -2]
|
[2, 1, -2],
|
||||||
[1, 2, 4]
|
[1, 2, 4],
|
||||||
[1, -1, 2]])
|
[1, -1, 2]])
|
||||||
b = np.array([24, 23, 10])
|
b = np.array([24, 23, 10])
|
||||||
x_0 = np.array([1, 1])
|
x_0 = np.array([1, 1, 1])
|
||||||
eps = 0.01
|
eps = 1e-4
|
||||||
alpha = 0.5
|
alpha = 0.5
|
||||||
maximize = True
|
maximize = False
|
||||||
|
|
||||||
|
print_initial_inputs(C, A, b, x_0, eps, alpha, maximize)
|
||||||
print_initial_inputs(C, A, b, x_0, eps, alpha, maximize);
|
result = interior_point(C, A, b, x_0, eps, alpha, maximize)
|
||||||
result = interior_point(C, A, b, x_0, eps, alpha, maximize);
|
|
||||||
|
|
||||||
expected_state = State.SOLVED
|
expected_state = State.SOLVED
|
||||||
if result.state == expected_state:
|
if result.state == expected_state:
|
||||||
print_result(result)
|
print_result(result)
|
||||||
|
return 1
|
||||||
else:
|
else:
|
||||||
state_name = ""
|
state_name = ""
|
||||||
if result.state == State.UNSOLVED:
|
if result.state == State.UNSOLVED:
|
||||||
@@ -293,11 +315,43 @@ def TEST_MINIMIZE_CASE():
|
|||||||
elif result.state == State.SOLVED:
|
elif result.state == State.SOLVED:
|
||||||
state_name = "SOLVED"
|
state_name = "SOLVED"
|
||||||
print(f"incorrect state type. expected SOLVED, got {state_name}.")
|
print(f"incorrect state type. expected SOLVED, got {state_name}.")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def TEST_MINIMIZE_CASE_A09():
|
||||||
|
print("----------------------RUNNING_TEST_MINIMIZE_CASE----------------------")
|
||||||
|
C = np.array([-2, 2, -6])
|
||||||
|
A = np.array([
|
||||||
|
[2, 1, -2],
|
||||||
|
[1, 2, 4],
|
||||||
|
[1, -1, 2]])
|
||||||
|
b = np.array([24, 23, 10])
|
||||||
|
x_0 = np.array([1, 1, 1])
|
||||||
|
eps = 1e-4
|
||||||
|
alpha = 0.9
|
||||||
|
maximize = False
|
||||||
|
|
||||||
def TEST_WITH_SLACK_CASE():
|
print_initial_inputs(C, A, b, x_0, eps, alpha, maximize)
|
||||||
print("----------------------------RUNNING_TEST_WITH_SLACK_CASE----------------------------")
|
result = interior_point(C, A, b, x_0, eps, alpha, maximize)
|
||||||
|
|
||||||
|
expected_state = State.SOLVED
|
||||||
|
if result.state == expected_state:
|
||||||
|
print_result(result)
|
||||||
|
return 1
|
||||||
|
else:
|
||||||
|
state_name = ""
|
||||||
|
if result.state == State.UNSOLVED:
|
||||||
|
state_name = "UNSOLVED"
|
||||||
|
elif result.state == State.INAPPLICABLE:
|
||||||
|
state_name = "INAPPLICABLE"
|
||||||
|
elif result.state == State.SOLVED:
|
||||||
|
state_name = "SOLVED"
|
||||||
|
print(f"incorrect state type. expected SOLVED, got {state_name}.")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def TEST_WITH_SLACK_CASE_A05():
|
||||||
|
print("----------------------RUNNING_TEST_WITH_SLACK_CASE----------------------")
|
||||||
|
|
||||||
C = np.array([2, -1, 0, -1])
|
C = np.array([2, -1, 0, -1])
|
||||||
A = np.array([
|
A = np.array([
|
||||||
@@ -305,18 +359,18 @@ def TEST_WITH_SLACK_CASE():
|
|||||||
[-2, -1, 0, -2],
|
[-2, -1, 0, -2],
|
||||||
[3, 2, 0, 1]])
|
[3, 2, 0, 1]])
|
||||||
b = np.array([10, 18, 36])
|
b = np.array([10, 18, 36])
|
||||||
x_0 = np.array([1, 1])
|
x_0 = np.array([1, 1, 1, 1])
|
||||||
eps = 0.01
|
eps = 1e-4
|
||||||
alpha = 0.5
|
alpha = 0.5
|
||||||
maximize = True
|
maximize = True
|
||||||
|
|
||||||
|
print_initial_inputs(C, A, b, x_0, eps, alpha, maximize)
|
||||||
print_initial_inputs(C, A, b, x_0, eps, alpha, maximize);
|
result = interior_point(C, A, b, x_0, eps, alpha, maximize)
|
||||||
result = interior_point(C, A, b, x_0, eps, alpha, maximize);
|
|
||||||
|
|
||||||
expected_state = State.SOLVED
|
expected_state = State.SOLVED
|
||||||
if result.state == expected_state:
|
if result.state == expected_state:
|
||||||
print_result(result)
|
print_result(result)
|
||||||
|
return 1
|
||||||
else:
|
else:
|
||||||
state_name = ""
|
state_name = ""
|
||||||
if result.state == State.UNSOLVED:
|
if result.state == State.UNSOLVED:
|
||||||
@@ -327,8 +381,41 @@ def TEST_WITH_SLACK_CASE():
|
|||||||
state_name = "SOLVED"
|
state_name = "SOLVED"
|
||||||
print(f"incorrect state type. expected SOLVED, got {state_name}.")
|
print(f"incorrect state type. expected SOLVED, got {state_name}.")
|
||||||
|
|
||||||
def TEST_UNBOUNDED_CASE():
|
|
||||||
print("----------------------------RUNNING_TEST_UNBOUNDED_CASE----------------------------")
|
def TEST_WITH_SLACK_CASE_A09():
|
||||||
|
print("----------------------RUNNING_TEST_WITH_SLACK_CASE----------------------")
|
||||||
|
|
||||||
|
C = np.array([2, -1, 0, -1])
|
||||||
|
A = np.array([
|
||||||
|
[1, -2, 1, 0],
|
||||||
|
[-2, -1, 0, -2],
|
||||||
|
[3, 2, 0, 1]])
|
||||||
|
b = np.array([10, 18, 36])
|
||||||
|
x_0 = np.array([1, 1, 1, 1])
|
||||||
|
eps = 1e-4
|
||||||
|
alpha = 0.9
|
||||||
|
maximize = True
|
||||||
|
|
||||||
|
print_initial_inputs(C, A, b, x_0, eps, alpha, maximize)
|
||||||
|
result = interior_point(C, A, b, x_0, eps, alpha, maximize)
|
||||||
|
|
||||||
|
expected_state = State.SOLVED
|
||||||
|
if result.state == expected_state:
|
||||||
|
print_result(result)
|
||||||
|
return 1
|
||||||
|
else:
|
||||||
|
state_name = ""
|
||||||
|
if result.state == State.UNSOLVED:
|
||||||
|
state_name = "UNSOLVED"
|
||||||
|
elif result.state == State.INAPPLICABLE:
|
||||||
|
state_name = "INAPPLICABLE"
|
||||||
|
elif result.state == State.SOLVED:
|
||||||
|
state_name = "SOLVED"
|
||||||
|
print(f"incorrect state type. expected SOLVED, got {state_name}.")
|
||||||
|
|
||||||
|
|
||||||
|
def TEST_UNBOUNDED_CASE_A05():
|
||||||
|
print("----------------------RUNNING_TEST_UNBOUNDED_CASE----------------------")
|
||||||
|
|
||||||
C = np.array([2, 1])
|
C = np.array([2, 1])
|
||||||
A = np.array([
|
A = np.array([
|
||||||
@@ -336,17 +423,17 @@ def TEST_UNBOUNDED_CASE():
|
|||||||
[2, 0]])
|
[2, 0]])
|
||||||
b = np.array([10, 40])
|
b = np.array([10, 40])
|
||||||
x_0 = np.array([1, 1])
|
x_0 = np.array([1, 1])
|
||||||
eps = 0.01
|
eps = 1e-4
|
||||||
alpha = 0.5
|
alpha = 0.5
|
||||||
maximize = True
|
maximize = True
|
||||||
|
|
||||||
|
print_initial_inputs(C, A, b, x_0, eps, alpha, maximize)
|
||||||
|
result = interior_point(C, A, b, x_0, eps, alpha, maximize)
|
||||||
|
|
||||||
print_initial_inputs(C, A, b, x_0, eps, alpha, maximize);
|
expected_state = State.UNSOLVED
|
||||||
result = interior_point(C, A, b, x_0, eps, alpha, maximize);
|
|
||||||
|
|
||||||
expected_state = State.SOLVED
|
|
||||||
if result.state == expected_state:
|
if result.state == expected_state:
|
||||||
print_result(result)
|
print_result(result)
|
||||||
|
return 1
|
||||||
else:
|
else:
|
||||||
state_name = ""
|
state_name = ""
|
||||||
if result.state == State.UNSOLVED:
|
if result.state == State.UNSOLVED:
|
||||||
@@ -356,9 +443,43 @@ def TEST_UNBOUNDED_CASE():
|
|||||||
elif result.state == State.SOLVED:
|
elif result.state == State.SOLVED:
|
||||||
state_name = "SOLVED"
|
state_name = "SOLVED"
|
||||||
print(f"incorrect state type. expected SOLVED, got {state_name}.")
|
print(f"incorrect state type. expected SOLVED, got {state_name}.")
|
||||||
|
return 0
|
||||||
|
|
||||||
def TEST_UNSOLVABLE_CASE():
|
|
||||||
print("----------------------------RUNNING_TEST_UNSOLVABLE_CASE----------------------------")
|
def TEST_UNBOUNDED_CASE_A09():
|
||||||
|
print("----------------------RUNNING_TEST_UNBOUNDED_CASE----------------------")
|
||||||
|
|
||||||
|
C = np.array([2, 1])
|
||||||
|
A = np.array([
|
||||||
|
[1, -1],
|
||||||
|
[2, 0]])
|
||||||
|
b = np.array([10, 40])
|
||||||
|
x_0 = np.array([1, 1])
|
||||||
|
eps = 1e-4
|
||||||
|
alpha = 0.9
|
||||||
|
maximize = True
|
||||||
|
|
||||||
|
print_initial_inputs(C, A, b, x_0, eps, alpha, maximize)
|
||||||
|
result = interior_point(C, A, b, x_0, eps, alpha, maximize)
|
||||||
|
|
||||||
|
expected_state = State.UNSOLVED
|
||||||
|
if result.state == expected_state:
|
||||||
|
print_result(result)
|
||||||
|
return 1
|
||||||
|
else:
|
||||||
|
state_name = ""
|
||||||
|
if result.state == State.UNSOLVED:
|
||||||
|
state_name = "UNSOLVED"
|
||||||
|
elif result.state == State.INAPPLICABLE:
|
||||||
|
state_name = "INAPPLICABLE"
|
||||||
|
elif result.state == State.SOLVED:
|
||||||
|
state_name = "SOLVED"
|
||||||
|
print(f"incorrect state type. expected SOLVED, got {state_name}.")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def TEST_UNSOLVABLE_CASE_A05():
|
||||||
|
print("----------------------RUNNING_TEST_UNSOLVABLE_CASE----------------------")
|
||||||
|
|
||||||
C = np.array([5, 4, 0, -5, 13])
|
C = np.array([5, 4, 0, -5, 13])
|
||||||
A = np.array([
|
A = np.array([
|
||||||
@@ -367,19 +488,18 @@ def TEST_UNSOLVABLE_CASE():
|
|||||||
[-1, 0, 0, 10, 0],
|
[-1, 0, 0, 10, 0],
|
||||||
[0, 1, 1, -5, 1]])
|
[0, 1, 1, -5, 1]])
|
||||||
b = np.array([-24, 6, 1, 2])
|
b = np.array([-24, 6, 1, 2])
|
||||||
x_0 = np.array([1, 1])
|
x_0 = np.array([-2, -3, -1, -1, 1])
|
||||||
eps = 0.01
|
eps = 1e-4
|
||||||
alpha = 0.5
|
alpha = 0.5
|
||||||
maximize = True
|
maximize = True
|
||||||
|
|
||||||
print_initial_inputs(C, A, b, x_0, eps, alpha, maximize);
|
print_initial_inputs(C, A, b, x_0, eps, alpha, maximize)
|
||||||
result = interior_point(C, A, b, x_0, eps, alpha, maximize);
|
result = interior_point(C, A, b, x_0, eps, alpha, maximize)
|
||||||
|
|
||||||
|
expected_state = State.INAPPLICABLE
|
||||||
|
|
||||||
expected_state = State.SOLVED
|
|
||||||
if result.state == expected_state:
|
if result.state == expected_state:
|
||||||
print_result(result)
|
print_result(result)
|
||||||
|
return 1
|
||||||
else:
|
else:
|
||||||
state_name = ""
|
state_name = ""
|
||||||
if result.state == State.UNSOLVED:
|
if result.state == State.UNSOLVED:
|
||||||
@@ -389,3 +509,76 @@ def TEST_UNSOLVABLE_CASE():
|
|||||||
elif result.state == State.SOLVED:
|
elif result.state == State.SOLVED:
|
||||||
state_name = "SOLVED"
|
state_name = "SOLVED"
|
||||||
print(f"incorrect state type. expected SOLVED, got {state_name}.")
|
print(f"incorrect state type. expected SOLVED, got {state_name}.")
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def TEST_UNSOLVABLE_CASE_A09():
|
||||||
|
print("----------------------RUNNING_TEST_UNSOLVABLE_CASE----------------------")
|
||||||
|
|
||||||
|
C = np.array([5, 4, 0, -5, 13])
|
||||||
|
A = np.array([
|
||||||
|
[6, 4, 1, 3, 4],
|
||||||
|
[1, 2, 0, 0, 2],
|
||||||
|
[-1, 0, 0, 10, 0],
|
||||||
|
[0, 1, 1, -5, 1]])
|
||||||
|
b = np.array([-24, 6, 1, 2])
|
||||||
|
x_0 = np.array([-2, -3, -1, -1, 1])
|
||||||
|
eps = 1e-4
|
||||||
|
alpha = 0.9
|
||||||
|
maximize = True
|
||||||
|
|
||||||
|
print_initial_inputs(C, A, b, x_0, eps, alpha, maximize)
|
||||||
|
result = interior_point(C, A, b, x_0, eps, alpha, maximize)
|
||||||
|
|
||||||
|
expected_state = State.INAPPLICABLE
|
||||||
|
if result.state == expected_state:
|
||||||
|
print_result(result)
|
||||||
|
return 1
|
||||||
|
else:
|
||||||
|
state_name = ""
|
||||||
|
if result.state == State.UNSOLVED:
|
||||||
|
state_name = "UNSOLVED"
|
||||||
|
elif result.state == State.INAPPLICABLE:
|
||||||
|
state_name = "INAPPLICABLE"
|
||||||
|
elif result.state == State.SOLVED:
|
||||||
|
state_name = "SOLVED"
|
||||||
|
print(f"incorrect state type. expected SOLVED, got {state_name}.")
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
simplex_general_case_decVar_str = ("----------------------SIMPLEX_TEST_GENERAL_CASE----------------------\n"
|
||||||
|
"Decision variables: [3, 1.5]")
|
||||||
|
simplex_minimize_case_decVar_str = ("----------------------SIMPLEX_TEST_MINIMIZE_CASE"
|
||||||
|
"----------------------\n"
|
||||||
|
"Decision variables: [0, 0.75, 5.375]")
|
||||||
|
simplex_slack_case_decVar_str = ("----------------------SIMPLEX_TEST_SLACK_CASE----------------------\n"
|
||||||
|
"Decision variables: [11.5, 0.75, 0, 0]")
|
||||||
|
simplex_unbounded_case_decVar_str = ("----------------------SIMPLEX_TEST_UNBOUNDED_CASE"
|
||||||
|
"----------------------\n"
|
||||||
|
"Decision variables: None")
|
||||||
|
simplex_unsolvable_case_decVar_str = ("----------------------SIMPLEX_TEST_UNSOLVABLE_CASE"
|
||||||
|
"----------------------\n"
|
||||||
|
"Decision variables: None")
|
||||||
|
|
||||||
|
|
||||||
|
tests = [
|
||||||
|
[TEST_CASE_GENERAL_A05, TEST_CASE_GENERAL_A09, simplex_general_case_decVar_str],
|
||||||
|
[TEST_MINIMIZE_CASE_A05, TEST_MINIMIZE_CASE_A09, simplex_minimize_case_decVar_str],
|
||||||
|
[TEST_WITH_SLACK_CASE_A05, TEST_WITH_SLACK_CASE_A09, simplex_slack_case_decVar_str],
|
||||||
|
[TEST_UNBOUNDED_CASE_A05, TEST_UNBOUNDED_CASE_A09, simplex_unbounded_case_decVar_str],
|
||||||
|
[TEST_UNSOLVABLE_CASE_A05, TEST_UNSOLVABLE_CASE_A09, simplex_unsolvable_case_decVar_str]
|
||||||
|
]
|
||||||
|
tests_passed = 0
|
||||||
|
for test in tests:
|
||||||
|
for test_variant_i in range(len(test)):
|
||||||
|
if (test_variant_i == 2):
|
||||||
|
print(test[2])
|
||||||
|
else:
|
||||||
|
tests_passed += test[test_variant_i]()
|
||||||
|
|
||||||
|
|
||||||
|
print("----------------------RESULTS----------------------")
|
||||||
|
print(f"Total number of tests: {len(tests) * 2}")
|
||||||
|
print(f"Total number of passed tests: {tests_passed}")
|
||||||
|
|||||||
Reference in New Issue
Block a user