45
45
46
46
function _info (
47
47
model:: Optimizer ,
48
- c:: MOI.ConstraintIndex{MOI.ScalarNonlinearFunction,S } ,
49
- ) where {S}
48
+ c:: MOI.ConstraintIndex{MOI.ScalarNonlinearFunction} ,
49
+ )
50
50
if haskey (model. nl_constraint_info, c. value)
51
51
return model. nl_constraint_info[c. value]
52
52
end
@@ -62,7 +62,8 @@ function _add_expression_tree_constant(
62
62
)
63
63
append! (opcode, GRB_OPCODE_CONSTANT)
64
64
append! (data, coeff)
65
- return append! (parent, index)
65
+ append! (parent, index)
66
+ return
66
67
end
67
68
68
69
function _add_expression_tree_variable (
@@ -78,15 +79,13 @@ function _add_expression_tree_variable(
78
79
append! (opcode, GRB_OPCODE_MULTIPLY)
79
80
append! (data, - 1.0 )
80
81
append! (parent, parent_index)
81
-
82
82
_add_expression_tree_constant (
83
83
opcode,
84
84
data,
85
85
parent,
86
86
coeff,
87
87
current_index,
88
88
)
89
-
90
89
append! (opcode, GRB_OPCODE_VARIABLE)
91
90
append! (data, var_index)
92
91
append! (parent, current_index)
104
103
# Check if a nonlinear is actually just a constant
105
104
function _check_nonlinear_constant (expr)
106
105
return (
107
- typeof (expr) == MOI. ScalarNonlinearFunction &&
108
- expr. head in [:+ , :- ] &&
106
+ expr. head in (:+ , :- ) &&
109
107
length (expr. args) == 1 &&
110
- typeof ( expr. args[1 ]) == Float64
108
+ expr. args[1 ] isa Float64
111
109
)
112
110
end
113
111
@@ -116,17 +114,19 @@ end
116
114
# 1. constant * var
117
115
# 2. +/- var
118
116
# 3. +/- constant * var
119
- function _check_nonlinear_singlevar (expr)
120
- return typeof (expr) == MOI. ScalarNonlinearFunction && (
117
+ _check_nonlinear_singlevar (:: Any ) = false
118
+
119
+ function _check_nonlinear_singlevar (expr:: MOI.ScalarNonlinearFunction )
120
+ return (
121
121
( # Case 1.
122
122
expr. head == :* &&
123
123
length (expr. args) == 2 &&
124
- typeof ( expr. args[2 ]) == MOI. VariableIndex
124
+ expr. args[2 ] isa MOI. VariableIndex
125
125
) || ( # Cases 2. and 3.
126
- expr. head in [ :+ , :- ] &&
126
+ expr. head in ( :+ , :- ) &&
127
127
length (expr. args) == 1 &&
128
128
(
129
- typeof ( expr. args[1 ]) == MOI. VariableIndex ||
129
+ expr. args[1 ] isa MOI. VariableIndex ||
130
130
_check_nonlinear_singlevar (expr. args[1 ])
131
131
)
132
132
)
@@ -140,15 +140,12 @@ function _process_nonlinear(
140
140
data:: Vector{Cdouble} ,
141
141
parent:: Vector{Cint} ,
142
142
)
143
- # TODO : use type hints here instead of Any
144
143
stack = Vector {Tuple{Any,Cint}} ([(f, Cint (- 1 ))])
145
144
current_index = Cint (- 1 )
146
-
147
- while length (stack) != 0
145
+ while ! isempty (stack)
148
146
current_index += Cint (1 )
149
147
s, parent_index = pop! (stack)
150
-
151
- if typeof (s) == MOI. ScalarNonlinearFunction
148
+ if s isa MOI. ScalarNonlinearFunction
152
149
ret = get (_OPCODE_MAP, s. head, nothing )
153
150
if ret === nothing
154
151
throw (MOI. UnsupportedNonlinearOperator (s. head))
@@ -162,11 +159,10 @@ function _process_nonlinear(
162
159
append! (data, - 1.0 )
163
160
append! (parent, parent_index)
164
161
end
165
-
166
162
for expr in reverse (s. args)
167
163
push! (stack, (expr, current_index))
168
164
end
169
- elseif typeof (s) == MOI. VariableIndex
165
+ elseif s isa MOI. VariableIndex
170
166
_add_expression_tree_variable (
171
167
opcode,
172
168
data,
@@ -176,14 +172,14 @@ function _process_nonlinear(
176
172
current_index,
177
173
parent_index,
178
174
)
179
- elseif typeof (s) == MOI. ScalarAffineFunction{Float64}
175
+ elseif s isa MOI. ScalarAffineFunction{Float64}
180
176
if length (s. terms) > 1
181
177
append! (opcode, GRB_OPCODE_PLUS)
182
178
append! (data, - 1.0 )
183
179
append! (parent, parent_index)
184
180
parent_index += Cint (1 )
185
181
end
186
- if s. constant != 0.0
182
+ if ! iszero ( s. constant)
187
183
append! (opcode, GRB_OPCODE_CONSTANT)
188
184
append! (data, s. constant)
189
185
append! (parent, parent_index)
@@ -203,9 +199,9 @@ function _process_nonlinear(
203
199
)
204
200
current_index += Cint (1 )
205
201
end
206
- elseif typeof (s) == Float64
202
+ elseif s isa Float64
207
203
_add_expression_tree_constant (opcode, data, parent, s, parent_index)
208
- elseif typeof (s) == Int64
204
+ elseif s isa Int
209
205
_add_expression_tree_constant (
210
206
opcode,
211
207
data,
@@ -217,7 +213,6 @@ function _process_nonlinear(
217
213
throw (MOI. UnsupportedAttribute (s))
218
214
end
219
215
end
220
-
221
216
return
222
217
end
223
218
@@ -226,18 +221,14 @@ function MOI.add_constraint(
226
221
f:: MOI.ScalarNonlinearFunction ,
227
222
s:: _SCALAR_SETS ,
228
223
)
229
- opcode = Vector {Cint} ()
230
- data = Vector {Cdouble} ()
231
- parent = Vector {Cint} ()
232
-
224
+ opcode = Cint[]
225
+ data = Cdouble[]
226
+ parent = Cint[]
233
227
sense, rhs = _sense_and_rhs (s)
234
-
235
228
_process_nonlinear (model, f, opcode, data, parent)
236
-
237
229
# Add resultant variable
238
230
vi, ci = MOI. add_constrained_variable (model, s)
239
231
resvar_index = c_column (model, vi)
240
-
241
232
ret = GRBaddgenconstrNL (
242
233
model,
243
234
C_NULL ,
@@ -248,7 +239,6 @@ function MOI.add_constraint(
248
239
parent,
249
240
)
250
241
_check_ret (model, ret)
251
- # GRBwrite(model, "checkjl.lp")
252
242
_require_update (model, model_change = true )
253
243
model. last_constraint_index += 1
254
244
model. nl_constraint_info[model. last_constraint_index] =
@@ -263,7 +253,7 @@ function MOI.is_valid(
263
253
c:: MOI.ConstraintIndex{MOI.ScalarNonlinearFunction,S} ,
264
254
) where {S}
265
255
info = get (model. nl_constraint_info, c. value, nothing )
266
- return info != = nothing && typeof ( info. set) == S
256
+ return info != = nothing && info. set isa S
267
257
end
268
258
269
259
function MOI. delete (
@@ -272,8 +262,7 @@ function MOI.delete(
272
262
) where {S}
273
263
info = _info (model, c)
274
264
_update_if_necessary (model)
275
-
276
- ret = GRBdelgenconstrs (model, 1 , [Cint (info. row - 1 )])
265
+ ret = GRBdelgenconstrs (model, 1 , Ref (Cint (info. row - 1 )))
277
266
_check_ret (model, ret)
278
267
for (_, info_2) in model. nl_constraint_info
279
268
if info_2. row > info. row
@@ -290,51 +279,45 @@ end
290
279
291
280
function MOI. delete (
292
281
model:: Optimizer ,
293
- cs:: Vector{<:MOI.ConstraintIndex{MOI.ScalarNonlinearFunction,S }} ,
294
- ) where {S}
282
+ cs:: Vector{<:MOI.ConstraintIndex{MOI.ScalarNonlinearFunction}} ,
283
+ )
295
284
rows_to_delete = sort! ([Cint (_info (model, c). row - 1 ) for c in cs])
296
- println (rows_to_delete)
297
285
_update_if_necessary (model)
298
286
ret = GRBdelgenconstrs (model, length (rows_to_delete), rows_to_delete)
299
287
_check_ret (model, ret)
300
-
301
288
for (_, info) in model. nl_constraint_info
302
289
info. row -= searchsortedlast (rows_to_delete, info. row - 1 )
303
290
end
304
-
305
291
model. name_to_constraint_index = nothing
306
-
307
292
# Delete resultant variables
308
293
resvars = [_info (model, c). resvar for c in cs]
309
294
MOI. delete (model, resvars)
310
-
311
295
cs_values = sort! (getfield .(cs, :value ))
312
296
filter! (model. nl_constraint_info) do pair
313
297
return isempty (searchsorted (cs_values, pair. first))
314
298
end
315
-
316
299
_require_update (model, model_change = true )
317
300
return
318
301
end
319
302
320
303
function MOI. get (
321
304
model:: Optimizer ,
322
305
:: MOI.ConstraintName ,
323
- c:: MOI.ConstraintIndex{MOI.ScalarNonlinearFunction,S } ,
324
- ) where {S}
306
+ c:: MOI.ConstraintIndex{MOI.ScalarNonlinearFunction} ,
307
+ )
325
308
return _info (model, c). name
326
309
end
327
310
328
311
function MOI. set (
329
312
model:: Optimizer ,
330
313
:: MOI.ConstraintName ,
331
- c:: MOI.ConstraintIndex{MOI.ScalarNonlinearFunction,S } ,
314
+ c:: MOI.ConstraintIndex{MOI.ScalarNonlinearFunction} ,
332
315
name:: String ,
333
- ) where {S}
316
+ )
334
317
_update_if_necessary (model, force = true )
335
318
info = _info (model, c)
336
319
info. name = name
337
- if ! isempty (name)
320
+ if length (name) <= GRB_MAX_NAMELEN
338
321
ret = GRBsetstrattrelement (
339
322
model,
340
323
" GenConstrName" ,
0 commit comments