@@ -15,6 +15,7 @@ def __init__(self, file_name):
15
15
16
16
17
17
def show (self , out = None , inn = None , answer = None , custom = None ):
18
+
18
19
if answer is not None :
19
20
tableau_info = "Tableau Final: " + answer
20
21
final_space = "\n \n "
@@ -32,28 +33,29 @@ def show(self, out=None, inn=None, answer=None, custom=None):
32
33
33
34
def __str__ (self ):
34
35
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 )
41
36
header = ['z' ] + ['x' + str (i ) for i in self .I ]
37
+ sets = "I: " + str (self .I ) + " J: " + str (self .J )
42
38
z = 'z* = ' + '{0:.3f}' .format (self .tableau [self .option - 1 ][0 ])
43
39
44
40
if self .option == 2 :
45
- sets += " A: " + str (self .A )
46
41
header = ['za' ] + header
42
+ sets += " A: " + str (self .A )
47
43
z = 'za* = ' + '{0:.3f}' .format (self .tableau [0 ][0 ]) + "\n " + z
48
44
49
45
solution = [0.0 ] * (len (self .tableau [0 ]) - 1 )
50
46
for i in xrange (0 , len (self .I )):
51
47
solution [self .I [i ] - 1 ] = self .tableau [i + self .option ][0 ]
52
48
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 + \
57
59
"\n " + sets + "\n " + z + '\n x* = (' + ', ' .join (['{0:.3f}' .format (s ) for s in solution ]) + ')'
58
60
59
61
@@ -67,11 +69,11 @@ def __load_tableau_from_file(self, file_name):
67
69
if self .option != 1 and self .option != 2 :
68
70
raise ValueError (not_in_format_msg )
69
71
70
- rows ,cols = map (int , file .readline ().replace ('\n ' ,'' ).split (" " ))
72
+ rows ,cols = map (int , file .readline ().replace ('\n ' ,'' ).split ())
71
73
72
74
m = rows - self .option
73
75
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 )]
75
77
76
78
# ld na primeira coluna
77
79
for i in xrange (0 , rows ):
@@ -85,7 +87,7 @@ def __load_tableau_from_file(self, file_name):
85
87
n_ones = 0
86
88
87
89
jj = j
88
- for i in xrange (self .option , rows ):
90
+ for i in xrange (self .option - 1 , rows ):
89
91
90
92
if self .tableau [i ][j ] == 1.0 :
91
93
n_ones += 1
@@ -95,9 +97,10 @@ def __load_tableau_from_file(self, file_name):
95
97
else :
96
98
break
97
99
98
- if n_ones == 1 and n_zeros == m - 1 :
100
+ if n_ones == 1 and n_zeros == m :
99
101
self .I .append ((ii , jj ))
100
102
103
+
101
104
if len (self .I ) != m :
102
105
raise ValueError (not_in_format_msg )
103
106
@@ -119,6 +122,9 @@ def __load_tableau_from_file(self, file_name):
119
122
raise ValueError (not_in_format_msg )
120
123
121
124
125
+ # colocando a artificial na base efetivamente
126
+ self .show (custom = "Vai zerar os valores das variáveis artificiais de za" )
127
+
122
128
artificial_in_base = [i + 2 for i in xrange (0 , len (self .I )) if self .I [i ] in self .A ]
123
129
124
130
for i in artificial_in_base :
@@ -151,49 +157,34 @@ def __adjust_JI(self, i, j):
151
157
152
158
def remove_artificial (self ):
153
159
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
-
165
160
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 )
167
163
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
169
165
170
- for i in l :
166
+ for i , a in l :
171
167
for j in xrange (0 , len (self .J )):
172
168
if self .tableau [i ][self .J [j ]] != 0.0 :
173
169
self .show (i , self .J [j ])
174
170
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 !
176
172
break
177
173
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
179
177
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 )
182
182
183
183
# old function truncate_tableau(self):
184
184
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 :]]
185
185
self .option = 1
186
186
187
187
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
-
197
188
def get_pivot (self ):
198
189
199
190
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):
230
221
if inf is True : # -infinito
231
222
return True
232
223
return False
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
0 commit comments