Skip to content
Snippets Groups Projects
Unverified Commit 7f0fc9d2 authored by St. Elmo's avatar St. Elmo
Browse files

added constraint modification callback

parent 56052b41
No related branches found
No related tags found
No related merge requests found
...@@ -65,16 +65,23 @@ sol = fba(model, biomass, optimizer; solver_attributes=atts) ...@@ -65,16 +65,23 @@ sol = fba(model, biomass, optimizer; solver_attributes=atts)
function fba( function fba(
model::CobraModel, model::CobraModel,
optimizer; optimizer;
objective_func::Union{Reaction,Array{Reaction,1}} = Reaction[], modifications = [(model, opt_model)->nothing]
weights = Float64[],
solver_attributes = Dict{Any,Any}(),
constraints = Dict{String,Tuple{Float64,Float64}}(),
sense = MOI.MAX_SENSE,
) )
objective_func::Union{Reaction,Array{Reaction,1}} = Reaction[]
weights = Float64[]
solver_attributes = Dict{Any,Any}()
sense = MOI.MAX_SENSE
# get core optimization problem # get core optimization problem
cbm = make_optimization_model(model, optimizer, sense = sense) cbm = make_optimization_model(model, optimizer, sense = sense)
v = cbm[:x] # fluxes v = cbm[:x] # fluxes
# apply callbacks
for mod in modifications
mod(model, cbm)
end
# 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
...@@ -82,12 +89,6 @@ function fba( ...@@ -82,12 +89,6 @@ function fba(
end end
end end
# set additional constraints
for (rxnid, con) in constraints
ind = model.reactions[findfirst(model.reactions, rxnid)]
set_bound(ind, cbm; lb = con[1], ub = con[2])
end
# if an objective function is supplied, modify the default objective # if an objective function is supplied, modify the default objective
if typeof(objective_func) == Reaction || !isempty(objective_func) if typeof(objective_func) == Reaction || !isempty(objective_func)
# ensure that an array of objective indices are fed in # ensure that an array of objective indices are fed in
......
...@@ -34,3 +34,15 @@ function set_bound(vind, opt_model; ub = 1000, lb = -1000) ...@@ -34,3 +34,15 @@ function set_bound(vind, opt_model; ub = 1000, lb = -1000)
end end
set_normalized_rhs(opt_model[:ubs][vind], ub) set_normalized_rhs(opt_model[:ubs][vind], ub)
end end
"""
modify_constraint(reaction::Reaction, lb, ub)
Modify constraints of model reaction.
"""
function modify_constraint(reaction::Reaction, lb, ub)
(model, opt_model) -> begin
ind = model.reactions[reaction]
set_bound(ind, opt_model, lb=lb, ub=ub)
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment