# Problem setting the Local Optimizer

**URL:** https://discourse.julialang.org/t/problem-setting-the-local-optimizer/86257
**Category:** Optimization (Mathematical)
**Tags:** nlopt
**Created:** [August 24, 2022, 11:11am UTC](https://discourse.julialang.org/t/problem-setting-the-local-optimizer/86257 "2022-08-24T11:11:24Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Yagiz\_Dereboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yagiz_dereboy/32/38259_2.png) [@Yagiz\_Dereboy](https://discourse.julialang.org/u/Yagiz_Dereboy)
#### Post date: [August 24, 2022, 11:11am UTC](https://discourse.julialang.org/t/problem-setting-the-local-optimizer/86257/1 "2022-08-24T11:11:24Z")

</div>

I have problem with the Augmented Lagrangian Method. With my syntax algorithm does not move and gets a forced stop. I am almost sure problem is with the setting of the local optimizer. I am not very familiar with the Julia syntax. let me give you my code:

```julia
function myFunc(x::Vector, grad::Vector, cost::Vector)
    if length(grad) > 0
        grad[1:22500] = cost
        grad[22501:22650] = 1000000*x[22501:22650] ./ (10000*x[22501:22650].^2 .+ 1).^2
    end
    return sum(cost.*x[1:22500]) + 50*sum(10*x[22501:22650].^2 ./ (10*x[22501:22650] .+ 0.001))
end

function inEQ_1(x::Vector, grad::Vector, index_x, index_y)
    if length(grad) > 0
        grad[150*(index_x-1) + index_y] = 1
        grad[22500 + index_y] = -1
        grad[Not(150*(index_x-1) + index_y, 22500 + index_y)] .= 0
    end
    x[150*(index_x-1) + index_y] - x[22500 + index_y]
end

function EQ_1(x::Vector, grad::Vector, index_x)
    if length(grad) > 0
        grad[150*(index_x-1)+1:150*index_x] .= 1
        grad[Not(150*(index_x-1)+1:150*index_x)] .= 0
    end
    sum(x[150*(index_x-1)+1:150*index_x]) - 1
end

function EQ_k(x::Vector, grad::Vector)
    if length(grad) > 0
        grad[1:22500] .= 0
        grad[22501:22650] .= 1
    end
    sum(grad[22501:22650]) - 3
end

opt = Opt(:LD_AUGLAG_EQ, 22650)
opt.lower_bounds = 0
opt.upper_bounds = 1
opt.xtol_rel = 1e-4

min_objective!(opt, (x, grad) -> myFunc(x,grad,cost))

opt.local_optimizer = Opt(:LD_MMA, 22650)

for i in 1:150
    for j in 1:150
        inequality_constraint!(opt, (x,g) -> inEQ_1(x,g,j,i), 1e-8)
    end
end

for i in 1:150
    equality_constraint!(opt, (x,g) -> EQ_1(x,g,i), 1e-8)
end
equality_constraint!(opt, (x,g) -> EQ_k(x,g), 1e-8)

(minf,minx,ret) = optimize(opt, zeros(22650) .+ 1/150)

```

This gives me the output:

(Inf, [0.006666666666666667, 0.006666666666666667, 0.006666666666666667, 0.006666666666666667, 0.006666666666666667, 0.006666666666666667, 0.006666666666666667, 0.006666666666666667, 0.006666666666666667, 0.006666666666666667 … 0.006666666666666667, 0.006666666666666667, 0.006666666666666667, 0.006666666666666667, 0.006666666666666667, 0.006666666666666667, 0.006666666666666667, 0.006666666666666667, 0.006666666666666667, 0.006666666666666667], :FORCED\_STOP)

These values are the starting point. Could you please show me how to code this correctly. I believe minor changes are required but I don’t know what they are

---

<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 24, 2022, 9:44pm UTC](https://discourse.julialang.org/t/problem-setting-the-local-optimizer/86257/2 "2022-08-24T21:44:46Z")

</div>

What is the `cost` vector? `FORCED_STOP` usually means that there was an error somewhere.

---

<div class="post-metadata">

### Author: ![Yagiz\_Dereboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yagiz_dereboy/32/38259_2.png) [@Yagiz\_Dereboy](https://discourse.julialang.org/u/Yagiz_Dereboy)
#### Post date: [August 25, 2022, 10:42am UTC](https://discourse.julialang.org/t/problem-setting-the-local-optimizer/86257/3 "2022-08-25T10:42:41Z")

</div>

Cost vector is the vector is the distances of each point in the data set to each other point in the dataset. It is built as such:

```julia
using CSV
using DataFrames
using Distances
using StatsBase

df = DataFrame(CSV.File("iris.csv"))
data = Matrix(df[:,2:5])
dt = fit(ZScoreTransform, data, dims=1)
data = StatsBase.transform(dt,data)

cost = pairwise(Euclidean(), data, dims=1)
cost = vec(reshape(cost,22500,1))

```

This is a version of the p-median problem in Operations Research. What about me setting the secondary algorithm? Is that correct?

---

<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 25, 2022, 7:36pm UTC](https://discourse.julialang.org/t/problem-setting-the-local-optimizer/86257/4 "2022-08-25T19:36:32Z")

</div>

The problem is that `LD_MMA` supports nonlinear inequality constraints, but _not_ equality constraints.:  
[https://nlopt.readthedocs.io/en/latest/NLopt\_Algorithms/#mma-method-of-moving-asymptotes-and-ccsa](https://nlopt.readthedocs.io/en/latest/NLopt_Algorithms/#mma-method-of-moving-asymptotes-and-ccsa)

You could try `:LD_SLSQP` instead.

Alternatively, it might be more readable if you used JuMP:

```julia
using JuMP, Ipopt
model = Model(Ipopt.Optimizer)
@variable(model, 0 <= x[1:22650] <= 1, start = 1 / 150)
@NLobjective(
    model,
    Min,
    sum(cost[i] * x[i] for i in 1:22500) +
    50 * sum(10 * x[i]^2 / (10 * x[i] + 0.001) for i in 22501:22650),
)
@constraint(model, [i=1:150, j=1:150], x[150*(i-1)+j] <= x[22500+j])
@constraint(model, [i=1:150], sum(x[150*(i-1)+j] for j in 1:150) == 1)
@constraint(model, sum(x[i] for i in 22501:22650) == 3)
optimize!(model)

```

---

<div class="post-metadata">

### Author: ![Yagiz\_Dereboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yagiz_dereboy/32/38259_2.png) [@Yagiz\_Dereboy](https://discourse.julialang.org/u/Yagiz_Dereboy)
#### Post date: [August 26, 2022, 6:56am UTC](https://discourse.julialang.org/t/problem-setting-the-local-optimizer/86257/5 "2022-08-26T06:56:24Z")

</div>

Thank you so much for your help. It worked and it is fast and gives great results.
