Skip to content

Commit

Permalink
Merge pull request #825 from LCSB-BioCore/mk-workaround-coupling-equa…
Browse files Browse the repository at this point in the history
…lities

work-around equalities in coupling bounds in MAT
  • Loading branch information
exaexa committed Jun 9, 2024
2 parents f47a40b + 9db803f commit 8742d35
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/base/solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ function make_optimization_model(model::MetabolicModel, optimizer; sense = MAX_S
C = coupling(model) # empty if no coupling
isempty(C) || begin
cl, cu = coupling_bounds(model)
@constraint(optimization_model, c_lbs, cl .<= C * x) # coupling lower bounds
@constraint(optimization_model, c_ubs, C * x .<= cu) # coupling upper bounds
@constraint(
optimization_model,
c_lbs,
cl[isfinite.(cl)] .<= C[isfinite.(cl), :] * x
) # coupling lower bounds
@constraint(
optimization_model,
c_ubs,
C[isfinite.(cu), :] * x .<= cu[isfinite.(cu)]
) # coupling upper bounds
end

return optimization_model
Expand Down
17 changes: 13 additions & 4 deletions src/base/types/MATModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,23 @@ Extracts the coupling constraints. Currently, there are several accepted ways to
function coupling_bounds(m::MATModel)
nc = n_coupling_constraints(m)
if _mat_has_squashed_coupling(m.mat)
c = reshape(m.mat["b"], length(m.mat["b"]))[n_reactions(m)+1:end]
csense = reshape(m.mat["csense"], length(m.mat["csense"]))[n_reactions(m)+1:end],
(
sparse(fill(-Inf, nc)),
sparse(reshape(m.mat["b"], length(m.mat["b"]))[n_reactions(m)+1:end]),
[sense in ["G", "E"] ? val : -Inf for (val, sense) in zip(c, csense)],
[sense in ["L", "E"] ? val : Inf for (val, sense) in zip(c, csense)],
)
elseif haskey(m.mat, "d") && haskey(m.mat, "dsense")
d = reshape(m.mat["d"], nc)
dsense = reshape(m.mat["dsense"], nc)
(
[sense in ["G", "E"] ? val : -Inf for (val, sense) in zip(d, dsense)],
[sense in ["L", "E"] ? val : Inf for (val, sense) in zip(d, dsense)],
)
else
(
sparse(reshape(get(m.mat, "cl", fill(-Inf, nc, 1)), nc)),
sparse(reshape(get(m.mat, "cu", fill(Inf, nc, 1)), nc)),
reshape(get(m.mat, "cl", fill(-Inf, nc, 1)), nc),
reshape(get(m.mat, "cu", fill(Inf, nc, 1)), nc),
)
end
end
Expand Down

0 comments on commit 8742d35

Please sign in to comment.