Skip to content

Commit

Permalink
Add name in VariableInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Apr 18, 2018
1 parent afc911c commit 2384b8a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
19 changes: 10 additions & 9 deletions src/macros.jl
Expand Up @@ -879,17 +879,18 @@ mutable struct VariableInfo{S, T, U, V}
upperbound::T
hasfix::Bool
fixedvalue::U
binary::Bool
integer::Bool
hasstart::Bool
start::V
binary::Bool
integer::Bool
name::String
end

# Returns the type of what `constructvariable!` would return with these starting positional arguments.
variabletype(m::Model) = Variable
# Returns a new variable belonging to the model `m`. Additional positional arguments can be used to dispatch the call to a different method.
# The return type should only depends on the positional arguments for `variabletype` to make sense.
function constructvariable!(m::Model, _error::Function, info::VariableInfo, name::String; extra_kwargs...)
function constructvariable!(m::Model, _error::Function, info::VariableInfo; extra_kwargs...)
for (kwarg, _) in extra_kwargs
_error("Unrecognized keyword argument $kwarg")
end
Expand All @@ -912,8 +913,8 @@ function constructvariable!(m::Model, _error::Function, info::VariableInfo, name
if info.hasstart
setstartvalue(v, info.start)
end
if name != EMPTYSTRING
setname(v, name)
if info.name != EMPTYSTRING
setname(v, info.name)
end
return v
end
Expand Down Expand Up @@ -1105,12 +1106,11 @@ macro variable(args...)
end
extra = esc.(filter(ex -> !(ex in [:Int,:Bin]), extra))

info = :(VariableInfo($haslb, $lb, $hasub, $ub, $hasfix, $fixedvalue, $binary, $integer, $hasstart, $value))

if isa(var,Symbol)
# Easy case - a single variable
sdp && _error("Cannot add a semidefinite scalar variable")
variablecall = :( constructvariable!($m, $(extra...), $_error, $info, $basename) )
info = :(VariableInfo($haslb, $lb, $hasub, $ub, $hasfix, $fixedvalue, $hasstart, $value, $binary, $integer, $basename))
variablecall = :( constructvariable!($m, $(extra...), $_error, $info) )
addkwargs!(variablecall, extra_kwargs)
code = :($variable = $variablecall)
if !anonvar
Expand All @@ -1130,7 +1130,8 @@ macro variable(args...)
clear_dependencies(i) = (isdependent(idxvars,idxsets[i],i) ? () : idxsets[i])

# Code to be used to create each variable of the container.
variablecall = :( constructvariable!($m, $(extra...), $_error, $info, $(namecall(basename, idxvars))) )
info = :(VariableInfo($haslb, $lb, $hasub, $ub, $hasfix, $fixedvalue, $hasstart, $value, $binary, $integer, $(namecall(basename, idxvars))))
variablecall = :( constructvariable!($m, $(extra...), $_error, $info) )
addkwargs!(variablecall, extra_kwargs)
code = :( $(refcall) = $variablecall )
# Determine the return type of constructvariable!. This is needed to create the container holding them.
Expand Down
5 changes: 2 additions & 3 deletions test/macros.jl
Expand Up @@ -2,7 +2,6 @@

mutable struct MyVariable
info::JuMP.VariableInfo
name::String
test_kw::Int
end

Expand Down Expand Up @@ -40,7 +39,7 @@ end
@test !x.info.integer
@test x.info.hasstart
@test x.info.start == 3
@test x.name == "x"
@test x.info.name == "x"
@test x.test_kw == 1
@variable(m, y[1:3] >= 0, MyVariable, test_kw = 2)
@test isa(y, Vector{MyVariable})
Expand All @@ -55,7 +54,7 @@ end
@test !y[i].info.integer
@test !y[i].info.hasstart
@test isnan(y[i].info.start)
@test y[i].name == "y[$i]"
@test y[i].info.name == "y[$i]"
@test y[i].test_kw == 2
end
end
Expand Down

0 comments on commit 2384b8a

Please sign in to comment.