-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtableauxSolver.py
More file actions
240 lines (201 loc) · 10.2 KB
/
tableauxSolver.py
File metadata and controls
240 lines (201 loc) · 10.2 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import numpy as np
import math
from numpy.lib.index_tricks import IndexExpression
from maxPL import PL
from tableaux import Tableaux
class TableauxSolver():
PRECISAO = 0.0001
def comBaseNaPl(self, pl, colocouEmFPI):
self._tableaux = Tableaux()
self._tableaux.setPL(pl)
self._defineEstadoInicial(colocouEmFPI)
def comBaseNoTableaux(self, tableaux, colocouEmFPI):
self._tableaux = tableaux
self._defineEstadoInicial(colocouEmFPI)
def _defineEstadoInicial(self, colocouEmFPI):
self._solucaoViavel = np.zeros(self._tableaux.numVariaveisC())
self._isViavel = False
self._isIlimitada = False
self._isOtimo = False
self.__indexColunasBaseDict = {}
if colocouEmFPI:
self._indexColunaInicialVarFolga = self._tableaux.numVariaveisC() - self._tableaux.numRestricoes()
else:
self._indexColunaInicialVarFolga = self._tableaux.numVariaveisC()//2
self._certificadoIlimitada = np.zeros(self._indexColunaInicialVarFolga)
def imprimirTudo(self):
print("Meu Tableaux")
print("É Viável: {}".format(self._isViavel))
print("É Ilimitada: {}".format(self._isIlimitada))
print("É Ótima: {}".format(self._isOtimo))
self._tableaux.print()
print("Solução Viável: {}".format(self._solucaoViavel))
print("Certificado Ilimitada: {}".format(self._certificadoIlimitada))
def resolver(self, colocouEmFPI):
#Não preciso verificar B negativo pois já foi feito no Tableaux Aux
self._trataCNegativo()
self._produzSolucaoViavel(colocouEmFPI)
if self._isIlimitada:
self._isOtimo = False
self._isViavel = True
self._produzCertificadoIlimitada()
else:
self._isOtimo = True
self._isIlimitada = False
self._isViavel = True
def _produzCertificadoIlimitada(self):
if self.__indexColunaATodaNegativa < len(self._certificadoIlimitada):
self._certificadoIlimitada[self.__indexColunaATodaNegativa]=1
colunaTodaNegativa = self._tableaux._matrizA[:,self.__indexColunaATodaNegativa]
for i in range(self._tableaux.numRestricoes()):
indexColunaNaBase = self.__indexColunasBaseDict.get(i,-1)
if(indexColunaNaBase != -1):
self._certificadoIlimitada[indexColunaNaBase] = -1*colunaTodaNegativa[i]
def _getVetorSolucaoZerado(self, colocouEmFPI):
if colocouEmFPI:
return np.zeros(self._tableaux.numVariaveisC()-self._tableaux.numRestricoes())
else:
return np.zeros(self._indexColunaInicialVarFolga)
def _produzSolucaoViavel(self, colocouEmFPI):
matrizCanonica = np.eye(self._tableaux.numRestricoes())
self._solucaoViavel = self._getVetorSolucaoZerado(colocouEmFPI)
for i in range(self._indexColunaInicialVarFolga):
if self._colunaEstaNaBase(i, matrizCanonica):
indexElementoUm = self._getIndexElementoUm(self._tableaux._matrizA[:,i])
self._solucaoViavel[i] = self._tableaux.getValorB(indexElementoUm)
self.__indexColunasBaseDict[indexElementoUm]=i
def _getIndexElementoUm(self, vetor):
for i in range(vetor.shape[0]):
if math.isclose(vetor[i], 1, abs_tol=self.PRECISAO):
return i
return -1
def _colunaEstaNaBase(self, indexColuna, matrizCanonica):
if math.isclose(self._tableaux.getElementoC(indexColuna), 0, abs_tol=self.PRECISAO):
return self._seColunaDeACanonico(indexColuna, matrizCanonica)
else:
return False
def _seColunaDeACanonico(self, indexColuna, matrizCanonica):
coluna = self._tableaux._matrizA[:,indexColuna]
for i in range(matrizCanonica.shape[0]):
comparison = coluna == matrizCanonica[:,i]
if comparison.all():
return True
return False
def _trataCNegativo(self):
while True:
indexCINegativo = self._getIndexCINegativo()
if indexCINegativo < 0:
break
else:
if(not self._trataCiNegativo(indexCINegativo)):
self._isIlimitada = True
self.__indexColunaATodaNegativa = indexCINegativo
break
def _getIndexCINegativo(self):
c = self._tableaux.getC()
for i in range(self._tableaux.numVariaveisC()):
if math.isclose(c[i], 0, abs_tol=self.PRECISAO):
#Define como 0.0, já que é perto mesmo
self._tableaux.attValorC(i, 0.0)
elif c[i] < 0:
return i
return -1
def _trataCiNegativo(self,indexElementoC):
indexElementoAPivotear = self._escolherElementoAPivotearNaColuna(indexElementoC)
if(indexElementoAPivotear == -1):
return False
else:
self._pivotearElementoDeA(indexElementoC, indexElementoAPivotear)
return True
def _escolherElementoAPivotearNaColuna(self,indexColuna):
indexElementoASerPivoteado = -1
matrizA = self._tableaux.getMatrizA()
vetorB = self._tableaux.getB()
menorRazao = np.Infinity
for i in range(self._tableaux.numRestricoes()):
valorAtual = matrizA[i][indexColuna]
if(valorAtual>0):
razaoComB= (vetorB[i])/valorAtual
if(razaoComB < menorRazao):
menorRazao = razaoComB
indexElementoASerPivoteado=i
return indexElementoASerPivoteado
def _pivotearElementoDeA(self, indexColuna, indexLinha):
if math.isclose(self._tableaux.getElementoA(indexLinha, indexColuna), 1, abs_tol=self.PRECISAO):
self._tableaux.attElementoA(indexLinha,indexColuna,1)
else:
self._transformarElementoEmUm(indexColuna, indexLinha)
self._zerarColunaPeloElemento(indexColuna, indexLinha)
def _transformarElementoEmUm(self, indexColuna, indexLinha):
self._multiplicaLinhaPor(indexLinha+1, (1/self._tableaux.getElementoA(indexLinha, indexColuna)))
def _zerarColunaPeloElemento(self, indexColuna, indexLinha):
self._zeraColunaMatrizA(indexColuna, indexLinha)
self._zeraElementoVetorC(indexColuna, indexLinha)
def _zeraColunaMatrizA(self, indexColuna, indexLinha):
for i in range(self._tableaux.numRestricoes()):
if i != indexLinha:
valorElementoASerZerado = self._tableaux.getElementoA(i,indexColuna)
if math.isclose(valorElementoASerZerado, 0.0, abs_tol=self.PRECISAO):
self._tableaux.attElementoA(i, indexColuna, 0)
else:
#Assumindo que o elemento sendo pivoteado tem valor igual a 1
valorAMultiplicarALinhaComElementoPivo = -1*valorElementoASerZerado
linhaAtualRelativaAoTableauxInteiro = i+1
self._adicionaLinhaMatrizANaLinhaAlvoTableauxNumVezes(indexLinha,linhaAtualRelativaAoTableauxInteiro,valorAMultiplicarALinhaComElementoPivo)
def _zeraElementoVetorC(self, indexColuna, indexLinha):
valorElementoASerZerado = self._tableaux.getElementoC(indexColuna)
if math.isclose(valorElementoASerZerado, 0.0, abs_tol=self.PRECISAO):
self._tableaux.attValorC(indexColuna, 0)
else:
#Assumindo que o elemento sendo pivoteado tem valor igual a 1
valorAMultiplicarALinhaComElementoPivo = -1*valorElementoASerZerado
linhaAtualRelativaAoTableauxInteiro = 0
self._adicionaLinhaMatrizANaLinhaAlvoTableauxNumVezes(indexLinha,linhaAtualRelativaAoTableauxInteiro,valorAMultiplicarALinhaComElementoPivo)
def _adicionaLinhaMatrizANaLinhaAlvoTableauxNumVezes(self, indexLinhaMatrizAOrigem, numLinhaAlvo, numVezes):
if numLinhaAlvo != indexLinhaMatrizAOrigem+1:
linhaA = self._tableaux.getCopiaLinhaA(indexLinhaMatrizAOrigem)
linhaMTransf = self._tableaux.getCopiaLinhaMTransf(indexLinhaMatrizAOrigem)
valorB = self._tableaux.getValorB(indexLinhaMatrizAOrigem)
if numLinhaAlvo == 0:
self._tableaux.addNoCertificadoOtimo(linhaMTransf*numVezes)
self._tableaux.addNoVetorC(linhaA*numVezes)
self._tableaux.addNoValorOtimo(valorB*numVezes)
else:
numLinhaCorrigidoParaRestoTableaux = numLinhaAlvo-1
self._tableaux.addNaMatrizTransf(numLinhaCorrigidoParaRestoTableaux, linhaMTransf*numVezes)
self._tableaux.addNaMatrizA(numLinhaCorrigidoParaRestoTableaux, linhaA*numVezes)
self._tableaux.addNoVetorB(numLinhaCorrigidoParaRestoTableaux, valorB*numVezes)
def _multiplicaLinhaPor(self,numLinhaTableaux, valor):
if numLinhaTableaux == 0:
self._multiplicaPrimeiraLinhaPor(valor)
else:
self._multiplicaLinhaRestoTableauxPor(numLinhaTableaux, valor)
def _multiplicaPrimeiraLinhaPor(self,valor):
pass
def _multiplicaLinhaRestoTableauxPor(self,numLinha, valor):
self._tableaux.attLinhaMatrizTransformacoes(numLinha-1,self._tableaux.getMatrizTransformacoes()[numLinha-1]*valor)
self._tableaux.attLinhaA(numLinha-1, self._tableaux.getMatrizA()[numLinha-1]*valor)
self._tableaux.attValorB(numLinha-1,self._tableaux.getB()[numLinha-1]*valor)
def getSolucaoViavel(self):
if(self._isViavel):
return self._solucaoViavel.copy()
def getCertificadoOtimalidade(self):
if self._isOtimo:
return self._tableaux.getCertificadoOtimo()
def getValorOtimo(self):
if(self._isViavel):
return self._tableaux.getValorOtimo()
def isViavel(self):
return self._isViavel
def isIlimitada(self):
return self._isIlimitada
def isOtima(self):
return self._isOtimo
def getCertificadoIlimitada(self):
if self._isIlimitada:
return self._certificadoIlimitada.copy()
def getCertificadoOtima(self):
if self._isOtimo:
return self._tableaux.getCertificadoOtimo()
def getTableaux(self):
return self._tableaux