def lp_engine(c, A, b): # c = asarray(c) # A = asarray(A) # b = asarray(b) m,n = A.shape ind = b < 0 if any(ind): b = abs(b) #A[ind, :] = -A[ind, :] doesn't work for sparse if type(A) == ndarray and not isPyPy: A[ind] = -A[ind] else: for i in where(ind)[0]: A[i,:] = -A[i,:] d = -A.sum(axis=0) if not isscalar(d) and type(d) != ndarray: d = d.A.flatten() if not isinstance(d, ndarray): d = d.A.flatten() # d may be dense matrix w0 = sum(b) # H = [A b;c' 0;d -w0]; #H = bmat('A b; c 0; d -w0') '''''' H = Vstack([ # The initial _simplex table of phase one Hstack([A, atleast_2d(b).T]), # first m rows hstack([c, 0.]), # last-but-one hstack([d, -asfarray(w0)])]) # last #print sum(abs(Hstack([A, array([b]).T]))) if isspmatrix(H): H = H.tolil() '''''' indx = arange(n) basis = arange(n, n+m) #H, basis, is_bounded = _simplex(H, basis, indx, 1) is_bounded = _simplex(H, basis, indx, 1) if H[m+1,n] < -1e-10: # last row, last column sol = False #print('unsolvable') optx = [] zmin = [] is_bounded = False else: sol = True j = -1 #NEW tmp = H[m+1,:] if type(tmp) != ndarray: tmp = tmp.A.flatten() ind = tmp > 1e-10 if any(ind): j = where(logical_not(ind))[0] H = H[:, j] indx = indx[j] #Old # for i in range(n): # j = j+1 # if H[m+1,j] > 1e-10: # H[:,j] = [] # indx[j] = [] # j = j-1 #H(m+2,:) = [] % delete last row H = H[0:m+1,:] if size(indx) > 0: # Phase two #H, basis, is_bounded = _simplex(H,basis,indx,2); is_bounded = _simplex(H,basis,indx,2) if is_bounded: optx = zeros(n+m) n1,n2 = H.shape for i in range(m): optx[basis[i]] = H[i,n2-1] # optx(n+1:n+m,1) = []; % delete last m elements optx = optx[0:n] zmin = -H[n1-1,n2-1] # last row, last column else: optx = [] zmin = -Inf else: optx = zeros(n+m) zmin = 0 return (optx, zmin, is_bounded, sol, basis)
def lp_engine(c, A, b): # c = asarray(c) # A = asarray(A) # b = asarray(b) m, n = A.shape ind = b < 0 if any(ind): b = abs(b) #A[ind, :] = -A[ind, :] doesn't work for sparse if type(A) == ndarray and not isPyPy: A[ind] = -A[ind] else: for i in where(ind)[0]: A[i, :] = -A[i, :] d = -A.sum(axis=0) if not isscalar(d) and type(d) != ndarray: d = d.A.flatten() if not isinstance(d, ndarray): d = d.A.flatten() # d may be dense matrix w0 = sum(b) # H = [A b;c' 0;d -w0]; #H = bmat('A b; c 0; d -w0') '''''' H = Vstack([ # The initial _simplex table of phase one Hstack([A, atleast_2d(b).T]), # first m rows hstack([c, 0.]), # last-but-one hstack([d, -asfarray(w0)]) ]) # last #print sum(abs(Hstack([A, array([b]).T]))) if isspmatrix(H): H = H.tolil() '''''' indx = arange(n) basis = arange(n, n + m) #H, basis, is_bounded = _simplex(H, basis, indx, 1) is_bounded = _simplex(H, basis, indx, 1) if H[m + 1, n] < -1e-10: # last row, last column sol = False #print('unsolvable') optx = [] zmin = [] is_bounded = False else: sol = True j = -1 #NEW tmp = H[m + 1, :] if type(tmp) != ndarray: tmp = tmp.A.flatten() ind = tmp > 1e-10 if any(ind): j = where(logical_not(ind))[0] H = H[:, j] indx = indx[j] #Old # for i in range(n): # j = j+1 # if H[m+1,j] > 1e-10: # H[:,j] = [] # indx[j] = [] # j = j-1 #H(m+2,:) = [] % delete last row H = H[0:m + 1, :] if size(indx) > 0: # Phase two #H, basis, is_bounded = _simplex(H,basis,indx,2); is_bounded = _simplex(H, basis, indx, 2) if is_bounded: optx = zeros(n + m) n1, n2 = H.shape for i in range(m): optx[basis[i]] = H[i, n2 - 1] # optx(n+1:n+m,1) = []; % delete last m elements optx = optx[0:n] zmin = -H[n1 - 1, n2 - 1] # last row, last column else: optx = [] zmin = -Inf else: optx = zeros(n + m) zmin = 0 return (optx, zmin, is_bounded, sol, basis)