-
Notifications
You must be signed in to change notification settings - Fork 0
/
north_west.py
51 lines (38 loc) · 1.54 KB
/
north_west.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from abc import ABC
from approximation_method import ApproximationMethod
class NorthWestMethod(ApproximationMethod, ABC):
# current position for assigning cost
i: int
j: int
def __init__(self, file):
super().__init__(file=file)
# initial north west position
self.i, self.j = 0, 0
def solve(self) -> None:
"""
Finds the minimum value of the north west corner in the
cost table and continues moving right or down depending
of the best value
"""
while self.has_rows_and_columns_left():
self.choose_cost()
self.writer.write_initial_solution(self.assign_table,
demand=self.cost_table[self.demand_row],
supply=self.cost_table[:, self.supply_column])
self.writer.write_initial_cost(self.total_cost())
self.improve()
def choose_cost(self) -> None:
"""
Assigns the current position with the best value &
updates the current assigning position
"""
previous_rows = len(self.deleted_rows)
previous_columns = len(self.deleted_cols)
best = self.best_value_at(self.i, self.j)
self.assign(*best)
current_rows = len(self.deleted_rows)
current_columns = len(self.deleted_cols)
# j += 1 or 0 depending if a column was added
self.j += current_columns - previous_columns
# i += 1 or 0 depending if a row was added
self.i += current_rows - previous_rows