Skip to content

Commit

Permalink
improve general var defaults (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsipher authored Oct 22, 2024
1 parent b8b4627 commit 40c4008
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/general_variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -820,10 +820,14 @@ for op = (:has_lower_bound, :has_upper_bound, :is_fixed, :is_binary, :is_integer
:unset_integer)
@eval begin
# define the fallback method
function JuMP.$op(vref::DispatchVariableRef)
str = string("`JuMP.$($op)` not defined for variable reference type ",
"`$(typeof(vref))`.")
throw(ArgumentError(str))
if JuMP.$op in (JuMP.is_binary, JuMP.is_fixed, JuMP.is_integer)
JuMP.$op(vref::DispatchVariableRef) = false
else
function JuMP.$op(vref::DispatchVariableRef)
str = string("`JuMP.$($op)` not defined for variable reference type ",
"`$(typeof(vref))`.")
throw(ArgumentError(str))
end
end

# define the dispatch version
Expand Down
12 changes: 8 additions & 4 deletions test/general_variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -484,14 +484,18 @@ end
gvref = GeneralVariableRef(m, 1, TestIndex)
pref = GeneralVariableRef(m, -1, IndependentParameterIndex)
# test 1 argument methods
for f in (:has_lower_bound, :has_upper_bound, :is_fixed, :is_binary,
:is_integer, :lower_bound, :upper_bound, :fix_value, :start_value,
:set_binary, :set_integer, :LowerBoundRef, :UpperBoundRef, :FixRef,
:BinaryRef, :IntegerRef, :delete_lower_bound, :delete_upper_bound,
for f in (:has_lower_bound, :has_upper_bound, :lower_bound, :upper_bound,
:fix_value, :start_value, :set_binary, :set_integer,
:LowerBoundRef, :UpperBoundRef, :FixRef, :BinaryRef, :IntegerRef,
:delete_lower_bound, :delete_upper_bound,
:unfix, :unset_binary, :unset_integer)
@test_throws ArgumentError eval(f)(dvref)
@test_throws ArgumentError eval(f)(gvref)
end
for f in (:is_fixed, :is_binary, :is_integer)
@test !eval(f)(dvref)
@test !eval(f)(gvref)
end
# test setting methods
for f in (:set_lower_bound, :set_upper_bound, :set_start_value)
@test_throws ArgumentError eval(f)(dvref, 42)
Expand Down

0 comments on commit 40c4008

Please sign in to comment.