Skip to content

Commit

Permalink
rename basename to base_name (#1524)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlubin committed Oct 7, 2018
1 parent c2d6ef4 commit 3fe99f5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 36 deletions.
4 changes: 2 additions & 2 deletions docs/src/names.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ a container with the syntax `name[...]` or when creating a constraint with
`@constraint(m, name, expr)`, the name used is `name`.

The name of the variable/constraint used for printing is based on the base name
which is specified by the `basename` keyword argument. When the `basename`
which is specified by the `base_name` keyword argument. When the `base_name`
keyword argument is not specified, the name depends on whether the variable is
anonymous:

Expand All @@ -38,5 +38,5 @@ anonymous:

The name of the variable/constraint set to the
`MOI.VariableName`/`MOI.ConstraintName` attribute and used for printing is then
`basename` for single variable/constraint and `basename[i1,i2,...,in]` for the
`base_name` for single variable/constraint and `base_name[i1,i2,...,in]` for the
reference at indices `i1`, `i2`, ..., `in` in a container.
8 changes: 4 additions & 4 deletions docs/src/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ variables with their corresponding prefix.
JuMP variables can have attributes, such as names or an initial primal start
value. We illustrate the name attribute in the following example:
```jldoctest variables
julia> @variable(model, y, basename="decision variable")
julia> @variable(model, y, base_name="decision variable")
decision variable
```
This code does four things:
Expand All @@ -59,7 +59,7 @@ This code does four things:
variable
3. it binds the JuMP variable to the Julia variable `y`
4. it tells JuMP that the *name* attribute of this JuMP variable is "decision
variable". JuMP uses the value of `basename` when it has to print the variable
variable". JuMP uses the value of `base_name` when it has to print the variable
as a string.

For example, when we print `y` at the REPL we get:
Expand Down Expand Up @@ -456,7 +456,7 @@ noname
```
This shows how `(model, x)` is really short for:
```jldoctest anon_variables; setup=:(model=Model())
julia> x = model[:x] = @variable(model, basename="x")
julia> x = model[:x] = @variable(model, base_name="x")
x
```
An `Array` of anonymous JuMP variables can be created as follows:
Expand All @@ -478,7 +478,7 @@ use the `binary` and `integer` keywords.

Thus, the anonymous variant of `@variable(model, x[i=1:2] >= i, Int)` is:
```jldoctest; setup=:(model=Model())
julia> x = @variable(model, [i=1:2], basename="x", lower_bound=i, integer=true)
julia> x = @variable(model, [i=1:2], base_name="x", lower_bound=i, integer=true)
2-element Array{VariableRef,1}:
x[1]
x[2]
Expand Down
39 changes: 20 additions & 19 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,8 @@ function constraint_macro(args, macro_name::Symbol, parsefun::Function)
anonvar = isexpr(c, :vect) || isexpr(c, :vcat) || length(extra) != 1
variable = gensym()
name = getname(c)
basename = anonvar ? "" : string(name)
# TODO: support the basename keyword argument
base_name = anonvar ? "" : string(name)
# TODO: support the base_name keyword argument

if isa(x, Symbol)
_error("Incomplete constraint specification $x. Are you missing a comparison (<=, >=, or ==)?")
Expand All @@ -508,7 +508,7 @@ function constraint_macro(args, macro_name::Symbol, parsefun::Function)
# TODO: Pass through names here.
constraintcall = :(add_constraint.($m, $buildcall))
else
constraintcall = :(add_constraint($m, $buildcall, $(namecall(basename, idxvars))))
constraintcall = :(add_constraint($m, $buildcall, $(namecall(base_name, idxvars))))
end
addkwargs!(constraintcall, kwargs)
code = quote
Expand Down Expand Up @@ -1034,13 +1034,13 @@ function macro_error(macroname, args, str...)
error("In @$macroname($(join(args,","))): ", str...)
end

# Given a basename and idxvars, returns an expression that constructs the name
# Given a base_name and idxvars, returns an expression that constructs the name
# of the object. For use within macros only.
function namecall(basename, idxvars)
if isempty(idxvars) || basename == ""
return basename
function namecall(base_name, idxvars)
if isempty(idxvars) || base_name == ""
return base_name
end
ex = Expr(:call, :string, basename, "[")
ex = Expr(:call, :string, base_name, "[")
for i in 1:length(idxvars)
# Converting the arguments to strings before concatenating is faster:
# https://github.com/JuliaLang/julia/issues/29550.
Expand Down Expand Up @@ -1151,9 +1151,10 @@ The recognized positional arguments in `args` are the following:
The recognized keyword arguments in `kwargs` are the following:
* `basename`: Sets the base name used to generate variable names. It
* `base_name`: Sets the name prefix used to generate variable names. It
corresponds to the variable name for scalar variable, otherwise, the
variable names are `basename[...]` for each indices `...` of the axes `axes`.
variable names are set to `base_name[...]` for each index `...` of the axes
`axes`.
* `lower_bound`: Sets the value of the variable lower bound.
* `upper_bound`: Sets the value of the variable upper bound.
* `start`: Sets the variable starting value used as initial guess in optimization.
Expand All @@ -1172,7 +1173,7 @@ lower bound 0:
# Specify the lower bound using a keyword argument
@variable(model, x, lower_bound=0)
# Specify everything in `kwargs`
x = @variable(model, basename="x", lower_bound=0)
x = @variable(model, base_name="x", lower_bound=0)
```
The following are equivalent ways of creating a `JuMPArray` of index set
Expand All @@ -1195,7 +1196,7 @@ extra_kwargs...))` where
* `_error` is an error function with a single `String` argument showing the
`@variable` call in addition to the error message given as argument;
* `info` is the `VariableInfo` struct containing the information gathered in
`expr`, the recognized keyword arguments (except `basename` and
`expr`, the recognized keyword arguments (except `base_name` and
`variable_type`) and the recognized positional arguments (except `Symmetric`
and `PSD`);
* `extra_args` are the unrecognized positional arguments of `args` plus the
Expand Down Expand Up @@ -1264,8 +1265,8 @@ macro variable(args...)
end

info_kwargs = filter(is_info_keyword, kwargs)
extra_kwargs = filter(kw -> kw.args[1] != :basename && kw.args[1] != :variable_type && !is_info_keyword(kw), kwargs)
basename_kwargs = filter(kw -> kw.args[1] == :basename, kwargs)
extra_kwargs = filter(kw -> kw.args[1] != :base_name && kw.args[1] != :variable_type && !is_info_keyword(kw), kwargs)
base_name_kwargs = filter(kw -> kw.args[1] == :base_name, kwargs)
variable_type_kwargs = filter(kw -> kw.args[1] == :variable_type, kwargs)
infoexpr = VariableInfoExpr(; keywordify.(info_kwargs)...)

Expand All @@ -1289,10 +1290,10 @@ macro variable(args...)
variable = gensym()
# TODO: Should we generate non-empty default names for variables?
name = getname(var)
if isempty(basename_kwargs)
basename = anonvar ? "" : string(name)
if isempty(base_name_kwargs)
base_name = anonvar ? "" : string(name)
else
basename = esc(basename_kwargs[1].args[2])
base_name = esc(base_name_kwargs[1].args[2])
end

if !isa(name, Symbol) && !anonvar
Expand Down Expand Up @@ -1323,7 +1324,7 @@ macro variable(args...)
sdp && _error("Cannot add a semidefinite scalar variable")
buildcall = :( build_variable($_error, $info, $(extra...)) )
addkwargs!(buildcall, extra_kwargs)
variablecall = :( add_variable($model, $buildcall, $basename) )
variablecall = :( add_variable($model, $buildcall, $base_name) )
# The looped code is trivial here since there is a single variable
creationcode = :($variable = $variablecall)
final_variable = variable
Expand All @@ -1338,7 +1339,7 @@ macro variable(args...)
# Code to be used to create each variable of the container.
buildcall = :( build_variable($_error, $info, $(extra...)) )
addkwargs!(buildcall, extra_kwargs)
variablecall = :( add_variable($model, $buildcall, $(namecall(basename, idxvars))) )
variablecall = :( add_variable($model, $buildcall, $(namecall(base_name, idxvars))) )
code = :( $(refcall) = $variablecall )
# Determine the return type of add_variable. This is needed to create the container holding them.
vartype = :( variable_type($model, $(extra...)) )
Expand Down
12 changes: 6 additions & 6 deletions test/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ Base.isless(u::UnitNumber, v::UnitNumber) = isless(u.α, v.α)
io_test(IJuliaMode, a_v, "v_{4,5,2,3,2,2,4}")
end

@testset "basename keyword argument" begin
@testset "base_name keyword argument" begin
m = Model()
@variable(m, x, basename="foo")
@variable(m, y[1:3], basename="bar")
@variable(m, x, base_name="foo")
@variable(m, y[1:3], base_name="bar")
num = 123
@variable(m, z[[:red,:blue]], basename="color_$num")
@variable(m, v[1:2,1:2], PSD, basename=string("i","$num",num))
@variable(m, w[1:3,1:3], Symmetric, basename="symm")
@variable(m, z[[:red,:blue]], base_name="color_$num")
@variable(m, v[1:2,1:2], PSD, base_name=string("i","$num",num))
@variable(m, w[1:3,1:3], Symmetric, base_name="symm")

io_test(REPLMode, x, "foo")
io_test(IJuliaMode, x, "foo")
Expand Down
10 changes: 5 additions & 5 deletions test/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,14 @@ function test_variable_oneto_index_set(ModelType, VariableRefType)
@test length.(Compat.axes(jumparray_var)) == (3, 2)
end

function test_variable_basename_in_macro(ModelType)
function test_variable_base_name_in_macro(ModelType)
model = ModelType()
@variable(model, normal_var)
@test JuMP.name(normal_var) == "normal_var"
no_indices = @variable(model, basename="foo")
no_indices = @variable(model, base_name="foo")
@test JuMP.name(no_indices) == "foo"
# Note that `z` will be ignored in name.
indices = @variable(model, z[i=2:3], basename="t")
indices = @variable(model, z[i=2:3], base_name="t")
@test JuMP.name(indices[2]) == "t[2]"
@test JuMP.name(indices[3]) == "t[3]"
end
Expand Down Expand Up @@ -484,8 +484,8 @@ function variables_test(ModelType::Type{<:JuMP.AbstractModel},
test_variable_oneto_index_set(ModelType, VariableRefType)
end

@testset "basename= in @variable" begin
test_variable_basename_in_macro(ModelType)
@testset "base_name= in @variable" begin
test_variable_base_name_in_macro(ModelType)
end

@testset "condition in indexing" begin
Expand Down

0 comments on commit 3fe99f5

Please sign in to comment.