# Write .NL file containing JuMP model

**URL:** https://discourse.julialang.org/t/write-nl-file-containing-jump-model/101113
**Category:** Optimization (Mathematical)
**Tags:** question, jump
**Created:** [July 3, 2023, 9:16am UTC](https://discourse.julialang.org/t/write-nl-file-containing-jump-model/101113 "2023-07-03T09:16:57Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Luca](https://avatars.discourse-cdn.com/v4/letter/l/51bf81/32.png) [@Luca](https://discourse.julialang.org/u/Luca)
#### Post date: [July 3, 2023, 9:16am UTC](https://discourse.julialang.org/t/write-nl-file-containing-jump-model/101113/1 "2023-07-03T09:16:57Z")

</div>

Dear all,

I would ask you if there exists an easy way to write the .nl file corresponding to a JuMP problem. I already checked the solution proposed in [https://discourse.julialang.org/t/how-to-write-nl-file-containing-jump-problem-using-amplnlwriter-jl/2954](https://discourse.julialang.org/t/how-to-write-nl-file-containing-jump-problem-using-amplnlwriter-jl/2954), but it seems no more working.

Thanks very much. Best regards,  
Luca

---

<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: [July 4, 2023, 2:54am UTC](https://discourse.julialang.org/t/write-nl-file-containing-jump-model/101113/2 "2023-07-04T02:54:48Z")

</div>

Try `write_to_file(model, "model.nl")`

---

<div class="post-metadata">

### Author: ![ink](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ink/32/212560_2.png) [@ink](https://discourse.julialang.org/u/ink)
#### Post date: [August 31, 2025, 2:59pm UTC](https://discourse.julialang.org/t/write-nl-file-containing-jump-model/101113/3 "2025-08-31T14:59:09Z")

</div>

This isnt working for me, no matter which Optimizer I use.

Writing to .lp or .mps files is fine, but those are rejected by AMPL solvers. Where writing to .nl files results in weird formatting (that causes parse errors when I try to feed it to a solver.)

This is what my .nl file looks like, when I generate it with `JuMP.write_to_file(model, "pentomino.nl")`

```julia-auto
g3 1 1 0
 7865 9126 1 0 12 0
 0 1
 0 0
 0 0 0
 0 0 0 1
 7865 0 0 0 0
 18919288 7865
 0 0
 0 0 0 0 0
C0
n0
C1

```

---

<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: [August 31, 2025, 10:59pm UTC](https://discourse.julialang.org/t/write-nl-file-containing-jump-model/101113/4 "2025-08-31T22:59:37Z")

</div>

Hi @ink,

What’s the error? And with what solver? What happens if you use AmplNLWriter.jl?

Do you have a reproducible example? Can you send me the `pentomino.nl` file that gets written? Your Jacobian looks very dense.

---

<div class="post-metadata">

### Author: ![ink](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ink/32/212560_2.png) [@ink](https://discourse.julialang.org/u/ink)
#### Post date: [September 1, 2025, 8:09am UTC](https://discourse.julialang.org/t/write-nl-file-containing-jump-model/101113/5 "2025-09-01T08:09:44Z")

</div>

I use AmplNLWriter with Bonmin\_jll and it printed the same .nl file as when I just use HiGHS.Optimizer (without AmplNLWriter). I exported the model “normally” with `JuMP.write_to_file(model, nl_path)` - but when trying to load this file into AMPL in python (which supposedly accepts .nl files) with `ampl.read(nl_filename)`, it threw a parse error. (The .nl file looks like it’s missing spaces or something, I’m not sure, I’ve never used this format before.)

I’ll try and prepare a minimal reproducible example, sure, since my codes are like 300+ lines long atm. Thank you for your interest in helping !

It’s also possible that ampl simply doesn’t accept .nl files at all? I know it prefers its own .mod and .dat files, but those are not trivial to generate with JuMP - I have to generate them manually from Julia by printing strings, which is kind of annoying (and error prone).

Edit: worth mentioning, my problem is linear. So I want to use specialized NL solvers like CPLEX and stuff, not Bonmin and those ones.

---

<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: [September 1, 2025, 8:18am UTC](https://discourse.julialang.org/t/write-nl-file-containing-jump-model/101113/6 "2025-09-01T08:18:50Z")

</div>

I haven’t used AMPLs Python API.

There might be a bug in how we’re writing the nl file or you might be calling AMPL wrong. Contact their support?

Why go via nl? Why not CPLEX.jl?

---

<div class="post-metadata">

### Author: ![ink](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ink/32/212560_2.png) [@ink](https://discourse.julialang.org/u/ink)
#### Post date: [September 1, 2025, 8:28am UTC](https://discourse.julialang.org/t/write-nl-file-containing-jump-model/101113/7 "2025-09-01T08:28:25Z")

</div>

Thanks for the help in DMs, seems that AMPL’s Python API doesn’t support the .nl file format at all, that’s where the problem lies.

To anwser why not CPLEX directly: I have an AMPL license which provides online CPLEX solving, but don’t have a CPLEX license personally, and my model is much too big for the free version (1000x1000 max), that’s why I used AMPL - and I was told it would read .nl files, not just .mod and .dat files, which was my mistake! Problem closed 😄
