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?
xlocs, ylocs = rand(5), rand(5)
counter = 0
for (xx,yy) in zip(x,y)
val = xx - yy
if (val > zero(val))
counter += 1
counter += -1
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.
 error(::String) at .\error.jl:33
 top-level scope at C:\Users\tfr004\.julia\packages\JuMP\Xvn0n\src\parsenlp.jl:96
 top-level scope at C:\Users\tfr004\.julia\packages\JuMP\Xvn0n\src\macros.jl:1310
 top-level scope at In:26
Here are the xlocs and ylocs used (just to test the demo_problem):
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:
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.
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.
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, SCIP.
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?