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

,

Thank you it works!

1 Like

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> 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:

MathOptInterface.VariableIndex(1)
... 
MathOptInterface.VariableIndex(N)

See the documentation: Models ¡ JuMP

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.

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> 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?

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:

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:

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:

1 Like

Thank you @odow, this helps a lot.

1 Like

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?

See Models ¡ JuMP

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

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.

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

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.

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.