Advertisement
Guest User

test

a guest
Mar 22nd, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. """
  3. ECE406: Assignment 5, Q4
  4. You will need to install scipy ('pip3 install scipy') for the import command to work.
  5. """
  6. from scipy.optimize import linprog
  7.  
  8.  
  9. def max_flow(cap, s, t):
  10. """
  11. Input: A matrix giving the capacity on each edge.
  12. If c[i,j] = 0, then the edge (i,j) is not in the graph
  13. A source s and sink node t,
  14. A number giving the value of the max flow.
  15. Output: A matrix giving the flow on each edge,
  16. """
  17. n = len(cap)
  18. bounds = []
  19. Aeq = []
  20. beq = [0]
  21. c = [0] * n
  22.  
  23. inFlow = [[] for x in range(n)]
  24. outFlow = [[] for x in range(n)]
  25.  
  26. for i in range(n):
  27. for j in range(n):
  28. bounds.append((0, cap[i][j]))
  29.  
  30. if (cap[i][j] > 0):
  31. inFlow[j].append(i)
  32. outFlow[i].append(j)
  33.  
  34. c[i] = 1
  35. c[j] = 1
  36.  
  37. # assign the proper rows to A for each flow equality
  38. for i in range(n):
  39. Arow = [0]*(n*n)
  40.  
  41. # Add in flow
  42. for inIndex in inFlow[i]:
  43. Arow[i + inIndex*n] = 1
  44.  
  45. # Add out flow
  46. for outIndex in outFlow[i]:
  47. Arow[i + outIndex*n] = -1
  48.  
  49. Aeq.append(Arow)
  50.  
  51. opt = linprog(c=c, A_eq=Aeq, b_eq=beq, bounds=bounds)
  52. return (opt.val, opt.x)
  53.  
  54. def example():
  55. """
  56. The following is an example to get you started
  57. """
  58. # nine variables, each has an upper and lower bound
  59. bounds = [(0, 0), (0, 3), (0, 1),
  60. (0, 0), (0, 0), (0, 1),
  61. (0, 0), (0, 0), (0, 0)]
  62.  
  63. # an equality constraint
  64. Aeq = [[0, -1, 0, 1, 1, 1, 0, -1, 0]]
  65. beq = [0]
  66.  
  67. # the objective c function
  68. c = [-1, -1, -1, 0, 0, 0, 0, 0, 0]
  69.  
  70. # solving
  71. opt = linprog(c=c, A_eq=Aeq, b_eq=beq, bounds=bounds)
  72. print("\n example output: \n", opt)
  73.  
  74.  
  75. def main():
  76. """
  77. Testing your LP. The following is a single example. Your alg
  78. should work for any input.
  79. """
  80. example()
  81. # the output of the example is the following :
  82. #
  83. # fun: -2.0
  84. # message: 'Optimization terminated successfully.'
  85. # nit: 6
  86. # slack: array([ 0., 2., 0., 0., 0., 0., 0., 0., 0.])
  87. # status: 0
  88. # success: True
  89. # x: array([ 0., 1., 1., 0., 0., 1., 0., 0., 0.])
  90. #
  91. # Note:
  92. # 1) the optimization has a value of opt.val = -2.0. linprog solves minimization problems.
  93. # To solve a maximization we solve: min -c^T x.
  94. #
  95. # 2) the array opt.x contains the value of each variable.
  96. # For example, the value of variable 1 (0 indexing) has value 1.0, which lies between
  97. # its lower and upper bounds of 0 and 3.
  98.  
  99.  
  100. # TEST PROBLEM -- the optimal solution for this should be 7
  101. c = [[0, 3, 4, 0, 0],
  102. [0, 0, 1, 0, 2],
  103. [0, 0, 0, 5, 0],
  104. [0, 0, 0, 0, 6],
  105. [1, 1, 0, 0, 0]]
  106. s = 0
  107. t = 4
  108.  
  109. print(max_flow(c, s, t))
  110. ##############
  111. ## Hint:
  112. ## To call linprog, you need variables with a single index. That is, you can't directly
  113. ## define the variables flow[i,j]. So, you'll have to unwrap these into an array x of length
  114. ## n^2. A common way is to store flow[i,j] in position i * n + j of the array x.
  115. ##
  116. ## Notice, that when thought of like this, example() encodes a max flow problem
  117. ## for a graph with three vertices and nine edges.
  118. ## Just three of the edges have non-zero capacity: (0,1), (0,2) and (1,2),
  119. ## with capacities of 3, 1, and 1, respectively. s = 0 and t = 2.
  120. ####################
  121.  
  122.  
  123. if __name__ == '__main__':
  124. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement