# JuMP : not getting the correct solution of the optimization problem

**URL:** <https://discourse.julialang.org/t/jump-not-getting-the-correct-solution-of-the-optimization-problem/29947>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [October 15, 2019, 2:47pm UTC](https://discourse.julialang.org/t/jump-not-getting-the-correct-solution-of-the-optimization-problem/29947 "2019-10-15T14:47:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Val](https://avatars.discourse-cdn.com/v4/letter/v/e56c9b/32.png) [@Val](https://discourse.julialang.org/u/Val)\
**Post date:** [October 15, 2019, 2:47pm UTC](https://discourse.julialang.org/t/jump-not-getting-the-correct-solution-of-the-optimization-problem/29947/1 "2019-10-15T14:47:08Z")

</div>

Dear everyone

I am solving a minimization problem under equality and inequality constraints. I solve this problem with COSMO and I get a solution which is the vector alpha\_opt=[0 0 0]  
But I found another vector which is alpha\_test=[1 1 0] which also respects the constraints and such that the objective function is equal to -5.5\<0

julia\> sum(0.5\*alpha\_opt[k]\*G[k,i]\*y[k]\*y[i]\*alpha\_opt[i] -alpha\_opt[k] for i=1:n for k=1:n)  
0.0

julia\> sum(0.5\*alpha\_test[k]\*G[k,i]\*y[k]\*y[i]\*alpha\_test[i] -alpha\_test[k] for i=1:n for k=1:n)  
-5.5

However, I do not understand what I did wrong when I defined my optimization problem.

Can anyone help me please?

---

<div class="post-metadata">

**Author:** ![dpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpo/32/3335_2.png) [@dpo](https://discourse.julialang.org/u/dpo)\
**Post date:** [October 15, 2019, 10:47pm UTC](https://discourse.julialang.org/t/jump-not-getting-the-correct-solution-of-the-optimization-problem/29947/2 "2019-10-15T22:47:24Z")

</div>

Could you show what’s in `alpha_opt`, `G`? Based on your question on Slack, your constraints are all linear, is that right? Your objective function is probably nonconvex and your problem has local minima (and/or saddle points). Most solver only look for a stationary point. In order to find the point you’re looking for, you can start your solver from a different initial guess or use a global optimization solver.

---

<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:** [October 16, 2019, 2:27am UTC](https://discourse.julialang.org/t/jump-not-getting-the-correct-solution-of-the-optimization-problem/29947/3 "2019-10-16T02:27:32Z")

</div>

Please read [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) and provide a minimal working example which demonstrates your problem.

---

<div class="post-metadata">

**Author:** ![Val](https://avatars.discourse-cdn.com/v4/letter/v/e56c9b/32.png) [@Val](https://discourse.julialang.org/u/Val)\
**Post date:** [October 16, 2019, 12:41pm UTC](https://discourse.julialang.org/t/jump-not-getting-the-correct-solution-of-the-optimization-problem/29947/4 "2019-10-16T12:41:22Z")

</div>

@dpo : this is right : my constraints are linear and my objective function is quadratic.

Here is the minimal working example

```julia
using LinearAlgebra
using JuMP
using COSMO

X=[1 1; 1 2; 2 2];
y=[+1 -1 -1];
n=length(y);
p=size(X,2);
G=X*X';

model = Model(with_optimizer(COSMO.Optimizer))    
@variables model begin
  alpha[i=1:length(y)] 
end  
@NLobjective(model, Min, sum(0.5*alpha[k]*G[k,i]*y[k]*y[i]*alpha[i] -alpha[k] for i=1:n for k=1:n))
@constraint(model, sum( y[i] * alpha[i] for i=1:n ) == 0)
@constraint(model, [i=1:n], -alpha[i]<=0)

optimize!(model);   
alpha_opt = value.(alpha);

```

And here is the output I get :

julia\> optimize!(model);

* * *

```
      COSMO v0.5.0 - A Quadratic Objective Conic Solver
                     Michael Garstka
            University of Oxford, 2017 - 2019

```

* * *

Problem: x ∈ R^{3},  
constraints: A ∈ R^{4x3} (6 nnz),  
matrix size to factor: 7x7 (19 nnz)  
Sets: Nonnegatives of dim: 3  
ZeroSet of dim: 1  
Settings: ϵ\_abs = 1.0e-04, ϵ\_rel = 1.0e-04,  
ϵ\_prim\_inf = 1.0e-06, ϵ\_dual\_inf = 1.0e-04,  
ρ = 0.1, σ = 1.0e-6, α = 1.6,  
max\_iter = 2500,  
scaling iter = 10 (on),  
check termination every 40 iter,  
check infeasibility every 40 iter,  
KKT system solver: QDLDL  
Setup Time: 1967.41ms

Iter: Objective: Primal Res: Dual Res: Rho:  
40 0.0000e+00 0.0000e+00 0.0000e+00 1.0000e-01

* * *

> > > Results  
> > > Status: Solved  
> > > Iterations: 40  
> > > Optimal objective: 0.0  
> > > Runtime: 2.754s (2754.23ms)

julia\> alpha\_opt = value.(alpha)  
3-element Array{Float64,1}:  
0.0  
0.0  
0.0

I was expecting julia to give me this solution : [2 ,2 ,0].

---

<div class="post-metadata">

**Author:** ![abelsiqueira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelsiqueira/32/47269_2.png) [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)\
**Post date:** [October 16, 2019, 1:32pm UTC](https://discourse.julialang.org/t/jump-not-getting-the-correct-solution-of-the-optimization-problem/29947/5 "2019-10-16T13:32:50Z")

</div>

`alpha = zeros(3)` doesn’t seem like a solution. The feasible set is given by \alpha = Zu for u \geq 0, where Z = \begin{bmatrix} 1 & 1 \\ 1 & 0 \\ 0 & 1 \end{bmatrix}. The objective by f(\alpha) = \tfrac{1}{2}\alpha^T H \alpha + \alpha^T g, where `H = y' .* G .* y` and `g = -3*ones(3)`. The reduced subproblem f(Zu) has Hessian  
Z^T H Z = \begin{bmatrix} 1 & 1 \\ 1 & 2 \end{bmatrix},  
and  
Z^T g = \begin{bmatrix} -6 \\ -6 \end{bmatrix}. This implies the solution is u = (6,0)^T, i.e., \alpha = Zu = (6,6,0)^T, and looks like it’s the only one because the problem is convex.

I ran with IPOPT (using [NLPModelsIpopt.jl](https://github.com/JuliaSmoothOptimizers/NLPModelsIpopt.jl)) and found that solution. I’m guessing changing COSMO to IPOPT might also give that same solution.

I’ve also noticed your problem looks like the dual of a SVM. In that case, the `-alpha[k]` in the sum should be outside the `for i`, otherwise you’re adding them up for each `i`. If that change is made, your expected solution is found.

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [October 16, 2019, 2:10pm UTC](https://discourse.julialang.org/t/jump-not-getting-the-correct-solution-of-the-optimization-problem/29947/6 "2019-10-16T14:10:49Z")

</div>

`COSMO` is a conic solver. I’m surprised it works at all with `@NLobjective`. I’m guessing that there is a bug in the wrapper that causes the nonlinear objective to be completely ignored (as you see the objective value is reported to be zero).  
CC @migarstka

You should use `@objective` instead of `@NLobjective`.

---

<div class="post-metadata">

**Author:** ![Val](https://avatars.discourse-cdn.com/v4/letter/v/e56c9b/32.png) [@Val](https://discourse.julialang.org/u/Val)\
**Post date:** [October 16, 2019, 3:00pm UTC](https://discourse.julialang.org/t/jump-not-getting-the-correct-solution-of-the-optimization-problem/29947/7 "2019-10-16T15:00:04Z")

</div>

Thank you very much for your answers.I will use Ipopt.

---

<div class="post-metadata">

**Author:** ![migarstka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/migarstka/32/3828_2.png) [@migarstka](https://discourse.julialang.org/u/migarstka)\
**Post date:** [October 16, 2019, 3:23pm UTC](https://discourse.julialang.org/t/jump-not-getting-the-correct-solution-of-the-optimization-problem/29947/8 "2019-10-16T15:23:28Z")

</div>

Indeed `COSMO.jl` doesn’t support `@NLobjective` and should throw a warning when people try to use it in their models. I will fix the bug in the MOI wrapper.

As Miles pointed out, if you change `@NLobjective` in your example to `@objective` you should get the correct result.  
**Edit:** The bug has been fixed in the latest master commit.
