Skip to content

Commit

Permalink
rename fields of Model for style compliance (#1440)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlubin committed Aug 26, 2018
1 parent e7bc047 commit 0d281d0
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 165 deletions.
92 changes: 46 additions & 46 deletions src/JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ end
# Model

# Model has three modes:
# 1) Automatic: moibackend field holds a LazyBridgeOptimizer{CachingOptimizer} in Automatic mode.
# 2) Manual: moibackend field holds a LazyBridgeOptimizer{CachingOptimizer} in Manual mode.
# 3) Direct: moibackend field holds an AbstractOptimizer. No extra copy of the model is stored. The moibackend must support addconstraint! etc.
# 1) Automatic: moi_backend field holds a LazyBridgeOptimizer{CachingOptimizer} in Automatic mode.
# 2) Manual: moi_backend field holds a LazyBridgeOptimizer{CachingOptimizer} in Manual mode.
# 3) Direct: moi_backend field holds an AbstractOptimizer. No extra copy of the model is stored. The moi_backend must support addconstraint! etc.
# Methods to interact with the CachingOptimizer are defined in solverinterface.jl.
@enum ModelMode Automatic Manual Direct

Expand All @@ -139,21 +139,21 @@ A mathematical model of an optimization problem.
mutable struct Model <: AbstractModel
# Special variablewise properties that we keep track of:
# lower bound, upper bound, fixed, integrality, binary
variabletolowerbound::Dict{MOIVAR, MOILB}
variabletoupperbound::Dict{MOIVAR, MOIUB}
variabletofix::Dict{MOIVAR, MOIFIX}
variabletointegrality::Dict{MOIVAR, MOIINT}
variabletozeroone::Dict{MOIVAR, MOIBIN}
variable_to_lower_bound::Dict{MOIVAR, MOILB}
variable_to_upper_bound::Dict{MOIVAR, MOIUB}
variable_to_fix::Dict{MOIVAR, MOIFIX}
variable_to_integrality::Dict{MOIVAR, MOIINT}
variable_to_zero_one::Dict{MOIVAR, MOIBIN}
# In Manual and Automatic modes, LazyBridgeOptimizer{CachingOptimizer}.
# In Direct mode, will hold an AbstractOptimizer.
moibackend::MOI.AbstractOptimizer
moi_backend::MOI.AbstractOptimizer
# Hook into a solve call...function of the form f(m::Model; kwargs...),
# where kwargs get passed along to subsequent solve calls.
optimizehook
optimize_hook
# TODO: Document.
nlpdata#::NLPData
nlp_data
# Dictionary from variable and constraint names to objects.
objdict::Dict{Symbol, Any}
obj_dict::Dict{Symbol, Any}
# Number of times we add large expressions. Incremented and checked by
# the `operator_warn` method.
operator_counter::Int
Expand Down Expand Up @@ -250,16 +250,16 @@ if VERSION >= v"0.7-"
end


# In Automatic and Manual mode, `model.moibackend` is either directly the
# In Automatic and Manual mode, `model.moi_backend` is either directly the
# `CachingOptimizer` if `bridge_constraints=false` was passed in the constructor
# or it is a `LazyBridgeOptimizer` and the `CachingOptimizer` is stored in the
# `model` field
function caching_optimizer(model::Model)
if model.moibackend isa MOIU.CachingOptimizer
return model.moibackend
elseif (model.moibackend isa
if model.moi_backend isa MOIU.CachingOptimizer
return model.moi_backend
elseif (model.moi_backend isa
MOI.Bridges.LazyBridgeOptimizer{<:MOIU.CachingOptimizer})
return model.moibackend.model
return model.moi_backend.model
else
error("The function `caching_optimizer` cannot be called on a model " *
"in `Direct` mode.")
Expand All @@ -272,8 +272,8 @@ end
Return mode (Direct, Automatic, Manual) of model.
"""
function mode(model::Model)
if !(model.moibackend isa MOI.Bridges.LazyBridgeOptimizer{<:MOIU.CachingOptimizer} ||
model.moibackend isa MOIU.CachingOptimizer)
if !(model.moi_backend isa MOI.Bridges.LazyBridgeOptimizer{<:MOIU.CachingOptimizer} ||
model.moi_backend isa MOIU.CachingOptimizer)
return Direct
elseif caching_optimizer(model).mode == MOIU.Automatic
return Automatic
Expand All @@ -295,7 +295,7 @@ num_variables(model::Model) = MOI.get(model, MOI.NumberOfVariables())
Returns the number of nonlinear constraints associated with the `model`.
"""
function numnlconstr(model::Model)
return model.nlpdata !== nothing ? length(model.nlpdata.nlconstr) : 0
return model.nlp_data !== nothing ? length(model.nlp_data.nlconstr) : 0
end

"""
Expand Down Expand Up @@ -332,11 +332,11 @@ end

# TODO(IainNZ): Document these too.
# TODO(#1381): Implement Base.copy for Model.
object_dictionary(model::Model) = model.objdict
object_dictionary(model::Model) = model.obj_dict
terminationstatus(m::Model) = MOI.get(m, MOI.TerminationStatus())
primalstatus(m::Model) = MOI.get(m, MOI.PrimalStatus())
dualstatus(m::Model) = MOI.get(m, MOI.DualStatus())
setoptimizehook(m::Model, f) = (m.optimizehook = f)
set_optimize_hook(m::Model, f) = (m.optimize_hook = f)


#############################################################################
Expand Down Expand Up @@ -478,10 +478,10 @@ end
# TODO: should model be a parameter here?
function MOI.delete!(m::Model, cr::ConstraintRef{Model})
@assert m === cr.m
MOI.delete!(m.moibackend, index(cr))
MOI.delete!(m.moi_backend, index(cr))
end

MOI.isvalid(m::Model, cr::ConstraintRef{Model}) = cr.m === m && MOI.isvalid(m.moibackend, cr.index)
MOI.isvalid(m::Model, cr::ConstraintRef{Model}) = cr.m === m && MOI.isvalid(m.moi_backend, cr.index)

"""
addconstraint(m::Model, c::AbstractConstraint, name::String="")
Expand All @@ -490,15 +490,15 @@ Add a constraint `c` to `Model m` and sets its name.
"""
function addconstraint(m::Model, c::AbstractConstraint, name::String="")
f, s = moi_function_and_set(c)
if !MOI.supportsconstraint(m.moibackend, typeof(f), typeof(s))
if m.moibackend isa MOI.Bridges.LazyBridgeOptimizer
if !MOI.supportsconstraint(m.moi_backend, typeof(f), typeof(s))
if m.moi_backend isa MOI.Bridges.LazyBridgeOptimizer
bridge_message = " and there are no bridges that can reformulate it into supported constraints."
else
bridge_message = ", try using `bridge_constraints=true` in the `JuMP.Model` constructor if you believe the constraint can be reformulated to constraints supported by the solver."
end
error("Constraints of type $(typeof(f))-in-$(typeof(s)) are not supported by the solver" * bridge_message)
end
cindex = MOI.addconstraint!(m.moibackend, f, s)
cindex = MOI.addconstraint!(m.moi_backend, f, s)
cref = ConstraintRef(m, cindex, shape(c))
if !isempty(name)
setname(cref, name)
Expand Down Expand Up @@ -583,36 +583,36 @@ setname(cr::ConstraintRef{Model,<:MOICON}, s::String) = MOI.set!(cr.m, MOI.Const
Return `true` if one may query the attribute `attr` from the model's MOI backend.
false if not.
"""
MOI.canget(m::Model, attr::MOI.AbstractModelAttribute) = MOI.canget(m.moibackend, attr)
MOI.canget(m::Model, attr::MOI.AbstractVariableAttribute, ::Type{VariableRef}) = MOI.canget(m.moibackend, attr, MOIVAR)
MOI.canget(m::Model, attr::MOI.AbstractModelAttribute) = MOI.canget(m.moi_backend, attr)
MOI.canget(m::Model, attr::MOI.AbstractVariableAttribute, ::Type{VariableRef}) = MOI.canget(m.moi_backend, attr, MOIVAR)
function MOI.canget(model::Model, attr::MOI.AbstractConstraintAttribute,
::Type{<:ConstraintRef{Model, T}}) where {T <: MOICON}
return MOI.canget(model.moibackend, attr, T)
return MOI.canget(model.moi_backend, attr, T)
end

"""
get(m::JuMP.Model, attr::MathOptInterface.AbstractModelAttribute)
Return the value of the attribute `attr` from model's MOI backend.
"""
MOI.get(m::Model, attr::MOI.AbstractModelAttribute) = MOI.get(m.moibackend, attr)
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
MOI.get(m.moibackend, attr, index(v))
MOI.get(m.moi_backend, attr, index(v))
end
function MOI.get(m::Model, attr::MOI.AbstractConstraintAttribute, cr::ConstraintRef)
@assert m === cr.m
MOI.get(m.moibackend, attr, index(cr))
MOI.get(m.moi_backend, attr, index(cr))
end

MOI.set!(m::Model, attr::MOI.AbstractModelAttribute, value) = MOI.set!(m.moibackend, attr, value)
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
MOI.set!(m.moibackend, attr, index(v), value)
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
MOI.set!(m.moibackend, attr, index(cr), value)
MOI.set!(m.moi_backend, attr, index(cr), value)
end

###############################################################################
Expand Down Expand Up @@ -648,12 +648,12 @@ end
registercon(m::AbstractModel, conname, value) = error("Invalid constraint name $conname")

function registerobject(m::AbstractModel, name::Symbol, value, errorstring::String)
objdict = object_dictionary(m)
if haskey(objdict, name)
obj_dict = object_dictionary(m)
if haskey(obj_dict, name)
error(errorstring)
objdict[name] = nothing
obj_dict[name] = nothing
else
objdict[name] = value
obj_dict[name] = value
end
return value
end
Expand All @@ -666,13 +666,13 @@ To allow easy accessing of JuMP tVariables and Constraints via `[]` syntax.
Returns the variable, or group of variables, or constraint, or group of constraints, of the given name which were added to the model. This errors if multiple variables or constraints share the same name.
"""
function Base.getindex(m::JuMP.AbstractModel, name::Symbol)
objdict = object_dictionary(m)
if !haskey(objdict, name)
obj_dict = object_dictionary(m)
if !haskey(obj_dict, name)
throw(KeyError("No object with name $name"))
elseif objdict[name] === nothing
elseif obj_dict[name] === nothing
error("There are multiple variables and/or constraints named $name that are already attached to this model. If creating variables programmatically, use the anonymous variable syntax x = @variable(m, [1:N], ...). If creating constraints programmatically, use the anonymous constraint syntax con = @constraint(m, ...).")
else
return objdict[name]
return obj_dict[name]
end
end

Expand All @@ -682,10 +682,10 @@ end
stores the object `value` in the model `m` using so that it can be accessed via `getindex`. Can be called with `[]` syntax.
"""
function Base.setindex!(m::JuMP.Model, value, name::Symbol)
# if haskey(m.objdict, name)
# if haskey(m.obj_dict, name)
# warn("Overwriting the object $name stored in the model. Consider using anonymous variables and constraints instead")
# end
m.objdict[name] = value
m.obj_dict[name] = value
end

"""
Expand Down
6 changes: 3 additions & 3 deletions src/affexpr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ function setobjective(m::Model, sense::Symbol, a::AffExpr)
@assert sense == :Max
moisense = MOI.MaxSense
end
MOI.set!(m.moibackend, MOI.ObjectiveSense(), moisense)
MOI.set!(m.moibackend, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(), MOI.ScalarAffineFunction(a))
MOI.set!(m.moi_backend, MOI.ObjectiveSense(), moisense)
MOI.set!(m.moi_backend, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(), MOI.ScalarAffineFunction(a))
nothing
end

Expand All @@ -299,7 +299,7 @@ Return an `AffExpr` object representing the objective function.
Error if the objective is not linear.
"""
function objectivefunction(m::Model, ::Type{AffExpr})
f = MOI.get(m.moibackend, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}())::MOI.ScalarAffineFunction
f = MOI.get(m.moi_backend, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}())::MOI.ScalarAffineFunction
return AffExpr(m, f)
end

Expand Down
8 changes: 4 additions & 4 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1442,8 +1442,8 @@ macro NLconstraint(m, x, extra...)
lhs = :($(x.args[2]) - $(x.args[3]))
code = quote
c = NonlinearConstraint($(processNLExpr(m, lhs)), $lb, $ub)
push!($esc_m.nlpdata.nlconstr, c)
$(refcall) = ConstraintRef($esc_m, NonlinearConstraintIndex(length($esc_m.nlpdata.nlconstr)), ScalarShape())
push!($esc_m.nlp_data.nlconstr, c)
$(refcall) = ConstraintRef($esc_m, NonlinearConstraintIndex(length($esc_m.nlp_data.nlconstr)), ScalarShape())
end
elseif isexpr(x, :comparison)
# ranged row
Expand All @@ -1459,8 +1459,8 @@ macro NLconstraint(m, x, extra...)
error(string("in @NLconstraint (",$(string(x)),"): expected ",$(string(ub))," to be a number."))
end
c = NonlinearConstraint($(processNLExpr(m, x.args[3])), $(esc(lb)), $(esc(ub)))
push!($esc_m.nlpdata.nlconstr, c)
$(refcall) = ConstraintRef($esc_m, NonlinearConstraintIndex(length($esc_m.nlpdata.nlconstr)), ScalarShape())
push!($esc_m.nlp_data.nlconstr, c)
$(refcall) = ConstraintRef($esc_m, NonlinearConstraintIndex(length($esc_m.nlp_data.nlconstr)), ScalarShape())
end
else
# Unknown
Expand Down

0 comments on commit 0d281d0

Please sign in to comment.