本文整理汇总了Python中cobra.Model.optimize方法的典型用法代码示例。如果您正苦于以下问题:Python Model.optimize方法的具体用法?Python Model.optimize怎么用?Python Model.optimize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cobra.Model
的用法示例。
在下文中一共展示了Model.optimize方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Metabolite
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import optimize [as 别名]
Cone_production.objective_coefficient = -1*cone_production_cost
Popsicle_production.objective_coefficient = -1*popsicle_production_cost
production_capacity_constraint = Metabolite(id='production_capacity_constraint')
production_capacity_constraint._constraint_sense = 'L'
production_capacity_constraint._bound = starting_budget;
Cone_production.add_metabolites({production_capacity_constraint: cone_production_cost })
Popsicle_production.add_metabolites({production_capacity_constraint: popsicle_production_cost })
print()
print('Here is what happens in the continuous (LP) case...')
the_program = cobra_model.optimize(objective_sense='maximize')
print()
print(('Status is: %s'%cobra_model.solution.status))
print(('Objective value is: %1.2f'%cobra_model.solution.f))
for the_reaction, the_value in cobra_model.solution.x_dict.items():
print('%s: %1.2f'%(the_reaction, the_value))
print()
print('Who wants 1/3 of a cone? Cones and popsicles are units aka integers, reformulate as MILP')
Cone_production.variable_kind = 'integer'
Popsicle_production.variable_kind = 'integer'
the_program = cobra_model.optimize(objective_sense='maximize')
print()
print(('Status is: %s'%cobra_model.solution.status))
示例2: solve_mip
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import optimize [as 别名]
def solve_mip(self):
cone_selling_price = 7.0
cone_production_cost = 3.0
popsicle_selling_price = 2.0
popsicle_production_cost = 1.0
starting_budget = 100.0
cobra_model = Model("MILP_implementation_test")
cone_out = Metabolite(id="cone_out", compartment="c")
cone_in = Metabolite(id="cone_in", compartment="c")
cone_consumed = Metabolite(id="cone_consumed", compartment="c")
popsicle_out = Metabolite(id="popsicle_out", compartment="c")
popsicle_in = Metabolite(id="popsicle_in", compartment="c")
popsicle_consumed = Metabolite(id="popsicle_consumed", compartment="c")
the_reactions = []
# SOURCE
Cone_source = Reaction(name="Cone_source")
temp_metabolite_dict = {cone_out: 1}
Cone_source.add_metabolites(temp_metabolite_dict)
the_reactions.append(Cone_source)
Popsicle_source = Reaction(name="Popsicle_source")
temp_metabolite_dict = {popsicle_out: 1}
Popsicle_source.add_metabolites(temp_metabolite_dict)
the_reactions.append(Popsicle_source)
## PRODUCTION
Cone_production = Reaction(name="Cone_production")
temp_metabolite_dict = {cone_out: -1, cone_in: 1}
Cone_production.add_metabolites(temp_metabolite_dict)
the_reactions.append(Cone_production)
Popsicle_production = Reaction(name="Popsicle_production")
temp_metabolite_dict = {popsicle_out: -1, popsicle_in: 1}
Popsicle_production.add_metabolites(temp_metabolite_dict)
the_reactions.append(Popsicle_production)
## CONSUMPTION
Cone_consumption = Reaction(name="Cone_consumption")
temp_metabolite_dict = {cone_in: -1, cone_consumed: 1}
Cone_consumption.add_metabolites(temp_metabolite_dict)
the_reactions.append(Cone_consumption)
Popsicle_consumption = Reaction(name="Popsicle_consumption")
temp_metabolite_dict = {popsicle_in: -1, popsicle_consumed: 1}
Popsicle_consumption.add_metabolites(temp_metabolite_dict)
the_reactions.append(Popsicle_consumption)
# SINK
Cone_consumed_sink = Reaction(name="Cone_consumed_sink")
temp_metabolite_dict = {cone_consumed: -1}
Cone_consumed_sink.add_metabolites(temp_metabolite_dict)
the_reactions.append(Cone_consumed_sink)
Popsicle_consumed_sink = Reaction(name="Popsicle_consumed_sink")
temp_metabolite_dict = {popsicle_consumed: -1}
Popsicle_consumed_sink.add_metabolites(temp_metabolite_dict)
the_reactions.append(Popsicle_consumed_sink)
## add all reactions
cobra_model.add_reactions(the_reactions)
# set objective coefficients
Cone_consumption.objective_coefficient = cone_selling_price
Popsicle_consumption.objective_coefficient = popsicle_selling_price
Cone_production.objective_coefficient = -1 * cone_production_cost
Popsicle_production.objective_coefficient = -1 * popsicle_production_cost
# Make sure we produce whole cones
Cone_production.variable_kind = "integer"
Popsicle_production.variable_kind = "integer"
production_capacity_constraint = Metabolite(id="production_capacity_constraint")
production_capacity_constraint._constraint_sense = "L"
production_capacity_constraint._bound = starting_budget
Cone_production.add_metabolites({production_capacity_constraint: cone_production_cost})
Popsicle_production.add_metabolites({production_capacity_constraint: popsicle_production_cost})
cobra_model.optimize(solver=solver_name)
self.assertEqual(133, cobra_model.solution.f)
self.assertEqual(33, cobra_model.solution.x_dict["Cone_consumption"])
示例3: print
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import optimize [as 别名]
# or cplex. If a solver which supports quadratic programming is installed, this
# function will return its name.
print(solvers.get_solver_name(qp=True))
# Prints:
# gurobi
c = Metabolite("c")
c._bound = 2
x = Reaction("x")
y = Reaction("y")
x.add_metabolites({c: 1})
y.add_metabolites({c: 1})
m = Model()
m.add_reactions([x, y])
sol = m.optimize(quadratic_component=Q, objective_sense="minimize")
sol.x_dict
# Output:
# {'x': 1.0, 'y': 1.0}
# Suppose we change the problem to have a mixed linear and quadratic objective.
#
# > **min** $\frac{1}{2}\left(x^2 + y^2 \right) - y$
#
# > *subject to*
#
# > $x + y = 2$
#
# > $x \ge 0$
#
# > $y \ge 0$
示例4: Metabolite
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import optimize [as 别名]
# constrainted to a budget
budget = Metabolite("budget")
budget._constraint_sense = "L"
budget._bound = starting_budget
cone.add_metabolites({budget: cone_production_cost})
popsicle.add_metabolites({budget: popsicle_production_cost})
# objective coefficient is the profit to be made from each unit
cone.objective_coefficient = cone_selling_price - cone_production_cost
popsicle.objective_coefficient = popsicle_selling_price - \
popsicle_production_cost
m = Model("lerman_ice_cream_co")
m.add_reactions((cone, popsicle))
m.optimize().x_dict
# Output:
# {'cone': 33.333333333333336, 'popsicle': 0.0}
# In reality, cones and popsicles can only be sold in integer amounts. We can
# use the variable kind attribute of a cobra.Reaction to enforce this.
cone.variable_kind = "integer"
popsicle.variable_kind = "integer"
m.optimize().x_dict
# Output:
# {'cone': 33.0, 'popsicle': 1.0}
# Now the model makes both popsicles and cones.
### Restaurant Order
示例5: dict
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import optimize [as 别名]
reaction_dict = dict([(x.id, x) for x in cobra_model.reactions])
production_capacity_constraint = Metabolite(id='production_capacity_constraint')
production_capacity_constraint._constraint_sense = 'L'
production_capacity_constraint._bound = starting_budget;
the_reaction = reaction_dict['Cone_production']
the_reaction.add_metabolites({production_capacity_constraint: cone_production_cost })
the_reaction = reaction_dict['Popsicle_production']
the_reaction.add_metabolites({production_capacity_constraint: popsicle_production_cost })
print
print('Here is what happens in the continuous (LP) case...')
the_program = cobra_model.optimize(objective_sense='maximize',solver=solver)
print
print('Status is: %s'%cobra_model.solution.status)
print('Objective value is: %1.2f'%cobra_model.solution.f)
for the_reaction, the_value in cobra_model.solution.x_dict.items():
print '%s: %1.2f'%(the_reaction, the_value)
print
print('Who wants 1/3 of a cone, WTF??? Cones and popsicles are units aka integers, reformulate as MILP')