@expression v.s. @constraint (when to use what)

Thank you. I just noticed it. In short, ex = @expression(model1, 2x + y - 1) is preferred:

Use JuMP’s macros (or add_to_expression! to build expressions. Avoid constructing expressions outside the macros.

As a side note, it may be better to define a named expression with @expression(model1, ex, 2x + y - 1) such that we can access it later in a different scope with model[:ex], e.g., when a model is returned from a builder function.

2 Likes