I’m pretty sure this question will be redundant, but I could not find an answer anywhere. My question is more like asking a style guide so that my code can avoid any unnecessary build/calculation time.
Q1. My guess is basically I should use
@expression whenever possible rather than using
@constraint to reduce the size of an optimization problem. Can anyone confirm this?
Q2. In pre-solve or any internal process to determine the class of optimization (either provided by JuMP or Solvers), can one be faster than the other? For example, in model2 below, it is straightforward that the objective is quadratic (variable x variable), but I’m not sure how model1 determines it is quadratic (variable x expression).
In the examples below,
@expression to define the objective while
using JuMP, Ipopt model1 = Model(Ipopt.Optimizer) @variables(model1, begin x >= 0 y >= 0 end) ex = @expression(model1, 2x + y - 1) @objective(model1, Min, 2 * x * ex - 1) optimize!(model1) value(ex)
using JuMP, Ipopt model2 = Model(Ipopt.Optimizer) @variables(model2, begin x >= 0 y >= 0 ex end) @constraint(model2, ex == 2x + y - 1) @objective(model2, Min, 2 * x * ex - 1) optimize!(model2) value(model2[:ex])