Skip to content

Commit

Permalink
Merge pull request #13 from COBREXA/sew-constraint-bounds-take-ints
Browse files Browse the repository at this point in the history
Allow `Constraint` to convert `Int`s into `Float64` in the bounds
  • Loading branch information
exaexa authored Dec 16, 2023
2 parents 5c7496e + 12eb582 commit b0b09f0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
8 changes: 4 additions & 4 deletions docs/src/metabolic-modeling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ C.constraint_values(c.objective, optimal_variable_assignment)
# organisms:
c =
:community^(
:species1^(c * :handicap^C.Constraint(c.fluxes.R_PFK.value, 0.0)) +
:species2^(c * :handicap^C.Constraint(c.fluxes.R_ACALD.value, 0.0))
:species1^(c * :handicap^C.Constraint(c.fluxes.R_PFK.value, 0)) +
:species2^(c * :handicap^C.Constraint(c.fluxes.R_ACALD.value, 0))
)

# We can create additional variables that represent total community intake of
Expand Down Expand Up @@ -349,10 +349,10 @@ Dict(k => v.fluxes.R_BIOMASS_Ecoli_core_w_GAM for (k, v) in result.community)
c.exchanges.oxygen.bound = (-20.0, 20.0)

# ...or rebuild a whole constraint:
c.exchanges.biomass = C.Constraint(c.exchanges.biomass.value, (-20.0, 20.0))
c.exchanges.biomass = C.Constraint(c.exchanges.biomass.value, (-20, 20))

# ...or even add new constraints, here using the index syntax for demonstration:
c[:exchanges][:production_is_zero] = C.Constraint(c.exchanges.biomass.value, 0.0)
c[:exchanges][:production_is_zero] = C.Constraint(c.exchanges.biomass.value, 0)

# ...or remove some constraints (this erases the constraint that was added just
# above):
Expand Down
2 changes: 1 addition & 1 deletion docs/src/quadratic-optimization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ error_val =

# This allows us to naturally express quadratic constraint (e.g., that an error
# must not be too big); and directly observe the error values in the system.
system = :vars^system * :error^C.Constraint(value = error_val, bound = (0.0, 100.0))
system = :vars^system * :error^C.Constraint(error_val, (0, 100))

# (For simplicity, you can also use the `Constraint` constructor to make
# quadratic constraints out of `QuadraticValue`s -- it will overload properly.)
Expand Down
4 changes: 4 additions & 0 deletions src/constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Base.@kwdef mutable struct Constraint{V}
end
end

Constraint(v::T, b::Int) where {T<:Value} = Constraint(v, Float64(b))
Constraint(v::T, b::Tuple{X,Y}) where {T<:Value,X<:Real,Y<:Real} =
Constraint(v, Float64.(b))

Base.:-(a::Constraint) = -1 * a
Base.:*(a::Real, b::Constraint) = b * a
Base.:*(a::Constraint, b::Real) = Constraint(
Expand Down

0 comments on commit b0b09f0

Please sign in to comment.