# How to save nonlinear JuMP model to file?

**URL:** <https://discourse.julialang.org/t/how-to-save-nonlinear-jump-model-to-file/86322>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, nlp\
**Created:** [August 25, 2022, 12:28pm UTC](https://discourse.julialang.org/t/how-to-save-nonlinear-jump-model-to-file/86322 "2022-08-25T12:28:43Z")\
**Posts on this page:** 2\
**Page:** 2

<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 30, 2022, 9:48pm UTC](https://discourse.julialang.org/t/how-to-save-nonlinear-jump-model-to-file/86322/21 "2022-08-30T21:48:38Z")

</div>

You need something like:

```julia
using JuMP, Symbolics, Ipopt

to_jump_expr(c::Any, ::Dict{Symbol}) = c
to_jump_expr(x::Symbol, map::Dict{Symbol}) = JuMP.index(map[x])
to_jump_expr(f::Function, ::Dict{Symbol}) = Symbol(f)
function to_jump_expr(expr::Expr, map::Dict{Symbol})
    if Meta.isexpr(expr, :call, 3) && expr.args[1] == getindex
        return JuMP.index(getindex(map[expr.args[2]], expr.args[3]))
    end
    for i in 1:length(expr.args)
        expr.args[i] = to_jump_expr(expr.args[i], map)
    end
    return expr
end

foo(x, y) = sqrt(sin(x) + cos(y))

Symbolics.@variables X[1:2]
sym_expr = Symbolics.toexpr(foo(X[1], X[2]))

model = Model()
@variable(model, x[1:2])
jump_expr = to_jump_expr(sym_expr, Dict(:X => x))
add_nonlinear_constraint(model, :($jump_expr <= 0))

```

In general, I don’t like the macro approaches. Just walk the expression and modify it as needed.

---

<div class="post-metadata">

**Author:** ![a6a3uh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/a6a3uh/32/33773_2.png) [@a6a3uh](https://discourse.julialang.org/u/a6a3uh)\
**Post date:** [August 31, 2022, 11:13am UTC](https://discourse.julialang.org/t/how-to-save-nonlinear-jump-model-to-file/86322/22 "2022-08-31T11:13:37Z")

</div>

That works well! Thanks again for great recipe!

And thank you for quick fix to the atan2 issue in MathOptInterface repo! It works as well. At least program producing no errors and able to calculate some trajectories.

[Previous page](https://discourse.julialang.org/t/how-to-save-nonlinear-jump-model-to-file/86322.md?page=1)
