From 51418a53dd6898397f51e0af4863dbec4398fa69 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 8 Jan 2026 09:38:24 +1300 Subject: [PATCH 1/2] Fix vector-valued constraint example in the README --- README.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 43e6124..3f0562f 100644 --- a/README.md +++ b/README.md @@ -440,12 +440,13 @@ julia> function constraints(result::Vector, x::Vector, grad::Matrix) 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') + # grad has shape (num variables, num constraints) + grad[:, 1] .= ∇g1 + grad[:, 2] .= ∇g2 end return end -constraints (generic function with 2 methods) +constraints (generic function with 1 method) julia> opt = NLopt.Opt(:LD_MMA, 2) Opt(LD_MMA, 2) @@ -459,10 +460,10 @@ julia> NLopt.min_objective!(opt, my_objective_fn) julia> NLopt.inequality_constraint!(opt, constraints, fill(1e-8, 2)) julia> min_f, min_x, ret = NLopt.optimize(opt, [1.234, 5.678]) -(0.5443310692851157, [0.33333332182948433, 0.29629631298907744], :XTOL_REACHED) +(0.5443310477213124, [0.3333333342139688, 0.29629628951338166], :XTOL_REACHED) julia> num_evals = NLopt.numevals(opt) -45 +18 julia> println( """ @@ -472,10 +473,10 @@ julia> println( # function evaluation : $num_evals """ ) -objective value : 0.5443310692851157 -solution : [0.33333332182948433, 0.29629631298907744] +objective value : 0.5443310477213124 +solution : [0.3333333342139688, 0.29629628951338166] solution status : XTOL_REACHED -# function evaluation : 45 +# function evaluation : 18 ``` Not all of the optimization algorithms (below) use the gradient information: for From 33674df9b246d26be765302b0951d8378528aae5 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 8 Jan 2026 09:39:30 +1300 Subject: [PATCH 2/2] Format --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3f0562f..161f610 100644 --- a/README.md +++ b/README.md @@ -441,8 +441,8 @@ julia> function constraints(result::Vector, x::Vector, grad::Matrix) result[2] = my_constraint_fn(x, ∇g2, -1, 1) if length(grad) > 0 # grad has shape (num variables, num constraints) - grad[:, 1] .= ∇g1 - grad[:, 2] .= ∇g2 + grad[:, 1] .= ∇g1 + grad[:, 2] .= ∇g2 end return end