Skip to content

Commit 48ec10b

Browse files
refactor: remove integer and binary variable metadata
1 parent 16d6311 commit 48ec10b

File tree

6 files changed

+14
-65
lines changed

6 files changed

+14
-65
lines changed

src/ModelingToolkit.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ export connect, domain_connect, @connector, Connection, Flow, Stream, instream
222222
export @component, @mtkmodel, @mtkbuild
223223
export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbance,
224224
istunable, getdist, hasdist,
225-
tunable_parameters, isirreducible, getdescription, hasdescription, isbinaryvar,
226-
isintegervar
225+
tunable_parameters, isirreducible, getdescription, hasdescription
227226
export ode_order_lowering, dae_order_lowering, liouville_transform
228227
export PDESystem
229228
export Differential, expand_derivatives, @derivatives

src/systems/model_parsing.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs;
120120
(:misc, VariableMisc),
121121
(:disturbance, VariableDisturbance),
122122
(:tunable, VariableTunable),
123-
(:dist, VariableDistribution),
124-
(:binary, VariableBinary),
125-
(:integer, VariableInteger)]
123+
(:dist, VariableDistribution)]
126124

127125
arg isa LineNumberNode && return
128126
MLStyle.@match arg begin

src/systems/optimization/optimizationsystem.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,9 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map,
258258
if isnothing(lb) && isnothing(ub) # use the symbolically specified bounds
259259
lb = first.(getbounds.(dvs))
260260
ub = last.(getbounds.(dvs))
261-
lb[isbinaryvar.(dvs)] .= 0
262-
ub[isbinaryvar.(dvs)] .= 1
261+
isboolean = symtype.(unwrap.(dvs)) .<: Bool
262+
lb[isboolean] .= 0
263+
ub[isboolean] .= 1
263264
else # use the user supplied variable bounds
264265
xor(isnothing(lb), isnothing(ub)) &&
265266
throw(ArgumentError("Expected both `lb` and `ub` to be supplied"))
@@ -269,7 +270,7 @@ function DiffEqBase.OptimizationProblem{iip}(sys::OptimizationSystem, u0map,
269270
throw(ArgumentError("Expected both `ub` to be of the same length as the vector of optimization variables"))
270271
end
271272

272-
int = isintegervar.(dvs) .| isbinaryvar.(dvs)
273+
int = symtype.(unwrap.(dvs)) .<: Integer
273274

274275
defs = defaults(sys)
275276
defs = mergedefaults(defs, parammap, ps)
@@ -476,8 +477,9 @@ function OptimizationProblemExpr{iip}(sys::OptimizationSystem, u0map,
476477
if isnothing(lb) && isnothing(ub) # use the symbolically specified bounds
477478
lb = first.(getbounds.(dvs))
478479
ub = last.(getbounds.(dvs))
479-
lb[isbinaryvar.(dvs)] .= 0
480-
ub[isbinaryvar.(dvs)] .= 1
480+
isboolean = symtype.(unwrap.(dvs)) .<: Bool
481+
lb[isboolean] .= 0
482+
ub[isboolean] .= 1
481483
else # use the user supplied variable bounds
482484
xor(isnothing(lb), isnothing(ub)) &&
483485
throw(ArgumentError("Expected both `lb` and `ub` to be supplied"))
@@ -487,7 +489,7 @@ function OptimizationProblemExpr{iip}(sys::OptimizationSystem, u0map,
487489
throw(ArgumentError("Expected `ub` to be of the same length as the vector of optimization variables"))
488490
end
489491

490-
int = isintegervar.(dvs) .| isbinaryvar.(dvs)
492+
int = symtype.(unwrap.(dvs)) .<: Integer
491493

492494
defs = defaults(sys)
493495
defs = mergedefaults(defs, parammap, ps)

src/variables.jl

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -439,40 +439,6 @@ function hasdescription(x)
439439
getdescription(x) != ""
440440
end
441441

442-
## binary variables =================================================================
443-
struct VariableBinary end
444-
Symbolics.option_to_metadata_type(::Val{:binary}) = VariableBinary
445-
446-
isbinaryvar(x::Num) = isbinaryvar(Symbolics.unwrap(x))
447-
448-
"""
449-
isbinaryvar(x)
450-
451-
Determine if a variable is binary.
452-
"""
453-
function isbinaryvar(x)
454-
p = Symbolics.getparent(x, nothing)
455-
p === nothing || (x = p)
456-
return Symbolics.getmetadata(x, VariableBinary, false)
457-
end
458-
459-
## integer variables =================================================================
460-
struct VariableInteger end
461-
Symbolics.option_to_metadata_type(::Val{:integer}) = VariableInteger
462-
463-
isintegervar(x::Num) = isintegervar(Symbolics.unwrap(x))
464-
465-
"""
466-
isintegervar(x)
467-
468-
Determine if a variable is an integer.
469-
"""
470-
function isintegervar(x)
471-
p = Symbolics.getparent(x, nothing)
472-
p === nothing || (x = p)
473-
return Symbolics.getmetadata(x, VariableInteger, false)
474-
end
475-
476442
## Brownian
477443
"""
478444
tobrownian(s::Sym)

test/model_parsing.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,14 @@ end
273273

274274
@testset "Metadata in variables" begin
275275
metadata = Dict(:description => "Variable to test metadata in the Model.structure",
276-
:input => true, :bounds => (-1, 1), :connection_type => :Flow, :integer => true,
277-
:binary => false, :tunable => false, :disturbance => true, :dist => Normal(1, 1))
276+
:input => true, :bounds => (-1, 1), :connection_type => :Flow,
277+
:tunable => false, :disturbance => true, :dist => Normal(1, 1))
278278

279279
@connector MockMeta begin
280280
m(t),
281281
[description = "Variable to test metadata in the Model.structure",
282-
input = true, bounds = (-1, 1), connect = Flow, integer = true,
283-
binary = false, tunable = false, disturbance = true, dist = Normal(1, 1)]
282+
input = true, bounds = (-1, 1), connect = Flow,
283+
tunable = false, disturbance = true, dist = Normal(1, 1)]
284284
end
285285

286286
for (k, v) in metadata

test/test_variable_metadata.jl

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,3 @@ sp = Set(p)
112112
@named sys = ODESystem([u ~ p], t)
113113

114114
@test_nowarn show(stdout, "text/plain", sys)
115-
116-
@testset "binary" begin
117-
@parameters t
118-
@variables u(t) [binary = true]
119-
@parameters p [binary = true]
120-
@test isbinaryvar(u)
121-
@test isbinaryvar(p)
122-
end
123-
124-
@testset "integer" begin
125-
@parameters t
126-
@variables u(t) [integer = true]
127-
@parameters p [integer = true]
128-
@test isintegervar(u)
129-
@test isintegervar(p)
130-
end

0 commit comments

Comments
 (0)