Skip to content

Commit

Permalink
fix small things in new base cobrexa
Browse files Browse the repository at this point in the history
  • Loading branch information
stelmo committed Oct 20, 2023
1 parent 53d43e5 commit a3476b9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 55 deletions.
31 changes: 15 additions & 16 deletions src/builders/core.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import AbstractFBCModels as F
import AbstractFBCModels as A
import SparseArrays: sparse

"""
Expand All @@ -8,22 +8,21 @@ $(TYPEDSIGNATURES)
A constraint tree that models the content of the given instance of
`AbstractFBCModel`.
"""
metabolic_model(model::F.AbstractFBCModel) =
let
rxns =
Symbol.(F.reactions(model)), mets =
Symbol.(F.metabolites(model)), lbs, ubs =
F.bounds(model), stoi =
F.stoichiometry(model), bal =
F.balance(model), obj = F.objective(model)
function metabolic_model(model::A.AbstractFBCModel)
rxns = Symbol.(A.reactions(model))
mets = Symbol.(A.metabolites(model))
lbs, ubs = A.bounds(model)
stoi = A.stoichiometry(model)
bal = A.balance(model)
obj = A.objective(model)

:fluxes^C.variables(keys = rxns, bounds = zip(lbs, ubs)) *
:balance^C.ConstraintTree(
m => Constraint(value = Value(sparse(row)), bound = b) for
(m, row, b) in zip(mets, eachrow(stoi), bals)
) *
:objective^C.Constraint(C.Value(sparse(obj)))
end
:fluxes^C.variables(keys = rxns, bounds = zip(lbs, ubs)) *
:stoichiometry^C.ConstraintTree(
met => C.Constraint(value = C.LinearValue(sparse(row)), bound = b) for
(met, row, b) in zip(mets, eachrow(stoi), bal)
) *
:objective^C.Constraint(C.LinearValue(sparse(obj)))
end

"""
$(TYPEDSIGNATURES)
Expand Down
51 changes: 12 additions & 39 deletions src/solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,29 @@ Construct a JuMP `Model` that describes the precise constraint system into the
JuMP `Model` created for solving in `optimizer`, with a given optional
`objective` and optimization `sense`.
"""
function J.Model(
constraints::C.ConstraintTree;
objective::Maybe{C.Value} = nothing,
function to_jump_model(
cs::C.ConstraintTree;
objective::Union{C.LinearValue,C.QuadraticValue, Nothing} = nothing,
optimizer,
sense = J.MAX_SENSE,
)
# TODO this might better have its own name to avoid type piracy.
model = J.Model(optimizer)

J.@variable(model, x[1:C.var_count(cs)])

# objectives
if objective isa C.Value
JuMP.@objective(model, sense, C.value_product(objective, x))
elseif objective isa C.QValue
JuMP.@objective(model, sense, C.qvalue_product(objective, x))
end


isnothing(objective) || J.@objective(model, sense, C.substitute(objective, x))

# constraints
function add_constraint(c::C.Constraint)
if c.bound isa Float64
J.@constraint(model, C.value_product(c.value, x) == c.bound)
J.@constraint(model, C.substitute(c.value, x) == c.bound)
elseif c.bound isa C.IntervalBound
val = C.value_product(c.value, x)
val = C.substitute(c.value, x)
isinf(c.bound[1]) || J.@constraint(model, val >= c.bound[1])
isinf(c.bound[2]) || J.@constraint(model, val <= c.bound[2])
end
end
function add_constraint(c::C.QConstraint)
if c.bound isa Float64
JuMP.@constraint(model, C.qvalue_product(c.qvalue, x) == c.bound)
elseif c.bound isa Tuple{Float64,Float64}
val = C.qvalue_product(c.qvalue, x)
isinf(c.bound[1]) || JuMP.@constraint(model, val >= c.bound[1])
isinf(c.bound[2]) || JuMP.@constraint(model, val <= c.bound[2])
end
end
function add_constraint(c::C.ConstraintTree)
add_constraint.(values(c))
end
Expand All @@ -55,20 +42,6 @@ end
"""
$(TYPEDSIGNATURES)
Convenience re-export of `Model` from JuMP.
"""
const Model = J.Model

"""
$(TYPEDSIGNATURES)
Convenience re-export of `optimize!` from JuMP.
"""
const optimize! = J.optimize!

"""
$(TYPEDSIGNATURES)
`true` if `opt_model` solved successfully (solution is optimal or
locally optimal). `false` if any other termination status is reached.
"""
Expand All @@ -89,14 +62,14 @@ $(TYPEDSIGNATURES)
The optimized variable assignment of a JuMP model, if solved.
"""
optimized_variable_assignment(opt_model::J.Model)::Maybe{Vector{Float64}} =
is_solved(opt_model) ? J.value.(model[:x]) : nothing
is_solved(opt_model) ? J.value.(opt_model[:x]) : nothing

"""
$(TYPEDSIGNATURES)
Convenience overload for making solution trees out of JuMP models
"""
C.SolutionTree(c::C.ConstraintTree, opt_model::J.Model)::Maybe{C.SolutionTree} =
C.ValueTree(c::C.ConstraintTree, opt_model::J.Model)::Maybe{C.ValueTree} =
let vars = optimized_variable_assignment(opt_model)
isnothing(vars) ? nothing : C.SolutionTree(c, vars)
isnothing(vars) ? nothing : C.ValueTree(c, vars)
end

0 comments on commit a3476b9

Please sign in to comment.