Skip to content

Commit

Permalink
Merge pull request #479 from JuliaOpt/mpbidx
Browse files Browse the repository at this point in the history
implement getLinearIndex
  • Loading branch information
mlubin committed Jul 14, 2015
2 parents ac78ec1 + a7b72d3 commit 3de2ceb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/refvariable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ This form of constructing variables is not considered idiomatic JuMP code.
The reference to the first set of variables has been lost, although they will remain
in the model.

Finally, the constructor ``Variable(m::Model,idx::Int)`` may be used to create a variable object corresponding to an *existing* variable in the model (the constructor does not add a new variable to the model). The variable indices correspond to those of the internal MathProgBase model. This method is only useful if you intend to interact with solver properties which are not directly exposed through JuMP.
Finally, the constructor ``Variable(m::Model,idx::Int)`` may be used to create a variable object corresponding to an *existing* variable in the model (the constructor does not add a new variable to the model). The variable indices correspond to those of the internal MathProgBase model. The inverse of this operation is ``getLinearIndex(x::Variable)``, which returns the flattened out (linear) index of a variable as JuMP provides it to a solver. We guarantee that ``Variable(m,getLinearIndex(x))`` returns ``x`` itself. These methods are only useful if you intend to interact with solver properties which are not directly exposed through JuMP.

Semidefinite and symmetric variables
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
4 changes: 3 additions & 1 deletion src/JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export
setName, getName, setLower, setUpper, getLower, getUpper,
getValue, setValue, getDual, setCategory, getCategory,
getVar,
getLinearIndex,
# Expressions and constraints
affToStr, quadToStr, conToStr, chgConstrRHS,

Expand Down Expand Up @@ -223,7 +224,8 @@ immutable Variable <: ReverseDiffSparse.Placeholder
col::Int
end

ReverseDiffSparse.getplaceindex(x::Variable) = x.col
getLinearIndex(x::Variable) = x.col
ReverseDiffSparse.getplaceindex(x::Variable) = getLinearIndex(x)
Base.isequal(x::Variable,y::Variable) = isequal(x.col,y.col) && isequal(x.m,y.m)

Variable(m::Model, lower, upper, cat::Symbol, name::String="", value::Number=NaN) =
Expand Down
8 changes: 8 additions & 0 deletions test/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -601,3 +601,11 @@ facts("[model] Setting print hook") do
print(m)
@fact dummy => [2]
end

facts("[model] Test getLinearIndex") do
m = Model()
@defVar(m, x[1:5])
for i in 1:5
@fact isequal(Variable(m,getLinearIndex(x[i])),x[i]) => true
end
end

0 comments on commit 3de2ceb

Please sign in to comment.