Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. ''' Simplex method '''
  2.  
  3. # Funcao objetivo na ultima linha
  4.  
  5. def Simplex(A):
  6.  
  7. EPSILON = 0.0001
  8.  
  9. m = len(A) # Linhas
  10. n = len(A[0]) # Colunas
  11.  
  12. fxLine = len(A) - 1
  13.  
  14. pivotCol = -1
  15. pivotLine = -1
  16.  
  17. minor = A[fxLine][0]
  18.  
  19. negativeExists = False
  20. i = 0
  21. while (i < (n-1)) and not negativeExists:
  22. negativeExists = (A[m - 1][i] < 0)
  23. i = i + 1
  24.  
  25. while negativeExists:
  26.  
  27. # Busca o elemento de menor valor na linha da funcao objetivo
  28. for i in range(n): # Colunas
  29. if (minor > A[fxLine][i]):
  30. minor = A[fxLine][i]
  31. pivotCol = i
  32.  
  33. minor = A[0][n - 1]
  34. print("pivotCol: " + str(pivotCol))
  35.  
  36. # Busca a linha que fornecera o pivot para a iteracao atual
  37. for j in range(m - 1): # Linhas, exceto a da funcao objetivo
  38. if(abs(A[j][pivotCol]) < EPSILON):
  39. coef = 0
  40. else:
  41. coef = (1/float(A[j][pivotCol]))*A[j][n - 1]
  42. print("coef: " + str(coef) + ", minor: " + str(minor))
  43.  
  44. if (minor >= coef and coef > 0): # Elemento com menor valor positivo
  45. minor = coef
  46. pivotLine = j
  47.  
  48. # Recalcula a linha pivot se for necessario
  49. if(A[pivotLine][pivotCol] != 1):
  50. coef = A[pivotLine][pivotCol]
  51. if(coef < EPSILON):
  52. coef = 0
  53.  
  54. for i in range(n):
  55. A[pivotLine][i] = 1/float(coef) * (A[pivotLine][i])
  56.  
  57. # Recalcula as outras linhas, exceto a linha pivot
  58. l = range(m)
  59. del l[pivotLine]
  60.  
  61. for i in l:
  62. coef = A[i][pivotCol]
  63. coef = (-1)*coef
  64. for j in range(n):
  65. A[i][j] = A[i][j] + coef*A[pivotLine][j]
  66.  
  67. # Busca por negativos na funcao objetivo
  68. i = 0
  69. negativeExists = False
  70. while (i < (n-1)) and not negativeExists:
  71. negativeExists = (A[m - 1][i] < 0)
  72. i = i + 1
  73.  
  74.  
  75. return A
  76.  
  77.  
  78. # Matriz com Fx, Restricoes e Identidade
  79. A = [[1, 1, 1, 0, 0, 6], [1, -1, 0, 1, 0, 4], [-1, 1, 0, 0, 1, 4], [-1, -2, 0, 0, 0, 0]]
  80. print(Simplex(A))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement