# Difficulty to understand the JuMP problem setting

**URL:** https://discourse.julialang.org/t/difficulty-to-understand-the-jump-problem-setting/18112
**Category:** Optimization (Mathematical)
**Tags:** first-steps
**Created:** [November 28, 2018, 8:02pm UTC](https://discourse.julialang.org/t/difficulty-to-understand-the-jump-problem-setting/18112 "2018-11-28T20:02:41Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)
#### Post date: [November 28, 2018, 8:02pm UTC](https://discourse.julialang.org/t/difficulty-to-understand-the-jump-problem-setting/18112/1 "2018-11-28T20:02:41Z")

</div>

Here is my MWE of the problem:

```julia
using JuMP 
using Clp
Random.seed!(2017)
xlocs, ylocs = rand(5), rand(5)

function demo_problem(x,y)
    counter = 0
    for (xx,yy) in zip(x,y)
        val = xx - yy
        
        if (val > epsillon)
            counter += 1 
        else    
            counter += -1 
        end 
    end
    return counter
end

mm = Model(solver=ClpSolver())
@variable(mm, 0.0 <= x[1:5] <= 1.0)
@variable(mm, 0.0 <= y[1:5] <= 1.0)
@objective(mm, Min, demo_problem(x,y))

MethodError: no method matching isless(::Float64, ::JuMP.GenericAffExpr{Float64,Variable})
Closest candidates are:
  isless(::Float64, !Matched::Float64) at float.jl:459
  isless(!Matched::Missing, ::Any) at missing.jl:66
  isless(::AbstractFloat, !Matched::AbstractFloat) at operators.jl:148
  ...

Stacktrace:
 [1] <(::Float64, ::JuMP.GenericAffExpr{Float64,Variable}) at .\operators.jl:260
 [2] >(::JuMP.GenericAffExpr{Float64,Variable}, ::Float64) at .\operators.jl:286
 [3] demo_problem(::Array{Variable,1}, ::Array{Variable,1}) at .\In[25]:12
 [4] top-level scope at C:\Users\tfr004\.julia\packages\JuMP\Xvn0n\src\macros.jl:859
 [5] top-level scope at In[25]:25

```

In my actual problem, I’m counting line section intersections with this algorithm: [https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/](https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/) and my idea was to minize the intersections by using JuMP. I am open to any proposals, because the optimization is not my strongest competence.

---

<div class="post-metadata">

### Author: ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)
#### Post date: [November 28, 2018, 8:13pm UTC](https://discourse.julialang.org/t/difficulty-to-understand-the-jump-problem-setting/18112/2 "2018-11-28T20:13:01Z")

</div>

If you use `ClpSolver`, you are restricted to models of the LP type, where constraints and objective need to be linear. So, you can not supply a user-defined function.

In that case, you should use the `@NLobjective` macro and also use a nonlinear solver, such as `IPOPT`. However, I don’t think your function will work well then either, as it is not differentiable (or continuous)?

Also, you set up `xlocs` and `ylocs` in the beginning, but then never use them again. Is something missing here?

---

<div class="post-metadata">

### Author: ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)
#### Post date: [November 28, 2018, 8:54pm UTC](https://discourse.julialang.org/t/difficulty-to-understand-the-jump-problem-setting/18112/3 "2018-11-28T20:54:58Z")

</div>

Thanks for the quick reply. I changed to this:

```julia
using JuMP 
using Ipopt
using Random
Random.seed!(2017)
xlocs, ylocs = rand(5), rand(5)

function demo_problem(x,y)
    counter = 0
    for (xx,yy) in zip(x,y)
        val = xx - yy
        
        if (val > zero(val))
            counter += 1 
        else    
            counter += -1 
        end 
    end
    return counter
end

mm = Model(solver=IpoptSolver())
@variable(mm, 0.0 <= x[1:5] <= 1.0)
@variable(mm, 0.0 <= y[1:5] <= 1.0)
@NLobjective(mm, Min, demo_problem(x,y))

Unrecognized function "demo_problem" used in nonlinear expression.

Stacktrace:
 [1] error(::String) at .\error.jl:33
 [2] top-level scope at C:\Users\tfr004\.julia\packages\JuMP\Xvn0n\src\parsenlp.jl:96
 [3] top-level scope at C:\Users\tfr004\.julia\packages\JuMP\Xvn0n\src\macros.jl:1310
 [4] top-level scope at In[5]:26

```

Here are the xlocs and ylocs used (just to test the demo\_problem):

```julia
demo_problem(xlocs,ylocs)
1

```

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [November 28, 2018, 9:35pm UTC](https://discourse.julialang.org/t/difficulty-to-understand-the-jump-problem-setting/18112/4 "2018-11-28T21:35:49Z")

</div>

Rather than using a generic nonlinear solver like Ipopt, I think your problem is very well suited to reformulation as a mixed-integer linear problem (assuming the problem you actually care about also has only linear constraints). It is very similar to the one in this post:

> [@What JuMP algorithm to use for this optimization problem?](https://discourse.julialang.org/t/what-jump-algorithm-to-use-for-this-optimization-problem/17936):
>
> I have a question about what type of algorithm I should be using for a fairly straightforward optimization problem. I have the [following data](https://gist.github.com/tyleransom/3738fa6cd1b3b5602e0621b08d068380) id,val,score,id1 1,9,0,64 2,4,0,7 3,11,12,53 4,7,0,8 5,4,35,11 6,14,0,31 7,13,29,2 8,10,11,4 9,2,18,18 10,5,6,38 ... I want to choose 3 “good” ids and 3 “bad” ids. Without loss of generality, number these 1,2,3 and 4,5,6 respectively. Choose these 6 ids to maximize: objective = score[1]+score[2]+score[3]-score[4]-score[5]-score[6] subject to constra…

you should be able to use the same reformulation. If you choose to go this route, you can use e.g. CBC (free) or Gurobi (proprietary, free academic license) as your solver.

If you do choose to use a generic nonlinear solver like Ipopt, please read this section of the JuMP documentation: [Nonlinear Modeling — JuMP -- Julia for Mathematical Optimization 0.18 documentation](http://www.juliaopt.org/JuMP.jl/0.18/nlp.html#user-defined-functions) (you need to register the `demo_problem` function.

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [November 29, 2018, 1:08am UTC](https://discourse.julialang.org/t/difficulty-to-understand-the-jump-problem-setting/18112/5 "2018-11-29T01:08:34Z")

</div>

> [@tkoolen](#):
>
> you should be able to use the same reformulation

This is not true, but it doesn’t matter anyway because your original problem won’t have only linear constraints due to the cross product (`orientation` function), and so the MILP story doesn’t apply. Sorry I hadn’t noticed your link before. I’d stick to the general nonlinear interface.

---

<div class="post-metadata">

### Author: ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)
#### Post date: [November 29, 2018, 7:36am UTC](https://discourse.julialang.org/t/difficulty-to-understand-the-jump-problem-setting/18112/6 "2018-11-29T07:36:32Z")

</div>

Sorry, my first reply was not so clear. I was only trying to explain the error message, not actually suggesting to use the nonlinear interface with the function you provide. I don’t think the automatic differentiation packages will be able to handle that.

To expand on what @tkoolen wrote, I think a reformulation of your function is a better strategy (although I don’t have one in mind). Even if it turns out that there are some nonlinear constraints as well as binary variables, there are some MINLP-capable solvers compatible with JuMP out there (e.g. [POD](https://github.com/lanl-ansi/POD.jl), [SCIP](https://github.com/SCIP-Interfaces/SCIP.jl).

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [November 29, 2018, 3:34pm UTC](https://discourse.julialang.org/t/difficulty-to-understand-the-jump-problem-setting/18112/7 "2018-11-29T15:34:05Z")

</div>

Yeah, but before going that route, it’s probably a good idea to determine what additional constraints there are on the variables. What would forbid a trivial solution with all line segments in parallel, for example?

---

<div class="post-metadata">

### Author: ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)
#### Post date: [November 29, 2018, 3:49pm UTC](https://discourse.julialang.org/t/difficulty-to-understand-the-jump-problem-setting/18112/8 "2018-11-29T15:49:36Z")

</div>

I am trying to rearrange karatekas in the below graph:

 ![](https://global.discourse-cdn.com/julialang/original/3X/1/9/19df44d4c5625dd2c96a844f69a0fab97703593d.png)

In such an order that intersections of the connecting lines are minimized. Currently I believe that it would produce more beautiful end result.

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [November 29, 2018, 5:17pm UTC](https://discourse.julialang.org/t/difficulty-to-understand-the-jump-problem-setting/18112/9 "2018-11-29T17:17:26Z")

</div>

This is a graph layout problem: [Graph drawing - Wikipedia](https://en.wikipedia.org/wiki/Graph_drawing). Finding a global minimizer, one that achieves the [crossing number](https://en.wikipedia.org/wiki/Crossing_number_(graph_theory)), is an NP-hard problem. You can try to go the MINLP route as suggested earlier, but it won’t scale well, is a pretty non-standard way of doing this, and seems like overkill. See the Wikipedia article for a list of heuristic-based algorithms, and [https://github.com/IainNZ/GraphLayout.jl](https://github.com/IainNZ/GraphLayout.jl) for some Julia implementations.

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [November 29, 2018, 5:20pm UTC](https://discourse.julialang.org/t/difficulty-to-understand-the-jump-problem-setting/18112/10 "2018-11-29T17:20:42Z")

</div>

Actually, [GitHub - JuliaGraphs/GraphPlot.jl: Graph visualization for Julia.](https://github.com/JuliaGraphs/GraphPlot.jl) might be in better shape for Julia 1.0.

---

<div class="post-metadata">

### Author: ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)
#### Post date: [November 29, 2018, 5:45pm UTC](https://discourse.julialang.org/t/difficulty-to-understand-the-jump-problem-setting/18112/11 "2018-11-29T17:45:42Z")

</div>

[https://github.com/JuliaGraphs/GraphPlot.jl/pull/75](https://github.com/JuliaGraphs/GraphPlot.jl/pull/75)

---

<div class="post-metadata">

### Author: ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)
#### Post date: [December 1, 2018, 12:30pm UTC](https://discourse.julialang.org/t/difficulty-to-understand-the-jump-problem-setting/18112/12 "2018-12-01T12:30:56Z")

</div>

> [@Tero\_Frondelius](#):
>
> In such an order that intersections of the connecting lines are minimized. Currently I believe that it would produce more beautiful end result.

I don’t believe anymore. Clearly my model is too simple. Here is the optimal result:

 ![optimal_karate](https://global.discourse-cdn.com/julialang/original/3X/6/f/6f2eba4506324d6a5c5203140cf66c74b13115e7.jpeg)

I would like to animate the solutions through iterations. How can I get iteration i variable values?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [December 1, 2018, 3:40pm UTC](https://discourse.julialang.org/t/difficulty-to-understand-the-jump-problem-setting/18112/13 "2018-12-01T15:40:55Z")

</div>

Seeing this reminded me very much of a game where you got given a graph and you were supposed to uncross the lines by moving nodes: [https://www.ologames.com/Free\_Games/Uncross-The-Lines](https://www.ologames.com/Free_Games/Uncross-The-Lines). Now I have to play it again…

---

<div class="post-metadata">

### Author: ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)
#### Post date: [December 2, 2018, 5:50pm UTC](https://discourse.julialang.org/t/difficulty-to-understand-the-jump-problem-setting/18112/14 "2018-12-02T17:50:51Z")

</div>

> [@kristoffer.carlsson](#):
>
> [https://www.ologames.com/Free\_Games/Uncross-The-Lines](https://www.ologames.com/Free_Games/Uncross-The-Lines).

Really addictive game.
