本文整理汇总了Python中pulp.LpProblem.addConstraint方法的典型用法代码示例。如果您正苦于以下问题:Python LpProblem.addConstraint方法的具体用法?Python LpProblem.addConstraint怎么用?Python LpProblem.addConstraint使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pulp.LpProblem
的用法示例。
在下文中一共展示了LpProblem.addConstraint方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: OptKnock
# 需要导入模块: from pulp import LpProblem [as 别名]
# 或者: from pulp.LpProblem import addConstraint [as 别名]
class OptKnock(object):
def __init__(self, model, verbose=False):
self.model = deepcopy(model)
self.verbose = verbose
# locate the biomass reaction
biomass_reactions = [r for r in self.model.reactions
if r.objective_coefficient != 0]
if len(biomass_reactions) != 1:
raise Exception('There should be only one single biomass reaction')
self.r_biomass = biomass_reactions[0]
self.has_flux_as_variables = False
def create_prob(self, sense=LpMaximize, use_glpk=False):
# create the LP
self.prob = LpProblem('OptKnock', sense=sense)
if use_glpk:
self.prob.solver = solvers.GLPK()
else:
self.prob.solver = solvers.CPLEX(msg=self.verbose)
if not self.prob.solver.available():
raise Exception("CPLEX not available")
def add_primal_variables_and_constraints(self):
# create the continuous flux variables (can be positive or negative)
self.var_v = {}
for r in self.model.reactions:
self.var_v[r] = LpVariable("v_%s" % r.id,
lowBound=r.lower_bound,
upBound=r.upper_bound,
cat=LpContinuous)
# this flag will be used later to know if to expect the flux
# variables to exist
self.has_flux_as_variables = True
# add the mass-balance constraints to each of the metabolites (S*v = 0)
for m in self.model.metabolites:
S_times_v = LpAffineExpression([(self.var_v[r], r.get_coefficient(m))
for r in m.reactions])
self.prob.addConstraint(S_times_v == 0, 'mass_balance_%s' % m.id)
def add_dual_variables_and_constraints(self):
# create dual variables associated with stoichiometric constraints
self.var_lambda = dict([(m, LpVariable("lambda_%s" % m.id,
lowBound=-M,
upBound=M,
cat=LpContinuous))
for m in self.model.metabolites])
# create dual variables associated with the constraints on the primal fluxes
self.var_w_U = dict([(r, LpVariable("w_U_%s" % r.id, lowBound=0, upBound=M, cat=LpContinuous))
for r in self.model.reactions])
self.var_w_L = dict([(r, LpVariable("w_L_%s" % r.id, lowBound=0, upBound=M, cat=LpContinuous))
for r in self.model.reactions])
# add the dual constraints:
# S'*lambda + w_U - w_L = c_biomass
for r in self.model.reactions:
S_times_lambda = LpAffineExpression([(self.var_lambda[m], coeff)
for m, coeff in r._metabolites.iteritems()
if coeff != 0])
row_sum = S_times_lambda + self.var_w_U[r] - self.var_w_L[r]
self.prob.addConstraint(row_sum == r.objective_coefficient, 'dual_%s' % r.id)
def prepare_FBA_primal(self, use_glpk=False):
"""
Run standard FBA (primal)
"""
self.create_prob(sense=LpMaximize, use_glpk=use_glpk)
self.add_primal_variables_and_constraints()
self.prob.setObjective(self.var_v[self.r_biomass])
def prepare_FBA_dual(self, use_glpk=False):
"""
Run shadow FBA (dual)
"""
self.create_prob(sense=LpMinimize, use_glpk=use_glpk)
self.add_dual_variables_and_constraints()
w_sum = LpAffineExpression([(self.var_w_U[r], r.upper_bound)
for r in self.model.reactions if r.upper_bound != 0] +
[(self.var_w_L[r], -r.lower_bound)
for r in self.model.reactions if r.lower_bound != 0])
self.prob.setObjective(w_sum)
def get_reaction_by_id(self, reaction_id):
if reaction_id not in self.model.reactions:
return None
reaction_ind = self.model.reactions.index(reaction_id)
reaction = self.model.reactions[reaction_ind]
return reaction
def add_optknock_variables_and_constraints(self):
# create the binary variables indicating which reactions knocked out
self.var_y = dict([(r, LpVariable("y_%s" % r.id, cat=LpBinary))
for r in self.model.reactions])
#.........这里部分代码省略.........