Compare commits
11
Commits
81a2ec372b
...
2d1f7d14b8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2d1f7d14b8 | ||
|
|
9578a1a71d | ||
|
|
9419fdefd7 | ||
|
|
06638c782e | ||
|
|
ff40263bbe | ||
|
|
66141164f2 | ||
|
|
ca8b4ac07e | ||
|
|
8a698c716b | ||
|
|
3aea4bfcad | ||
|
|
d774775cb2 | ||
|
|
a59fbbf1dc |
@@ -25,8 +25,6 @@ class Result:
|
||||
self.solution = solution
|
||||
|
||||
|
||||
|
||||
|
||||
def NorthwestCorner(S: np.array,
|
||||
C: np.array,
|
||||
D: np.array) -> Result:
|
||||
@@ -50,169 +48,68 @@ def NorthwestCorner(S: np.array,
|
||||
return Result(State.UNAPPLICABLE)
|
||||
|
||||
|
||||
#SAMPLE INPUT FOR TESTING
|
||||
|
||||
S = np.array([50, 60, 50, 50])
|
||||
|
||||
C = np.array([
|
||||
[16, 16, 13, 22, 17],
|
||||
[14, 14, 13, 19, 15],
|
||||
[19, 19, 20, 23, M ],
|
||||
[M, 0, M, 0, 0]])
|
||||
|
||||
D = np.array([30, 20, 70, 30, 60])
|
||||
|
||||
|
||||
def Vogel(
|
||||
S: np.array,
|
||||
C: np.array,
|
||||
D: np.array) -> Result:
|
||||
|
||||
|
||||
remaining_rows = np.ones(C.shape[0], dtype=bool)
|
||||
remaining_cols = np.ones(C.shape[1], dtype=bool)
|
||||
x_0 = np.zeros(C.shape, dtype=np.int64)
|
||||
iteration = 0
|
||||
|
||||
C_initial = C
|
||||
C_init_height = len(C)
|
||||
C_init_length = len(C[0])
|
||||
|
||||
solution_matrix = np.zeros((C_init_height, C_init_length), dtype=np.int64)
|
||||
|
||||
|
||||
print(solution_matrix)
|
||||
def add_to_solutions(val, x, y):
|
||||
for yi in range(C_init_height):
|
||||
for xi in range(C_init_length):
|
||||
if (yi == y and xi == x):
|
||||
solution_matrix[y][x] = val
|
||||
|
||||
C_numerated = np.zeros((C_init_height, C_init_length), dtype=np.int64)
|
||||
C_numerated = np.insert(C_numerated, 0, [str(i+1) for i in range( C_init_length)], axis=0)
|
||||
C_numerated = np.insert(C_numerated, 0, [str(i) for i in range( C_init_height+1)], axis=1)
|
||||
|
||||
|
||||
|
||||
print(C_numerated)
|
||||
|
||||
while (len(C[0]) > 1 and len(C) > 1):
|
||||
while True:
|
||||
iteration += 1
|
||||
C_map = dict()
|
||||
mask = np.outer(remaining_rows, remaining_cols)
|
||||
|
||||
for y in range(len(C)):
|
||||
for x in range(len(C[0])):
|
||||
C_map[x, y] = C[y][x]
|
||||
|
||||
C_length = len(C[0])
|
||||
C_height = len(C)
|
||||
|
||||
RowD = np.array
|
||||
ColD = np.array
|
||||
|
||||
RowD = np.resize(RowD, C_height)
|
||||
ColD = np.resize(ColD, C_length)
|
||||
if iteration > 1000:
|
||||
return Result(State.UNAPPLICABLE)
|
||||
|
||||
# Finding differences
|
||||
for y in range(C_height):
|
||||
RowD[y] = abs(sorted(C[y])[0] - sorted(C[y])[1])
|
||||
for x in range(C_length):
|
||||
ColD[x] = abs(sorted(C.T[x])[0] - sorted(C.T[x])[1])
|
||||
_C = np.sort(np.where(mask, C.copy(), M*M))
|
||||
RowD = _C[:, 1] - _C[:, 0]
|
||||
_C = np.sort(np.where(mask.T, C.copy().T, M*M))
|
||||
ColD = _C[:, 1] - _C[:, 0]
|
||||
|
||||
# Maximum difference
|
||||
maxD = max(np.concatenate((ColD, RowD)))
|
||||
|
||||
target_array = None
|
||||
target_number = None
|
||||
row_index_to_eleminate = None
|
||||
column_index_to_eleminate = None
|
||||
x_num = None
|
||||
y_num = None
|
||||
if maxD in RowD:
|
||||
y = np.where(RowD == maxD)[0][0]
|
||||
target_array = C[np.where(RowD == maxD)[0]][0]
|
||||
target_number = min(target_array)
|
||||
x = np.where(target_array == target_number)[0][0]
|
||||
x = np.argmax(RowD, axis=0)
|
||||
y = np.argmin(np.where(mask, C.copy(), M)[x], axis=0)
|
||||
|
||||
x_num = C_numerated[0][x+1]
|
||||
y_num = C_numerated[y+1][0]
|
||||
|
||||
|
||||
if (D[x] >= S[y]):
|
||||
row_index_to_eleminate = y
|
||||
selected_value = S[y]
|
||||
|
||||
D[x] -= selected_value
|
||||
|
||||
|
||||
C = np.delete(C, row_index_to_eleminate, 0)
|
||||
S = np.delete(S, row_index_to_eleminate, 0)
|
||||
C_numerated = np.delete(C_numerated, row_index_to_eleminate+1, 0)
|
||||
if (D[y] == 0):
|
||||
break
|
||||
|
||||
if (D[y] >= S[x]):
|
||||
selected_value = S[x]
|
||||
D[y] -= selected_value
|
||||
S[x] = 0
|
||||
remaining_rows[x] = 0
|
||||
else:
|
||||
column_index_to_eleminate = x
|
||||
selected_value = D[x]
|
||||
|
||||
S[y] -= selected_value
|
||||
selected_value = D[y]
|
||||
S[x] -= selected_value
|
||||
D[y] = 0
|
||||
remaining_cols[y] = 0
|
||||
x_0[x][y] = selected_value
|
||||
elif maxD in ColD:
|
||||
y = np.argmax(ColD, axis=0)
|
||||
x = np.argmin(np.where(mask, C.copy(), M)[:, y], axis=0)
|
||||
|
||||
|
||||
C = np.delete(C, column_index_to_eleminate, 1)
|
||||
D = np.delete(D, column_index_to_eleminate, 0)
|
||||
C_numerated = np.delete(C_numerated, column_index_to_eleminate+1, 1)
|
||||
if maxD in ColD:
|
||||
x = np.where(ColD == maxD)[0][0]
|
||||
target_array = C.T[np.where(ColD == maxD)[0]][0]
|
||||
target_number = min(target_array)
|
||||
y = np.where(target_array == target_number)[0][0]
|
||||
if (D[y] == 0):
|
||||
break
|
||||
|
||||
x_num = C_numerated[0][x+1]
|
||||
y_num = C_numerated[y+1][0]
|
||||
|
||||
if (D[x] >= S[y]):
|
||||
row_index_to_eleminate = y
|
||||
selected_value = S[y]
|
||||
|
||||
D[x] -= selected_value
|
||||
|
||||
|
||||
C = np.delete(C, row_index_to_eleminate, 0)
|
||||
S = np.delete(S, row_index_to_eleminate, 0)
|
||||
C_numerated = np.delete(C_numerated, row_index_to_eleminate+1, 0)
|
||||
if (D[y] >= S[x]):
|
||||
selected_value = S[x]
|
||||
D[y] -= selected_value
|
||||
S[x] = 0
|
||||
remaining_rows[x] = 0
|
||||
else:
|
||||
column_index_to_eleminate = x
|
||||
selected_value = D[x]
|
||||
|
||||
S[y] -= selected_value
|
||||
|
||||
|
||||
C = np.delete(C, column_index_to_eleminate, 1)
|
||||
D = np.delete(D, column_index_to_eleminate, 0)
|
||||
C_numerated = np.delete(C_numerated, column_index_to_eleminate+1, 1)
|
||||
print(f"x_num: {x_num}, y_num: {y_num}")
|
||||
add_to_solutions(selected_value, x_num, y_num)
|
||||
print("C")
|
||||
print(C)
|
||||
print("Numerated")
|
||||
print(C_numerated)
|
||||
print(selected_value)
|
||||
|
||||
|
||||
print(solution_matrix)
|
||||
|
||||
|
||||
Z_matrix = np.zeros((C_init_height, C_init_length), dtype=np.int64)
|
||||
for y in range(C_init_height):
|
||||
for x in range(C_init_length):
|
||||
Z_matrix[y][x] = solution_matrix[y][x] * C_initial[y][x]
|
||||
|
||||
|
||||
Z = np.sum(Z_matrix)
|
||||
print(f"Z = {Z}")
|
||||
result = Result(State.SOLVED, Z, solution_matrix)
|
||||
return result
|
||||
''' if target_array is not None:
|
||||
objective_function_value = np.sum(np.dot(C, target_array))
|
||||
return Result(State.SOLVED, objective_function_value, target_array)
|
||||
else:
|
||||
return Result(State.UNAPPLICABLE)'''
|
||||
|
||||
Vogel(S,C,D)
|
||||
selected_value = D[y]
|
||||
S[x] -= selected_value
|
||||
D[y] = 0
|
||||
remaining_cols[y] = 0
|
||||
x_0[x][y] = selected_value
|
||||
return Result(State.SOLVED, np.sum(C * x_0), x_0)
|
||||
|
||||
|
||||
def Russell(
|
||||
@@ -259,8 +156,7 @@ def Russell(
|
||||
it_count += 1
|
||||
if (it_count > 1000):
|
||||
return Result(State.UNAPPLICABLE)
|
||||
return Result(State.SOLVED, C * x_0, x_0)
|
||||
|
||||
return Result(State.SOLVED, np.sum(C * x_0), x_0)
|
||||
|
||||
|
||||
def print_problem_statement(
|
||||
@@ -285,10 +181,9 @@ def print_problem_statement(
|
||||
if len(str(matrix[y][x])) == 1:
|
||||
row += " "
|
||||
if (y == len(matrix)-1):
|
||||
table += f"\n{"_ " * ((len(matrix[0])-1) * 2)}"
|
||||
table += "\n" + "_ " * ((len(matrix[0])-1) * 2)
|
||||
table += f"\n{row}"
|
||||
print(table)
|
||||
print(print_problem_statement(S,C,D))
|
||||
|
||||
|
||||
def solve(
|
||||
@@ -337,71 +232,66 @@ def solve(
|
||||
def TEST_CASE_1():
|
||||
print("----------------------RUNNING_TEST_CASE_1----------------------")
|
||||
C = np.array([
|
||||
[16, 16, 13, 22, 17],
|
||||
[14, 14, 13, 19, 15],
|
||||
[19, 19, 20, 23, M],
|
||||
[M, 0, M, 0, 0]
|
||||
[4, 8, 6, 5],
|
||||
[3, 2, 7, 4],
|
||||
[6, 5, 3, 9],
|
||||
], dtype=np.int64)
|
||||
|
||||
S = np.array([
|
||||
50, 60, 50, 50
|
||||
150, 200, 100
|
||||
], dtype=np.int64)
|
||||
|
||||
D = np.array([
|
||||
30, 20, 70, 30, 60
|
||||
80, 120, 100, 150
|
||||
], dtype=np.int64)
|
||||
print_problem_statement(S, C, D)
|
||||
|
||||
NWExpected = np.array([
|
||||
[30, 20, 0, 0, 0],
|
||||
[0, 0, 60, 0, 0],
|
||||
[0, 0, 10, 30, 10],
|
||||
[0, 0, 0, 0, 50]
|
||||
[80, 70, 0, 0],
|
||||
[0, 50, 100, 50],
|
||||
[0, 0, 0, 100]
|
||||
], dtype=np.int64)
|
||||
|
||||
VogelExpected = np.array([
|
||||
[0, 0, 50, 0, 0],
|
||||
[0, 0, 20, 0, 40],
|
||||
[30, 20, 0, 0, 0],
|
||||
[0, 0, 0, 30, 20]
|
||||
[80, 0, 0, 70],
|
||||
[0, 120, 0, 80],
|
||||
[0, 0, 100, 0]
|
||||
], dtype=np.int64)
|
||||
|
||||
RussellExpected = np.array([
|
||||
[0, 0, 40, 0, 10],
|
||||
[30, 0, 30, 0, 0],
|
||||
[0, 20, 0, 30, 0],
|
||||
[0, 0, 0, 0, 50]
|
||||
[0, 0, 0, 150],
|
||||
[80, 120, 0, 0],
|
||||
[0, 0, 100, 0]
|
||||
], dtype=np.int64)
|
||||
|
||||
return solve(S, C, D, NWExpected, VogelExpected, RussellExpected)
|
||||
|
||||
|
||||
def TEST_CASE_2():
|
||||
print("----------------------RUNNING_TEST_CASE_2----------------------")
|
||||
C = np.array([[5, 8, 6],
|
||||
[4, 7, 9],
|
||||
[3, 8, 5]], dtype=np.int64)
|
||||
C = np.array([[7, 3, 8, 6],
|
||||
[4, 9, 5, 3],
|
||||
[2, 6, 7, 4]], dtype=np.int64)
|
||||
|
||||
S = np.array([20, 30, 25], dtype=np.int64)
|
||||
S = np.array([180, 160, 140], dtype=np.int64)
|
||||
|
||||
D = np.array([10, 25, 40], dtype=np.int64)
|
||||
print_problem_statement(S, C, D)
|
||||
D = np.array([100, 110, 90, 180], dtype=np.int64)
|
||||
|
||||
NWExpected = np.array([
|
||||
[10, 10, 0],
|
||||
[0, 15, 15],
|
||||
[0, 0, 25],
|
||||
[100, 80, 0, 0],
|
||||
[0, 30, 90, 40],
|
||||
[0, 0, 0, 140]
|
||||
], dtype=np.int64)
|
||||
|
||||
VogelExpected = np.array([
|
||||
[0],
|
||||
[0],
|
||||
[0] # Заполнить
|
||||
[0, 110, 0, 70],
|
||||
[0, 0, 90, 70],
|
||||
[100, 0, 0, 40]
|
||||
], dtype=np.int64)
|
||||
|
||||
RussellExpected = np.array([
|
||||
[5, 0, 15],
|
||||
[5, 25, 0],
|
||||
[0, 0, 25]
|
||||
[0, 110, 70, 0],
|
||||
[0, 0, 20, 140],
|
||||
[100, 0, 0, 40]
|
||||
], dtype=np.int64)
|
||||
|
||||
return solve(S, C, D, NWExpected, VogelExpected, RussellExpected)
|
||||
@@ -410,36 +300,35 @@ def TEST_CASE_2():
|
||||
def TEST_CASE_3():
|
||||
print("----------------------RUNNING_TEST_CASE_3----------------------")
|
||||
C = np.array([
|
||||
[7, 8, 1, 2],
|
||||
[4, 5, 9, 8],
|
||||
[9, 2, 3, 6],
|
||||
[5, 7, 4, 8],
|
||||
[3, 6, 5, 2],
|
||||
[8, 4, 7, 3],
|
||||
], dtype=np.int64)
|
||||
|
||||
S = np.array([
|
||||
160, 140, 170
|
||||
130, 170, 150
|
||||
], dtype=np.int64)
|
||||
|
||||
D = np.array([
|
||||
120, 50, 190, 110
|
||||
90, 80, 140, 140
|
||||
], dtype=np.int64)
|
||||
print_problem_statement(S, C, D)
|
||||
|
||||
NWExpected = np.array([
|
||||
[120, 40, 0, 0],
|
||||
[0, 10, 130, 0],
|
||||
[0, 0, 60, 110]
|
||||
[90, 40, 0, 0],
|
||||
[0, 40, 130, 0],
|
||||
[0, 0, 10, 140]
|
||||
], dtype=np.int64)
|
||||
|
||||
VogelExpected = np.array([
|
||||
[0, 0, 50, 110],
|
||||
[120, 20, 0, 0],
|
||||
[0, 30, 140, 0],
|
||||
[0, 0, 130, 0],
|
||||
[90, 0, 0, 80],
|
||||
[0, 80, 10, 60]
|
||||
], dtype=np.int64)
|
||||
|
||||
RussellExpected = np.array([
|
||||
[0, 0, 160, 0],
|
||||
[120, 0, 0, 20],
|
||||
[0, 50, 30, 90],
|
||||
[0, 0, 130, 0],
|
||||
[90, 70, 10, 0],
|
||||
[0, 10, 0, 140]
|
||||
], dtype=np.int64)
|
||||
|
||||
return solve(S, C, D, NWExpected, VogelExpected, RussellExpected)
|
||||
|
||||
Reference in New Issue
Block a user