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

remove extra returns

parent 15efb26a
......@@ -71,7 +71,8 @@ function fba(
sense = MOI.MAX_SENSE,
)
# 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
if !isempty(solver_attributes) # set other attributes
......@@ -83,7 +84,7 @@ function fba(
# set additional constraints
for (rxnid, con) in constraints
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
# if an objective function is supplied, modify the default objective
......
......@@ -37,14 +37,15 @@ function flux_variability_analysis(
throw(DomainError(reactions, "Index exceeds number of reactions."))
end
(optimization_model, x0) = flux_balance_analysis(model, optimizer)
optimization_model = flux_balance_analysis(model, optimizer)
Z0 = COBREXA.JuMP.objective_value(optimization_model)
optimization_model = nothing # we won't need this one anymore, so free the memory
# store a JuMP optimization model at all workers
save_model = :(
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)
optmodel
end
......@@ -140,7 +141,8 @@ function fva(
constraints = Dict{String,Tuple{Float64,Float64}}(),
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
for (k, v) in solver_attributes
......@@ -151,7 +153,7 @@ function fva(
# set additional constraints
for (rxnid, con) in constraints
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
# if an objective function is supplied, modify the default objective
......
......@@ -33,16 +33,16 @@ function pfba(
if typeof(optimizer) <: AbstractArray # choose optimizer
cbm, v, mb, lbcons, ubcons =
make_optimization_model(model, optimizer[1], sense = sense)
cbm = make_optimization_model(model, optimizer[1], sense = sense)
v = cbm[:x]
if !isempty(solver_attributes["opt1"]) # set other attributes
for (k, v) in solver_attributes["opt1"]
set_optimizer_attribute(cbm, k, v)
end
end
else # singe optimizer
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
for (k, v) in solver_attributes
set_optimizer_attribute(cbm, k, v)
......@@ -53,7 +53,7 @@ function pfba(
# set additional constraints
for (rxnid, con) in constraints
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
# check if default objective should be used
......
......@@ -13,11 +13,11 @@ function make_optimization_model(
optimization_model = COBREXA.JuMP.Model(optimizer)
@variable(optimization_model, x[i = 1:n])
@objective(optimization_model, sense, objective(model)' * x)
mb = @constraint(optimization_model, mb, stoichiometry(model) * x .== balance(model)) # mass balance
lbs = @constraint(optimization_model, lbs, xl .<= x) # lower bounds
ubs = @constraint(optimization_model, ubs, x .<= xu) # upper bounds
@constraint(optimization_model, mb, stoichiometry(model) * x .== balance(model)) # mass balance
@constraint(optimization_model, lbs, xl .<= x) # lower bounds
@constraint(optimization_model, ubs, x .<= xu) # upper bounds
return optimization_model, x, mb, lbs, ubs
return optimization_model
end
"""
......@@ -28,8 +28,7 @@ function optimize_model(
optimizer;
sense = MOI.MIN_SENSE,
) where {LM<:MetabolicModel}
optimization_model, x, _, _, _ =
make_optimization_model(model, optimizer; sense = sense)
optimization_model = make_optimization_model(model, optimizer; sense = sense)
COBREXA.JuMP.optimize!(optimization_model)
return (optimization_model, x)
return optimization_model
end
......@@ -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.
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
set_normalized_rhs(lbs[vind], abs(lb))
set_normalized_rhs(opt_model[:lbs][vind], abs(lb))
else
set_normalized_rhs(lbs[vind], -abs(lb))
set_normalized_rhs(opt_model[:lbs][vind], -abs(lb))
end
set_normalized_rhs(ubs[vind], ub)
set_normalized_rhs(opt_model[:ubs][vind], ub)
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