當前位置: 首頁>>代碼示例>>Python>>正文


Python LpProblem.addConstraint方法代碼示例

本文整理匯總了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])

#.........這裏部分代碼省略.........
開發者ID:eladnoor,項目名稱:optslope,代碼行數:103,代碼來源:optknock.py


注:本文中的pulp.LpProblem.addConstraint方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。