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