-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
154 lines (114 loc) · 4.45 KB
/
main.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from problems.transport_problem import *
result_matrix = []
def reset_result_matrix():
column = []
for i in range(0, len(matrix)):
for j in range(0, len(matrix[0])):
column.append(0)
result_matrix.append(column.copy())
column.clear()
def sum_without_none(iterable):
result = 0
for number in iterable:
if number is not None:
result += number
return result
def insert_artificial_origin():
origin.append('dummy')
line = []
for i in range(0, len(destination)):
line.append(0)
matrix.append(line)
availability.append(sum(need) - sum(availability))
def insert_artificial_destination():
destination.append('dummy')
for line in matrix:
line.append(999)
need.append(sum(availability) - sum(need))
def calculate_penalties():
origin_penalty = []
destination_penalty = []
column = []
for i, line in enumerate(matrix):
origin_penalty.append(difference_lower_costs(iterable_without_none(line.copy(), need)))
for j in range(0, len(matrix[0])):
for k in range(0, len(matrix)):
column.append(matrix[k][j])
destination_penalty.append(difference_lower_costs(iterable_without_none(column, availability)))
column.clear()
return [origin_penalty, destination_penalty]
def difference_lower_costs(iterable):
best = min(iterable)
iterable.remove(best)
if len(iterable) == 0:
return best
alternative = min(iterable)
return abs(alternative - best)
def get_column(index):
column = []
for j in range(0, len(matrix)):
column.append(matrix[j][index])
return column
def iterable_without_none(iterable, comparable=None):
iterable_remove_none = []
for i, x in enumerate(iterable):
if comparable is not None:
if comparable[i] is not None:
iterable_remove_none.append(x)
else:
if iterable[i] is not None:
iterable_remove_none.append(x)
return iterable_remove_none
def find_lower_cell(origin_penalty, destination_penalty):
result = []
max_difference_origin = max(origin_penalty)
max_difference_destination = max(destination_penalty)
if max_difference_origin < max_difference_destination:
index_max_difference = destination_penalty.index(max_difference_destination)
result.append(index_max_difference)
column = get_column(index_max_difference)
lower_cost_value = min(iterable_without_none(column, availability))
result.append(lower_cost_value)
result.append(column.index(lower_cost_value))
else:
index_max_difference = origin_penalty.index(max_difference_origin)
result.append(index_max_difference)
line = matrix[index_max_difference]
lower_cost_value = min(iterable_without_none(line, need))
result.append(lower_cost_value)
result.append(line.index(lower_cost_value))
result.reverse()
return result
def calculate_result():
z = 0
for i in range(0, len(result_matrix)):
for j in range(0, len(result_matrix[0])):
z += result_matrix[i][j]
return z
def main():
if sum(need) > sum(availability):
insert_artificial_origin()
elif sum(availability) > sum(need):
insert_artificial_destination()
reset_result_matrix()
while (sum_without_none(availability) + sum_without_none(need)) != 0:
origin_penalty, destination_penalty = calculate_penalties()
index_column_need, lower_cost_value, index_line_availability = find_lower_cell(
origin_penalty, destination_penalty)
value_availability = availability[index_line_availability]
value_need = need[index_column_need]
if value_need < value_availability:
result_matrix[index_line_availability][index_column_need] = lower_cost_value * value_need
for i in range(0, len(matrix)):
matrix[i][index_column_need] = 0
need[index_column_need] = None
availability[index_line_availability] -= value_need
else:
result_matrix[index_line_availability][index_column_need] = lower_cost_value * value_availability
for i in range(0, len(matrix[0])):
matrix[index_line_availability][i] = 0
availability[index_line_availability] = None
need[index_column_need] -= value_availability
main()
print(result_matrix)
print(calculate_result())