def sub_eq_constraints(self): # Assembles the matrix of contraints for the LP problem a_plus = self.A_plus() a_minus = self.A_minus() c_plus = self.C_plus() c_minus = self.C_minus() zeroFill_plus = np.zeros(c_plus.shape) zeroFill_minus = np.zeros(c_minus.shape) a_matrix = np.concatenate((a_plus, -a_minus), axis=1) Matrix = a_matrix if (c_plus.shape[0] != 0): # if the c_plus matrix is not empty, then concatenate it: c_matrix_plus = np.concatenate((c_plus, zeroFill_plus), axis=1) Matrix = np.concatenate((Matrix, c_matrix_plus), axis=0) if (c_minus.shape[0] != 0): # if the c_minus matrix is not empty, then concatenate it: c_matrix_mins = np.concatenate((zeroFill_minus, c_minus), axis=1) Matrix = np.concatenate((Matrix, c_matrix_mins), axis=0) nodeList = self.get_nodeList() b = map(attrgetter('b'), nodeList) b = np.asmatrix(b) b = np.reshape(b, (1, -1)) size = Matrix.shape B = np.zeros(shape=(size[0], 1)) b_sub_eq = placeMat(B, b.T, 0, 0) return ( Matrix, b_sub_eq ) # the minus sign allows the inequality to be in the correct direction, e.g. the flow can be more than the minimun, but not less.
def B_prop(allCommodityNames, propulsionName, phi, **kwargs): # Creates a default B_sup matrix if none is given in input: B_prop = np.identity(len(allCommodityNames)) for key, value in kwargs.items(): if (key == 'B_prop'): B_prop = kwargs[key] # Find which line we must replace for the propellant: p_index = findName( allCommodityNames, [propulsionName ])[0] # need to convert the proplusion name to a string array for the # Get the name locations of the elements in the small matrix to the large matrix: subMat = B_prop[p_index, :] - np.matrix([phi] * len(allCommodityNames)) B_prop = placeMat(B_prop, subMat, p_index, 0) return B_prop
def eq_constraints(self): # Preparatory functions: arcList = self.get_arcList() ncom = self.get_numCommodity() size1 = ncom * len( self.get_arcList() ) # size of the total A matrices: number of arcs times the number of commodities B = np.zeros(shape=(size1, size1)) # allocate zero matrices B_mat_List = map(attrgetter('B'), arcList) for x in range(len(self.get_arcList())): b = B_mat_List[x] B = placeMat(B, b, x * ncom, x * ncom) eyeFill = np.identity(B.shape[0]) B_eq = np.concatenate((B, -eyeFill), axis=1) b_eq = np.zeros(shape=(B.shape[0], 1)) return (B_eq, b_eq)
def A_minus(self): ncom = self.get_numCommodity() size1 = ncom * len( self.get_nodeList() ) # size of the total A matrices: number of arcs times the number of commodities size2 = ncom * len( self.get_arcList() ) # size of the total A matrices: number of arcs times the number of commodities A_minus = np.zeros(shape=(size1, size2)) # allocate zero matrices arcList = self.get_arcList() nodeList = self.get_nodeList() arcIdx = -1 # initialize the arc index for arc in arcList: # Sweep across all edges. Cannot use network maps because of possible multiple edges between 2 nodes. arcIdx = arcIdx + 1 a_minus = arc.A_minus inNode = arc.get_incoming() nodeIdx = nodeList.index(inNode) A_minus = placeMat(A_minus, a_minus, nodeIdx * ncom, arcIdx * ncom) return A_minus
def C_minus(self): # Preparatory functions: arcList = self.get_arcList() ncom = self.get_numCommodity() size2 = ncom * len( self.get_arcList() ) # size of the total A matrices: number of arcs times the number of commodities C_mat_List = map(attrgetter('C_minus'), arcList) C_minus = np.zeros( shape=(1, size2) ) # creates a line of zeros to which the rest will be appended, then this is removed... for x in range(len(self.get_arcList())): C_element = C_mat_List[x] C_elem_size = C_element.shape C_line = np.zeros(shape=(C_elem_size[0], size2)) C_line = placeMat(C_line, C_element, 0, x * ncom) C_minus = np.vstack((C_minus, C_line)) C_minus = C_minus[~np.all(C_minus == 0, axis=1)] # the zero lines are removed. return C_minus
def displaySol(self, x, commodity_names): # Classify the results into a table x_plus = x[0:x.shape[0] / 2] x_minus = x[ x.shape[0] / 2:x.shape[0]] # x_minus are the incoming flows...Could be used... nCom = self.get_numCommodity() table_plus = np.zeros(shape=(nCom, len(self.get_arcList()))) print 'table plus' print table_plus table_minus = np.zeros(shape=(nCom, len(self.get_arcList()))) for x in range(len(self.get_arcList())): subMat = x_plus[x * nCom:(x + 1) * nCom] subMat = np.asmatrix(subMat) subMat = subMat.transpose() print 'x_plus' print x_plus print 'submat' print subMat print 'nCom' print nCom print 'x' print x print 'table plus' print table_plus table_plus = placeMat(table_plus, subMat, 0, x) subMat = x_minus[x * nCom:(x + 1) * nCom] subMat = np.asmatrix(subMat) subMat = subMat.transpose() table_minus = placeMat(table_minus, subMat, 0, x) # Get names of commodities for display # this is included in the arguments of the function... # make the plot N = nCom N = 5 ind = np.arange(N) # the x locations for the groups width = 0.35 # the width of the bars newFig = plt.figure() cmap = plt.get_cmap('gnuplot') colorses = [cmap(i) for i in np.linspace(0, 1, nCom)] xaxisrange = range(1, len(self.get_arcList()) + 1) for i in range(nCom): plt.plot(xaxisrange, table_plus[i, :], linewidth=2, label=commodity_names[i]) arcList = self.get_arcList() out_List = map(attrgetter('outgoingNode'), arcList) a = map(attrgetter('name'), out_List) in_List = map(attrgetter('incomingNode'), arcList) b = map(attrgetter('name'), in_List) my_xticks = ["{}{}".format(b_, a_) for a_, b_ in zip(a, b)] print my_xticks plt.xticks(xaxisrange, my_xticks) plt.legend(loc='best') plt.xlim(0, len(self.get_arcList()) + 1) plt.ylim(-1, table_plus.max() + 2) plt.show() return table_plus, table_minus