Skip to content

Commit 53ec269

Browse files
authored
[Utilities] remove unecessary acronyms (#2281)
1 parent 52bf172 commit 53ec269

File tree

6 files changed

+65
-38
lines changed

6 files changed

+65
-38
lines changed

src/Utilities/Utilities.jl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@ import MathOptInterface as MOI
1111
import MutableArithmetics as MA
1212
import OrderedCollections: OrderedDict
1313

14-
const SVF = MOI.VariableIndex
15-
const VVF = MOI.VectorOfVariables
16-
const SAF{T} = MOI.ScalarAffineFunction{T}
17-
const VAF{T} = MOI.VectorAffineFunction{T}
18-
const SQF{T} = MOI.ScalarQuadraticFunction{T}
19-
const VQF{T} = MOI.VectorQuadraticFunction{T}
20-
21-
const VI = MOI.VariableIndex
22-
const CI{F,S} = MOI.ConstraintIndex{F,S}
23-
2414
function print_with_acronym(io::IO, s::AbstractString)
2515
return print(io, replace_acronym(s))
2616
end

src/Utilities/cachingoptimizer.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ end
553553

554554
function MOI.modify(
555555
m::CachingOptimizer,
556-
cindex::CI,
556+
cindex::MOI.ConstraintIndex,
557557
change::MOI.AbstractFunctionModification,
558558
)
559559
if m.state == ATTACHED_OPTIMIZER
@@ -621,7 +621,7 @@ end
621621
function MOI.set(
622622
m::CachingOptimizer,
623623
::MOI.ConstraintSet,
624-
cindex::CI{F,S},
624+
cindex::MOI.ConstraintIndex{F,S},
625625
set::S,
626626
) where {F,S}
627627
_replace_constraint_function_or_set(m, MOI.ConstraintSet(), cindex, set)

src/Utilities/mockoptimizer.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,14 @@ This is good at catching bugs in solvers which assume indices are ordered 1, 2,
138138
"""
139139
const _INTERNAL_XOR_MASK = Int64(12345678)
140140

141-
xor_index(vi::VI) = VI(xor(vi.value, _INTERNAL_XOR_MASK))
142-
xor_index(ci::CI{F,S}) where {F,S} = CI{F,S}(xor(ci.value, _INTERNAL_XOR_MASK))
141+
function xor_index(vi::MOI.VariableIndex)
142+
return MOI.VariableIndex(xor(vi.value, _INTERNAL_XOR_MASK))
143+
end
144+
145+
function xor_index(ci::MOI.ConstraintIndex{F,S}) where {F,S}
146+
return MOI.ConstraintIndex{F,S}(xor(ci.value, _INTERNAL_XOR_MASK))
147+
end
148+
143149
xor_indices(x) = map_indices(xor_index, x)
144150

145151
function MOI.add_variable(mock::MockOptimizer)
@@ -854,7 +860,7 @@ end
854860

855861
function MOI.modify(
856862
mock::MockOptimizer,
857-
c::CI,
863+
c::MOI.ConstraintIndex,
858864
change::MOI.AbstractFunctionModification,
859865
)
860866
if !mock.modify_allowed
@@ -867,7 +873,7 @@ end
867873
function MOI.set(
868874
mock::MockOptimizer,
869875
::MOI.ConstraintSet,
870-
c::CI{<:MOI.AbstractFunction,S},
876+
c::MOI.ConstraintIndex{<:MOI.AbstractFunction,S},
871877
set::S,
872878
) where {S<:MOI.AbstractSet}
873879
MOI.set(mock.inner_model, MOI.ConstraintSet(), xor_index(c), set)
@@ -877,7 +883,7 @@ end
877883
function MOI.set(
878884
mock::MockOptimizer,
879885
::MOI.ConstraintFunction,
880-
c::CI{F},
886+
c::MOI.ConstraintIndex{F},
881887
func::F,
882888
) where {F<:MOI.AbstractFunction}
883889
MOI.set(

src/Utilities/model.jl

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ function `f` are removed and the dimension of the set `s` is updated if
4141
needed (e.g. when `f` is a `VectorOfVariables` with `vi` being one of the
4242
variables).
4343
"""
44-
remove_variable(f, s, vi::VI) = remove_variable(f, vi), s
45-
function remove_variable(f::MOI.VectorOfVariables, s, vi::VI)
44+
remove_variable(f, s, vi::MOI.VariableIndex) = remove_variable(f, vi), s
45+
46+
function remove_variable(f::MOI.VectorOfVariables, s, vi::MOI.VariableIndex)
4647
g = remove_variable(f, vi)
4748
if length(g.variables) != length(f.variables)
4849
t = MOI.update_dimension(s, length(g.variables))
@@ -139,14 +140,30 @@ function MOI.set(model::AbstractModel, ::MOI.Name, name::String)
139140
end
140141
MOI.get(model::AbstractModel, ::MOI.Name) = model.name
141142

142-
MOI.supports(::AbstractModel, ::MOI.VariableName, vi::Type{VI}) = true
143-
function MOI.set(model::AbstractModel, ::MOI.VariableName, vi::VI, name::String)
143+
function MOI.supports(
144+
::AbstractModel,
145+
::MOI.VariableName,
146+
::Type{MOI.VariableIndex},
147+
)
148+
return true
149+
end
150+
151+
function MOI.set(
152+
model::AbstractModel,
153+
::MOI.VariableName,
154+
vi::MOI.VariableIndex,
155+
name::String,
156+
)
144157
model.var_to_name[vi] = name
145158
model.name_to_var = nothing # Invalidate the name map.
146159
return
147160
end
148161

149-
function MOI.get(model::AbstractModel, ::MOI.VariableName, vi::VI)
162+
function MOI.get(
163+
model::AbstractModel,
164+
::MOI.VariableName,
165+
vi::MOI.VariableIndex,
166+
)
150167
return get(model.var_to_name, vi, "")
151168
end
152169

@@ -157,13 +174,13 @@ Create and return a reverse map from name to variable index, given a map from
157174
variable index to name. The special value `MOI.VariableIndex(0)` is used to
158175
indicate that multiple variables have the same name.
159176
"""
160-
function build_name_to_var_map(var_to_name::Dict{VI,String})
161-
name_to_var = Dict{String,VI}()
177+
function build_name_to_var_map(var_to_name::Dict{MOI.VariableIndex,String})
178+
name_to_var = Dict{String,MOI.VariableIndex}()
162179
for (var, var_name) in var_to_name
163180
if haskey(name_to_var, var_name)
164181
# 0 is a special value that means this string does not map to
165182
# a unique variable name.
166-
name_to_var[var_name] = VI(0)
183+
name_to_var[var_name] = MOI.VariableIndex(0)
167184
else
168185
name_to_var[var_name] = var
169186
end
@@ -184,7 +201,7 @@ function throw_if_multiple_with_name(index::MOI.Index, name::String)
184201
end
185202
end
186203

187-
function MOI.get(model::AbstractModel, ::Type{VI}, name::String)
204+
function MOI.get(model::AbstractModel, ::Type{MOI.VariableIndex}, name::String)
188205
if model.name_to_var === nothing
189206
# Rebuild the map.
190207
model.name_to_var = build_name_to_var_map(model.var_to_name)
@@ -201,12 +218,18 @@ function MOI.get(
201218
return isempty(model.var_to_name) ? [] : [MOI.VariableName()]
202219
end
203220

204-
MOI.supports(model::AbstractModel, ::MOI.ConstraintName, ::Type{<:CI}) = true
221+
function MOI.supports(
222+
::AbstractModel,
223+
::MOI.ConstraintName,
224+
::Type{<:MOI.ConstraintIndex},
225+
)
226+
return true
227+
end
205228

206229
function MOI.set(
207230
model::AbstractModel,
208231
::MOI.ConstraintName,
209-
ci::CI,
232+
ci::MOI.ConstraintIndex,
210233
name::String,
211234
)
212235
model.con_to_name[ci] = name
@@ -231,7 +254,11 @@ function MOI.set(
231254
return throw(MOI.VariableIndexConstraintNameError())
232255
end
233256

234-
function MOI.get(model::AbstractModel, ::MOI.ConstraintName, ci::CI)
257+
function MOI.get(
258+
model::AbstractModel,
259+
::MOI.ConstraintName,
260+
ci::MOI.ConstraintIndex,
261+
)
235262
return get(model.con_to_name, ci, "")
236263
end
237264

@@ -243,19 +270,23 @@ constraint index to name. The special value
243270
`MOI.ConstraintIndex{Nothing, Nothing}(0)` is used to indicate that multiple
244271
constraints have the same name.
245272
"""
246-
function build_name_to_con_map(con_to_name::Dict{CI,String})
247-
name_to_con = Dict{String,CI}()
273+
function build_name_to_con_map(con_to_name::Dict{<:MOI.ConstraintIndex,String})
274+
name_to_con = Dict{String,MOI.ConstraintIndex}()
248275
for (con, con_name) in con_to_name
249276
if haskey(name_to_con, con_name)
250-
name_to_con[con_name] = CI{Nothing,Nothing}(0)
277+
name_to_con[con_name] = MOI.ConstraintIndex{Nothing,Nothing}(0)
251278
else
252279
name_to_con[con_name] = con
253280
end
254281
end
255282
return name_to_con
256283
end
257284

258-
function MOI.get(model::AbstractModel, ConType::Type{<:CI}, name::String)
285+
function MOI.get(
286+
model::AbstractModel,
287+
::Type{ConType},
288+
name::String,
289+
) where {ConType<:MOI.ConstraintIndex}
259290
if model.name_to_con === nothing
260291
# Rebuild the map.
261292
model.name_to_con = build_name_to_con_map(model.con_to_name)

src/Utilities/universalfallback.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ mutable struct UniversalFallback{MT} <: MOI.ModelLike
2929
name_to_con::Union{Dict{String,MOI.ConstraintIndex},Nothing}
3030
optattr::Dict{MOI.AbstractOptimizerAttribute,Any}
3131
modattr::Dict{MOI.AbstractModelAttribute,Any}
32-
varattr::Dict{MOI.AbstractVariableAttribute,Dict{VI,Any}}
32+
varattr::Dict{MOI.AbstractVariableAttribute,Dict{MOI.VariableIndex,Any}}
3333
conattr::Dict{MOI.AbstractConstraintAttribute,Dict{MOI.ConstraintIndex,Any}}
3434
function UniversalFallback{MT}(model::MOI.ModelLike) where {MT}
3535
return new{typeof(model)}(
@@ -41,7 +41,7 @@ mutable struct UniversalFallback{MT} <: MOI.ModelLike
4141
nothing,
4242
Dict{MOI.AbstractOptimizerAttribute,Any}(),
4343
Dict{MOI.AbstractModelAttribute,Any}(),
44-
Dict{MOI.AbstractVariableAttribute,Dict{VI,Any}}(),
44+
Dict{MOI.AbstractVariableAttribute,Dict{MOI.VariableIndex,Any}}(),
4545
Dict{
4646
MOI.AbstractConstraintAttribute,
4747
Dict{MOI.ConstraintIndex,Any},
@@ -629,8 +629,8 @@ function MOI.get(
629629
return get(uf.con_to_name, ci, "")
630630
end
631631

632-
function MOI.get(uf::UniversalFallback, ::Type{VI}, name::String)
633-
return MOI.get(uf.model, VI, name)
632+
function MOI.get(uf::UniversalFallback, ::Type{MOI.VariableIndex}, name::String)
633+
return MOI.get(uf.model, MOI.VariableIndex, name)
634634
end
635635

636636
check_type_and_multiple_names(::Type, ::Nothing, ::Nothing, name) = nothing

src/Utilities/variables_container.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ end
311311
function MOI.get(
312312
model::VariablesContainer,
313313
::MOI.ConstraintFunction,
314-
ci::CI{MOI.VariableIndex},
314+
ci::MOI.ConstraintIndex{MOI.VariableIndex},
315315
)
316316
MOI.throw_if_not_valid(model, ci)
317317
return MOI.VariableIndex(ci.value)

0 commit comments

Comments
 (0)