Skip to content

Commit

Permalink
implement dictionary of variable names and getVar, closes #399
Browse files Browse the repository at this point in the history
  • Loading branch information
mlubin committed May 18, 2015
1 parent 86af866 commit 60c4f67
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Unversioned
* Support for conditions in indexing ``@defVar`` and ``@addConstraint`` constructs, e.g. ``@defVar(m, x[i=1:5,j=1:5; i+j >= 3])``
* Support for vectorized operations on Variables, expressions, and JuMPArrays with indexing over unit-step ranges starting at one. See
the documentation for details.
* New ``getVar()`` method to access variables in a model by name

Version 0.9.1 (April 25, 2015)
------------------------------
Expand Down
1 change: 1 addition & 0 deletions doc/refmodel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Methods
* ``solve(m::Model; suppress_warnings=false)`` - solves the model using the selected solver (or a default for the problem class), and takes two optional arguments that are disabled by default. Setting ``suppress_warnings`` to ``true`` will suppress all JuMP-specific output (e.g. warnings about infeasibility and lack of dual information) but will not suppress solver output (which should be done by passing options to the solver).
* ``buildInternalModel(m::Model)`` - builds the model in memory at the MathProgBase level without optimizing.
* ``setSolver(m::Model,s::AbstractMathProgSolver)`` - changes the solver which will be used for the next call to ``solve()``, discarding the current internal model if present.
* ``getVar(m::Model,name::Symbol)`` - returns the variable or group of variables of the given name which were added to the model with ``@defVar``. Throws an error if multiple variables were created with the same name.

**Objective**

Expand Down
27 changes: 26 additions & 1 deletion src/JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export
# Variable
setName, getName, setLower, setUpper, getLower, getUpper,
getValue, setValue, getDual, setCategory, getCategory,
getVar,
# Expressions and constraints
affToStr, quadToStr, conToStr, chgConstrRHS,

Expand Down Expand Up @@ -95,6 +96,8 @@ type Model

nlpdata#::NLPData

varDict::Dict{Symbol,Any} # dictionary from variable names to variable objects

# Extension dictionary - e.g. for robust
# Extensions should define a type to hold information particular to
# their functionality, and store an instance of the type in this
Expand All @@ -115,7 +118,7 @@ function Model(;solver=UnsetSolver())
0,String[],String[],Float64[],Float64[],Symbol[],
0,Float64[],Float64[],Float64[],nothing,solver,
false,Any[],nothing,nothing,JuMPContainer[],
IndexedVector(Float64,0),nothing,Dict{Symbol,Any}())
IndexedVector(Float64,0),nothing,Dict{Symbol,Any}(),Dict{Symbol,Any}())
end

# Getters/setters
Expand Down Expand Up @@ -730,6 +733,28 @@ function Variable(m::Model,lower::Number,upper::Number,cat::Symbol,objcoef::Numb
return v
end

# handle dictionary of variables
function registervar(m::Model, varname::Symbol, value)
if haskey(m.varDict, varname)
m.varDict[varname] = nothing # indicate duplicate variable
else
m.varDict[varname] = value
end
return value
end
registervar(m::Model, varname, value) = value # variable name isn't a simple symbol, ignore

function getVar(m::Model, varname::Symbol)
if !haskey(m.varDict, varname)
error("No variable with name $varname")
elseif m.varDict[varname] === nothing
error("Multiple variables with name $varname")
else
return m.varDict[varname]
end
end


##########################################################################
# Operator overloads
include("operators.jl")
Expand Down
3 changes: 2 additions & 1 deletion src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ macro defVar(args...)
# Easy case - a single variable
return assert_validmodel(m, quote
$(esc(var)) = Variable($m,$lb,$ub,$(quot(t)),$(string(var)),$value)
registervar($m, $(quot(var)), $(esc(var)))
end)
end
isa(var,Expr) || error("in @defVar: expected $var to be a variable name")
Expand All @@ -712,7 +713,7 @@ macro defVar(args...)
return assert_validmodel(m, quote
$looped
push!($(m).dictList, $varname)
$varname
registervar($m, $(quot(getname(var))), $varname)
end)
end

Expand Down
9 changes: 9 additions & 0 deletions test/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ facts("[variable] constructors") do
@defVar(mcon, x[i=-10:10,s] <= 5.5, Int, start=i+1)
@fact getUpper(x[-4,"Green"]) => 5.5
@fact getValue(x[-3,"Blue"]) => -2
@fact isequal(getVar(mcon, :lbonly),lbonly) => true
@fact isequal(getVar(mcon, :ubonly),ubonly) => true
@fact isequal(getVar(mcon, :onerangeub)[-7],onerangeub[-7]) => true
@defVar(mcon, lbonly)
@fact_throws ErrorException getVar(mcon, :lbonly)
@fact_throws ErrorException getVar(mcon, :foo)
d = Dict()
@defVar(mcon, d["bar"][1:10] == 1)
@fact getValue(d["bar"][1]) => 1
end

facts("[variable] get and set bounds") do
Expand Down

0 comments on commit 60c4f67

Please sign in to comment.