From 092ea8ca25301cc70a9186f3f4cfe39db3ef1683 Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 28 May 2021 10:46:36 +1200 Subject: [PATCH] Renaming in variables.jl --- src/variables.jl | 144 ++++++++++++++++++++++++++--------------------- 1 file changed, 81 insertions(+), 63 deletions(-) diff --git a/src/variables.jl b/src/variables.jl index d142ac84cd1..981339f8a57 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -437,8 +437,8 @@ end # _moi_* methods allow us to work around the type instability of the backend of # a model. -function _moi_has_lower_bound(backend, v::VariableRef) - return MOI.is_valid(backend, _lower_bound_index(v)) +function _moi_has_lower_bound(moi_backend, v::VariableRef) + return MOI.is_valid(moi_backend, _lower_bound_index(v)) end function _lower_bound_index(v::VariableRef) @@ -458,14 +458,14 @@ function set_lower_bound(v::VariableRef, lower::Number) return _moi_set_lower_bound(backend(owner_model(v)), v, lower) end -function _moi_set_lower_bound(backend, v::VariableRef, lower::Number) +function _moi_set_lower_bound(moi_backend, v::VariableRef, lower::Number) new_set = MOI.GreaterThan(convert(Float64, lower)) - if _moi_has_lower_bound(backend, v) + if _moi_has_lower_bound(moi_backend, v) cindex = _lower_bound_index(v) - MOI.set(backend, MOI.ConstraintSet(), cindex, new_set) + MOI.set(moi_backend, MOI.ConstraintSet(), cindex, new_set) else - @assert !_moi_is_fixed(backend, v) - MOI.add_constraint(backend, MOI.SingleVariable(index(v)), new_set) + @assert !_moi_is_fixed(moi_backend, v) + moi_add_constraint(moi_backend, MOI.SingleVariable(index(v)), new_set) end return end @@ -536,8 +536,8 @@ function has_upper_bound(v::VariableRef) return _moi_has_upper_bound(backend(owner_model(v)), v) end -function _moi_has_upper_bound(backend, v::VariableRef) - return MOI.is_valid(backend, _upper_bound_index(v)) +function _moi_has_upper_bound(moi_backend, v::VariableRef) + return MOI.is_valid(moi_backend, _upper_bound_index(v)) end function _upper_bound_index(v::VariableRef) @@ -557,14 +557,14 @@ function set_upper_bound(v::VariableRef, upper::Number) return _moi_set_upper_bound(backend(owner_model(v)), v, upper) end -function _moi_set_upper_bound(backend, v::VariableRef, upper::Number) +function _moi_set_upper_bound(moi_backend, v::VariableRef, upper::Number) new_set = MOI.LessThan(convert(Float64, upper)) - if _moi_has_upper_bound(backend, v) + if _moi_has_upper_bound(moi_backend, v) cindex = _upper_bound_index(v) - MOI.set(backend, MOI.ConstraintSet(), cindex, new_set) + MOI.set(moi_backend, MOI.ConstraintSet(), cindex, new_set) else - @assert !_moi_is_fixed(backend, v) - MOI.add_constraint(backend, MOI.SingleVariable(index(v)), new_set) + @assert !_moi_is_fixed(moi_backend, v) + moi_add_constraint(moi_backend, MOI.SingleVariable(index(v)), new_set) end return end @@ -634,8 +634,8 @@ function is_fixed(v::VariableRef) return _moi_is_fixed(backend(owner_model(v)), v) end -function _moi_is_fixed(backend, v::VariableRef) - return MOI.is_valid(backend, _fix_index(v)) +function _moi_is_fixed(moi_backend, v::VariableRef) + return MOI.is_valid(moi_backend, _fix_index(v)) end function _fix_index(v::VariableRef) @@ -660,14 +660,19 @@ function fix(variable::VariableRef, value::Number; force::Bool = false) return _moi_fix(backend(owner_model(variable)), variable, value, force) end -function _moi_fix(backend, variable::VariableRef, value::Number, force::Bool) +function _moi_fix( + moi_backend, + variable::VariableRef, + value::Number, + force::Bool, +) new_set = MOI.EqualTo(convert(Float64, value)) - if _moi_is_fixed(backend, variable) # Update existing fixing constraint. + if _moi_is_fixed(moi_backend, variable) # Update existing fixing constraint. c_index = _fix_index(variable) - MOI.set(backend, MOI.ConstraintSet(), c_index, new_set) + MOI.set(moi_backend, MOI.ConstraintSet(), c_index, new_set) else # Add a new fixing constraint. - if _moi_has_upper_bound(backend, variable) || - _moi_has_lower_bound(backend, variable) + if _moi_has_upper_bound(moi_backend, variable) || + _moi_has_lower_bound(moi_backend, variable) if !force error( "Unable to fix $(variable) to $(value) because it has " * @@ -676,15 +681,15 @@ function _moi_fix(backend, variable::VariableRef, value::Number, force::Bool) "delete existing bounds before fixing the variable.", ) end - if _moi_has_upper_bound(backend, variable) - MOI.delete(backend, _upper_bound_index(variable)) + if _moi_has_upper_bound(moi_backend, variable) + MOI.delete(moi_backend, _upper_bound_index(variable)) end - if _moi_has_lower_bound(backend, variable) - MOI.delete(backend, _lower_bound_index(variable)) + if _moi_has_lower_bound(moi_backend, variable) + MOI.delete(moi_backend, _lower_bound_index(variable)) end end - MOI.add_constraint( - backend, + moi_add_constraint( + moi_backend, MOI.SingleVariable(index(variable)), new_set, ) @@ -750,8 +755,8 @@ function is_integer(v::VariableRef) return _moi_is_integer(backend(owner_model(v)), v) end -function _moi_is_integer(backend, v::VariableRef) - return MOI.is_valid(backend, _integer_index(v)) +function _moi_is_integer(moi_backend, v::VariableRef) + return MOI.is_valid(moi_backend, _integer_index(v)) end function _integer_index(v::VariableRef) @@ -769,17 +774,17 @@ function set_integer(variable_ref::VariableRef) return _moi_set_integer(backend(owner_model(variable_ref)), variable_ref) end -function _moi_set_integer(backend, variable_ref::VariableRef) - if _moi_is_integer(backend, variable_ref) +function _moi_set_integer(moi_backend, variable_ref::VariableRef) + if _moi_is_integer(moi_backend, variable_ref) return - elseif _moi_is_binary(backend, variable_ref) + elseif _moi_is_binary(moi_backend, variable_ref) error( "Cannot set the variable_ref $(variable_ref) to integer as it " * "is already binary.", ) end - MOI.add_constraint( - backend, + moi_add_constraint( + moi_backend, MOI.SingleVariable(index(variable_ref)), MOI.Integer(), ) @@ -826,8 +831,8 @@ function is_binary(v::VariableRef) return _moi_is_binary(backend(owner_model(v)), v) end -function _moi_is_binary(backend, v::VariableRef) - return MOI.is_valid(backend, _binary_index(v)) +function _moi_is_binary(moi_backend, v::VariableRef) + return MOI.is_valid(moi_backend, _binary_index(v)) end function _binary_index(v::VariableRef) @@ -846,17 +851,17 @@ function set_binary(variable_ref::VariableRef) return _moi_set_binary(backend(owner_model(variable_ref)), variable_ref) end -function _moi_set_binary(backend, variable_ref) - if _moi_is_binary(backend, variable_ref) +function _moi_set_binary(moi_backend, variable_ref) + if _moi_is_binary(moi_backend, variable_ref) return - elseif _moi_is_integer(backend, variable_ref) + elseif _moi_is_integer(moi_backend, variable_ref) error( "Cannot set the variable_ref $(variable_ref) to binary as it " * "is already integer.", ) end - MOI.add_constraint( - backend, + moi_add_constraint( + moi_backend, MOI.SingleVariable(index(variable_ref)), MOI.ZeroOne(), ) @@ -972,48 +977,61 @@ function add_variable(model::Model, v::ScalarVariable, name::String = "") return _moi_add_variable(backend(model), model, v, name) end -function _moi_add_variable(backend, model, v::ScalarVariable, name::String) - index = MOI.add_variable(backend) +function _moi_add_variable(moi_backend, model, v::ScalarVariable, name::String) + index = MOI.add_variable(moi_backend) var_ref = VariableRef(model, index) - _moi_constrain_variable(backend, index, v.info) + _moi_constrain_variable(moi_backend, index, v.info) if !isempty(name) set_name(var_ref, name) end return var_ref end -function _moi_constrain_variable(backend::MOI.ModelLike, index, info) +function _moi_constrain_variable(moi_backend::MOI.ModelLike, index, info) # We don't call the _moi* versions (e.g., _moi_set_lower_bound) because they # have extra checks that are not necessary for newly created variables. if info.has_lb - MOI.add_constraint( - backend, + moi_add_constraint( + moi_backend, MOI.SingleVariable(index), MOI.GreaterThan{Float64}(info.lower_bound), ) end if info.has_ub - MOI.add_constraint( - backend, + moi_add_constraint( + moi_backend, MOI.SingleVariable(index), MOI.LessThan{Float64}(info.upper_bound), ) end if info.has_fix - MOI.add_constraint( - backend, + moi_add_constraint( + moi_backend, MOI.SingleVariable(index), MOI.EqualTo{Float64}(info.fixed_value), ) end if info.binary - MOI.add_constraint(backend, MOI.SingleVariable(index), MOI.ZeroOne()) + moi_add_constraint( + moi_backend, + MOI.SingleVariable(index), + MOI.ZeroOne(), + ) end if info.integer - MOI.add_constraint(backend, MOI.SingleVariable(index), MOI.Integer()) + moi_add_constraint( + moi_backend, + MOI.SingleVariable(index), + MOI.Integer(), + ) end if info.has_start - MOI.set(backend, MOI.VariablePrimalStart(), index, Float64(info.start)) + MOI.set( + moi_backend, + MOI.VariablePrimalStart(), + index, + Float64(info.start), + ) end end @@ -1057,15 +1075,15 @@ function add_variable( end function _moi_add_constrained_variable( - backend::MOI.ModelLike, + moi_backend::MOI.ModelLike, scalar_variable::ScalarVariable, set::MOI.AbstractScalarSet, name::String, ) - var_index, con_index = MOI.add_constrained_variable(backend, set) - _moi_constrain_variable(backend, var_index, scalar_variable.info) + var_index, con_index = MOI.add_constrained_variable(moi_backend, set) + _moi_constrain_variable(moi_backend, var_index, scalar_variable.info) if !isempty(name) - MOI.set(backend, MOI.VariableName(), var_index, name) + MOI.set(moi_backend, MOI.VariableName(), var_index, name) end return var_index end @@ -1121,22 +1139,22 @@ function add_variable( end function _moi_add_constrained_variables( - backend::MOI.ModelLike, + moi_backend::MOI.ModelLike, scalar_variables::Vector{<:ScalarVariable}, set::MOI.AbstractVectorSet, names::Vector{String}, ) if set isa MOI.Reals - var_indices = MOI.add_variables(backend, MOI.dimension(set)) + var_indices = MOI.add_variables(moi_backend, MOI.dimension(set)) else - var_indices, con_index = MOI.add_constrained_variables(backend, set) + var_indices, con_index = MOI.add_constrained_variables(moi_backend, set) end for (index, variable) in zip(var_indices, scalar_variables) - _moi_constrain_variable(backend, index, variable.info) + _moi_constrain_variable(moi_backend, index, variable.info) end for (var_index, name) in zip(var_indices, names) if !isempty(name) - MOI.set(backend, MOI.VariableName(), var_index, name) + MOI.set(moi_backend, MOI.VariableName(), var_index, name) end end return var_indices