21
21
Utility functions for identifying JuMP constraints that define equalities.
22
22
23
23
"""
24
- module GetEquality
25
- import JuMP as jmp
26
- import MathOptInterface as moi
27
-
28
- function get_set_of_constraint (
29
- model:: jmp .Model ,
30
- constraint:: jmp .ConstraintRef ,
31
- index:: moi .ConstraintIndex ,
24
+
25
+ import JuMP
26
+ import MathOptInterface as MOI
27
+
28
+ function _get_set_of_constraint (
29
+ model:: JuMP .Model ,
30
+ constraint:: JuMP .ConstraintRef ,
31
+ index:: MOI .ConstraintIndex ,
32
32
)
33
- set = moi . get (model, moi . ConstraintSet (), constraint)
33
+ set = MOI . get (model, MOI . ConstraintSet (), constraint)
34
34
return set
35
35
end
36
36
37
- function get_set_of_constraint (
38
- model:: jmp .Model ,
39
- constraint:: jmp .ConstraintRef ,
40
- index:: moi .Nonlinear.ConstraintIndex ,
37
+ function _get_set_of_constraint (
38
+ model:: JuMP .Model ,
39
+ constraint:: JuMP .ConstraintRef ,
40
+ index:: MOI .Nonlinear.ConstraintIndex ,
41
41
)
42
42
nl_con = model. nlp_model. constraints[index]
43
43
return nl_con. set
44
44
end
45
45
46
- function set_implies_equality (set:: moi .EqualTo ):: Bool
46
+ function set_implies_equality (set:: MOI .EqualTo ):: Bool
47
47
return true
48
48
end
49
49
50
50
# Q: How does Julia determine if an argument has the right type.
51
51
# I.e. why is this interpreted as "set must be a subtype of Union(...)"
52
52
# rather than "set must be an 'instance' of Union()".
53
53
# function set_implies_equality(
54
- # set::Union{moi .GreaterThan, moi .LessThan},
54
+ # set::Union{MOI .GreaterThan, MOI .LessThan},
55
55
# # Note that ^this works, but this doesn't
56
- # # set::Type{<:Union{moi .GreaterThan, moi .LessThan}},
56
+ # # set::Type{<:Union{MOI .GreaterThan, MOI .LessThan}},
57
57
# )::Bool
58
58
# return false
59
59
# end
60
60
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)))
63
63
end
64
64
65
65
# NOTE: Have not implemented tolerance arg in any calling function yet.
66
66
function set_implies_equality (
67
- set:: moi .Interval ,
67
+ set:: MOI .Interval ,
68
68
tolerance:: Float64 = Float64 (0 )
69
69
):: Bool
70
70
return abs (set. upper - set. lower) <= tolerance
75
75
#
76
76
# I intend this to catch anything that is a subtype of ConstraintSet.
77
77
# 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.
79
79
# This is non-intuitive for me...
80
- # So this won't catch arbitrary moi Sets...
80
+ # So this won't catch arbitrary MOI Sets...
81
81
# The right type to use is actually AbstractSet?
82
82
# Nope. This doesn't catch GreatherThan/LessThan
83
83
# ^ Wrong. Actually it does, but my syntax was wrong. I guess providing
@@ -103,7 +103,7 @@ types of constraints in [`is_equality`](@ref) and
103
103
"""
104
104
function set_implies_equality (
105
105
set:: T
106
- ):: Bool where T<: moi .AbstractSet
106
+ ):: Bool where T<: MOI .AbstractSet
107
107
return false
108
108
end
109
109
@@ -113,17 +113,17 @@ end
113
113
Detect whether a constraint is an equality constraint.
114
114
115
115
"""
116
- function is_equality (constraint:: jmp .ConstraintRef ):: Bool
116
+ function is_equality (constraint:: JuMP .ConstraintRef ):: Bool
117
117
model = constraint. model
118
118
index = constraint. index
119
- set = get_set_of_constraint (model, constraint, index)
119
+ set = _get_set_of_constraint (model, constraint, index)
120
120
return set_implies_equality (set)
121
121
end
122
122
123
123
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} ()
127
127
for con in constraints
128
128
if is_equality (con)
129
129
push! (eq_cons, con)
137
137
138
138
Return a vector of equality constraints in the provided model.
139
139
140
- This function is also accessible via the `JuMPIn` module.
141
-
142
140
# Example
143
141
```julia-repl
144
142
julia> using JuMP
@@ -153,12 +151,10 @@ julia> display(eq_cons)
153
151
```
154
152
155
153
"""
156
- function get_equality_constraints (model:: jmp .Model ):: Vector{jmp .ConstraintRef}
157
- constraints = jmp . all_constraints (
154
+ function get_equality_constraints (model:: JuMP .Model ):: Vector{JuMP .ConstraintRef}
155
+ constraints = JuMP . all_constraints (
158
156
model,
159
157
include_variable_in_set_constraints= false ,
160
158
)
161
159
return get_equality_constraints (constraints)
162
160
end
163
-
164
- end # module get_equality
0 commit comments