def create_smatrix(cpids, rctnids, stm, obfunc): """Create the stiochiomatrix with the fba module""" # Use stoichiometric Dictionary to create S matrix # Preallocate size of array st_mat = [[0.0] * len(rctnids) for i in cpids] if verbose: print(f"Number of total compounds: {len(cpids)}") print(f"Number of total reactions: {len(rctnids)}") print( f"Stiochiomatrix dimensions before fill: {len(st_mat)} x {len(st_mat[0])}" ) # Fill in S matrix for cidx, c in enumerate(cpids): for ridx, r in enumerate(rctnids): try: val = stm[c][r] except KeyError: val = 0.0 st_mat[cidx][ridx] = val if verbose: print( f"Stiochiomatrix dimensions after fill: fill: {len(st_mat)} x {len(st_mat[0])}" ) # Load the data lp.load(st_mat, cpids, rctnids) # Objective function lp.objective_coefficients(obfunc) return cpids, rctnids
def createSMatrix(cpdIds, rxnIds, sm, objFunc): """Create the SMatrix with the fba module""" # Use stoichiometric Dictionary to create S matrix # Preallocate size of array SMat = [[0.0] * len(rxnIds) for i in cpdIds] if verbose: print "Number of total compounds: %d" % len(cpdIds) print "Number of total reactions: %d" % len(rxnIds) print "SMat dimensions before fill: %d x %d" % (len(SMat), len( SMat[0])) # Fill in S matrix for cIdx, c in enumerate(cpdIds): for rIdx, r in enumerate(rxnIds): try: val = sm[c][r] except KeyError: val = 0.0 SMat[cIdx][rIdx] = val if verbose: print "SMat dimensions after fill: %d x %d" % (len(SMat), len(SMat[0])) # Load the data lp.load(SMat, cpdIds, rxnIds) # Objective function lp.objective_coefficients(objFunc) return cpdIds, rxnIds
def createSMatrix(cpdIds, rxnIds, sm, objFunc): """Create the SMatrix with the fba module""" # Use stoichiometric Dictionary to create S matrix # Preallocate size of array SMat = [[0.0] * len(rxnIds) for i in cpdIds] if verbose: print "Number of total compounds: %d" % len(cpdIds) print "Number of total reactions: %d" % len(rxnIds) print "SMat dimensions before fill: %d x %d" % (len(SMat), len(SMat[0])) # Fill in S matrix for cIdx, c in enumerate(cpdIds): for rIdx, r in enumerate(rxnIds): try: val = sm[c][r] except KeyError: val = 0.0 SMat[cIdx][rIdx] = val if verbose: print "SMat dimensions after fill: %d x %d" % (len(SMat), len(SMat[0])) # Load the data lp.load(SMat, cpdIds, rxnIds) # Objective function lp.objective_coefficients(objFunc) return cpdIds, rxnIds
def test_objective_coeff(self): """Test adding the objective coefficients. Note that there should be as many coefficients as columns in the matrix.""" mat = [ [ 1.0, 1.0, 1.0], [10.0, 4.0, 5.0], [ 2.0, 2.0, 6.0], [ 2.0, 2.0, 6.0], ] lp.load(mat) lp.objective_coefficients([ 10.0, 6.0, 4.0 ])
def test_solve(self): """Test the complete linear programing solution, using the example from the documentation""" mat = [ [ 1.0, 1.0, 1.0], [10.0, 4.0, 5.0], [ 2.0, 2.0, 6.0], ] lp.load(mat) lp.objective_coefficients([ 10.0, 6.0, 4.0 ]) lp.row_bounds([(None, 100.0), (None, 600.0), (None, 300.0)]) lp.col_bounds([(0, None), (0, None), (0, None)]) status, result = lp.solve() r = "%0.3f" % result self.assertEqual(r, "733.333") self.assertEqual(status, 'opt')
def test_primals(self): """Test getting the primals back as a list""" mat = [ [ 1.0, 1.0, 1.0], [10.0, 4.0, 5.0], [ 2.0, 2.0, 6.0], ] rh = ['a', 'b', 'c'] ch = ['x', 'y', 'z'] lp.load(mat, rh, ch) lp.objective_coefficients([ 10.0, 6.0, 4.0 ]) lp.row_bounds([(None, 100.0), (None, 600.0), (None, 300.0)]) lp.col_bounds([(0, None), (0, None), (0, None)]) status, result = lp.solve() col_pri = [33.333333333333336, 66.66666666666666, 0.0] col_res = lp.col_primals() self.assertEqual(col_pri, col_res)