# Construction of the objective function for a mathematical model

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

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

I think you might need a right bracket here

Please read the first post of PSA: make it easier to help you 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.

1 Like

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

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

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

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

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

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

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

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

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

``````@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:

``````# 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))
``````

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

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

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

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

``````11*(y[80,18]+y1[80,18])+50*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!!

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

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

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

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

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

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

Here is the documentation: 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.)

2 Likes