Skip to content

Commit 9d8384f

Browse files
committed
use JuMP and MOI instead of jmp and moi for consistency with other projects
1 parent 2f5f19b commit 9d8384f

File tree

4 files changed

+85
-86
lines changed

4 files changed

+85
-86
lines changed

src/get_equality.jl

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,49 @@ Utility functions for identifying JuMP constraints that define equalities.
2222
2323
"""
2424

25-
import JuMP as jmp
26-
import MathOptInterface as moi
25+
import JuMP
26+
import MathOptInterface as MOI
2727

2828
function _get_set_of_constraint(
29-
model::jmp.Model,
30-
constraint::jmp.ConstraintRef,
31-
index::moi.ConstraintIndex,
29+
model::JuMP.Model,
30+
constraint::JuMP.ConstraintRef,
31+
index::MOI.ConstraintIndex,
3232
)
33-
set = moi.get(model, moi.ConstraintSet(), constraint)
33+
set = MOI.get(model, MOI.ConstraintSet(), constraint)
3434
return set
3535
end
3636

3737
function _get_set_of_constraint(
38-
model::jmp.Model,
39-
constraint::jmp.ConstraintRef,
40-
index::moi.Nonlinear.ConstraintIndex,
38+
model::JuMP.Model,
39+
constraint::JuMP.ConstraintRef,
40+
index::MOI.Nonlinear.ConstraintIndex,
4141
)
4242
nl_con = model.nlp_model.constraints[index]
4343
return nl_con.set
4444
end
4545

46-
function set_implies_equality(set::moi.EqualTo)::Bool
46+
function set_implies_equality(set::MOI.EqualTo)::Bool
4747
return true
4848
end
4949

5050
# Q: How does Julia determine if an argument has the right type.
5151
# I.e. why is this interpreted as "set must be a subtype of Union(...)"
5252
# rather than "set must be an 'instance' of Union()".
5353
#function set_implies_equality(
54-
# set::Union{moi.GreaterThan, moi.LessThan},
54+
# set::Union{MOI.GreaterThan, MOI.LessThan},
5555
# # Note that ^this works, but this doesn't
56-
# # set::Type{<:Union{moi.GreaterThan, moi.LessThan}},
56+
# # set::Type{<:Union{MOI.GreaterThan, MOI.LessThan}},
5757
#)::Bool
5858
# return false
5959
#end
6060

61-
function set_implies_equality(set::T)::Bool where T<:moi.AbstractVectorSet
62-
throw(TypeError(set, moi.AbstractScalarSet, typeof(set)))
61+
function set_implies_equality(set::T)::Bool where T<:MOI.AbstractVectorSet
62+
throw(TypeError(set, MOI.AbstractScalarSet, typeof(set)))
6363
end
6464

6565
# NOTE: Have not implemented tolerance arg in any calling function yet.
6666
function set_implies_equality(
67-
set::moi.Interval,
67+
set::MOI.Interval,
6868
tolerance::Float64=Float64(0)
6969
)::Bool
7070
return abs(set.upper - set.lower) <= tolerance
@@ -75,9 +75,9 @@ end
7575
#
7676
# I intend this to catch anything that is a subtype of ConstraintSet.
7777
# Is the "subtype syntax" necessary?
78-
# It appears, e.g., moi.GreaterThan is not a subtype of moi.ConstraintSet.
78+
# It appears, e.g., MOI.GreaterThan is not a subtype of MOI.ConstraintSet.
7979
# This is non-intuitive for me...
80-
# So this won't catch arbitrary moi Sets...
80+
# So this won't catch arbitrary MOI Sets...
8181
# The right type to use is actually AbstractSet?
8282
# Nope. This doesn't catch GreatherThan/LessThan
8383
# ^ Wrong. Actually it does, but my syntax was wrong. I guess providing
@@ -103,7 +103,7 @@ types of constraints in [`is_equality`](@ref) and
103103
"""
104104
function set_implies_equality(
105105
set::T
106-
)::Bool where T<:moi.AbstractSet
106+
)::Bool where T<:MOI.AbstractSet
107107
return false
108108
end
109109

@@ -113,17 +113,17 @@ end
113113
Detect whether a constraint is an equality constraint.
114114
115115
"""
116-
function is_equality(constraint::jmp.ConstraintRef)::Bool
116+
function is_equality(constraint::JuMP.ConstraintRef)::Bool
117117
model = constraint.model
118118
index = constraint.index
119119
set = _get_set_of_constraint(model, constraint, index)
120120
return set_implies_equality(set)
121121
end
122122

123123
function get_equality_constraints(
124-
constraints::Vector{jmp.ConstraintRef}
125-
)::Vector{jmp.ConstraintRef}
126-
eq_cons = Vector{jmp.ConstraintRef}()
124+
constraints::Vector{JuMP.ConstraintRef}
125+
)::Vector{JuMP.ConstraintRef}
126+
eq_cons = Vector{JuMP.ConstraintRef}()
127127
for con in constraints
128128
if is_equality(con)
129129
push!(eq_cons, con)
@@ -151,8 +151,8 @@ julia> display(eq_cons)
151151
```
152152
153153
"""
154-
function get_equality_constraints(model::jmp.Model)::Vector{jmp.ConstraintRef}
155-
constraints = jmp.all_constraints(
154+
function get_equality_constraints(model::JuMP.Model)::Vector{JuMP.ConstraintRef}
155+
constraints = JuMP.all_constraints(
156156
model,
157157
include_variable_in_set_constraints=false,
158158
)

src/identify_variables.jl

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Utility functions for identifying variables that participate in constraints.
2222
2323
"""
2424

25-
import JuMP as jmp
26-
import MathOptInterface as moi
25+
import JuMP
26+
import MathOptInterface as MOI
2727

2828
import JuMPIn: get_equality_constraints
2929

@@ -63,8 +63,8 @@ julia> display(vars)
6363
function identify_unique_variables(
6464
constraints::Vector,
6565
# FIXME: Couldn't get this working with Vector{ConstraintRef}...
66-
)::Vector{jmp.VariableRef}
67-
variables = Vector{jmp.VariableRef}()
66+
)::Vector{JuMP.VariableRef}
67+
variables = Vector{JuMP.VariableRef}()
6868
for con in constraints
6969
append!(variables, identify_unique_variables(con))
7070
end
@@ -81,14 +81,14 @@ Each variable appears at most one time in the returned vector.
8181
8282
"""
8383
function identify_unique_variables(
84-
model::jmp.Model; include_inequality::Bool=false,
85-
)::Vector{jmp.VariableRef}
84+
model::JuMP.Model; include_inequality::Bool=false,
85+
)::Vector{JuMP.VariableRef}
8686
if include_inequality
8787
# Note that this may include some constraints which are not compatible
8888
# with downstream function calls (e.g. constraints where the function
8989
# and terms are vectors). We will allow downstream errors to be raised
9090
# rather than silently ignoring these constraints.
91-
constraints = jmp.all_constraints(
91+
constraints = JuMP.all_constraints(
9292
model,
9393
# TODO: Should this be an optional argument to this function?
9494
include_variable_in_set_constraints=false,
@@ -109,8 +109,8 @@ Each variable appears at most one time in the returned vector.
109109
110110
"""
111111
function identify_unique_variables(
112-
constraint::jmp.ConstraintRef,
113-
)::Vector{jmp.VariableRef}
112+
constraint::JuMP.ConstraintRef,
113+
)::Vector{JuMP.VariableRef}
114114
return identify_unique_variables(constraint, constraint.index)
115115
end
116116

@@ -129,13 +129,13 @@ constraint or a nonlinear constraint.
129129
130130
"""
131131
function identify_unique_variables(
132-
constraint::jmp.ConstraintRef,
133-
index::moi.ConstraintIndex,
134-
)::Vector{jmp.VariableRef}
132+
constraint::JuMP.ConstraintRef,
133+
index::MOI.ConstraintIndex,
134+
)::Vector{JuMP.VariableRef}
135135
model = constraint.model
136-
fcn = moi.get(model, moi.ConstraintFunction(), constraint)
136+
fcn = MOI.get(model, MOI.ConstraintFunction(), constraint)
137137
varidxs = identify_unique_variables(fcn)
138-
varrefs = [jmp.VariableRef(model, idx) for idx in varidxs]
138+
varrefs = [JuMP.VariableRef(model, idx) for idx in varidxs]
139139
return varrefs
140140
end
141141

@@ -154,9 +154,9 @@ constraint or a nonlinear constraint.
154154
155155
"""
156156
function identify_unique_variables(
157-
constraint::jmp.ConstraintRef,
158-
index::moi.Nonlinear.ConstraintIndex,
159-
)::Vector{jmp.VariableRef}
157+
constraint::JuMP.ConstraintRef,
158+
index::MOI.Nonlinear.ConstraintIndex,
159+
)::Vector{JuMP.VariableRef}
160160
model = constraint.model
161161
nlmod = model.nlp_model
162162
nlcons = nlmod.constraints
@@ -167,11 +167,11 @@ function identify_unique_variables(
167167
# This could be its own function, but I inlined it here to allow a more
168168
# useful error message if we ever encounter NODE_VARIABLE.
169169
# Will be moved when/if the need arises.
170-
variables = Vector{moi.VariableIndex}()
170+
variables = Vector{MOI.VariableIndex}()
171171
for node in expr.nodes
172-
if node.type == moi.Nonlinear.NODE_MOI_VARIABLE
173-
push!(variables, moi.VariableIndex(node.index))
174-
elseif node.type == moi.Nonlinear.NODE_VARIABLE
172+
if node.type == MOI.Nonlinear.NODE_MOI_VARIABLE
173+
push!(variables, MOI.VariableIndex(node.index))
174+
elseif node.type == MOI.Nonlinear.NODE_VARIABLE
175175
# I do not know under what situation this could occur, but
176176
# am throwing this error to be defensive.
177177
throw(DomainError(
@@ -185,7 +185,7 @@ function identify_unique_variables(
185185
end
186186

187187
# Return VariableRefs
188-
refs = [jmp.VariableRef(model, idx) for idx in variables]
188+
refs = [JuMP.VariableRef(model, idx) for idx in variables]
189189
return _filter_duplicates(refs)
190190
end
191191

@@ -204,9 +204,9 @@ function should be implemented.
204204
205205
"""
206206
function identify_unique_variables(
207-
fcn::Union{moi.ScalarQuadraticFunction, moi.ScalarAffineFunction},
208-
)::Vector{moi.VariableIndex}
209-
variables = Vector{moi.VariableIndex}()
207+
fcn::Union{MOI.ScalarQuadraticFunction, MOI.ScalarAffineFunction},
208+
)::Vector{MOI.VariableIndex}
209+
variables = Vector{MOI.VariableIndex}()
210210
for terms in _get_variable_terms(fcn)
211211
for term in terms
212212
for var in identify_unique_variables(term)
@@ -219,10 +219,10 @@ end
219219

220220
function identify_unique_variables(
221221
fcn::T
222-
)::Vector{moi.VariableIndex} where {T<:moi.AbstractVectorFunction}
222+
)::Vector{MOI.VariableIndex} where {T<:MOI.AbstractVectorFunction}
223223
throw(TypeError(
224224
fcn,
225-
Union{moi.ScalarQuadraticFunction, moi.ScalarAffineFunction},
225+
Union{MOI.ScalarQuadraticFunction, MOI.ScalarAffineFunction},
226226
typeof(fcn),
227227
))
228228
end
@@ -238,11 +238,11 @@ Currently implemented only for `ScalarQuadraticFunction` and
238238
`ScalarAffineFunction`.
239239
240240
"""
241-
function _get_variable_terms(fcn::moi.ScalarQuadraticFunction)
241+
function _get_variable_terms(fcn::MOI.ScalarQuadraticFunction)
242242
return (fcn.quadratic_terms, fcn.affine_terms)
243243
end
244244

245-
function _get_variable_terms(fcn::moi.ScalarAffineFunction)
245+
function _get_variable_terms(fcn::MOI.ScalarAffineFunction)
246246
return (fcn.terms,)
247247
end
248248

@@ -260,14 +260,14 @@ Currently implemented only for `ScalarAffineTerm` and `ScalarQuadraticTerm`.
260260
261261
"""
262262
function identify_unique_variables(
263-
term::moi.ScalarAffineTerm,
264-
)::Vector{moi.VariableIndex}
263+
term::MOI.ScalarAffineTerm,
264+
)::Vector{MOI.VariableIndex}
265265
return [term.variable]
266266
end
267267

268268
function identify_unique_variables(
269-
term::moi.ScalarQuadraticTerm,
270-
)::Vector{moi.VariableIndex}
269+
term::MOI.ScalarQuadraticTerm,
270+
)::Vector{MOI.VariableIndex}
271271
# Note that this compares indices only. If somehow these indices refer to
272272
# different models, this could be incorrect.
273273
if term.variable_1 === term.variable_2
@@ -285,12 +285,12 @@ Return a vector of variables of indices that does not contain duplicates.
285285
286286
"""
287287
function _filter_duplicates(
288-
indices::Vector{moi.VariableIndex},
289-
)::Vector{moi.VariableIndex}
288+
indices::Vector{MOI.VariableIndex},
289+
)::Vector{MOI.VariableIndex}
290290
# Note that by hashing only the variable index, we implicitly assume that
291291
# all variables come from the same model. I believe this is safe.
292-
seen = Set{moi.VariableIndex}()
293-
filtered = Vector{moi.VariableIndex}()
292+
seen = Set{MOI.VariableIndex}()
293+
filtered = Vector{MOI.VariableIndex}()
294294
for idx in indices
295295
if !(idx in seen)
296296
push!(seen, idx)
@@ -302,12 +302,12 @@ end
302302

303303

304304
function _filter_duplicates(
305-
variables::Vector{jmp.VariableRef},
306-
)::Vector{jmp.VariableRef}
305+
variables::Vector{JuMP.VariableRef},
306+
)::Vector{JuMP.VariableRef}
307307
# What happens when VariableRef gets hashed? I.e. how is the
308308
# hash of the model computed?
309-
seen = Set{jmp.VariableRef}()
310-
filtered = Vector{jmp.VariableRef}()
309+
seen = Set{JuMP.VariableRef}()
310+
filtered = Vector{JuMP.VariableRef}()
311311
for var in variables
312312
if !(var in seen)
313313
push!(seen, var)

src/incidence_graph.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ variables.
2323
2424
"""
2525

26-
import JuMP as jmp
27-
import MathOptInterface as moi
26+
import JuMP
2827

2928
import JuMPIn: get_equality_constraints, identify_unique_variables
3029

@@ -84,11 +83,11 @@ regardless of which variables participate in the constraints.
8483
8584
"""
8685
function get_bipartite_incidence_graph(
87-
model::jmp.Model;
86+
model::JuMP.Model;
8887
include_inequality::Bool = false,
8988
)
9089
if include_inequality
91-
constraints = jmp.all_constraints(
90+
constraints = JuMP.all_constraints(
9291
model,
9392
# TODO: Should this be an optional argument to this function?
9493
include_variable_in_set_constraints=false,
@@ -103,16 +102,16 @@ function get_bipartite_incidence_graph(
103102
return get_bipartite_incidence_graph(constraints)
104103
end
105104

106-
function get_bipartite_incidence_graph(constraints::Vector{jmp.ConstraintRef})
105+
function get_bipartite_incidence_graph(constraints::Vector{JuMP.ConstraintRef})
107106
variables = identify_unique_variables(constraints)
108107
# We could build up a variable-index map dynamically to get the incidence
109108
# in a single loop over the constraints, but this is easier to implement.
110109
return get_bipartite_incidence_graph(constraints, variables)
111110
end
112111

113112
function get_bipartite_incidence_graph(
114-
constraints::Vector{jmp.ConstraintRef},
115-
variables::Vector{jmp.VariableRef},
113+
constraints::Vector{JuMP.ConstraintRef},
114+
variables::Vector{JuMP.VariableRef},
116115
)
117116
ncon = length(constraints)
118117
nvar = length(variables)
@@ -121,8 +120,8 @@ function get_bipartite_incidence_graph(
121120
con_nodes = Vector(1:ncon)
122121
var_nodes = Vector(ncon+1:ncon+nvar)
123122

124-
con_node_map = Dict{jmp.ConstraintRef, Int64}(zip(constraints, con_nodes))
125-
var_node_map = Dict{jmp.VariableRef, Int64}(zip(variables, var_nodes))
123+
con_node_map = Dict{JuMP.ConstraintRef, Int64}(zip(constraints, con_nodes))
124+
var_node_map = Dict{JuMP.VariableRef, Int64}(zip(variables, var_nodes))
126125

127126
# This could be violated if a variable or constraint appears multiple times.
128127
# TODO: Fail more gracefully if this happens.

0 commit comments

Comments
 (0)