Advertisement
Vermiculus

Stuff

Oct 26th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.75 KB | None | 0 0
  1. ROUND_CONST = 0
  2.  
  3. def tryintlst(n):
  4.     def tryint(n):
  5.         try:
  6.             return float(n)
  7.         except:
  8.             return n
  9.     return map(tryint, n)
  10. def tryround(n, i=2):
  11.     try:
  12.         return round(n,i)
  13.     except:
  14.         return n
  15. def isoptimal(matrix)
  16.     for i in matrix[-1][1:]:
  17.         if i < 0:
  18.             return False
  19.     return True
  20.  
  21. def makematrix(cols, rows):
  22.     return [cols*[0] for i in range(rows)]
  23. def solvematrix(m, row, col):
  24.     #sets pivot value
  25.     piv = m[row][col]
  26.  
  27.     import copy
  28.     newstuff = copy.deepcopy(m)
  29.     newstuff [row][0] = m[0][col]
  30.     #iterates through the whole array
  31.     for x in range (1, len(m)):
  32.         for y in range (1, len(m)):
  33.  
  34.             #if we are in the pivot row... divide the number in
  35.             #m by piv and put it in the same place in the new matrix
  36.             if (x==row and y!=col):
  37.                 oldval = m[x][y]
  38.                 newstuff[x][y]= oldval/piv
  39.  
  40.                
  41.             #if we are in the pivot column BUT NOT THE PIVOT NUMBER...
  42.             #the number in the new matrix in that spot is replaced by 0
  43.             elif (y==col and x!=row):
  44.                 newstuff[x][y]=0
  45.  
  46.  
  47.             #look down.  Look up
  48.             #the pivot number is now 1...alternatively diamonds
  49.             elif (x==row and y==col):
  50.                 newstuff[x][y]=1
  51.  
  52.                
  53.             #a=(a)-((b*c)/d)
  54.             #in the following matrix
  55.                 #a b
  56.                 #c d
  57.             #where d is the pivot number
  58.             else:
  59.                 old=m[x][y]
  60.                 r = m[row][y]
  61.                 c = m[x][col]
  62.                 newstuff[x][y]=(old)-((r*c)/piv)
  63.    
  64.     #print m
  65.     print ""
  66.     print "Newstuff:"
  67.     pprintmatrix(newstuff)
  68.     return newstuff
  69.  
  70. ##input: full/complete 2d list (first and last columns will be ignored), pivotRowIndex, pivotColIndex
  71. def printMatrix(em, row, col):
  72.     row+=1
  73.     maxlength = 0
  74.     for x in em[1:-1]:
  75.         for y in x[1:-1]:
  76.             ystring = str(y)
  77.             if(len(ystring) > maxlength):
  78.                 maxlength = y
  79.  
  80.     xcount = 0
  81.     for x in em:
  82.         ycount = 0
  83.         count = 2
  84.         line = ''
  85.  
  86.         if(xcount == row):
  87.             line += "*, "
  88.         else:
  89.             line += " , "
  90.  
  91.         hackShit = False
  92.  
  93.         if(em.index(x) == 0):
  94.             hackShit = True
  95.        
  96.         ycount = 0
  97.         for y in x[1:]:
  98.             if(hackShit):
  99.                 if(ycount == row):
  100.                     line += "*, "
  101.                 else:
  102.                     line += " , "
  103.             else:
  104.                 line += (str(y) + ", ")
  105.             ycount += 1
  106.         print line[0:-2]
  107.         xcount += 1
  108.     print " "
  109.  
  110. # takes only numbers
  111. def select(matrix):
  112.     matrix = [l[1:] for l in matrix[1:]] # strip headers
  113.     # finds the objective row (the z-row)
  114.     obj = len(matrix)-1
  115.    
  116.     ## Find the pivot column
  117.     pivotcol = 0
  118.     mincol = 100**100
  119.    
  120.     # for every possible index in the objective function
  121.     # (less the answer column, which is last, hence len-1 (remember that range is upper-bound-exclusive)
  122.     for i in range(len(matrix[obj]) - 1):
  123.         # matrix[obj][i] is a candidate pivot column
  124.         if matrix[obj][i] < mincol:
  125.             pivotcol = i
  126.             mincol = matrix[obj][i]
  127.    
  128.     # pivot column is now found
  129.    
  130.     ## Find pivot row
  131.     pivotrow = 0
  132.     ratio = 100**100
  133.     curindex = 0
  134.     for (pivotnum, ans) in [(row[pivotcol], row[-1]) for row in matrix]:
  135.         if (pivotnum > 0):
  136.             if (ans / pivotnum < ratio):
  137.                 ratio = ans / pivotnum
  138.                 pivotrow = curindex
  139.         curindex += 1
  140.    
  141.     return (pivotrow, pivotcol)
  142.  
  143. mat = [[ 1,  4,  3,  6,  6,  3,  5,  3,  3],
  144.        [ 1,  4,  3,  2,  5,  6,  7, -3,  2],
  145.        [-3,  0,  1, -4, 12,  1, 10, -5,  2],
  146.        [ 4, -2,  2,  3,  2,  1,  0,  2,  3],
  147.        [ 4,  1,  1,  2,  4,  0,  4,  0, 12],
  148.        [ 4, 12,  4,  2,  6, -1,  9,  1,  2]]
  149.  
  150. def main(stuff):
  151.     while not isoptimal(stuff):
  152.         (row, col) = select(stuff)
  153.         print (row,col)
  154.         row+=1;col+=1
  155.        
  156.         ppm(roundmatrix(stuff), row, col)
  157.         stuff = solvematrix(stuff, row, col)
  158.    
  159.     print "answer column:", [l[-1] for l in stuff[1:]]
  160.  
  161. def pprintmatrix(m):
  162.     pr = roundmatrix(m)
  163.    
  164.     justifys = [max([len(str(row[i]))+1 for row in pr]) for i in range(len(pr[0]))]
  165.     for row in pr:
  166.         for n in range(len(row)):
  167.             just = justifys[n]
  168.             print ("{0:>" + str(just) + "}").format(row[n], 2),
  169.         print " "
  170.  
  171. def ppm(m, r, c):
  172.     # this line works so don't fucking touch it, idiot
  173.     justifys = [max([len(str(row[i])) + 1 for row in m]) for i in range(len(m[0]))]
  174.    
  175.     # allow room for star
  176.     justifys[0] += 1
  177.     justifys[c] += 1
  178.    
  179.     for rowi in range(len(m)):
  180.         for coli in range(len(m[rowi])):
  181.             #print rowi,r,
  182.             if coli == c and rowi == 0:
  183.                 s = "{0:>" + str(justifys[coli] - 1) + "}*"
  184.             elif rowi == r and coli == 0:
  185.                 s = "*{0:>" + str(justifys[coli] - 1) + "}"
  186.             else:
  187.                 s = "{0:>" + str(justifys[coli]) + "}"
  188.             print s.format(m[rowi][coli]),
  189.         print ""
  190.  
  191. def roundmatrix(m,i=ROUND_CONST):
  192.     """Rounds a matrix to 'i' decimal places where applicable"""
  193.     def tryrlst(r):
  194.         def tryr(n):
  195.             try:
  196.                 return round(n,i)
  197.             except:
  198.                 return n
  199.         return map(tryr, r)
  200.     import copy
  201.     p = copy.deepcopy(m)
  202.     p = map(tryrlst, p)
  203.     return p
  204.  
  205. import csv
  206. with open('or.csv','rb') as csvfile:
  207.     myreader = csv.reader(csvfile)
  208.     stuff = map(tryintlst, [row for row in myreader])
  209.  
  210. main(stuff)
  211. print isoptimal(stuff)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement