From 41563170869e33cc52c479bbb3951f18f1682cff Mon Sep 17 00:00:00 2001 From: "St. Elmo Wilken" Date: Sat, 16 Dec 2023 15:20:25 +0100 Subject: [PATCH 1/7] create convenience constructors --- src/constraint.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/constraint.jl b/src/constraint.jl index 8397e1c..ae29a4d 100644 --- a/src/constraint.jl +++ b/src/constraint.jl @@ -26,6 +26,11 @@ Base.@kwdef mutable struct Constraint{V} end end +Constraint(v::T, b::Int) where {T<:Value} = Constraint{T}(v, float(b)) +Constraint(v::T, b::Tuple{Int, Int}) where {T<:Value} = Constraint{T}(v, float.(b)) +Constraint(v::T, b::Tuple{Int, Float64}) where {T<:Value} = Constraint{T}(v, float.(b)) +Constraint(v::T, b::Tuple{Float64, Int}) where {T<:Value} = Constraint{T}(v, float.(b)) + Base.:-(a::Constraint) = -1 * a Base.:*(a::Real, b::Constraint) = b * a Base.:*(a::Constraint, b::Real) = Constraint( From 47db514f5ee41fb219e09cdc285eca70b5be26ec Mon Sep 17 00:00:00 2001 From: exaexa Date: Sat, 16 Dec 2023 15:31:26 +0000 Subject: [PATCH 2/7] automatic formatting triggered by @exaexa on PR #13 --- src/constraint.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/constraint.jl b/src/constraint.jl index ae29a4d..2a722e5 100644 --- a/src/constraint.jl +++ b/src/constraint.jl @@ -27,9 +27,9 @@ Base.@kwdef mutable struct Constraint{V} end Constraint(v::T, b::Int) where {T<:Value} = Constraint{T}(v, float(b)) -Constraint(v::T, b::Tuple{Int, Int}) where {T<:Value} = Constraint{T}(v, float.(b)) -Constraint(v::T, b::Tuple{Int, Float64}) where {T<:Value} = Constraint{T}(v, float.(b)) -Constraint(v::T, b::Tuple{Float64, Int}) where {T<:Value} = Constraint{T}(v, float.(b)) +Constraint(v::T, b::Tuple{Int,Int}) where {T<:Value} = Constraint{T}(v, float.(b)) +Constraint(v::T, b::Tuple{Int,Float64}) where {T<:Value} = Constraint{T}(v, float.(b)) +Constraint(v::T, b::Tuple{Float64,Int}) where {T<:Value} = Constraint{T}(v, float.(b)) Base.:-(a::Constraint) = -1 * a Base.:*(a::Real, b::Constraint) = b * a From 9c4b0085f84a4257d1e2858593c2678cf329db5a Mon Sep 17 00:00:00 2001 From: "St. Elmo Wilken" Date: Sat, 16 Dec 2023 18:14:45 +0100 Subject: [PATCH 3/7] implement reviews --- docs/src/metabolic-modeling.jl | 8 ++++---- src/constraint.jl | 6 ++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/docs/src/metabolic-modeling.jl b/docs/src/metabolic-modeling.jl index 78ead1f..e72ea46 100644 --- a/docs/src/metabolic-modeling.jl +++ b/docs/src/metabolic-modeling.jl @@ -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 @@ -346,10 +346,10 @@ Dict(k => v.fluxes.R_BIOMASS_Ecoli_core_w_GAM for (k, v) in result.community) # both dot-access and array-index syntax. # You can thus, e.g., set a single bound: -c.exchanges.oxygen.bound = (-20.0, 20.0) +c.exchanges.oxygen.bound = (-20.0, 20.0) # typed as Tuple{Float64, Float64} # ...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)) # construct automatically converts bounds to floats # ...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) diff --git a/src/constraint.jl b/src/constraint.jl index 2a722e5..1d3e3c5 100644 --- a/src/constraint.jl +++ b/src/constraint.jl @@ -26,10 +26,8 @@ Base.@kwdef mutable struct Constraint{V} end end -Constraint(v::T, b::Int) where {T<:Value} = Constraint{T}(v, float(b)) -Constraint(v::T, b::Tuple{Int,Int}) where {T<:Value} = Constraint{T}(v, float.(b)) -Constraint(v::T, b::Tuple{Int,Float64}) where {T<:Value} = Constraint{T}(v, float.(b)) -Constraint(v::T, b::Tuple{Float64,Int}) where {T<:Value} = Constraint{T}(v, float.(b)) +Constraint(v::T, b::Int) where {T<:Value} = Constraint{T}(v, Float64(b)) +Constraint(v::T, b::Tuple{X,Y}) where {T<:Value,X<:Real,Y<:Real} = Constraint{T}(v, Float64.(b)) Base.:-(a::Constraint) = -1 * a Base.:*(a::Real, b::Constraint) = b * a From 11b8934d6cb06dc531629dcb1c4c8787cbed9b39 Mon Sep 17 00:00:00 2001 From: stelmo Date: Sat, 16 Dec 2023 17:16:39 +0000 Subject: [PATCH 4/7] automatic formatting triggered by @stelmo on PR #13 --- src/constraint.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/constraint.jl b/src/constraint.jl index 1d3e3c5..30d881a 100644 --- a/src/constraint.jl +++ b/src/constraint.jl @@ -27,7 +27,8 @@ Base.@kwdef mutable struct Constraint{V} end Constraint(v::T, b::Int) where {T<:Value} = Constraint{T}(v, Float64(b)) -Constraint(v::T, b::Tuple{X,Y}) where {T<:Value,X<:Real,Y<:Real} = Constraint{T}(v, Float64.(b)) +Constraint(v::T, b::Tuple{X,Y}) where {T<:Value,X<:Real,Y<:Real} = + Constraint{T}(v, Float64.(b)) Base.:-(a::Constraint) = -1 * a Base.:*(a::Real, b::Constraint) = b * a From 46da3ec7832fb958f4bcba62fef5378da80873ff Mon Sep 17 00:00:00 2001 From: "St. Elmo Wilken" Date: Sat, 16 Dec 2023 19:57:15 +0100 Subject: [PATCH 5/7] fix bug --- src/constraint.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/constraint.jl b/src/constraint.jl index 30d881a..2459fab 100644 --- a/src/constraint.jl +++ b/src/constraint.jl @@ -26,9 +26,9 @@ Base.@kwdef mutable struct Constraint{V} end end -Constraint(v::T, b::Int) where {T<:Value} = Constraint{T}(v, Float64(b)) +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{T}(v, Float64.(b)) + Constraint(v, Float64.(b)) Base.:-(a::Constraint) = -1 * a Base.:*(a::Real, b::Constraint) = b * a From 458add0d22e089f6edd43d6e685ecb2eb87c5367 Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Sat, 16 Dec 2023 20:00:48 +0100 Subject: [PATCH 6/7] don't talk too much about types (stuff should "just work") --- docs/src/metabolic-modeling.jl | 6 +++--- docs/src/quadratic-optimization.jl | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/src/metabolic-modeling.jl b/docs/src/metabolic-modeling.jl index e72ea46..628d554 100644 --- a/docs/src/metabolic-modeling.jl +++ b/docs/src/metabolic-modeling.jl @@ -346,13 +346,13 @@ Dict(k => v.fluxes.R_BIOMASS_Ecoli_core_w_GAM for (k, v) in result.community) # both dot-access and array-index syntax. # You can thus, e.g., set a single bound: -c.exchanges.oxygen.bound = (-20.0, 20.0) # typed as Tuple{Float64, Float64} +c.exchanges.oxygen.bound = (-20, 20) # ...or rebuild a whole constraint: -c.exchanges.biomass = C.Constraint(c.exchanges.biomass.value, (-20, 20)) # construct automatically converts bounds to floats +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): diff --git a/docs/src/quadratic-optimization.jl b/docs/src/quadratic-optimization.jl index 087967c..9728228 100644 --- a/docs/src/quadratic-optimization.jl +++ b/docs/src/quadratic-optimization.jl @@ -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.) From 12eb582b1988daf1e5764da5e21aa5662b89ac12 Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Sat, 16 Dec 2023 20:49:28 +0100 Subject: [PATCH 7/7] suffer with subtypes --- docs/src/metabolic-modeling.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/metabolic-modeling.jl b/docs/src/metabolic-modeling.jl index 628d554..0feaaa9 100644 --- a/docs/src/metabolic-modeling.jl +++ b/docs/src/metabolic-modeling.jl @@ -346,7 +346,7 @@ Dict(k => v.fluxes.R_BIOMASS_Ecoli_core_w_GAM for (k, v) in result.community) # both dot-access and array-index syntax. # You can thus, e.g., set a single bound: -c.exchanges.oxygen.bound = (-20, 20) +c.exchanges.oxygen.bound = (-20.0, 20.0) # ...or rebuild a whole constraint: c.exchanges.biomass = C.Constraint(c.exchanges.biomass.value, (-20, 20))