Skip to content

Documentation on vector-valued constraints (example) #271

Description

@OskarBergmann

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).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions