-
Notifications
You must be signed in to change notification settings - Fork 47
Description
According to the documentation, for the constraint function f(result::Vector{Float64}, x::Vector{Float64}, grad::Matrix{Float64}),
grad is an n x m matrix where n is the number of variables (rows) and m is the number of constraints (columns) and indeed, when debugging the code, this seems to be what NLopt is passing to f.
In the full example however, grad is constructed like this:
function constraints(result::Vector, x::Vector, grad::Matrix)
∇g1 = [0.0, 0.0]
∇g2 = [0.0, 0.0]
result[1] = my_constraint_fn(x, ∇g1, 2, 0)
result[2] = my_constraint_fn(x, ∇g2, -1, 1)
if length(grad) > 0
# Note the .=. You must modify grad in-place
grad .= vcat(∇g1', ∇g1')
end
return
end
I.e., the constraints form the rows and the variables form the columns (I assume it was meant to be vcat(∇g1', ∇g2') by the way?).
The fact that the example works seems to be conincidence, the same solution is reached when replacing it with grad .= hcat(∇g1, ∇g2).