-
I am looping through bounds for different conditions and finding the solution for each: input: model = cobra.io.sbml.read_sbml_model(“file.xml”)
conditions =[“condition_1”, “condition_2”]
for condition in conditions:
if condition == “condition_1”:
model.Reactions.get_by_id(“R1”).bounds=0,1000
model.Reactions.get_by_id(“R2”).bounds=-1000,1000
if condition == “condition_2”:
model.Reactions.get_by_id(“R1”).bounds=0,500
model.Reactions.get_by_id(“R2”).bounds=-500,500
with model:
solution= model.optimize()
print(“H2 produced when condition = ”, condition, “:”, solution.fluxes[“SK_H2[c]”]) output:
However I have found that optimising for a second solution (say, with another objective of interest) in the loop somehow changes the output for the following conditions: input: model = cobra.io.sbml.read_sbml_model(“file.xml”)
conditions =[“condition_1”, “condition_2”]
for condition in conditions:
if condition == “condition_1”:
model.Reactions.get_by_id(“R1”).bounds=0,1000
model.Reactions.get_by_id(“R2”).bounds=-1000,1000
if condition == “condition_2”:
model.Reactions.get_by_id(“R1”).bounds=0,500
model.Reactions.get_by_id(“R2”).bounds=-500,500
with model:
solution= model.optimize()
print(“H2 produced when condition = ”, condition, “:”, solution.fluxes[“SK_H2[c]”])
with model:
model.objective = new_objective
new_solution= model.optimize()
print(“H2 produced when condition = ”, condition, “and objective = new_objective:”, new_solution.fluxes[“SK_H2[c]”]) output:
I took this result to mean that the model is somehow being modified by the second model.optimize(). I have noticed this same result when performing with model:
flux_variability_analysis(model, model.reactions.get_by_id('SK_H2_c[c]')) after a model.optimize(). Is the only resolution to redefine the model for each condition?: for condition in conditions:
if condition == “condition_1”:
model = cobra.io.sbml.read_sbml_model(“file.xml”)
model.Reactions.get_by_id(“R1”).bounds=0,1000
model.Reactions.get_by_id(“R2”).bounds=-1000,1000
if condition == “condition_2”:
model = cobra.io.sbml.read_sbml_model(“file.xml”)
model.Reactions.get_by_id(“R1”).bounds=0,500
model.Reactions.get_by_id(“R2”).bounds=-500,500 I was reluctant to do this as I have an extensive 'tuning' process in between defining the model and the for loop. Any help will be appreciated! Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You need to move the bounds setting into the |
Beta Was this translation helpful? Give feedback.
It's not stochastic but you are trying to reproduce non-unique solutions and that will be hard. So basically your objective value is guaranteed to be unique but your fluxes are not since there may be many different flux solutions yielding an optimal value. The solver just picks whatever is easiest for the solver. When you optimize several times the previous solution is used to seed the solver which speeds it up but also can lead to the behavior you are seeing.
The assignment
clean_model = model
unfortunately does nothing. They both still point to the same object (Python assigns by reference). If you want to reinitialize your model in every iteration you can do something likefor model.cop…