Specifying objective coefficient when creating a JuMP variable

What about adding a kwarg option obj in JuMP.@variable so that we can specify objective coefficients? just like the existing lower_bound?

julia> import JuMP

julia> model = JuMP.Model();

julia> JuMP.set_objective_sense(model, JuMP.MIN_SENSE);

julia> # [I propose this] JuMP.@variable(model, x[i=1:2, j=1:3], lower_bound=i-j, obj=i+j);

julia> JuMP.@variable(model, x[i=1:2, j=1:3], lower_bound=i-j); # existing

julia> for i=1:2, j=1:3 # currently I have to single this part out
           JuMP.set_objective_coefficient(model, x[i,j], i+j)
       end

julia> print(model)
Min 2 x[1,1] + 3 x[1,2] + 4 x[1,3] + 3 x[2,1] + 4 x[2,2] + 5 x[2,3]
Subject to
 x[1,1] ≥ 0
 x[2,1] ≥ 1
 x[1,2] ≥ -1
 x[2,2] ≥ 0
 x[1,3] ≥ -2
 x[2,3] ≥ -1

(I happen to think of this today, it’s a different issue. I put it here for convenience)

I think the design of the JuMP container is somewhat inconsistent. Look at

julia> JuMP.@variable(model, x[i=1:2, j=1:3])
2×3 Matrix{JuMP.VariableRef}:
 x[1,1]  x[1,2]  x[1,3]
 x[2,1]  x[2,2]  x[2,3]

julia> JuMP.@variable(model, y[i=1:2, j=ifelse(i>1, 2:3, 1:1)])
JuMP.Containers.SparseAxisArray{JuMP.VariableRef, 2, Tuple{Int64, Int64}} with 3 entries:
  [1, 1]  =  y[1,1]
  [2, 2]  =  y[2,2]
  [2, 3]  =  y[2,3]

For the x, from julia’s (column-wise storage) standpoint, j is the “major” or “outer” index.,
But concerning y, i becomes the “major” or “outer” index.

What I’m thinking is that if we change the JuMP’s grammar for the y’s construction to be

JuMP.@variable(model, y[j=ifelse(i>1, 2:3, 1:1), i=1:2])

, it would seems more consistent to me, resembling the math textbook style, e.g.

" some assertion about s holds, \forall s \in S(n), n \in \mathbb{Z}"

What about adding a kwarg option obj in JuMP.@variable so that we can specify objective coefficients? just like the existing lower_bound ?

I don’t think I want to add this. Use @objective or set_objective_coefficient instead.

The reason that lower_bound etc exist is to support anonymous variables with bounds, so @variable(model, lower_bound = 0). I strongly prefer that people use the >= syntax where possible.

it’s a different issue. I put it here for convenience)

In future, please make separate posts for separate issues.

I think the design of the JuMP container is somewhat inconsistent

The loops are unrolled left-to-right, following Julia’s convention:

julia> [(i, j) for i in 1:2, j in 1:3]
2×3 Matrix{Tuple{Int64, Int64}}:
 (1, 1)  (1, 2)  (1, 3)
 (2, 1)  (2, 2)  (2, 3)

Changing this is a breaking change that we will not be making.