Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Constraint to convert Ints into Float64 in the bounds #13

Merged
merged 7 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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