# JuMP Objective coefficient writing

Hi,
I am trying to write an objective using index for each element of an expression, just like a simple constraint (example: i, h, k in `@constraint(model1, con19B[i in V0_customer, h in Vstation, j in V0_customer, k in B], z1[i,h,j,k] <= y[h])`
However, I cant find how to do it. My example below is not working:

``````@objective(model1, Min, [g in VArc_dummy_length, k in Vdominante_length],
sum(x[Arc_dummy[g],Arc_dummy[g],Arc_dummy[g]]* Mtime[Arc_dummy[g],Arc_dummy[g]]))
+ sum(x[Arc_dominante_strg[k],Arc_dominante_strg[k],Arc_dominante_strg[k]]* Mtime[Arc_dominante_strg[k],Arc_dominante_strg[k]]
+ x[Arc_dominante_strg[k],Arc_dominante_strg[k],Arc_dominante_strg[k]]* Mtime[Arc_dominante_strg[k],Arc_dominante_strg[k]])
+ sum(Δ[Arc_dominante_strg[k],Arc_dominante_strg[k],Arc_dominante_strg[k]])
``````

write an objective using index for each element of an expression

Do you want to sum these elements? Typically, JuMP models require a scalar objective value.

``````@objective(
model1,
Min,
sum(
#  ... stuff here ...
for g in VArc_dummy_length, k in Vdominante_length
)
)
``````

Hi @odow , is not exactally to sum over those element index.
I want something like this ilustrative example:
Vtotal =[1, 2, 3, 4, 5]
Vstation = [4,5]
@variable(model1, x[i in Vtotal, h in Vstation, j in Vtotal], Bin)

V1 = [ [1,5,2] [1,4,2],[2,5,1],[3,5,2]] #V1 is a subset of specific arcs index

#The objective function should sum only over the specific arcs index at V1
@objective(model1, Min, x[1,5,2] + x[1,4,2] + x[2,5,1] + [3,5,2])

I don’t really understand how that example matches your previous example, but as a general rule, your objective should return a scalar. You can’t write something like:

``````@objective(model1, Min, [g in VArc_dummy_length, k in Vdominante_length], ... )
``````

``````Vtotal = [1, 2, 3, 4, 5]
Vstation = [4, 5]
V1 = [ [1,5,2] [1,4,2],[2,5,1],[3,5,2]]
model = Model()
@variable(model, x[Vtotal, Vstation, Vtotal], Bin)
@objective(model, Min, sum(x[i,h,j] for (i,h,j) in V1]))
``````
1 Like

Thanks @odow ,
It worked!
I think my first example wasnt clear, sorry for that.

1 Like