Skip to content

Commit

Permalink
Rename m field of VariableRef to model. (#1483)
Browse files Browse the repository at this point in the history
* Rename `m` field of VariableRef to `model`.

For style compliance. Also switch to `owner_model` method instead of
accessing the field directly.

* document owner_model for AbstractVariableRef
  • Loading branch information
mlubin committed Sep 23, 2018
1 parent 48aa920 commit 47ef41e
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 75 deletions.
1 change: 1 addition & 0 deletions docs/src/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -527,4 +527,5 @@ false

```@docs
@variable
owner_model
```
15 changes: 8 additions & 7 deletions src/JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,12 @@ Base.copy(x::Nothing, new_model::Model) = nothing
Base.copy(v::AbstractArray{VariableRef}, new_model::Model) = (var -> VariableRef(new_model, var.index)).(v)

function optimizer_index(v::VariableRef)
if mode(v.m) == Direct
model = owner_model(v)
if mode(model) == Direct
return index(v)
else
@assert caching_optimizer(v.m).state == MOIU.AttachedOptimizer
return caching_optimizer(v.m).model_to_optimizer_map[index(v)]
@assert caching_optimizer(model).state == MOIU.AttachedOptimizer
return caching_optimizer(model).model_to_optimizer_map[index(v)]
end
end

Expand Down Expand Up @@ -457,21 +458,21 @@ Return the value of the attribute `attr` from model's MOI backend.
"""
MOI.get(m::Model, attr::MOI.AbstractModelAttribute) = MOI.get(m.moi_backend, attr)
function MOI.get(m::Model, attr::MOI.AbstractVariableAttribute, v::VariableRef)
@assert m === v.m
@assert m === owner_model(v) # TODO: Improve the error message.
MOI.get(m.moi_backend, attr, index(v))
end
function MOI.get(m::Model, attr::MOI.AbstractConstraintAttribute, cr::ConstraintRef)
@assert m === cr.m
@assert m === cr.m # TODO: Improve the error message.
MOI.get(m.moi_backend, attr, index(cr))
end

MOI.set(m::Model, attr::MOI.AbstractModelAttribute, value) = MOI.set(m.moi_backend, attr, value)
function MOI.set(m::Model, attr::MOI.AbstractVariableAttribute, v::VariableRef, value)
@assert m === v.m
@assert m === owner_model(v) # TODO: Improve the error message.
MOI.set(m.moi_backend, attr, index(v), value)
end
function MOI.set(m::Model, attr::MOI.AbstractConstraintAttribute, cr::ConstraintRef, value)
@assert m === cr.m
@assert m === cr.m # TODO: Improve the error message.
MOI.set(m.moi_backend, attr, index(cr), value)
end

Expand Down
7 changes: 5 additions & 2 deletions src/parse_nlp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ function parseNLExpr_runtime(m::Model, x::Number, tape, parent, values)
end

function parseNLExpr_runtime(m::Model, x::VariableRef, tape, parent, values)
x.m === m || error("Variable in nonlinear expression does not belong to corresponding model")
if owner_model(x) !== m
error("Variable in nonlinear expression does not belong to the " *
"corresponding model")
end
push!(tape, NodeData(MOIVARIABLE, x.index.value, parent))
nothing
end
Expand Down Expand Up @@ -259,7 +262,7 @@ function checkexpr(m::Model, ex::Expr)
return
end
function checkexpr(m::Model, v::VariableRef)
v.m === m || error("Variable $v does not belong to this model")
owner_model(v) === m || error("Variable $v does not belong to this model.")
return
end
checkexpr(m::Model, ex) = nothing

0 comments on commit 47ef41e

Please sign in to comment.