# NLopt optimizer just return the initial value

**URL:** https://discourse.julialang.org/t/nlopt-optimizer-just-return-the-initial-value/102820
**Category:** Optimization (Mathematical)
**Created:** [August 15, 2023, 9:08am UTC](https://discourse.julialang.org/t/nlopt-optimizer-just-return-the-initial-value/102820 "2023-08-15T09:08:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![MKY](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@MKY](https://discourse.julialang.org/u/MKY)
#### Post date: [August 15, 2023, 9:08am UTC](https://discourse.julialang.org/t/nlopt-optimizer-just-return-the-initial-value/102820/1 "2023-08-15T09:08:10Z")

</div>

```julia
opt = Opt(:LD_SLSQP,4)
opt.lower_bounds = [0,0,0,0]
opt.ftol_abs = 1e-20
opt.min_objective = risk_budget_objective
equality_constraint!(opt,weights -> total_weight_constraint(x))
(optf,optx,ret) = optimize(opt,[0.25,0.25,0.25,0.25]) 

```

Sorrry for asking a silly question…  
There must be something wrong with the arguments passed to the function but I couldn’t figure it out  
The returned value of optx and numevals are the initial value and 1, relatively.  
Thank you for your patience and kindness to read this.

The following is for reference, if needed:

```julia
using CSV,DataFrames,StatsBase,Statistics,LinearAlgebra,NLopt

df = DataFrame(CSV.File("D:\\Julia\\data.csv";select=["000852.SH","H11008.CSI","AU9999.SGE","CBA00301.CS"]))
dropmissing(df)
X = Matrix(df)
dfcov = cov(X)

function risk_budget_objective(weights::Vector,cov)
    cov = dfcov
    sigma = sqrt.(weights'*(cov'*weights))
    MRC = (cov'*weights)./sigma
    TRC = weights .* MRC
    delta_TRC = [sum((i .- TRC).^2) for i in TRC]
    return sum(delta_TRC)
end

function total_weight_constraint(x::Vector)
    return sum(x)-1.0
end

opt = Opt(:LD_SLSQP,4)
opt.lower_bounds = [0,0,0,0]
opt.ftol_abs = 1e-20
opt.min_objective = risk_budget_objective
equality_constraint!(opt,weights -> total_weight_constraint(x))
(optf,optx,ret) = optimize(opt,[0.25,0.25,0.25,0.25]) 

```

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [August 15, 2023, 8:49pm UTC](https://discourse.julialang.org/t/nlopt-optimizer-just-return-the-initial-value/102820/2 "2023-08-15T20:49:28Z")

</div>

> equality\_constraint!(opt,weights → total\_weight\_constraint(x))

This part isn’t correct. Your constraint functions need to compute the gradient. I didn’t test because I don’t have the `data.csv`, but this should point you in the right direction:

```julia
function total_weight_constraint(x::Vector, grad::Vector)
    if length(grad) > 0
        grad .= 1.0 # Needs to be the actual gradient. Change if your function changes
    end
    return sum(x) - 1.0
end
equality_constraint!(opt, total_weight_constraint)

```

---

<div class="post-metadata">

### Author: ![MKY](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@MKY](https://discourse.julialang.org/u/MKY)
#### Post date: [August 16, 2023, 2:14am UTC](https://discourse.julialang.org/t/nlopt-optimizer-just-return-the-initial-value/102820/3 "2023-08-16T02:14:55Z")

</div>

Thank you for replying. I added gradient argument to my constraint function, however the optimization function is still returning the initial value. Should I also use gradient argument in my objective function?

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [August 16, 2023, 2:25am UTC](https://discourse.julialang.org/t/nlopt-optimizer-just-return-the-initial-value/102820/4 "2023-08-16T02:25:19Z")

</div>

Oops. Yes, I didn’t look at the objective. You need to follow:

> **[GitHub - JuliaOpt/NLopt.jl: Package to call the NLopt nonlinear-optimization...](https://github.com/JuliaOpt/NLopt.jl#tutorial)**
>
> Package to call the NLopt nonlinear-optimization library from the Julia language - GitHub - JuliaOpt/NLopt.jl: Package to call the NLopt nonlinear-optimization library from the Julia language

The objective function must be a function which accepts two arguments, `x` and `grad`, and if `length(grad) > 0`, fills in `grad` with the gradient of the function.
