# Construction of the objective function for a mathematical model

**URL:** <https://discourse.julialang.org/t/construction-of-the-objective-function-for-a-mathematical-model/40765>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [June 4, 2020, 9:00pm UTC](https://discourse.julialang.org/t/construction-of-the-objective-function-for-a-mathematical-model/40765 "2020-06-04T21:00:00Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [June 4, 2020, 9:00pm UTC](https://discourse.julialang.org/t/construction-of-the-objective-function-for-a-mathematical-model/40765/1 "2020-06-04T21:00:00Z")

</div>

Could someone help me in the construction of this objective function?  
Every time I try to compile the same array error appears

```julia
using JuMP, Cbc
C1 = [11; 11; 11; 11; 11]
C2 = [50; 50; 50; 50; 50]

#Sets
i = 80 #activities
j = 18 #workcenter
z = 5 #period of time

#variable
@variable(ModeloDeterministico,y[i,j], lower_bound=0)
@variable(ModeloDeterministico,y1[i,j], lower_bound=0)

#objective function
@objective(ModeloDeterministico,Min,sum(C1[1:z]*(y[i,j]+y1[i,j])+C2[1:z]*y1[i,j) for z ∈ 1:i)

```

---

<div class="post-metadata">

**Author:** ![Ian\_Slagle](https://avatars.discourse-cdn.com/v4/letter/i/b5a626/32.png) [@Ian\_Slagle](https://discourse.julialang.org/u/Ian_Slagle)\
**Post date:** [June 4, 2020, 10:12pm UTC](https://discourse.julialang.org/t/construction-of-the-objective-function-for-a-mathematical-model/40765/2 "2020-06-04T22:12:01Z")

</div>

> [@RaquelSantos](#):
>
> `y1[i,j)`

I think you might need a right bracket here

---

<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:** [June 4, 2020, 11:55pm UTC](https://discourse.julialang.org/t/construction-of-the-objective-function-for-a-mathematical-model/40765/3 "2020-06-04T23:55:39Z")

</div>

Please read the first post of [Please read: make it easier to help you - #19](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/19) and provide a reproducible example, along with the text of the error you encounter.

What do you expect to happen? `C1[1:z]` is a vector, `y[i, j] + y1[i, j]` is a scalar. You can’t sum vectors like this, because cause it is essentially (ignoring the scalar) `C1[1:1] + C1[1:2] + C1[1:3] + ...`.

It might be helpful if you can provide the mathematical form of the objective function you are trying to code.

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [June 8, 2020, 12:19pm UTC](https://discourse.julialang.org/t/construction-of-the-objective-function-for-a-mathematical-model/40765/4 "2020-06-08T12:19:41Z")

</div>

This is the objective function that I am trying to build.

```julia
C1z*(yij+y1ij) + C2z*y1ij 

```

I’m having trouble declaring variables with two indexes in the objective function.

```julia
using JuMP, Cbc
Model1 = Model (with_optimizer (Cbc.Optimizer))
C1 = [11; 11; 11; 11; 11]
C2 = [50; 50; 50; 50; 50]

#Sets
i = 80 #activities
j = 18 # time period
z = 5 #work center

#variable
@variable (Model1, y [i, j], lower_bound = 0)
@variable (Model1, y1 [i, j], lower_bound = 0) 

#objective function - wrong function
@objective(Model1,Min,sum(C1[z]*(y[i,j]+y1[i,j])+C2[z]*y1[i,j]) for z ∈ 1:i)

```

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [June 8, 2020, 1:17pm UTC](https://discourse.julialang.org/t/construction-of-the-objective-function-for-a-mathematical-model/40765/5 "2020-06-08T13:17:56Z")

</div>

This is the objective function that I am trying to build.

```julia
C1z*(yij+y1ij) + C2z*y1ij 

```

And this is the model that I’m having to build with this objective function

```julia
using JuMP, Cbc
Model1 = Model (with_optimizer (Cbc.Optimizer))
C1 = [11; 11; 11; 11; 11]
C2 = [50; 50; 50; 50; 50]

#Sets
i = 80 #activities
j = 18 # time period
z = 5 #work center

#variable
@variable (Model1, y [i, j], lower_bound = 0)
@variable (Model1, y1 [i, j], lower_bound = 0) 

#objective function - wrong function
@objective(Model1,Min,sum(C1[z]*(y[i,j]+y1[i,j])+C2[z]*y1[i,j]) for z ∈ 1:i)

```

---

<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:** [June 8, 2020, 1:56pm UTC](https://discourse.julialang.org/t/construction-of-the-objective-function-for-a-mathematical-model/40765/6 "2020-06-08T13:56:36Z")

</div>

Are `y` and `y1` scalars, or are they meant to be indexed over a set

```nohighlight
@variable (Model1, y[1:i, 1:j] >= 0)
@variable (Model1, y1[1:i, 1:j] >= 0) 

```

For your objective, you have some parentheses in the wrong place:

```nohighlight
# Wrong
@objective(Model1,Min,sum(C1[z]*(y[i,j]+y1[i,j])+C2[z]*y1[i,j]) for z ∈ 1:i)
# Right
@objective(Model1,Min,sum(C1[z]*(y[i,j]+y1[i,j])+C2[z]*y1[i,j] for z ∈ 1:i))

```

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [June 8, 2020, 4:16pm UTC](https://discourse.julialang.org/t/construction-of-the-objective-function-for-a-mathematical-model/40765/8 "2020-06-08T16:16:07Z")

</div>

Thanks for the help.  
Now the following error has appeared after compiling the objective function

```julia
BoundsError: attempt to access 5-element Array{Int64,1} at index [6]
getindex(::Array{Int64,1}, ::Int64) at array.jl:731
top-level scope at macros.jl:978

```

---

<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:** [June 8, 2020, 5:04pm UTC](https://discourse.julialang.org/t/construction-of-the-objective-function-for-a-mathematical-model/40765/9 "2020-06-08T17:04:20Z")

</div>

That’s because `z = 5`, but `i = 80`.

You need to properly define the linear program you are trying to build.

What are the sets?  
What are the decision variables? (What are they indexed over?)  
What is the objective function? (In math, with the proper sums and indices.)

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [June 8, 2020, 7:40pm UTC](https://discourse.julialang.org/t/construction-of-the-objective-function-for-a-mathematical-model/40765/12 "2020-06-08T19:40:55Z")

</div>

What are the sets? `i = 80, j = 18, z = 5`  
What are the decision variables? (What are they indexed on?) The variables must be indexed to `"i"` which are the activities and `"j"` time period. The variables `y[i,j] and y1[i,j]`  
What is the objective function? (In mathematics, with the appropriate sums and indices.)

```julia
11[5]*(y[80,18]+y1[80,18])+50[5]*y1[80,18]

@objective(Model1,Min,sum(C1[z]*(y[i,j]+y1[i,j])+C2[z]*y1[i,j] for z ∈ 1:i))

```

But once thanks for trying to help me!!

---

<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:** [June 9, 2020, 3:02am UTC](https://discourse.julialang.org/t/construction-of-the-objective-function-for-a-mathematical-model/40765/13 "2020-06-09T03:02:40Z")

</div>

But `i` is just the number `80`. Do you mean `1, 2, ..., 80`?

If `i` is just a number, don’t you just want:

```nohighlight
@objective(Model1, Min, C1[z] * (y[i, j] + y1[i, j]) + C2[z] * y1[i, j])

```

---

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [June 9, 2020, 12:27pm UTC](https://discourse.julialang.org/t/construction-of-the-objective-function-for-a-mathematical-model/40765/15 "2020-06-09T12:27:50Z")

</div>

It goes from `1, 2, .... 80`

---

<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:** [June 9, 2020, 1:29pm UTC](https://discourse.julialang.org/t/construction-of-the-objective-function-for-a-mathematical-model/40765/16 "2020-06-09T13:29:11Z")

</div>

If the index is from `1:80`, you need to create your variables like

```Julia
model = Model()
@variable(model, y[1:80, 1:18] >= 0)

```

Here is the documentation: [Variables · JuMP](https://jump.dev/JuMP.jl/v0.21.1/variables/#Variable-containers-1)

I think you need to step back from JuMP, and work out, on paper, how to formulate your model appropriately. In particular:

> What is the objective function? (In mathematics, with the appropriate sums and indices.)
