Linear Programming - Finding vertex


In [6]:
from numpy import *

In [11]:
A = array([[-1, 3, 0, 6, 0], [0, 2, 1, 3, 0], [0, 1, 0, -1, 1]])

In [12]:
b = array([[18,24,4]])

In [13]:
print b


[[18 24  4]]

In [61]:
def LP_verts(A,b):
    
    l1 = len(A)
    l2 = len(A[0])
    
    verts = []
    
    ind = 1
    
    for i in range(0,3):
        for j in range(i + 1 , 4):
            for k in range(j + 1, 5):
                
                matrix = A[:, [i,j,k]]
                
                x = array([[0] * l2])
                x[0, i], x[0, j], x[0, k] = dot(linalg.inv(matrix), b)[[i,j,k],0]
                
                if sum(x >= 0) == len(x):
                    
                    verts.append(x)
    
    return verts

In [62]:
range(0,3)


Out[62]:
[0, 1, 2]

In [63]:
LP_verts(A,b.T)


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-63-2c6d379b9465> in <module>()
----> 1 LP_verts(A,b.T)

<ipython-input-61-2a61384b74d2> in LP_verts(A, b)
     15 
     16                 x = array([[0] * l2])
---> 17                 x[0, i], x[0, j], x[0, k] = dot(linalg.inv(matrix), b)[[i,j,k],0]
     18 
     19                 if sum(x >= 0) == len(x):

IndexError: index 3 is out of bounds for axis 0 with size 3

In [65]:
x = array([[0] * 5])

In [35]:
x


Out[35]:
array([[0, 0, 0, 0, 0]])

In [66]:
x[0, 0], x[0, 1], x[0, 2]


Out[66]:
(0, 0, 0)

In [38]:
x[0, 0], x[0, 1], x[0, 2] = dot(linalg.inv(matrix), b)[[0,1,2],0]

In [39]:
x


Out[39]:
array([[-7,  0,  0,  0,  0]])

In [43]:
range(3)


Out[43]:
[0, 1, 2]

In [ ]: