Skip to content

Commit 7efb9e2

Browse files
committed
Fix errors in parser_VQF
1 parent 02317b4 commit 7efb9e2

File tree

5 files changed

+55
-27
lines changed

5 files changed

+55
-27
lines changed

src/utils.jl

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -291,53 +291,53 @@ function parser_VQF(fun, set, nvar, qcons, quad_lcon, quad_ucon, index_map)
291291
_index(v::MOI.VariableIndex) = index_map[v].value
292292

293293
ncon = length(fun.constants)
294-
for i = 1:ncon
294+
for k = 1:ncon
295295
b = spzeros(Float64, nvar)
296296
rows = Int[]
297297
cols = Int[]
298298
vals = Float64[]
299299

300-
quadratic_terms = fun.quadratic_terms[i]
301-
affine_terms = fun.affine_terms[i]
302-
constant = fun.constants[i]
303-
304300
# Parse a VectorAffineTerm{Float64}(output_index, scalar_term)
305-
for term in affine_terms
306-
@assert term.output_index == i
307-
b[_index(term.scalar_term.variable)] = term.scalar_term.coefficient
301+
for affine_term in fun.affine_terms
302+
if affine_term.output_index == k
303+
b[_index(affine_term.scalar_term.variable)] = affine_term.scalar_term.coefficient
304+
end
308305
end
309306

310307
# Parse a VectorQuadraticTerm{Float64}(output_index, scalar_term)
311-
for term in quadratic_terms
312-
@assert term.output_index == i
313-
i = _index(term.scalar_term.variable_1)
314-
j = _index(term.scalar_term.variable_2)
315-
if i j
316-
push!(rows, i)
317-
push!(cols, j)
318-
else
319-
push!(rows, j)
320-
push!(cols, i)
308+
for quadratic_term in fun.quadratic_terms
309+
if quadratic_term.output_index == k
310+
i = _index(quadratic_term.scalar_term.variable_1)
311+
j = _index(quadratic_term.scalar_term.variable_2)
312+
if i j
313+
push!(rows, i)
314+
push!(cols, j)
315+
else
316+
push!(rows, j)
317+
push!(cols, i)
318+
end
319+
push!(vals, quadratic_term.scalar_term.coefficient)
321320
end
322-
push!(vals, term.scalar_term.coefficient)
323321
end
324322

323+
constant = fun.constants[k]
324+
325325
if typeof(set) in (MOI.Nonnegatives, MOI.Zeros)
326-
append!(lin_lcon, constant)
326+
append!(quad_lcon, constant)
327327
else
328-
append!(lin_lcon, -Inf)
328+
append!(quad_lcon, -Inf)
329329
end
330330

331331
if typeof(set) in (MOI.Nonpositives, MOI.Zeros)
332-
append!(lin_ucon, -constant)
332+
append!(quad_ucon, -constant)
333333
else
334-
append!(lin_ucon, Inf)
334+
append!(quad_ucon, Inf)
335335
end
336336

337337
A = COO(rows, cols, vals)
338338
g = unique(vcat(rows, b.nzind)) # sparsity pattern of Ax + b
339339
nnzg = length(g)
340-
dg = Dict{Int,Int}(g[i] => i for i = 1:nnzg)
340+
dg = Dict{Int,Int}(g[p] => p for p=1:nnzg)
341341
nnzh = length(vals)
342342
qcon = QuadraticConstraint(A, b, g, dg, nnzg, nnzh)
343343
push!(qcons, qcon)

test/nlp_problems/quadcon.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function quadcon()
2+
nlp = Model()
3+
4+
@variable(nlp, x[1:3])
5+
6+
@constraint(nlp, [x[1], x[1]^2 + x[1] * x[2] + 3.0] in MOI.Nonnegatives(2))
7+
@constraint(nlp, [x[2], x[2]^2 + x[2] * x[3] + 4.0] in MOI.Nonpositives(2))
8+
@constraint(nlp, [x[3], x[3]^2 + x[3] * x[1] + 5.0] in MOI.Zeros(2))
9+
10+
@objective(nlp, Min, x[1] * x[2] * exp(x[3]))
11+
12+
return nlp
13+
end

test/nls_problems/nlsqc.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function nlsqc()
2+
nls = Model()
3+
4+
@variable(nls, x[1:3])
5+
6+
@constraint(nls, [x[1], x[1]^2 + x[1] * x[2] + 3.0] in MOI.Nonnegatives(2))
7+
@constraint(nls, [x[2], x[2]^2 + x[2] * x[3] + 4.0] in MOI.Nonpositives(2))
8+
@constraint(nls, [x[3], x[3]^2 + x[3] * x[1] + 5.0] in MOI.Zeros(2))
9+
10+
@NLexpression(nls, F[i = 1:3], x[i]^2 - i^2)
11+
12+
return MathOptNLSModel(nls, F, name = "nlsqc")
13+
end

test/runtests.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ using Test, Printf
44

55
nlp_problems = setdiff(NLPModelsTest.nlp_problems, ["MGH01Feas"])
66
nls_problems = NLPModelsTest.nls_problems
7-
extra_nls_problems = ["HS30", "HS43", "MGH07", "nlsnohesspb"]
87

9-
for problem in lowercase.(nlp_problems ["nohesspb", "hs61", "hs100", "hs219"])
8+
extra_nlp_problems = ["nohesspb", "hs61", "hs100", "hs219", "quadcon"]
9+
extra_nls_problems = ["nlsnohesspb", "HS30", "HS43", "MGH07", "nlsqc"]
10+
11+
for problem in lowercase.(nlp_problems extra_nlp_problems)
1012
include(joinpath("nlp_problems", "$problem.jl"))
1113
end
1214

test/test_moi_nlp_model.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ println("Testing MathOptNLPModel")
1111
"‖c(x₀)‖"
1212
)
1313
# Test that every problem can be instantiated.
14-
for prob in Symbol.(lowercase.(nlp_problems ["nohesspb", "hs61", "hs100", "hs219"]))
14+
for prob in Symbol.(lowercase.(nlp_problems extra_nlp_problems))
1515
prob_fn = eval(prob)
1616
nlp = MathOptNLPModel(prob_fn(), hessian = (prob != :nohesspb))
1717
n = nlp.meta.nvar

0 commit comments

Comments
 (0)