Skip to content

Commit fe90965

Browse files
torressaodow
andauthored
Apply suggestions from code review
Co-authored-by: Oscar Dowson <odow@users.noreply.github.com>
1 parent 121169c commit fe90965

File tree

1 file changed

+32
-49
lines changed

1 file changed

+32
-49
lines changed

src/MOI_wrapper/MOI_nonlinear.jl

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ end
4545

4646
function _info(
4747
model::Optimizer,
48-
c::MOI.ConstraintIndex{MOI.ScalarNonlinearFunction,S},
49-
) where {S}
48+
c::MOI.ConstraintIndex{MOI.ScalarNonlinearFunction},
49+
)
5050
if haskey(model.nl_constraint_info, c.value)
5151
return model.nl_constraint_info[c.value]
5252
end
@@ -62,7 +62,8 @@ function _add_expression_tree_constant(
6262
)
6363
append!(opcode, GRB_OPCODE_CONSTANT)
6464
append!(data, coeff)
65-
return append!(parent, index)
65+
append!(parent, index)
66+
return
6667
end
6768

6869
function _add_expression_tree_variable(
@@ -78,15 +79,13 @@ function _add_expression_tree_variable(
7879
append!(opcode, GRB_OPCODE_MULTIPLY)
7980
append!(data, -1.0)
8081
append!(parent, parent_index)
81-
8282
_add_expression_tree_constant(
8383
opcode,
8484
data,
8585
parent,
8686
coeff,
8787
current_index,
8888
)
89-
9089
append!(opcode, GRB_OPCODE_VARIABLE)
9190
append!(data, var_index)
9291
append!(parent, current_index)
@@ -104,10 +103,9 @@ end
104103
# Check if a nonlinear is actually just a constant
105104
function _check_nonlinear_constant(expr)
106105
return (
107-
typeof(expr) == MOI.ScalarNonlinearFunction &&
108-
expr.head in [:+, :-] &&
106+
expr.head in (:+, :-) &&
109107
length(expr.args) == 1 &&
110-
typeof(expr.args[1]) == Float64
108+
expr.args[1] isa Float64
111109
)
112110
end
113111

@@ -116,17 +114,19 @@ end
116114
# 1. constant * var
117115
# 2. +/- var
118116
# 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 (
121121
( # Case 1.
122122
expr.head == :* &&
123123
length(expr.args) == 2 &&
124-
typeof(expr.args[2]) == MOI.VariableIndex
124+
expr.args[2] isa MOI.VariableIndex
125125
) || ( # Cases 2. and 3.
126-
expr.head in [:+, :-] &&
126+
expr.head in (:+, :-) &&
127127
length(expr.args) == 1 &&
128128
(
129-
typeof(expr.args[1]) == MOI.VariableIndex ||
129+
expr.args[1] isa MOI.VariableIndex ||
130130
_check_nonlinear_singlevar(expr.args[1])
131131
)
132132
)
@@ -140,15 +140,12 @@ function _process_nonlinear(
140140
data::Vector{Cdouble},
141141
parent::Vector{Cint},
142142
)
143-
# TODO: use type hints here instead of Any
144143
stack = Vector{Tuple{Any,Cint}}([(f, Cint(-1))])
145144
current_index = Cint(-1)
146-
147-
while length(stack) != 0
145+
while !isempty(stack)
148146
current_index += Cint(1)
149147
s, parent_index = pop!(stack)
150-
151-
if typeof(s) == MOI.ScalarNonlinearFunction
148+
if s isa MOI.ScalarNonlinearFunction
152149
ret = get(_OPCODE_MAP, s.head, nothing)
153150
if ret === nothing
154151
throw(MOI.UnsupportedNonlinearOperator(s.head))
@@ -162,11 +159,10 @@ function _process_nonlinear(
162159
append!(data, -1.0)
163160
append!(parent, parent_index)
164161
end
165-
166162
for expr in reverse(s.args)
167163
push!(stack, (expr, current_index))
168164
end
169-
elseif typeof(s) == MOI.VariableIndex
165+
elseif s isa MOI.VariableIndex
170166
_add_expression_tree_variable(
171167
opcode,
172168
data,
@@ -176,14 +172,14 @@ function _process_nonlinear(
176172
current_index,
177173
parent_index,
178174
)
179-
elseif typeof(s) == MOI.ScalarAffineFunction{Float64}
175+
elseif s isa MOI.ScalarAffineFunction{Float64}
180176
if length(s.terms) > 1
181177
append!(opcode, GRB_OPCODE_PLUS)
182178
append!(data, -1.0)
183179
append!(parent, parent_index)
184180
parent_index += Cint(1)
185181
end
186-
if s.constant != 0.0
182+
if !iszero(s.constant)
187183
append!(opcode, GRB_OPCODE_CONSTANT)
188184
append!(data, s.constant)
189185
append!(parent, parent_index)
@@ -203,9 +199,9 @@ function _process_nonlinear(
203199
)
204200
current_index += Cint(1)
205201
end
206-
elseif typeof(s) == Float64
202+
elseif s isa Float64
207203
_add_expression_tree_constant(opcode, data, parent, s, parent_index)
208-
elseif typeof(s) == Int64
204+
elseif s isa Int
209205
_add_expression_tree_constant(
210206
opcode,
211207
data,
@@ -217,7 +213,6 @@ function _process_nonlinear(
217213
throw(MOI.UnsupportedAttribute(s))
218214
end
219215
end
220-
221216
return
222217
end
223218

@@ -226,18 +221,14 @@ function MOI.add_constraint(
226221
f::MOI.ScalarNonlinearFunction,
227222
s::_SCALAR_SETS,
228223
)
229-
opcode = Vector{Cint}()
230-
data = Vector{Cdouble}()
231-
parent = Vector{Cint}()
232-
224+
opcode = Cint[]
225+
data = Cdouble[]
226+
parent = Cint[]
233227
sense, rhs = _sense_and_rhs(s)
234-
235228
_process_nonlinear(model, f, opcode, data, parent)
236-
237229
# Add resultant variable
238230
vi, ci = MOI.add_constrained_variable(model, s)
239231
resvar_index = c_column(model, vi)
240-
241232
ret = GRBaddgenconstrNL(
242233
model,
243234
C_NULL,
@@ -248,7 +239,6 @@ function MOI.add_constraint(
248239
parent,
249240
)
250241
_check_ret(model, ret)
251-
# GRBwrite(model, "checkjl.lp")
252242
_require_update(model, model_change = true)
253243
model.last_constraint_index += 1
254244
model.nl_constraint_info[model.last_constraint_index] =
@@ -263,7 +253,7 @@ function MOI.is_valid(
263253
c::MOI.ConstraintIndex{MOI.ScalarNonlinearFunction,S},
264254
) where {S}
265255
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
267257
end
268258

269259
function MOI.delete(
@@ -272,8 +262,7 @@ function MOI.delete(
272262
) where {S}
273263
info = _info(model, c)
274264
_update_if_necessary(model)
275-
276-
ret = GRBdelgenconstrs(model, 1, [Cint(info.row - 1)])
265+
ret = GRBdelgenconstrs(model, 1, Ref(Cint(info.row - 1)))
277266
_check_ret(model, ret)
278267
for (_, info_2) in model.nl_constraint_info
279268
if info_2.row > info.row
@@ -290,51 +279,45 @@ end
290279

291280
function MOI.delete(
292281
model::Optimizer,
293-
cs::Vector{<:MOI.ConstraintIndex{MOI.ScalarNonlinearFunction,S}},
294-
) where {S}
282+
cs::Vector{<:MOI.ConstraintIndex{MOI.ScalarNonlinearFunction}},
283+
)
295284
rows_to_delete = sort!([Cint(_info(model, c).row - 1) for c in cs])
296-
println(rows_to_delete)
297285
_update_if_necessary(model)
298286
ret = GRBdelgenconstrs(model, length(rows_to_delete), rows_to_delete)
299287
_check_ret(model, ret)
300-
301288
for (_, info) in model.nl_constraint_info
302289
info.row -= searchsortedlast(rows_to_delete, info.row - 1)
303290
end
304-
305291
model.name_to_constraint_index = nothing
306-
307292
# Delete resultant variables
308293
resvars = [_info(model, c).resvar for c in cs]
309294
MOI.delete(model, resvars)
310-
311295
cs_values = sort!(getfield.(cs, :value))
312296
filter!(model.nl_constraint_info) do pair
313297
return isempty(searchsorted(cs_values, pair.first))
314298
end
315-
316299
_require_update(model, model_change = true)
317300
return
318301
end
319302

320303
function MOI.get(
321304
model::Optimizer,
322305
::MOI.ConstraintName,
323-
c::MOI.ConstraintIndex{MOI.ScalarNonlinearFunction,S},
324-
) where {S}
306+
c::MOI.ConstraintIndex{MOI.ScalarNonlinearFunction},
307+
)
325308
return _info(model, c).name
326309
end
327310

328311
function MOI.set(
329312
model::Optimizer,
330313
::MOI.ConstraintName,
331-
c::MOI.ConstraintIndex{MOI.ScalarNonlinearFunction,S},
314+
c::MOI.ConstraintIndex{MOI.ScalarNonlinearFunction},
332315
name::String,
333-
) where {S}
316+
)
334317
_update_if_necessary(model, force = true)
335318
info = _info(model, c)
336319
info.name = name
337-
if !isempty(name)
320+
if length(name) <= GRB_MAX_NAMELEN
338321
ret = GRBsetstrattrelement(
339322
model,
340323
"GenConstrName",

0 commit comments

Comments
 (0)