Skip to content
This repository has been archived by the owner on Jun 14, 2020. It is now read-only.

Commit

Permalink
Update enums to JuMP style guide (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Dec 19, 2018
1 parent 0fffd7f commit 7f4d09b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 40 deletions.
12 changes: 6 additions & 6 deletions src/LinQuadOptInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ function Base.isempty(map::ConstraintMapping)
end

@enum(ObjectiveType,
SingleVariableObjective,
AffineObjective,
QuadraticObjective)
SINGLE_VARIABLE_OBJECTIVE,
AFFINE_OBJECTIVE,
QUADRATIC_OBJECTIVE)

# Abstract + macro
abstract type LinQuadOptimizer <: MOI.AbstractOptimizer end
Expand Down Expand Up @@ -230,7 +230,7 @@ function MOI.get(::LinQuadOptimizer, ::MOI.ListOfConstraintAttributesSet)
return MOI.AbstractConstraintAttribute[]
end

@enum(VariableType, Continuous, Binary, Integer, Semiinteger, Semicontinuous)
@enum(VariableType, CONTINUOUS, BINARY, INTEGER, SEMI_INTEGER, SEMI_CONTINUOUS)

macro LinQuadOptimizerBase(inner_model_type=Any)
esc(quote
Expand Down Expand Up @@ -281,7 +281,7 @@ end
function MOI.is_empty(m::LinQuadOptimizer)
ret = true
ret = ret && m.name == ""
ret = ret && m.obj_type == AffineObjective
ret = ret && m.obj_type == AFFINE_OBJECTIVE
ret = ret && isa(m.single_obj_var, Nothing)
ret = ret && m.obj_sense == MOI.MIN_SENSE
ret = ret && m.last_variable_reference == 0
Expand Down Expand Up @@ -315,7 +315,7 @@ function MOI.empty!(m::M, env = nothing) where M<:LinQuadOptimizer
m.name = ""
m.inner = LinearQuadraticModel(M,env)

m.obj_type = AffineObjective
m.obj_type = AFFINE_OBJECTIVE
m.single_obj_var = nothing
# we assume the default is minimization
m.obj_sense = MOI.MIN_SENSE
Expand Down
26 changes: 13 additions & 13 deletions src/constraints/singlevariable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ end
function MOI.add_constraint(model::LinQuadOptimizer, variable::SinVar, set::S) where S <: LinSets
__assert_supported_constraint__(model, SinVar, S)
variable_type = model.variable_type[variable.variable]
if !(variable_type == Continuous || variable_type == Integer)
if !(variable_type == CONTINUOUS || variable_type == INTEGER)
error("Cannot set bounds because variable is of type: $(variable_type).")
end
set_variable_bound(model, variable, set)
Expand All @@ -124,7 +124,7 @@ function MOI.add_constraints(model::LinQuadOptimizer, variables::Vector{SinVar},
__assert_supported_constraint__(model, SinVar, S)
for variable in variables
variable_type = model.variable_type[variable.variable]
if !(variable_type == Continuous || variable_type == Integer)
if !(variable_type == CONTINUOUS || variable_type == INTEGER)
error("Cannot set bounds because variable is of type: $(variable_type).")
end
end
Expand All @@ -145,7 +145,7 @@ function MOI.delete(model::LinQuadOptimizer, index::SVCI{S}) where S <: LinSets
delete_constraint_name(model, index)
dict = constrdict(model, index)
variable = dict[index]
model.variable_type[variable] = Continuous
model.variable_type[variable] = CONTINUOUS
set_variable_bound(model, SinVar(variable), IV(-Inf, Inf))
delete!(dict, index)
return
Expand Down Expand Up @@ -197,10 +197,10 @@ user does.
function MOI.add_constraint(model::LinQuadOptimizer, variable::SinVar, set::MOI.ZeroOne)
__assert_supported_constraint__(model, SinVar, MOI.ZeroOne)
variable_type = model.variable_type[variable.variable]
if variable_type != Continuous
if variable_type != CONTINUOUS
error("Cannot make variable binary because it is $(variable_type).")
end
model.variable_type[variable.variable] = Binary
model.variable_type[variable.variable] = BINARY
model.last_constraint_reference += 1
index = SVCI{MOI.ZeroOne}(model.last_constraint_reference)
column = get_column(model, variable)
Expand All @@ -223,7 +223,7 @@ function MOI.delete(model::LinQuadOptimizer, index::SVCI{MOI.ZeroOne})
delete_constraint_name(model, index)
dict = constrdict(model, index)
(variable, lower, upper) = dict[index]
model.variable_type[variable] = Continuous
model.variable_type[variable] = CONTINUOUS
column = get_column(model, variable)
change_variable_types!(
model, [column], [backend_type(model, Val{:Continuous}())])
Expand Down Expand Up @@ -254,10 +254,10 @@ end
function MOI.add_constraint(model::LinQuadOptimizer, variable::SinVar, set::MOI.Integer)
__assert_supported_constraint__(model, SinVar, MOI.Integer)
variable_type = model.variable_type[variable.variable]
if variable_type != Continuous
if variable_type != CONTINUOUS
error("Cannot make variable integer because it is $(variable_type).")
end
model.variable_type[variable.variable] = Integer
model.variable_type[variable.variable] = INTEGER
change_variable_types!(model, [get_column(model, variable)],
[backend_type(model, set)])
model.last_constraint_reference += 1
Expand All @@ -273,7 +273,7 @@ function MOI.delete(model::LinQuadOptimizer, index::SVCI{MOI.Integer})
delete_constraint_name(model, index)
dict = constrdict(model, index)
variable = dict[index]
model.variable_type[variable] = Continuous
model.variable_type[variable] = CONTINUOUS
change_variable_types!(model, [get_column(model, variable)],
[backend_type(model, Val{:Continuous}())])
delete!(dict, index)
Expand All @@ -295,12 +295,12 @@ end
Semicontinuous / Semiinteger constraints
=#
const SEMI_TYPES = Union{MOI.Semicontinuous{Float64}, MOI.Semiinteger{Float64}}
variable_type_(::Type{<:MOI.Semicontinuous}) = Semicontinuous
variable_type_(::Type{<:MOI.Semiinteger}) = Semiinteger
variable_type_(::Type{<:MOI.Semicontinuous}) = SEMI_CONTINUOUS
variable_type_(::Type{<:MOI.Semiinteger}) = SEMI_INTEGER
function MOI.add_constraint(model::LinQuadOptimizer, variable::SinVar, set::S) where S <: SEMI_TYPES
__assert_supported_constraint__(model, SinVar, S)
variable_type = model.variable_type[variable.variable]
if variable_type != Continuous
if variable_type != CONTINUOUS
error("Cannot make variable $(S) because it is $(variable_type).")
end
model.variable_type[variable.variable] = variable_type_(S)
Expand All @@ -326,7 +326,7 @@ function MOI.delete(model::LinQuadOptimizer, index::SVCI{<:SEMI_TYPES})
dict = constrdict(model, index)
variable = dict[index]
column = get_column(model, variable)
model.variable_type[variable] = Continuous
model.variable_type[variable] = CONTINUOUS
change_variable_types!(model, [column], [backend_type(model, Val{:Continuous}())])
change_variable_bounds!(model,
[column, column],
Expand Down
36 changes: 18 additions & 18 deletions src/objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function MOI.set(model::LinQuadOptimizer, ::MOI.ObjectiveSense,
unsafe_set!(model, MOI.ObjectiveFunction{Linear}(),
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm{Float64}[],
0.0))
model.obj_type = AffineObjective
model.obj_type = AFFINE_OBJECTIVE
model.obj_sense = MOI.FEASIBILITY_SENSE
else
throw(MOI.CannotSetAttribute(MOI.ObjectiveSense,
Expand All @@ -46,10 +46,10 @@ function MOI.set(model::LinQuadOptimizer,
attribute::MOI.ObjectiveFunction{MOI.SingleVariable},
objective::MOI.SingleVariable)
__assert_objective__(model, attribute)
if model.obj_type == QuadraticObjective
if model.obj_type == QUADRATIC_OBJECTIVE
set_quadratic_objective!(model, Int[], Int[], Float64[])
end
model.obj_type = SingleVariableObjective
model.obj_type = SINGLE_VARIABLE_OBJECTIVE
model.single_obj_var = objective.variable
set_linear_objective!(model, [get_column(model, objective.variable)], [1.0])
set_constant_objective!(model, 0.0)
Expand All @@ -68,11 +68,11 @@ Sets a linear objective function without cannonicalizing `objective`.
"""
function unsafe_set!(model::LinQuadOptimizer, ::MOI.ObjectiveFunction{F},
objective::Linear) where F
if model.obj_type == QuadraticObjective
if model.obj_type == QUADRATIC_OBJECTIVE
# previous objective was quadratic, so zero quadratic part
set_quadratic_objective!(model, Int[], Int[], Float64[])
end
model.obj_type = AffineObjective
model.obj_type = AFFINE_OBJECTIVE
model.single_obj_var = nothing
set_linear_objective!(model,
map(term -> get_column(model, term.variable_index), objective.terms),
Expand All @@ -84,7 +84,7 @@ end
function MOI.set(model::LinQuadOptimizer, attribute::MOI.ObjectiveFunction,
objective::Quad)
__assert_objective__(model, attribute)
model.obj_type = QuadraticObjective
model.obj_type = QUADRATIC_OBJECTIVE
model.single_obj_var = nothing
aff_cols, aff_coefs, quad_rows, quad_cols, quad_coefs = canonical_reduction(model, objective)
set_linear_objective!(model, aff_cols, aff_coefs)
Expand All @@ -100,20 +100,20 @@ function MOI.supports(model::LinQuadOptimizer, ::MOI.ObjectiveFunction{F}) where
end

function MOI.get(model::LinQuadOptimizer, ::MOI.ObjectiveFunctionType)
if model.obj_type == SingleVariableObjective
if model.obj_type == SINGLE_VARIABLE_OBJECTIVE
return MOI.SingleVariable
elseif model.obj_type == AffineObjective
elseif model.obj_type == AFFINE_OBJECTIVE
return MOI.ScalarAffineFunction{Float64}
else
@assert model.obj_type == QuadraticObjective
@assert model.obj_type == QUADRATIC_OBJECTIVE
return MOI.ScalarQuadraticFunction{Float64}
end
end

function MOI.get(model::LinQuadOptimizer, ::MOI.ObjectiveFunction{MOI.SingleVariable})
if model.obj_type != SingleVariableObjective
if model.obj_type != SINGLE_VARIABLE_OBJECTIVE
if VERSION >= v"0.7-"
throw(InexactError(:convert, SingleVariableObjective, model.obj_type))
throw(InexactError(:convert, SINGLE_VARIABLE_OBJECTIVE, model.obj_type))
else
throw(InexactError())
end
Expand All @@ -122,9 +122,9 @@ function MOI.get(model::LinQuadOptimizer, ::MOI.ObjectiveFunction{MOI.SingleVari
end

function MOI.get(model::LinQuadOptimizer, ::MOI.ObjectiveFunction{Linear})
if model.obj_type == QuadraticObjective
if model.obj_type == QUADRATIC_OBJECTIVE
if VERSION >= v"0.7-"
throw(InexactError(:convert, AffineObjective, model.obj_type))
throw(InexactError(:convert, AFFINE_OBJECTIVE, model.obj_type))
else
throw(InexactError())
end
Expand All @@ -148,7 +148,7 @@ function MOI.get(model::LinQuadOptimizer, ::MOI.ObjectiveFunction{Quad})
variable_coefficients
)
quadratic_terms = MOI.ScalarQuadraticTerm{Float64}[]
if model.obj_type == QuadraticObjective
if model.obj_type == QUADRATIC_OBJECTIVE
Q = get_quadratic_terms_objective(model)
rows = rowvals(Q)
coefficients = nonzeros(Q)
Expand All @@ -174,15 +174,15 @@ end

function MOI.modify(model::LinQuadOptimizer, ::MOI.ObjectiveFunction{F},
change::MOI.ScalarCoefficientChange{Float64}) where F<:MOI.AbstractScalarFunction
if F <: MOI.ScalarQuadraticFunction && model.obj_type != QuadraticObjective
if F <: MOI.ScalarQuadraticFunction && model.obj_type != QUADRATIC_OBJECTIVE
throw(MOI.UnsupportedObjectiveModification(change,
"ObjectiveFunction is not a ScalarQuadraticFunction."))
elseif F <: MOI.ScalarAffineFunction && model.obj_type != AffineObjective
elseif F <: MOI.ScalarAffineFunction && model.obj_type != AFFINE_OBJECTIVE
throw(MOI.UnsupportedObjectiveModification(change,
"ObjectiveFunction is not a ScalarAffineFunction."))
end
if model.obj_type == SingleVariableObjective
model.obj_type = AffineObjective
if model.obj_type == SINGLE_VARIABLE_OBJECTIVE
model.obj_type = AFFINE_OBJECTIVE
model.single_obj_var = nothing
end
change_objective_coefficient!(model, get_column(model, change.variable),
Expand Down
2 changes: 1 addition & 1 deletion src/solve.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function has_quadratic(model::LinQuadOptimizer)
return model.obj_type == QuadraticObjective ||
return model.obj_type == QUADRATIC_OBJECTIVE ||
length(cmap(model).q_less_than) > 0 ||
length(cmap(model).q_greater_than) > 0 ||
length(cmap(model).q_equal_to) > 0
Expand Down
4 changes: 2 additions & 2 deletions src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function MOI.add_variable(model::LinQuadOptimizer)
push!(model.variable_references, index)
push!(model.variable_primal_solution, NaN)
push!(model.variable_dual_solution, NaN)
model.variable_type[index] = Continuous
model.variable_type[index] = CONTINUOUS
return index
end

Expand All @@ -133,7 +133,7 @@ function MOI.add_variables(model::LinQuadOptimizer, number_to_add::Int)
push!(model.variable_references, index)
push!(model.variable_primal_solution, NaN)
push!(model.variable_dual_solution, NaN)
model.variable_type[index] = Continuous
model.variable_type[index] = CONTINUOUS
end
return variable_indices
end
Expand Down

0 comments on commit 7f4d09b

Please sign in to comment.