# Help rewriting Convex.jl problem in JuMP.jl

**URL:** https://discourse.julialang.org/t/help-rewriting-convex-jl-problem-in-jump-jl/4321
**Category:** Optimization (Mathematical)
**Tags:** question, jump
**Created:** [June 17, 2017, 12:15am UTC](https://discourse.julialang.org/t/help-rewriting-convex-jl-problem-in-jump-jl/4321 "2017-06-17T00:15:14Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [June 17, 2017, 12:15am UTC](https://discourse.julialang.org/t/help-rewriting-convex-jl-problem-in-jump-jl/4321/1 "2017-06-17T00:15:14Z")

</div>

I am having a hard time trying to translate a convex optimization problem from Convex.jl to JuMP.jl:

```julia
using Convex, ECOS

# generate some data
n = 100
e = [sin(50i)*sin(50j) for i=1:n, j=1:n]

# optimization variable
h = Variable(n, n)

# objective
C = sum(pos(h[i,j] - e[i,j]) + pos(e[i,j] - h[i,j]) for i=1:n, j=1:n)

prob = minimize(C)
solve!(prob, ECOSSolver())

```

My attempt as a first-time user was as follows:

```julia
using JuMP, ECOS

# generate some data
n = 100
e = [sin(50i)*sin(50j) for i=1:n, j=1:n]

# define problem
m = Model(solver = ECOSSolver())

# optimization variable
@variable(m, h[1:n,1:n])

# objective
C = sum(max(0,h[i,j]-e[i,j]) + max(0,e[i,j]-h[i,j]) for i=1:n, j=1:n)

@objective(m, Min, C)
solve(m)

```

The code doesn’t compile and gives me the following error:

> MethodError: no method matching isless(::JuMP.GenericAffExpr{Float64,JuMP.Variable}, ::Int64)  
> Closest candidates are:  
> isless(::Char, ::Integer) at deprecated.jl:49  
> isless(::AbstractFloat, ::Real) at operators.jl:42  
> isless(::ForwardDiff.Dual{N,T\<:Real}, ::Real) at /home/juliohm/.julia/v0.5/ForwardDiff/src/dual.jl:160

1. Can you please explain what is the issue? Are we allowed to call Julia functions like `max` directly in JuMP.jl?

2. I am also wondering why the Convex.jl version takes a while (more than 5 minutes) to return even though the solver takes less than a second to solve the problem. Could someone please elaborate on what is happening with the Convex.jl approach?

---

<div class="post-metadata">

### Author: ![Joey\_Huchette](https://avatars.discourse-cdn.com/v4/letter/j/858c86/32.png) [@Joey\_Huchette](https://discourse.julialang.org/u/Joey_Huchette)
#### Post date: [June 17, 2017, 3:27pm UTC](https://discourse.julialang.org/t/help-rewriting-convex-jl-problem-in-jump-jl/4321/2 "2017-06-17T15:27:24Z")

</div>

No, you can’t call functions like max on JuMP variables or expressions.  
You’ll have to manually apply the transformations that Convex.jl does  
automatically.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [June 17, 2017, 4:36pm UTC](https://discourse.julialang.org/t/help-rewriting-convex-jl-problem-in-jump-jl/4321/3 "2017-06-17T16:36:53Z")

</div>

You mean rewrite the problem in LP form? Can you confirm that the following formulation is equivalent?

```julia
minimize sum(t)
subject to
t[i,j] >= 0
h[i,j] - e[i,j] <= t[i,j]
e[i,j] - h[i,j] <= t[i,j]

```

Also, I’d be happy to stick with Convex.jl if I could solve the slow parsing issue. Is there any workaround for that?

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [June 17, 2017, 4:53pm UTC](https://discourse.julialang.org/t/help-rewriting-convex-jl-problem-in-jump-jl/4321/4 "2017-06-17T16:53:44Z")

</div>

Yes, the transformation works. I like Convex.jl though, I wish I could stick with it.

---

<div class="post-metadata">

### Author: ![Joey\_Huchette](https://avatars.discourse-cdn.com/v4/letter/j/858c86/32.png) [@Joey\_Huchette](https://discourse.julialang.org/u/Joey_Huchette)
#### Post date: [June 17, 2017, 5:00pm UTC](https://discourse.julialang.org/t/help-rewriting-convex-jl-problem-in-jump-jl/4321/5 "2017-06-17T17:00:22Z")

</div>

PRs addressing any performance issue are welcomed.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [June 17, 2017, 5:02pm UTC](https://discourse.julialang.org/t/help-rewriting-convex-jl-problem-in-jump-jl/4321/6 "2017-06-17T17:02:42Z")

</div>

Well, if I knew what exactly is causing the performance issue to start with…

---

<div class="post-metadata">

### Author: ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)
#### Post date: [June 17, 2017, 8:45pm UTC](https://discourse.julialang.org/t/help-rewriting-convex-jl-problem-in-jump-jl/4321/7 "2017-06-17T20:45:57Z")

</div>

Are you sure the two formulations are equivalent?

I restrict myself to 1 variable: I call `d` one of your differences `h[i,j]-e[i,j]`. Let us also assume for the sake of argument that there is an additional constraint `d <= 0`. If I don’t get you wrong, you say that

```julia
maximize max(0,d)
subject to
d <= 0

```

which has optimal objective value of 0, is equivalent to

```julia
maximize t
subject to
t >= 0
d <= t
d <= 0

```

which to me seems unbounded.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [June 17, 2017, 9:32pm UTC](https://discourse.julialang.org/t/help-rewriting-convex-jl-problem-in-jump-jl/4321/8 "2017-06-17T21:32:45Z")

</div>

@mzaffalon `max(0,d)` is convex, you cannot maximize it. Or more precisely, the maximum is infinity. Now, if you make the explicit constraint `d <= 0` implicit to the objective, the `max(0,d)` becomes indeed `0` and you can maximize it.

I believe the formulation for the **minimization** problem I wrote is equivalent.

---

<div class="post-metadata">

### Author: ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)
#### Post date: [June 18, 2017, 5:24am UTC](https://discourse.julialang.org/t/help-rewriting-convex-jl-problem-in-jump-jl/4321/9 "2017-06-18T05:24:33Z")

</div>

You are right, I misread your post. Thank you for the explanation.
