Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def makematrix(cols, rows):
- return [cols*[0] for i in range(rows)]
- def select(matrix):
- # finds the objective row (the z-row)
- obj = len(matrix)-1
- ## Find the pivot column
- pivotcol = 0
- mincol = 100**100
- # for every possible index in the objective function
- # (less the answer column, which is last, hence len-1 (remember that range is upper-bound-exclusive)
- for i in range(len(matrix[obj]) - 1):
- # matrix[obj][i] is a candidate pivot column
- if matrix[obj][i] < mincol:
- pivotcol = i
- mincol = matrix[obj][i]
- # pivot column is now found
- pivotrow = 0
- ratio = 100**100
- curindex = 0
- for (pivotnum, ans) in [(row[pivotcol], row[len(row)-1]) for row in matrix]:
- if pivotnum > 0:
- if ans / pivotnum < ratio:
- ratio = ans / pivotnum
- pivotrow = curindex
- curindex += 1
- return (pivotrow, pivotcol)
- mat = [[ 1, 4, 3, 6, 6, 3, 5, 3, 3],
- [ 1, 4, 3, 2, 5, 6, 7, -3, 2],
- [-3, 0, 1, -4, 12, 1, 10, -5, 2],
- [ 4, -2, 2, 3, 2, 1, 0, 2, 3],
- [ 4, 1, 1, 2, 4, 0, 4, 0, 12],
- [ 4, 12, 4, 2, 6, -1, 9, 1, 2]]
- (row, col) = select(mat)
- print row
- print col
Advertisement
Add Comment
Please, Sign In to add comment