# JuMP Objective coefficient writing

**URL:** https://discourse.julialang.org/t/jump-objective-coefficient-writing/101286
**Category:** Optimization (Mathematical)
**Created:** [July 7, 2023, 1:47am UTC](https://discourse.julialang.org/t/jump-objective-coefficient-writing/101286 "2023-07-07T01:47:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Luiz\_Monteiro](https://avatars.discourse-cdn.com/v4/letter/l/b77776/32.png) [@Luiz\_Monteiro](https://discourse.julialang.org/u/Luiz_Monteiro)
#### Post date: [July 7, 2023, 1:47am UTC](https://discourse.julialang.org/t/jump-objective-coefficient-writing/101286/1 "2023-07-07T01:47:24Z")

</div>

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:

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

```

Thanks in advance

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [July 7, 2023, 2:01am UTC](https://discourse.julialang.org/t/jump-objective-coefficient-writing/101286/2 "2023-07-07T02:01:24Z")

</div>

> 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.

I think you want instead

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

```

---

<div class="post-metadata">

### Author: ![Luiz\_Monteiro](https://avatars.discourse-cdn.com/v4/letter/l/b77776/32.png) [@Luiz\_Monteiro](https://discourse.julialang.org/u/Luiz_Monteiro)
#### Post date: [July 7, 2023, 9:18pm UTC](https://discourse.julialang.org/t/jump-objective-coefficient-writing/101286/3 "2023-07-07T21:18:54Z")

</div>

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])

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [July 7, 2023, 9:49pm UTC](https://discourse.julialang.org/t/jump-objective-coefficient-writing/101286/4 "2023-07-07T21:49:50Z")

</div>

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:

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

```

Your example would be perhaps:

```julia
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]))

```

---

<div class="post-metadata">

### Author: ![Luiz\_Monteiro](https://avatars.discourse-cdn.com/v4/letter/l/b77776/32.png) [@Luiz\_Monteiro](https://discourse.julialang.org/u/Luiz_Monteiro)
#### Post date: [July 8, 2023, 7:03pm UTC](https://discourse.julialang.org/t/jump-objective-coefficient-writing/101286/5 "2023-07-08T19:03:54Z")

</div>

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