Exploring a model?

I have been investigating JuMP (with optimizers CBC, CLP, and GLPK) for linear models. I can obtain the values of the decision variables, the optimum of the objective function, the shadow prices of the binding constraints, and of course other values as well. And I can write the model to a file in a variety of formats.

It’s clear that the call to optimize! produces a great deal of information (depending on the solver), some of which is available through commands such as value, shadow_price, objective_value and so on.

What I want to know is: is there some way of seeing all the information held in a model after a successful optimization? That is, can I peer at the internal structure of a model and of the values of its various fields? I’m curious to know what’s in a model, so to speak. Is it possible, for example, to express a model as a database containing all its fields and their values? Or is there some way of “dumping” a model and all of its contents?

I’m using Julia 1.4.1 and JuMP 0.21.4.

Thanks very much.

2 Likes

This is not specific for JuMP models, but if the results are finally stored in a structure, you could use something like:

julia> struct A
         a
         b
       end

julia> x = A(1,2.0)
A(1, 2.0)
julia> for field in fieldnames(A)
         println("Field $field, value = ",getfield(x,field))
       end
Field a, value = 1
Field b, value = 2.0

I would not suggest this solution here. The results are not always stored in a structure at the Julia side, depending on the wrapper they are requested from C code on demand (i.e., they are stored only in a C structure and not in the corresponding Julia one). Also, I helped to write the Gurobi wrapper, and it is an even worse case, it has fields for cache (and not for all properties), so the values inspected may not be the correct values at the moment (it depends if those values were requested again after a re-solve, for example, then the cache will be refreshed correctly).

One problem you may hit, it is that not all solvers give the same information (many properties are common, but not all of them). And, as I said before, they are not always stored in Julia, and the name the solver gave to them (which you may need to use to query them) may be different from solver to solver. For the most important and common attributes, the people from JuMP have created functions with the same name that query independently from the underlying model, but for others you have to pass a String with the name of the attribute for the specific solver.

I would recommend you to read JuMP and MathOptInterface documentation to get a better grip of what is available, and then create your own function, suited to what matters to you.

3 Likes

is there some way of seeing all the information held in a model after a successful optimization? That is, can I peer at the internal structure of a model and of the values of its various fields? I’m curious to know what’s in a model, so to speak. Is it possible, for example, to express a model as a database containing all its fields and their values? Or is there some way of “dumping” a model and all of its contents?

No. Cbc, Clp, and GLPK are all written in C, which makes it hard to see “the” model.

You should use termination_status, primal_status and dual_status to check what type of solution the solver found. Then use value, shadow_price, and objective_value as appropriate.

2 Likes

Thank you everybody. I sort of guessed that a single easy solution would not be possible, but now I know it for sure. I’d still be curious - even if it meant experimenting with C code - to “look” inside a model somehow… but I’ll leave that for another time. For now, again many thanks.

One issue is that every solver is different, even solvers which claim to be pure LP solvers. This was one motivation for MathOptInterface, see http://www.optimization-online.org/DB_HTML/2020/02/7609.html.

The source for Clp is available, https://github.com/coin-or/Clp, but I wouldn’t recommend diving into 100,000 lines of C++ to go looking for the model.

The Gurobi C API is well documented, https://www.gurobi.com/documentation/9.0/refman/c_api_details.html, and there are a large number of attributes you can query about the model, https://www.gurobi.com/documentation/9.0/refman/attributes.html. The master branch of Gurobi.jl now wraps the complete C API, so this is all accessible from Julia.

2 Likes