-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplexMethod.py
More file actions
147 lines (102 loc) · 3.98 KB
/
SimplexMethod.py
File metadata and controls
147 lines (102 loc) · 3.98 KB
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
def simplex(c, A, b, eps, goal):
n = len(c)
m = len(A)
table = []
solution = [0] * (n + m)
if (goal == "max"):
for i in range(m):
table.append(A[i] + [0] * m + [b[i]])
table[i][n + i] = 1
table.append([-i for i in c] + [0] * m + [0])
pivots = []
while True:
if all(i >= 0 for i in table[-1][:n + m]):
break
minIndexCol = 0
minCol = 10**8
for i in range(n + m):
if (table[-1][i] < 0 and table[-1][i] < minCol):
minCol = table[-1][i]
minIndexCol = i
if (minCol == 10**8):
print("The method is not applicable!")
return None, None
ratios = []
for i in range(m):
if table[i][minIndexCol] > 0:
ratios.append(table[i][-1] / table[i][minIndexCol])
else:
ratios.append(10**8)
if all(i == 10**8 for i in ratios):
print("The method is not applicable!")
return None, None
minIndexRow = ratios.index(min(ratios))
pivots.append([minIndexRow, minIndexCol])
pivot = table[minIndexRow][minIndexCol]
table[minIndexRow] = [i / pivot for i in table[minIndexRow]]
for i in range(len(table)):
if i != minIndexRow:
factor = table[i][minIndexCol]
table[i] = [table[i][j] - factor * table[minIndexRow][j] for j in range(len(table[0]))]
for i in range(len(pivots)):
if (table[pivots[i][0]][pivots[i][1]] == 1):
solution[pivots[i][1]] = round(table[pivots[i][0]][-1], eps)
return solution[:n], table[-1][-1]
if (goal == "min"):
for i in range(m):
table.append(A[i] + [0] * m + [b[i]])
table[i][n + i] = 1
table.append([-i for i in c] + [0] * m + [0])
pivots = []
while True:
if all(i <= 0 for i in table[-1][:n + m]):
break
minIndexCol = 0
minCol = -10**8
for i in range(n + m):
if (table[-1][i] > 0 and table[-1][i] > minCol):
minCol = table[-1][i]
minIndexCol = i
if (minCol == -10**8):
print("The method is not applicable!")
return None, None
ratios = []
for i in range(m):
if table[i][minIndexCol] > 0:
ratios.append(table[i][-1] / table[i][minIndexCol])
else:
ratios.append(10**8)
if all(i == 10**8 for i in ratios):
print("The method is not applicable!")
return None, None
minIndexRow = ratios.index(min(ratios))
pivots.append([minIndexRow, minIndexCol])
pivot = table[minIndexRow][minIndexCol]
table[minIndexRow] = [i / pivot for i in table[minIndexRow]]
for i in range(len(table)):
if i != minIndexRow:
factor = table[i][minIndexCol]
table[i] = [table[i][j] - factor * table[minIndexRow][j] for j in range(len(table[0]))]
for i in range(len(pivots)):
if (table[pivots[i][0]][pivots[i][1]] == 1):
solution[pivots[i][1]] = round(table[pivots[i][0]][-1], eps)
return solution[:n], table[-1][-1]
c = []
A = []
b = []
number = int(input())
for i in range(number):
c.append(float(input()))
contraints = int(input())
for i in range(contraints):
A.append([])
for j in range(number):
A[i].append(float(input()))
for i in range(contraints):
b.append(float(input()))
eps = int(input())
goal = input()
solution, value = simplex(c, A, b, eps, goal)
if (solution != None and value != None):
print(solution)
print(value)