Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ROUND_CONST = 0
- def tryintlst(n):
- def tryint(n):
- try:
- return float(n)
- except:
- return n
- return map(tryint, n)
- def tryround(n, i=2):
- try:
- return round(n,i)
- except:
- return n
- def isoptimal(matrix):
- for i in matrix[-1][1:]:
- if i < 0:
- return False
- return True
- def makematrix(cols, rows):
- return [cols*[0] for i in range(rows)]
- def solvematrix(m, row, col):
- #sets pivot value
- piv = m[row][col]
- import copy
- newstuff = copy.deepcopy(m)
- newstuff [row][0] = m[0][col]
- #iterates through the whole array
- for x in range (1, len(m)):
- for y in range (1, len(m)):
- #if we are in the pivot row... divide the number in
- #m by piv and put it in the same place in the new matrix
- if (x==row and y!=col):
- oldval = m[x][y]
- newstuff[x][y]= oldval/piv
- #if we are in the pivot column BUT NOT THE PIVOT NUMBER...
- #the number in the new matrix in that spot is replaced by 0
- elif (y==col and x!=row):
- newstuff[x][y]=0
- #look down. Look up
- #the pivot number is now 1...alternatively diamonds
- elif (x==row and y==col):
- newstuff[x][y]=1
- #a=(a)-((b*c)/d)
- #in the following matrix
- #a b
- #c d
- #where d is the pivot number
- else:
- old=m[x][y]
- r = m[row][y]
- c = m[x][col]
- newstuff[x][y]=(old)-((r*c)/piv)
- #print m
- print ""
- print "Newstuff:"
- pprintmatrix(newstuff)
- return newstuff
- ##input: full/complete 2d list (first and last columns will be ignored), pivotRowIndex, pivotColIndex
- def printMatrix(em, row, col):
- row+=1
- maxlength = 0
- for x in em[1:-1]:
- for y in x[1:-1]:
- ystring = str(y)
- if(len(ystring) > maxlength):
- maxlength = y
- xcount = 0
- for x in em:
- ycount = 0
- count = 2
- line = ''
- if(xcount == row):
- line += "*, "
- else:
- line += " , "
- hackShit = False
- if(em.index(x) == 0):
- hackShit = True
- ycount = 0
- for y in x[1:]:
- if(hackShit):
- if(ycount == row):
- line += "*, "
- else:
- line += " , "
- else:
- line += (str(y) + ", ")
- ycount += 1
- print line[0:-2]
- xcount += 1
- print " "
- # takes only numbers
- def select(matrix):
- matrix = [l[1:] for l in matrix[1:]] # strip headers
- # 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
- ## Find pivot row
- pivotrow = 0
- ratio = 100**100
- curindex = 0
- for (pivotnum, ans) in [(row[pivotcol], 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]]
- def main(stuff):
- while not isoptimal(stuff):
- (row, col) = select(stuff)
- print (row,col)
- row+=1;col+=1
- ppm(roundmatrix(stuff), row, col)
- stuff = solvematrix(stuff, row, col)
- print "answer column:", [l[-1] for l in stuff[1:]]
- def pprintmatrix(m):
- pr = roundmatrix(m)
- justifys = [max([len(str(row[i]))+1 for row in pr]) for i in range(len(pr[0]))]
- for row in pr:
- for n in range(len(row)):
- just = justifys[n]
- print ("{0:>" + str(just) + "}").format(row[n], 2),
- print " "
- def ppm(m, r, c):
- # this line works so don't fucking touch it, idiot
- justifys = [max([len(str(row[i])) + 1 for row in m]) for i in range(len(m[0]))]
- # allow room for star
- justifys[0] += 1
- justifys[c] += 1
- for rowi in range(len(m)):
- for coli in range(len(m[rowi])):
- #print rowi,r,
- if coli == c and rowi == 0:
- s = "{0:>" + str(justifys[coli] - 1) + "}*"
- elif rowi == r and coli == 0:
- s = "*{0:>" + str(justifys[coli] - 1) + "}"
- else:
- s = "{0:>" + str(justifys[coli]) + "}"
- print s.format(m[rowi][coli]),
- print ""
- def roundmatrix(m,i=ROUND_CONST):
- """Rounds a matrix to 'i' decimal places where applicable"""
- def tryrlst(r):
- def tryr(n):
- try:
- return round(n,i)
- except:
- return n
- return map(tryr, r)
- import copy
- p = copy.deepcopy(m)
- p = map(tryrlst, p)
- return p
- import csv
- with open('or.csv','rb') as csvfile:
- myreader = csv.reader(csvfile)
- stuff = map(tryintlst, [row for row in myreader])
- main(stuff)
- print isoptimal(stuff)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement