# How to write .NL file containing JuMP problem using AmplNLWriter.jl

**URL:** https://discourse.julialang.org/t/how-to-write-nl-file-containing-jump-problem-using-amplnlwriter-jl/2954
**Category:** General Usage
**Tags:** package, jump
**Created:** [March 29, 2017, 6:40pm UTC](https://discourse.julialang.org/t/how-to-write-nl-file-containing-jump-problem-using-amplnlwriter-jl/2954 "2017-03-29T18:40:51Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![lruthotto](https://avatars.discourse-cdn.com/v4/letter/l/b5ac83/32.png) [@lruthotto](https://discourse.julialang.org/u/lruthotto)
#### Post date: [March 29, 2017, 6:40pm UTC](https://discourse.julialang.org/t/how-to-write-nl-file-containing-jump-problem-using-amplnlwriter-jl/2954/1 "2017-03-29T18:40:51Z")

</div>

I’m looking for ways to write a jump model to my hard drive so that it can be solved on another machine.

```julia
julia> using JuMP, AmplNLWriter
julia> m = Model(solver=AmplNLSolver("bonmin"))
julia> @variable(m,x[1:2])
julia> @objective(m,Min,dot(x,x))

```

I’ve tried: AmplNLsolver.write\_nl\_file(open(“/tmp/Problem.nl”,“w”),m)

but that didn’t work for me.

Any suggestions?  
Thanks,  
Lars

---

<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: [March 29, 2017, 7:12pm UTC](https://discourse.julialang.org/t/how-to-write-nl-file-containing-jump-problem-using-amplnlwriter-jl/2954/2 "2017-03-29T19:12:30Z")

</div>

See the code between [here](https://github.com/JuliaOpt/AmplNLWriter.jl/blob/b299174b527f305231777019916738823ed2d797/src/AmplNLWriter.jl#L338) and [here](https://github.com/JuliaOpt/AmplNLWriter.jl/blob/b299174b527f305231777019916738823ed2d797/src/AmplNLWriter.jl#L372). I think a PR to refactor this so that you can easily write an NL file from a model without solving it would be welcome.

---

<div class="post-metadata">

### Author: ![lruthotto](https://avatars.discourse-cdn.com/v4/letter/l/b5ac83/32.png) [@lruthotto](https://discourse.julialang.org/u/lruthotto)
#### Post date: [March 30, 2017, 4:04am UTC](https://discourse.julialang.org/t/how-to-write-nl-file-containing-jump-problem-using-amplnlwriter-jl/2954/3 "2017-03-30T04:04:34Z")

</div>

Thanks, I got one step further, I think. See this code.

```julia
function mywrite_nl_file(f::IO, mj::JuMP.Model)
    JuMP.build(mj)
    
    m = internalmodel(mj)
    m.status = :NotSolved
    m.solve_exitcode = -1
    m.solve_result_num = -1
    m.solve_result = "?"
    m.solve_message = ""

    # There is no non-linear binary type, only non-linear discrete, so make
    # sure binary vars have bounds in [0, 1]
    for i in 1:m.nvar
        if m.vartypes[i] == :Bin
            if m.x_l[i] < 0
                m.x_l[i] = 0
            end
            if m.x_u[i] > 1
                m.x_u[i] = 1
            end
        end
    end
    AmplNLWriter.make_var_index!(m)
    AmplNLWriter.make_con_index!(m)

    AmplNLWriter.write_nl_file(f, m.inner)
end

```

Using this function, I can write the example provided in the package even without solving them. However, for my own example things don’t work. Here it is:

```julia
using JuMP, AmplNLWriter

U = randn(10,6)
ft = [1;0;1;0;1;0]
dobs = U*ft

m = Model(solver=AmplNLSolver("bonmin"))
@variable(m, f[1:6], Bin )
@objective(m, Min, dot(U*f-dobs, U*f-dobs));

io = open("/tmp/myExample.nl","w")
mywrite_nl_file(io,m)
close(io)

```

I’m getting the following error

```julia
ERROR: LoadError: Solver does not support quadratic objectives
 in setquadobjterms!(::AmplNLWriter.AmplNLLinearQuadraticModel, ::Array{Int32,1}, ::Array{Int32,1}, ::Array{Float64,1}) at ~/.julia/v0.5/MathProgBase/src/SolverInterface/LinearQuadratic.jl:71
 in addQuadratics(::JuMP.Model) at ~/.julia/v0.5/JuMP/src/solvers.jl:484
 in #build#114(::Bool, ::Bool, ::JuMP.ProblemTraits, ::Function, ::JuMP.Model) at ~/.julia/v0.5/JuMP/src/solvers.jl:422
 in write_nl_file(::IOStream, ::JuMP.Model) at ~/.julia/v0.5/ConvDiff/src/io.jl:3
 in include_from_node1(::String) at ./loading.jl:488
 in include_from_node1(::String) at ~/Software/julia-0.5/usr/lib/julia/sys.dylib:?
while loading ~/.julia/v0.5/AmplNLWriter/examples/myEx.jl, in expression starting on line 14

```

I tried also with other solvers, like Ipopt and without the binary constraints… no success…

Any clue?

---

<div class="post-metadata">

### Author: ![JackDunnNZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jackdunnnz/32/3749_2.png) [@JackDunnNZ](https://discourse.julialang.org/u/JackDunnNZ)
#### Post date: [March 30, 2017, 1:28pm UTC](https://discourse.julialang.org/t/how-to-write-nl-file-containing-jump-problem-using-amplnlwriter-jl/2954/4 "2017-03-30T13:28:08Z")

</div>

AmplNLWriter doesn’t support quadratic objectives, you will need to add the objective via `@NLobjective` for it to work.

---

<div class="post-metadata">

### Author: ![lruthotto](https://avatars.discourse-cdn.com/v4/letter/l/b5ac83/32.png) [@lruthotto](https://discourse.julialang.org/u/lruthotto)
#### Post date: [March 30, 2017, 2:17pm UTC](https://discourse.julialang.org/t/how-to-write-nl-file-containing-jump-problem-using-amplnlwriter-jl/2954/5 "2017-03-30T14:17:12Z")

</div>

Okay. Is there a reason why AmplNLWriter doesn’t support QPs?

Tried that, but I ran into a new problem since in order to use `@NLobjective` I need to register a new function, right? Here is a minimal example

```julia
using JuMP, AmplNLWriter

mysquare(x) = x^2
mj = Model(solver=AmplNLSolver("bonmin"))
JuMP.register(mj, :mysquare, 1, mysquare, autodiff=true)
@variable(mj, x)
@NLobjective(mj, Min, mysquare(x))

io = open("/tmp/myExample.nl","w")
JuMP.build(mj)
m = mj.internalModel.inner
m.status = :NotSolved
m.solve_exitcode = -1
m.solve_result_num = -1
m.solve_result = "?"
m.solve_message = ""

# There is no non-linear binary type, only non-linear discrete, so make
# sure binary vars have bounds in [0, 1]
for i in 1:m.nvar
    if m.vartypes[i] == :Bin
        if m.x_l[i] < 0
            m.x_l[i] = 0
        end
        if m.x_u[i] > 1
            m.x_u[i] = 1
        end
    end
end
AmplNLWriter.make_var_index!(m)
AmplNLWriter.make_con_index!(m)

AmplNLWriter.write_nl_file(io, m)
close(io)

```

This gives the following error:

```julia
ERROR: LoadError: KeyError: key :mysquare not found

```

---

<div class="post-metadata">

### Author: ![JackDunnNZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jackdunnnz/32/3749_2.png) [@JackDunnNZ](https://discourse.julialang.org/u/JackDunnNZ)
#### Post date: [March 30, 2017, 2:36pm UTC](https://discourse.julialang.org/t/how-to-write-nl-file-containing-jump-problem-using-amplnlwriter-jl/2954/6 "2017-03-30T14:36:48Z")

</div>

There is no reason it can’t support the quadratic interface, it just hasn’t been added yet. PRs would be welcome if anybody requires this functionality.

You can’t use user-provided functions with AmplNLWriter. You have to write the nonlinear terms as a sequence of elementary operations. Your example works with the following:

```julia
using JuMP, AmplNLWriter

mj = Model(solver=AmplNLSolver("bonmin"))
@variable(mj, x)
@NLobjective(mj, Min, x^2)

io = open("/tmp/myExample.nl","w")
JuMP.build(mj)
m = mj.internalModel.inner
m.status = :NotSolved
m.solve_exitcode = -1
m.solve_result_num = -1
m.solve_result = "?"
m.solve_message = ""

# There is no non-linear binary type, only non-linear discrete, so make
# sure binary vars have bounds in [0, 1]
for i in 1:m.nvar
    if m.vartypes[i] == :Bin
        if m.x_l[i] < 0
            m.x_l[i] = 0
        end
        if m.x_u[i] > 1
            m.x_u[i] = 1
        end
    end
end
AmplNLWriter.make_var_index!(m)
AmplNLWriter.make_con_index!(m)

AmplNLWriter.write_nl_file(io, m)
close(io)

```

---

<div class="post-metadata">

### Author: ![lruthotto](https://avatars.discourse-cdn.com/v4/letter/l/b5ac83/32.png) [@lruthotto](https://discourse.julialang.org/u/lruthotto)
#### Post date: [March 30, 2017, 3:12pm UTC](https://discourse.julialang.org/t/how-to-write-nl-file-containing-jump-problem-using-amplnlwriter-jl/2954/7 "2017-03-30T15:12:50Z")

</div>

Thanks, but writing my actual problem out with elementary operations is probably infeasible.

Can you give me some clues what needs to be done to support quadratic programs? I have no feeling how complicated adding this is. There definitely is interest in it.

---

<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: [March 30, 2017, 3:31pm UTC](https://discourse.julialang.org/t/how-to-write-nl-file-containing-jump-problem-using-amplnlwriter-jl/2954/8 "2017-03-30T15:31:48Z")

</div>

See previous discussion at [https://github.com/JuliaOpt/AmplNLWriter.jl/issues/49](https://github.com/JuliaOpt/AmplNLWriter.jl/issues/49).

---

<div class="post-metadata">

### Author: ![lruthotto](https://avatars.discourse-cdn.com/v4/letter/l/b5ac83/32.png) [@lruthotto](https://discourse.julialang.org/u/lruthotto)
#### Post date: [March 31, 2017, 3:05am UTC](https://discourse.julialang.org/t/how-to-write-nl-file-containing-jump-problem-using-amplnlwriter-jl/2954/9 "2017-03-31T03:05:19Z")

</div>

Thanks for sharing this, Miles. Looks like some work is required here, but I think it’ll be worth it. Note sure if I’ll have time to work on it in the next months though.
