Unverified Commit 9dd778b7 authored by St. Elmo's avatar St. Elmo Committed by Miroslav Kratochvil
Browse files

remove extra returns

parent 15efb26a
...@@ -71,8 +71,9 @@ function fba( ...@@ -71,8 +71,9 @@ function fba(
sense = MOI.MAX_SENSE, sense = MOI.MAX_SENSE,
) )
# get core optimization problem # get core optimization problem
cbm, v, mb, lbcons, ubcons = make_optimization_model(model, optimizer, sense = sense) cbm = make_optimization_model(model, optimizer, sense = sense)
v = cbm[:x] # fluxes
# modify core optimization problem according to user specifications # modify core optimization problem according to user specifications
if !isempty(solver_attributes) # set other attributes if !isempty(solver_attributes) # set other attributes
for (k, val) in solver_attributes for (k, val) in solver_attributes
...@@ -83,7 +84,7 @@ function fba( ...@@ -83,7 +84,7 @@ function fba(
# set additional constraints # set additional constraints
for (rxnid, con) in constraints for (rxnid, con) in constraints
ind = model.reactions[findfirst(model.reactions, rxnid)] ind = model.reactions[findfirst(model.reactions, rxnid)]
set_bound(ind, lbcons, ubcons; lb = con[1], ub = con[2]) set_bound(ind, cbm; lb = con[1], ub = con[2])
end end
# if an objective function is supplied, modify the default objective # if an objective function is supplied, modify the default objective
......
...@@ -37,14 +37,15 @@ function flux_variability_analysis( ...@@ -37,14 +37,15 @@ function flux_variability_analysis(
throw(DomainError(reactions, "Index exceeds number of reactions.")) throw(DomainError(reactions, "Index exceeds number of reactions."))
end end
(optimization_model, x0) = flux_balance_analysis(model, optimizer) optimization_model = flux_balance_analysis(model, optimizer)
Z0 = COBREXA.JuMP.objective_value(optimization_model) Z0 = COBREXA.JuMP.objective_value(optimization_model)
optimization_model = nothing # we won't need this one anymore, so free the memory optimization_model = nothing # we won't need this one anymore, so free the memory
# store a JuMP optimization model at all workers # store a JuMP optimization model at all workers
save_model = :( save_model = :(
begin begin
optmodel, x = COBREXA.make_optimization_model($model, $optimizer) optmodel = COBREXA.make_optimization_model($model, $optimizer)
x = optmodel[:x]
COBREXA._FVA_add_constraint(optmodel, $(objective(model)), x, $Z0, $gamma) COBREXA._FVA_add_constraint(optmodel, $(objective(model)), x, $Z0, $gamma)
optmodel optmodel
end end
...@@ -140,7 +141,8 @@ function fva( ...@@ -140,7 +141,8 @@ function fva(
constraints = Dict{String,Tuple{Float64,Float64}}(), constraints = Dict{String,Tuple{Float64,Float64}}(),
sense = MOI.MAX_SENSE, sense = MOI.MAX_SENSE,
) )
cbm, v, mb, lbcons, ubcons = make_optimization_model(model, optimizer, sense = sense) cbm = make_optimization_model(model, optimizer, sense = sense)
v = cbm[:x]
if !isempty(solver_attributes) # set other attributes if !isempty(solver_attributes) # set other attributes
for (k, v) in solver_attributes for (k, v) in solver_attributes
...@@ -151,7 +153,7 @@ function fva( ...@@ -151,7 +153,7 @@ function fva(
# set additional constraints # set additional constraints
for (rxnid, con) in constraints for (rxnid, con) in constraints
ind = model.reactions[findfirst(model.reactions, rxnid)] ind = model.reactions[findfirst(model.reactions, rxnid)]
set_bound(ind, lbcons, ubcons; lb = con[1], ub = con[2]) set_bound(ind, cbm; lb = con[1], ub = con[2])
end end
# if an objective function is supplied, modify the default objective # if an objective function is supplied, modify the default objective
......
...@@ -33,16 +33,16 @@ function pfba( ...@@ -33,16 +33,16 @@ function pfba(
if typeof(optimizer) <: AbstractArray # choose optimizer if typeof(optimizer) <: AbstractArray # choose optimizer
cbm, v, mb, lbcons, ubcons = cbm = make_optimization_model(model, optimizer[1], sense = sense)
make_optimization_model(model, optimizer[1], sense = sense) v = cbm[:x]
if !isempty(solver_attributes["opt1"]) # set other attributes if !isempty(solver_attributes["opt1"]) # set other attributes
for (k, v) in solver_attributes["opt1"] for (k, v) in solver_attributes["opt1"]
set_optimizer_attribute(cbm, k, v) set_optimizer_attribute(cbm, k, v)
end end
end end
else # singe optimizer else # singe optimizer
cbm, v, mb, lbcons, ubcons = cbm = make_optimization_model(model, optimizer, sense = sense)
make_optimization_model(model, optimizer, sense = sense) v = cbm[:x]
if !isempty(solver_attributes) # set other attributes if !isempty(solver_attributes) # set other attributes
for (k, v) in solver_attributes for (k, v) in solver_attributes
set_optimizer_attribute(cbm, k, v) set_optimizer_attribute(cbm, k, v)
...@@ -53,7 +53,7 @@ function pfba( ...@@ -53,7 +53,7 @@ function pfba(
# set additional constraints # set additional constraints
for (rxnid, con) in constraints for (rxnid, con) in constraints
ind = model.reactions[findfirst(model.reactions, rxnid)] ind = model.reactions[findfirst(model.reactions, rxnid)]
set_bound(ind, lbcons, ubcons; lb = con[1], ub = con[2]) set_bound(ind, cbm; lb = con[1], ub = con[2])
end end
# check if default objective should be used # check if default objective should be used
......
...@@ -13,11 +13,11 @@ function make_optimization_model( ...@@ -13,11 +13,11 @@ function make_optimization_model(
optimization_model = COBREXA.JuMP.Model(optimizer) optimization_model = COBREXA.JuMP.Model(optimizer)
@variable(optimization_model, x[i = 1:n]) @variable(optimization_model, x[i = 1:n])
@objective(optimization_model, sense, objective(model)' * x) @objective(optimization_model, sense, objective(model)' * x)
mb = @constraint(optimization_model, mb, stoichiometry(model) * x .== balance(model)) # mass balance @constraint(optimization_model, mb, stoichiometry(model) * x .== balance(model)) # mass balance
lbs = @constraint(optimization_model, lbs, xl .<= x) # lower bounds @constraint(optimization_model, lbs, xl .<= x) # lower bounds
ubs = @constraint(optimization_model, ubs, x .<= xu) # upper bounds @constraint(optimization_model, ubs, x .<= xu) # upper bounds
return optimization_model, x, mb, lbs, ubs return optimization_model
end end
""" """
...@@ -28,8 +28,7 @@ function optimize_model( ...@@ -28,8 +28,7 @@ function optimize_model(
optimizer; optimizer;
sense = MOI.MIN_SENSE, sense = MOI.MIN_SENSE,
) where {LM<:MetabolicModel} ) where {LM<:MetabolicModel}
optimization_model, x, _, _, _ = optimization_model = make_optimization_model(model, optimizer; sense = sense)
make_optimization_model(model, optimizer; sense = sense) COBREXA.JuMP.optimize!(optimization_model)
COBREXA.JuMP.optimize!(optimization_model) return optimization_model
return (optimization_model, x)
end end
...@@ -21,16 +21,16 @@ Base.copy(model::CoupledLinearModel) = ...@@ -21,16 +21,16 @@ Base.copy(model::CoupledLinearModel) =
""" """
set_bound(index, ubconstaintref, lbconstaintref; ub=1000, lb=-1000) set_bound(index, optimization_model; ub=1000, lb=-1000)
Helper function to set the bounds of variables. Helper function to set the bounds of variables.
The JuMP `set_normalized_rhs` function is a little confusing, so this function simplifies setting The JuMP `set_normalized_rhs` function is a little confusing, so this function simplifies setting
constraints. Just supply the constraint `index` and the bound objects (`ubconstaintref`, `lbconstaintref`) and they will be set to `ub` and `lb`. constraints. Just supply the constraint `index` and the model and they will be set to `ub` and `lb`.
""" """
function set_bound(vind, lbs, ubs; ub = 1000, lb = -1000) function set_bound(vind, opt_model; ub = 1000, lb = -1000)
if lb <= 0 if lb <= 0
set_normalized_rhs(lbs[vind], abs(lb)) set_normalized_rhs(opt_model[:lbs][vind], abs(lb))
else else
set_normalized_rhs(lbs[vind], -abs(lb)) set_normalized_rhs(opt_model[:lbs][vind], -abs(lb))
end end
set_normalized_rhs(ubs[vind], ub) set_normalized_rhs(opt_model[:ubs][vind], ub)
end end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment