Skip to content

Commit

Permalink
Add getconstraint (close #639)
Browse files Browse the repository at this point in the history
  • Loading branch information
joehuchette committed Jun 4, 2016
1 parent fcf1365 commit 1e9cd00
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/JuMP.jl
Expand Up @@ -41,7 +41,7 @@ export
# Variable
setname, getname, setlowerbound, setupperbound, getlowerbound, getupperbound,
getvalue, setvalue, getdual, setcategory, getcategory,
getvariable,
getvariable, getconstraint,
linearindex,
# Expressions and constraints
linearterms,
Expand Down Expand Up @@ -120,6 +120,7 @@ type Model <: AbstractModel
simplify_nonlinear_expressions::Bool

varDict::Dict{Symbol,Any} # dictionary from variable names to variable objects
conDict::Dict{Symbol,Any} # dictionary from constraint names to constraint objects
varData::ObjectIdDict

getvalue_counter::Int # number of times we call getvalue on a JuMPContainer, so that we can print out a warning
Expand Down Expand Up @@ -173,6 +174,7 @@ function Model(;solver=UnsetSolver(), simplify_nonlinear_expressions::Bool=false
nothing, # nlpdata
simplify_nonlinear_expressions, # ...
Dict{Symbol,Any}(), # varDict
Dict{Symbol,Any}(), # conDict
ObjectIdDict(), # varData
0, # getvalue_counter
0, # operator_counter
Expand Down Expand Up @@ -280,6 +282,12 @@ function Base.copy(source::Model)
dest.varDict[symb] = copy(v, dest)
end

dest.conDict = Dict{Symbol,Any}()
# TODO: implement constraint copying
# for (symb,v) in source.conDict
# dest.conDict[symb] = copy(v, dest)
# end

# varData---possibly shouldn't copy

if source.nlpdata !== nothing
Expand Down Expand Up @@ -650,6 +658,16 @@ function registervar(m::Model, varname::Symbol, value)
end
registervar(m::Model, varname, value) = value # variable name isn't a simple symbol, ignore

function registercon(m::Model, conname::Symbol, value)
if haskey(m.conDict, conname)
m.conDict[conname] = nothing # indicate duplicate variable
else
m.conDict[conname] = value
end
return value
end
registercon(m::Model, conname, value) = value # constraint name isn't a simple symbol, ignore

function getvariable(m::Model, varname::Symbol)
if !haskey(m.varDict, varname)
error("No variable with name $varname")
Expand All @@ -660,6 +678,16 @@ function getvariable(m::Model, varname::Symbol)
end
end

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

# usage warnings
function getvalue_warn(x::JuMPContainer{Variable})
isempty(x) && return
Expand Down
1 change: 1 addition & 0 deletions src/macros.jl
Expand Up @@ -419,6 +419,7 @@ macro constraint(args...)
end
return assert_validmodel(m, quote
$(getloopedcode(variable, code, condition, idxvars, idxsets, idxpairs, :ConstraintRef))
registercon($m, $quotvarname, $variable)
$(anonvar ? variable : :($escvarname = $variable))
end)
end
Expand Down

0 comments on commit 1e9cd00

Please sign in to comment.