# MOF \`read\_from\_file\` fails when using MOI.Parameter and non-linear constraints

**URL:** <https://discourse.julialang.org/t/mof-read-from-file-fails-when-using-moi-parameter-and-non-linear-constraints/126688>\
**Category:** Optimization (Mathematical)\
**Created:** [March 7, 2025, 5:26pm UTC](https://discourse.julialang.org/t/mof-read-from-file-fails-when-using-moi-parameter-and-non-linear-constraints/126688 "2025-03-07T17:26:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrewrosemberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrewrosemberg/32/9902_2.png) [@andrewrosemberg](https://discourse.julialang.org/u/andrewrosemberg)\
**Post date:** [March 7, 2025, 5:26pm UTC](https://discourse.julialang.org/t/mof-read-from-file-fails-when-using-moi-parameter-and-non-linear-constraints/126688/1 "2025-03-07T17:26:26Z")

</div>

I am trying `read_from_file` and solve a model with `MOI.Parameter` but failing when I have non-linear constraints:

```julia
using JuMP

using MathOptInterface

using Ipopt

model = JuMP.Model(Ipopt.Optimizer)

set_silent(model)

# Parameters

@variable(model, p ∈ MOI.Parameter(1.5))

@variable(model, p_prox)

# Variables

@variable(model, x)

# Constraints

@constraint(model, con, p_prox == p)

@constraint(model, x * sin(p_prox) == 1)

@objective(model, Min, sum(x))

optimize!(model) # Works

@assert is_solved_and_feasible(model)

write_to_file(model, "test.mof.json")

model_test = read_from_file("test.mof.json")

set_optimizer(model_test, Ipopt.Optimizer)

JuMP.optimize!(model_test) # Fails

```

---

<div class="post-metadata">

**Author:** ![andrewrosemberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrewrosemberg/32/9902_2.png) [@andrewrosemberg](https://discourse.julialang.org/u/andrewrosemberg)\
**Post date:** [March 7, 2025, 5:28pm UTC](https://discourse.julialang.org/t/mof-read-from-file-fails-when-using-moi-parameter-and-non-linear-constraints/126688/2 "2025-03-07T17:28:50Z")

</div>

julia\> JuMP.optimize!(model\_test)  
ERROR: Cannot mix the new and legacy nonlinear APIs  
Stacktrace:  
[1] error(s::String)  
@ Base ./error.jl:35  
[2] set  
@ ~/.julia/packages/Ipopt/KZuET/src/MOI\_wrapper.jl:776 [inlined]  
[3] set(b::MathOptInterface.Bridges.LazyBridgeOptimizer{…}, attr::MathOptInterface.NLPBlock, value::MathOptInterface.NLPBlockData)  
@ MathOptInterface.Bridges ~/.julia/packages/MathOptInterface/TYq6d/src/Bridges/bridge\_optimizer.jl:944  
[4] \_pass\_attribute(dest::MathOptInterface.Bridges.LazyBridgeOptimizer{…}, src::MathOptInterface.Utilities.UniversalFallback{…}, index\_map::MathOptInterface.Utilities.IndexMap, attr::MathOptInterface.NLPBlock)  
@ MathOptInterface.Utilities ~/.julia/packages/MathOptInterface/TYq6d/src/Utilities/copy.jl:51  
[5] pass\_attributes(dest::MathOptInterface.Bridges.LazyBridgeOptimizer{…}, src::MathOptInterface.Utilities.UniversalFallback{…}, index\_map::MathOptInterface.Utilities.IndexMap)  
@ MathOptInterface.Utilities ~/.julia/packages/MathOptInterface/TYq6d/src/Utilities/copy.jl:38  
[6] default\_copy\_to(dest::MathOptInterface.Bridges.LazyBridgeOptimizer{…}, src::MathOptInterface.Utilities.UniversalFallback{…})  
@ MathOptInterface.Utilities ~/.julia/packages/MathOptInterface/TYq6d/src/Utilities/copy.jl:391  
[7] copy\_to  
@ ~/.julia/packages/MathOptInterface/TYq6d/src/Bridges/bridge\_optimizer.jl:443 [inlined]  
[8] optimize!  
@ ~/.julia/packages/MathOptInterface/TYq6d/src/MathOptInterface.jl:121 [inlined]  
[9] optimize!(m::MathOptInterface.Utilities.CachingOptimizer{…})  
@ MathOptInterface.Utilities ~/.julia/packages/MathOptInterface/TYq6d/src/Utilities/cachingoptimizer.jl:327  
[10] optimize!(model::Model; ignore\_optimize\_hook::Bool, \_differentiation\_backend::MathOptInterface.Nonlinear.SparseReverseMode, kwargs::@Kwargs{})  
@ JuMP ~/.julia/packages/JuMP/xlp0s/src/optimizer\_interface.jl:595  
[11] optimize!(model::Model)  
@ JuMP ~/.julia/packages/JuMP/xlp0s/src/optimizer\_interface.jl:546  
[12] top-level scope  
@ REPL[28]:1  
Some type information was truncated. Use `show(err)` to see complete types.

---

<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:** [March 7, 2025, 6:26pm UTC](https://discourse.julialang.org/t/mof-read-from-file-fails-when-using-moi-parameter-and-non-linear-constraints/126688/3 "2025-03-07T18:26:35Z")

</div>

Do

```julia
model_test = read_from_file("test.mof.json"; use_nlp_block = false)

```

I might need to think about how we can do this automatically: [[FileFormats.MOF] add a flag to the file when use\_nlp\_block=false · Issue #2687 · jump-dev/MathOptInterface.jl · GitHub](https://github.com/jump-dev/MathOptInterface.jl/issues/2687)

---

<div class="post-metadata">

**Author:** ![andrewrosemberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrewrosemberg/32/9902_2.png) [@andrewrosemberg](https://discourse.julialang.org/u/andrewrosemberg)\
**Post date:** [March 7, 2025, 9:13pm UTC](https://discourse.julialang.org/t/mof-read-from-file-fails-when-using-moi-parameter-and-non-linear-constraints/126688/4 "2025-03-07T21:13:17Z")

</div>

> [@odow](#):
>
> `model_test = read_from_file("test.mof.json"; use_nlp_block = false)`

He doesn’t wear a cape but he is still a hero! Thank you, it works!
