17
17
# This software is distributed under the 3-clause BSD license.
18
18
# ___________________________________________________________________________
19
19
20
- module TestIdentifyVariables
21
- import JuMP as jmp
22
- import MathOptInterface as moi
20
+ import JuMP
21
+ import MathOptInterface as MOI
23
22
using Test: @testset , @test , @test_throws
24
23
using JuMPIn: identify_unique_variables
25
24
26
25
# Local import of JuMP models for testing
27
- include (" models.jl" )
28
- using . Models: make_degenerate_flow_model
29
-
26
+ include (" models.jl" ) # make_degenerate_flow_model
30
27
31
28
function test_linear ()
32
29
m = make_degenerate_flow_model ()
@@ -55,9 +52,9 @@ function test_nonlinear()
55
52
end
56
53
57
54
function test_nonlinear_with_potential_duplicates ()
58
- m = jmp . Model ()
59
- @jmp . variable (m, var[1 : 2 ])
60
- @jmp . NLconstraint (m, con, var[1 ]^ 3 + var[1 ]* var[2 ] == 1.0 )
55
+ m = JuMP . Model ()
56
+ @JuMP . variable (m, var[1 : 2 ])
57
+ @JuMP . NLconstraint (m, con, var[1 ]^ 3 + var[1 ]* var[2 ] == 1.0 )
61
58
variables = identify_unique_variables (con)
62
59
@test (length (variables) == 2 )
63
60
@test (Set (variables) == Set ([m[:var ][1 ], m[:var ][2 ]]))
76
73
77
74
function test_several_constraints_with_ineq ()
78
75
m = make_degenerate_flow_model ()
79
- @jmp . constraint (m, ineq1, m[:flow_comp ][2 ] >= 1.0 )
80
- @jmp . constraint (m, ineq2, m[:flow_comp ][1 ]^ 2 + m[:flow_comp ][3 ]^ 2 <= 1.0 )
76
+ @JuMP . constraint (m, ineq1, m[:flow_comp ][2 ] >= 1.0 )
77
+ @JuMP . constraint (m, ineq2, m[:flow_comp ][1 ]^ 2 + m[:flow_comp ][3 ]^ 2 <= 1.0 )
81
78
constraints = [
82
79
m[:comp_flow_eqn ][1 ],
83
80
m[:sum_comp_eqn ],
102
99
103
100
function test_model ()
104
101
m = make_degenerate_flow_model ()
105
- @jmp . variable (m, dummy)
106
- @jmp . NLconstraint (m, dummy^ 3.0 <= 5 )
102
+ @JuMP . variable (m, dummy)
103
+ @JuMP . NLconstraint (m, dummy^ 3.0 <= 5 )
107
104
# Note that include_inequalities=false by default.
108
105
variables = identify_unique_variables (m)
109
106
pred_var_set = Set ([
122
119
123
120
function test_model_with_ineq ()
124
121
m = make_degenerate_flow_model ()
125
- @jmp . variable (m, dummy)
126
- @jmp . NLconstraint (m, dummy^ 3.0 <= 5 )
122
+ @JuMP . variable (m, dummy)
123
+ @JuMP . NLconstraint (m, dummy^ 3.0 <= 5 )
127
124
variables = identify_unique_variables (m, include_inequality= true )
128
125
pred_var_set = Set ([
129
126
m[:dummy ],
@@ -141,20 +138,20 @@ function test_model_with_ineq()
141
138
end
142
139
143
140
function test_function_with_variable_squared ()
144
- m = jmp . Model ()
145
- @jmp . variable (m, dummy1)
146
- @jmp . variable (m, dummy2)
147
- @jmp . constraint (m, dummy_con, dummy1^ 2 + dummy1* dummy2 == 2.0 )
148
- fcn = moi . get (m, moi . ConstraintFunction (), m[:dummy_con ])
141
+ m = JuMP . Model ()
142
+ @JuMP . variable (m, dummy1)
143
+ @JuMP . variable (m, dummy2)
144
+ @JuMP . constraint (m, dummy_con, dummy1^ 2 + dummy1* dummy2 == 2.0 )
145
+ fcn = MOI . get (m, MOI . ConstraintFunction (), m[:dummy_con ])
149
146
variables = identify_unique_variables (fcn)
150
147
@test (length (variables) == 2 )
151
148
@test (Set (variables) == Set ([m[:dummy1 ]. index, m[:dummy2 ]. index]))
152
149
end
153
150
154
151
function test_model_bad_constr ()
155
152
m = make_degenerate_flow_model ()
156
- @jmp . variable (m, dummy[1 : 2 ])
157
- @jmp . constraint (m, vectorcon, dummy in moi . Nonnegatives (2 ))
153
+ @JuMP . variable (m, dummy[1 : 2 ])
154
+ @JuMP . constraint (m, vectorcon, dummy in MOI . Nonnegatives (2 ))
158
155
@test_throws (
159
156
TypeError,
160
157
identify_unique_variables (m, include_inequality= true ),
@@ -163,15 +160,15 @@ end
163
160
164
161
function test_model_bad_constr_no_ineq ()
165
162
m = make_degenerate_flow_model ()
166
- @jmp . variable (m, dummy[1 : 2 ])
167
- @jmp . constraint (m, vectorcon, dummy in moi . Nonnegatives (2 ))
163
+ @JuMP . variable (m, dummy[1 : 2 ])
164
+ @JuMP . constraint (m, vectorcon, dummy in MOI . Nonnegatives (2 ))
168
165
# Note that we don't throw an error because we don't attempt to
169
166
# identify variables in "vectorcon" as we don't recognize it as an
170
167
# equality constraint.
171
168
#
172
169
# NOTE: We now do throw an error. I have added a method of
173
170
# set_implies_equality that throws an error if it is provided with a
174
- # subtype of moi .VectorSet. This is not ~strictly~ correct. However,
171
+ # subtype of MOI .VectorSet. This is not ~strictly~ correct. However,
175
172
# I have punted for now.
176
173
@test_throws (
177
174
TypeError,
@@ -180,37 +177,37 @@ function test_model_bad_constr_no_ineq()
180
177
end
181
178
182
179
function test_fixing_constraint ()
183
- m = jmp . Model ()
184
- @jmp . variable (m, x[1 : 2 ])
185
- jmp . fix (x[1 ], 1 )
186
- fixing_con = jmp . FixRef (x[1 ])
180
+ m = JuMP . Model ()
181
+ @JuMP . variable (m, x[1 : 2 ])
182
+ JuMP . fix (x[1 ], 1 )
183
+ fixing_con = JuMP . FixRef (x[1 ])
187
184
variables = identify_unique_variables (fixing_con)
188
185
@test length (variables) == 1
189
186
@test variables[1 ] === x[1 ]
190
187
end
191
188
192
189
function test_inequality_with_bounds ()
193
- m = jmp . Model ()
194
- @jmp . variable (m, x[1 : 2 ])
195
- @jmp . variable (m, 0 <= y[1 : 2 ])
196
- @jmp . constraint (m, x[1 ] + 2 * x[2 ]^ 2 == 1 )
190
+ m = JuMP . Model ()
191
+ @JuMP . variable (m, x[1 : 2 ])
192
+ @JuMP . variable (m, 0 <= y[1 : 2 ])
193
+ @JuMP . constraint (m, x[1 ] + 2 * x[2 ]^ 2 == 1 )
197
194
variables = identify_unique_variables (m, include_inequality = true )
198
195
pred_var_set = Set ([x[1 ], x[2 ], y[1 ], y[2 ]])
199
196
@test Set (variables) == pred_var_set
200
197
end
201
198
202
199
function test_two_constraints_same_type ()
203
- m = jmp . Model ()
204
- @jmp . variable (m, x[1 : 3 ])
205
- @jmp . constraint (m, eq1, x[1 ] + x[2 ] == 2 )
206
- @jmp . constraint (m, eq2, x[2 ] + 2 * x[3 ] == 3 )
200
+ m = JuMP . Model ()
201
+ @JuMP . variable (m, x[1 : 3 ])
202
+ @JuMP . constraint (m, eq1, x[1 ] + x[2 ] == 2 )
203
+ @JuMP . constraint (m, eq2, x[2 ] + 2 * x[3 ] == 3 )
207
204
cons = [eq1, eq2]
208
205
vars = identify_unique_variables (cons)
209
206
pred_var_set = Set ([x[1 ], x[2 ], x[3 ]])
210
207
@test Set (vars) == pred_var_set
211
208
end
212
209
213
- function runtests ()
210
+ @testset " get-equality " begin
214
211
test_linear ()
215
212
test_quadratic ()
216
213
test_nonlinear ()
@@ -225,11 +222,4 @@ function runtests()
225
222
test_fixing_constraint ()
226
223
test_inequality_with_bounds ()
227
224
test_two_constraints_same_type ()
228
- return
229
- end
230
-
231
- end
232
-
233
- if abspath (PROGRAM_FILE ) == @__FILE__
234
- TestIdentifyVariables. runtests ()
235
225
end
0 commit comments