How to debug JuMP code (not debug model feasiblity, etc.) in VScode

Hi, all

When the jump macro is included in my code, the debug using vscode always jumps to some internal functions. What I want to ask is how to step through these codes without jumping into places where no breakpoints are set. for example:

model = Model(Gurobi.Optimizer)
A = 2
B = 10
@variable(model, x[1:A, 1:B]) 
@constraint(model, sum(x) == 10)

When I set a breakpoint at the position of @variable(model, x[1:A, 1:B]) and step through it, it will be transferred to macros.jl.

When I set a breakpoint at the position of @constraint(model, sum(x) == 10) and step through it, it will be transferred to rewrite.jl, and then jump to macros.jl.

My current approach is to write println(...) after the code that needs to set breakpoints to see what variables or constraints are created. This is obviously very troublesome, especially when these JuMP macros are in for loops or functions. Difficult to modify my code in real time based on the results of debugging

By the way, clicking VScode’s debug button always takes a very long time to start. Is there any way to speed up this process?

Thanks a lot !

Others here may have advice (and I’d also be interested in hearing it), but I’ve had similar experience with the debugger.

The problem is that the code JuMP emits is not friendly to the debugger, so you can’t step into or through the @variable and @constraint macros in any meaningful way (i.e., if you write sum(x[k] for k in 1:2) we don’t actually call this function as written).

If you use the debugger, you’ll probably have to continue using print and setting breakpoints.

I’d suggest you avoid the debugger entirely and:

  1. build and test each constraint in isolation
  2. make use of print(model) or write_to_file(model, "model.lp") to see if the model is being constructed as you expect
  3. Make use of the Julia logging tools Logging · The Julia Language
1 Like