Skip to content

Commit 50ea894

Browse files
fix bugs
1 parent a5c09ce commit 50ea894

File tree

5 files changed

+70
-97
lines changed

5 files changed

+70
-97
lines changed

src/.idea/workspace.xml

Lines changed: 33 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/simplex.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ def __second_phase(self):
4242
self.answer = "Não há solução, pois z tende a infinito negativo"
4343
return
4444

45+
tmp = ""
4546
if self.tableau.is_degenerate():
46-
self.answer = "Solução degenerada"
47+
tmp = " e degenerada"
4748

4849
if self.tableau.has_multiple_solutions():
49-
self.answer = "Solução múltipla"
50+
self.answer = "Solução múltipla" + tmp
5051
else:
51-
self.answer = "Solução única"
52+
self.answer = "Solução única" + tmp

src/simplex.pyc

-2.14 KB
Binary file not shown.

src/tableau.py

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def __init__(self, file_name):
1515

1616

1717
def show(self, out=None, inn=None, answer=None, custom=None):
18+
1819
if answer is not None:
1920
tableau_info = "Tableau Final: " + answer
2021
final_space = "\n\n"
@@ -32,28 +33,29 @@ def show(self, out=None, inn=None, answer=None, custom=None):
3233

3334
def __str__(self):
3435

35-
def __format(llist):
36-
return '|' + '|'.join(['{0:>7}'.format('{0:.3f}'.format(e) if type(e) == float else e) for e in llist]) + '|\n'
37-
38-
separator1 = '+' + '+'.join(['~' * 7 for _ in xrange(0, len(self.tableau[0]) + 1)]) + '+'
39-
40-
sets = "I: " + str(self.I) + " J: " + str(self.J)
4136
header = ['z'] + ['x' + str(i) for i in self.I]
37+
sets = "I: " + str(self.I) + " J: " + str(self.J)
4238
z = 'z* = ' + '{0:.3f}'.format(self.tableau[self.option - 1][0])
4339

4440
if self.option == 2:
45-
sets += " A: " + str(self.A)
4641
header = ['za'] + header
42+
sets += " A: " + str(self.A)
4743
z = 'za* = ' + '{0:.3f}'.format(self.tableau[0][0]) + "\n" + z
4844

4945
solution = [0.0] * (len(self.tableau[0]) - 1)
5046
for i in xrange(0, len(self.I)):
5147
solution[self.I[i] - 1] = self.tableau[i + self.option][0]
5248

53-
return separator1 + "\n" + \
54-
__format(['', 'b'] + ['x' + str(x) for x in xrange(1, len(self.tableau[0]))]) + \
55-
''.join([__format([header[i]] + self.tableau[i]) for i in xrange(0, len(self.tableau))]) + \
56-
separator1 + \
49+
tmp = [['', 'b'] + ['x' + str(x) for x in xrange(1, len(self.tableau[0]))]]
50+
tmp += [[header[i]] + ['{0:.3f}'.format(x) for x in self.tableau[i]] for i in xrange(0, len(self.tableau))]
51+
52+
shift = max([len(e) for row in tmp for e in row])
53+
54+
separator = '+' + '+'.join(['~' * shift for _ in xrange(0, len(self.tableau[0]) + 1)]) + '+'
55+
56+
return separator + "\n" + \
57+
''.join(['|' + '|'.join('{0:>{shift}}'.format(x, shift=shift) for x in row) + '|\n' for row in tmp]) + \
58+
separator + \
5759
"\n" + sets + "\n" + z + '\nx* = (' + ', '.join(['{0:.3f}'.format(s) for s in solution]) + ')'
5860

5961

@@ -67,11 +69,11 @@ def __load_tableau_from_file(self, file_name):
6769
if self.option != 1 and self.option != 2:
6870
raise ValueError(not_in_format_msg)
6971

70-
rows,cols = map(int, file.readline().replace('\n','').split(" "))
72+
rows,cols = map(int, file.readline().replace('\n','').split())
7173

7274
m = rows - self.option
7375

74-
self.tableau = [map(float, file.readline().replace('\n','').split(' ')) for _ in xrange(0, rows)]
76+
self.tableau = [map(float, file.readline().replace('\n','').split()) for _ in xrange(0, rows)]
7577

7678
# ld na primeira coluna
7779
for i in xrange(0, rows):
@@ -85,7 +87,7 @@ def __load_tableau_from_file(self, file_name):
8587
n_ones = 0
8688

8789
jj = j
88-
for i in xrange(self.option, rows):
90+
for i in xrange(self.option - 1, rows):
8991

9092
if self.tableau[i][j] == 1.0:
9193
n_ones += 1
@@ -95,9 +97,10 @@ def __load_tableau_from_file(self, file_name):
9597
else:
9698
break
9799

98-
if n_ones == 1 and n_zeros == m - 1:
100+
if n_ones == 1 and n_zeros == m:
99101
self.I.append((ii, jj))
100102

103+
101104
if len(self.I) != m:
102105
raise ValueError(not_in_format_msg)
103106

@@ -119,6 +122,9 @@ def __load_tableau_from_file(self, file_name):
119122
raise ValueError(not_in_format_msg)
120123

121124

125+
# colocando a artificial na base efetivamente
126+
self.show(custom="Vai zerar os valores das variáveis artificiais de za")
127+
122128
artificial_in_base = [i + 2 for i in xrange(0, len(self.I)) if self.I[i] in self.A]
123129

124130
for i in artificial_in_base:
@@ -151,49 +157,34 @@ def __adjust_JI(self, i, j):
151157

152158
def remove_artificial(self):
153159

154-
# i = 2 old code
155-
# while i < len(self.tableau):
156-
# if self.I[i-2] in self.A:
157-
# for j in self.J:
158-
# if j not in self.A and self.tableau[i][j] != 0.0:
159-
# self.change_base(i, j)
160-
# break
161-
# i = 2 # problema e se nao conseguiu tirar a variavel artificial ?
162-
# else:
163-
# i += 1
164-
165160
for a in self.A:
166-
self.J.remove(a)
161+
if a in self.J: # if not in, é porque tem variável artificial na base e o processo abaixo irá retirar
162+
self.J.remove(a)
167163

168-
l = [index + 2 for index, i in enumerate(self.I) if i in self.A] # se tem basica artificial
164+
l = [(index + 2, i) for index, i in enumerate(self.I) if i in self.A] # se tem basica artificial
169165

170-
for i in l:
166+
for i, a in l:
171167
for j in xrange(0, len(self.J)):
172168
if self.tableau[i][self.J[j]] != 0.0:
173169
self.show(i, self.J[j])
174170
self.change_base(i, self.J[j])
175-
self.J.remove(self.I[i - 2]) # this should keep the order !
171+
self.J.remove(a) # this should keep the order !
176172
break
177173

178-
self.show(custom="Vai remover as variáveis artificiais")
174+
self.show(custom="Vai remover a coluna das variáveis artificiais e a linha za")
175+
176+
l = [(index + 2, i) for index, i in enumerate(self.I) if i in self.A] # checa se ainda tem basica artificial
179177

180-
if l != []: # remover na forca pois esta tudo nulo
181-
self.tableau = [self.tableau[i] for i in xrange(0, len(self.tableau)) if i not in l]
178+
if l != []: # remover na forca pois esta tudo nulo !!! só pensar que vai ver que está !
179+
self.tableau = [self.tableau[i] for i in xrange(0, len(self.tableau)) if i not in zip(*l)[0]]
180+
for i in zip(*l)[1]:
181+
self.I.remove(i)
182182

183183
# old function truncate_tableau(self):
184184
self.tableau = [[row[0]] + [row[j] for j in xrange(1, len(row)) if j not in self.A] for row in self.tableau[1:]]
185185
self.option = 1
186186

187187

188-
# def truncate_tableau(self):
189-
# #self.tableau = [[row[0]] + [row[j] for j in xrange(1, len(row)) if j not in self.A] for row in self.tableau[1:]]
190-
# # for a in self.A: old code
191-
# # self.J.remove(a)
192-
#
193-
# self.tableau = [[row[0]] + [row[j] for j in xrange(1, len(row)) if j not in self.A] for row in self.tableau[1:]]
194-
# self.option = 1
195-
196-
197188
def get_pivot(self):
198189

199190
j = max(map(lambda j: (j, self.tableau[0][j]), self.J), key=lambda x: x[1])[0]
@@ -230,11 +221,3 @@ def goes_to_minus_inf(self):
230221
if inf is True: # -infinito
231222
return True
232223
return False
233-
234-
235-
236-
237-
238-
239-
240-

src/tableau.pyc

-9.58 KB
Binary file not shown.

0 commit comments

Comments
 (0)