# Ways to save and load JuMP models from/to disk or memory

**URL:** <https://discourse.julialang.org/t/ways-to-save-and-load-jump-models-from-to-disk-or-memory/33465>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, io\
**Created:** [January 16, 2020, 11:26pm UTC](https://discourse.julialang.org/t/ways-to-save-and-load-jump-models-from-to-disk-or-memory/33465 "2020-01-16T23:26:17Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![rezabayani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rezabayani/32/25954_2.png) [@rezabayani](https://discourse.julialang.org/u/rezabayani)\
**Post date:** [April 12, 2022, 11:18pm UTC](https://discourse.julialang.org/t/ways-to-save-and-load-jump-models-from-to-disk-or-memory/33465/21 "2022-04-12T23:18:58Z")

</div>

Thank you it works!

---

<div class="post-metadata">

**Author:** ![rezabayani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rezabayani/32/25954_2.png) [@rezabayani](https://discourse.julialang.org/u/rezabayani)\
**Post date:** [June 24, 2022, 11:42pm UTC](https://discourse.julialang.org/t/ways-to-save-and-load-jump-models-from-to-disk-or-memory/33465/22 "2022-06-24T23:42:23Z")

</div>

Dear @odow I have reached a stage where I need to load my \*.mps models, solve them, and access the value of some of the variables. My models are loaded and solved to optimality (with objective values equal to what is expected because I have solved them earlier before storing in “mps” format). However, I fail to retrieve variable names and values.  
Here is what I am encountering:

```julia
julia> UC_Model_load = read_from_file(mps_name)
A JuMP Model
Minimization problem with:
Variables: 1176
Objective function type: GenericAffExpr{Float64,VariableRef}
`GenericAffExpr{Float64,VariableRef}`-in-`MathOptInterface.EqualTo{Float64}`: 504 constraints
`GenericAffExpr{Float64,VariableRef}`-in-`MathOptInterface.GreaterThan{Float64}`: 912 constraints
`GenericAffExpr{Float64,VariableRef}`-in-`MathOptInterface.LessThan{Float64}`: 1032 constraints
`VariableRef`-in-`MathOptInterface.Interval{Float64}`: 816 constraints
`VariableRef`-in-`MathOptInterface.ZeroOne`: 360 constraints
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> set_optimizer(UC_Model_load, Gurobi.Optimizer)
julia> optimize!(UC_Model_load);
julia> valu.(p_g) # p_g is a variable in my model
ERROR: UndefVarError: p_g not defined
julia> object_dictionary(UC_Model_load)
Dict{Symbol,Any} with 0 entries

```

On another note, could you please tell me how is it possible to access all of the variable and constraint containers in a model? I fail to find a way to access them even in cases where I am building the model not loading it. I know of `object_dictionary()` but it returns all of the var/cons holders together. Using `MOI.get(model, MOI.ListOfVariableIndices())` does not return anything useful other than:

```nohighlight
MathOptInterface.VariableIndex(1)
... 
MathOptInterface.VariableIndex(N)

```

---

<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:** [June 24, 2022, 11:51pm UTC](https://discourse.julialang.org/t/ways-to-save-and-load-jump-models-from-to-disk-or-memory/33465/23 "2022-06-24T23:51:39Z")

</div>

See the documentation: [Models · JuMP](https://jump.dev/JuMP.jl/stable/manual/models/#Read-a-model-from-file)

You cannot access variables like `p_g` just by reading a model, because `UC_Model_load` is different from the model in which `p_g` is defined. In addition, reading the model from MPS does not preserve variable and constraint containers, and there is no way to recover them.

What are trying to achieve? Reading and writing to MPS files might be the wrong way to go about it.

---

<div class="post-metadata">

**Author:** ![rezabayani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rezabayani/32/25954_2.png) [@rezabayani](https://discourse.julialang.org/u/rezabayani)\
**Post date:** [June 25, 2022, 12:51am UTC](https://discourse.julialang.org/t/ways-to-save-and-load-jump-models-from-to-disk-or-memory/33465/24 "2022-06-25T00:51:20Z")

</div>

Thank you for the prompt repsonse, @odow Well, It would be useful to be able to save a given JuMP model and load it later on. That’s what I am trying to achieve. By the way, how do I access variables/constraints in loaded models with `variable_by_name` method? It returns nothing:

```julia
julia> my_var = variable_by_name(UC_Model_load, "p_g")
julia> value.(my_var)
ERROR: MethodError: no method matching value(::Nothing)

julia> my_var = @variable(UC_Model_load, base_name = "p_g")
julia> value.(my_var)
ERROR: OptimizeNotCalled() # even though it is already called

```

EDIT: This issue was fixed after I assigned indices to my variables.  
What about the second part of my previous issue. How can we access all of the variables and constraints in a given JuMP model?

---

<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:** [June 25, 2022, 9:38am UTC](https://discourse.julialang.org/t/ways-to-save-and-load-jump-models-from-to-disk-or-memory/33465/25 "2022-06-25T09:38:54Z")

</div>

> It would be useful to be able to save a given JuMP model and load it later on

Rather than try to save a JuMP model, structure your code to save data to a file, and then write a function which builds the JuMP model from the saved data. See this tutorial for an example:

- [Design patterns for larger models · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/design_patterns_for_larger_models)

> By the way, how do I access variables/constraints in loaded models with `variable_by_name` method? It returns nothing:

See the documentation, which describes some limitations:

- [Variables · JuMP](https://jump.dev/JuMP.jl/stable/manual/variables/#Retrieve-a-variable-by-name)
- [Constraints · JuMP](https://jump.dev/JuMP.jl/stable/manual/constraints/#Retrieve-a-constraint-by-name)

> How can we access all of the variables and constraints in a given JuMP model?

Use `all_variables`, `list_of_constraint_types`, and `all_constraints`. See:

- [https://jump.dev/JuMP.jl/stable/reference/variables/#JuMP.all\_variables](https://jump.dev/JuMP.jl/stable/reference/variables/#JuMP.all_variables)
- [Constraints · JuMP](https://jump.dev/JuMP.jl/stable/manual/constraints/#Accessing-constraints-from-a-model)

---

<div class="post-metadata">

**Author:** ![rezabayani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rezabayani/32/25954_2.png) [@rezabayani](https://discourse.julialang.org/u/rezabayani)\
**Post date:** [June 28, 2022, 12:50am UTC](https://discourse.julialang.org/t/ways-to-save-and-load-jump-models-from-to-disk-or-memory/33465/26 "2022-06-28T00:50:11Z")

</div>

Thank you @odow, this helps a lot.

---

<div class="post-metadata">

**Author:** ![Betristor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/betristor/32/45264_2.png) [@Betristor](https://discourse.julialang.org/u/Betristor)\
**Post date:** [March 16, 2023, 8:13am UTC](https://discourse.julialang.org/t/ways-to-save-and-load-jump-models-from-to-disk-or-memory/33465/28 "2023-03-16T08:13:23Z")

</div>

I saved my model into a lp file and then want to recover it from file. `read_from-file` functioned well but after solving the model, no reference is reserved and I can’t access the variable and expressions I defined for wring outputs. Is there a way to get my defined reference name back?

---

<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 16, 2023, 7:44pm UTC](https://discourse.julialang.org/t/ways-to-save-and-load-jump-models-from-to-disk-or-memory/33465/29 "2023-03-16T19:44:23Z")

</div>

See [Models · JuMP](https://jump.dev/JuMP.jl/stable/manual/models/#Read-a-model-from-file)

You need to use `variable_by_name` and `constraint_by_name` to access variables in a model that you have read from file.

---

<div class="post-metadata">

**Author:** ![Betristor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/betristor/32/45264_2.png) [@Betristor](https://discourse.julialang.org/u/Betristor)\
**Post date:** [March 17, 2023, 4:07am UTC](https://discourse.julialang.org/t/ways-to-save-and-load-jump-models-from-to-disk-or-memory/33465/30 "2023-03-17T04:07:11Z")

</div>

Thanks, but I want to fuifill a function like this. If the model has been created, then read the model file and solve it directly, otherwise create the model from scratch and then solve it. The problem lies here that when I want to access the variables, expressions and constraints in the model after solving, the model read from file could not fit in the original output writing schema compared to the model created from scratch. Using `variable_by_name` and `variable_by_name` is quite effort-needing for me.

---

<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 17, 2023, 7:09am UTC](https://discourse.julialang.org/t/ways-to-save-and-load-jump-models-from-to-disk-or-memory/33465/31 "2023-03-17T07:09:16Z")

</div>

> I want to fuifill a function like this. If the model has been created, then read the model file and solve it directly, otherwise create the model from scratch and then solve it.

There is no way to serialize a JuMP model to disk that includes the variables, expressions, and constraints in the original form.

All the file formats supported by JuMP, including `.lp` files, are rudimentary plain-text file formats that are not specific to Julia. JuMP cannot recover the structural form of your model by reading in a model.

You’re better off saving the input data in a file format like JSON, and rebuilding the model based on that.

Take a read of this tutorial, it might point you in the right direction: [Design patterns for larger models · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/design_patterns_for_larger_models/)

---

<div class="post-metadata">

**Author:** ![Betristor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/betristor/32/45264_2.png) [@Betristor](https://discourse.julialang.org/u/Betristor)\
**Post date:** [March 17, 2023, 7:39am UTC](https://discourse.julialang.org/t/ways-to-save-and-load-jump-models-from-to-disk-or-memory/33465/32 "2023-03-17T07:39:38Z")

</div>

Much thanks. But I do regard that if JuMP/Julia could implement this, it should be more convenient for users cause they could seperate the model building and solving by breaking this process into two - one for constructing model and one for solving, connected by the model file I/O.

---

<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 17, 2023, 9:58pm UTC](https://discourse.julialang.org/t/ways-to-save-and-load-jump-models-from-to-disk-or-memory/33465/33 "2023-03-17T21:58:32Z")

</div>

> they could seperate the model building and solving by breaking this process into two - one for constructing model and one for solving, connected by the model file I/O

This is the wrong approach when using JuMP because JuMP doesn’t communicate with solvers via file I/O. Instead, in most cases it uses the direct C API.

As shown in that tutorial, you’re better off storing the _data_ in a file format, and using a Julia script to build the model.

[Previous page](https://discourse.julialang.org/t/ways-to-save-and-load-jump-models-from-to-disk-or-memory/33465.md?page=1)
