# Objective function error

**URL:** <https://discourse.julialang.org/t/objective-function-error/42482>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [July 3, 2020, 4:13pm UTC](https://discourse.julialang.org/t/objective-function-error/42482 "2020-07-03T16:13:22Z")\
**Posts on this page:** 3\
**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:** [July 3, 2020, 4:13pm UTC](https://discourse.julialang.org/t/objective-function-error/42482/1 "2020-07-03T16:13:22Z")

</div>

I need to reproduce that model.

```julia
using JuMP, Cbc
Model1 = Model(with_optimizer(Cbc.Optimizer))

a = 2
t = 2 
w = 2

A = 1:a 
T = 1:t 
W = 1:w 

CR = [11, 11] 

@variable(Model1,r[A,T], lower_bound=0)
@variable(Model1,o[A,T], lower_bound=0) 

@objective(Model1,Min,sum((CR[w]*(r[a,t]+o[a,t])) for w in W, a in A, t in T))

```

But when I try to compile the objective function it adds up the values and gives me the following result:

```julia
22 r[1,1] + 22 o[1,1] + 22 r[1,2] + 22 o[1,2] + 22 r[2,1] + 22 o[2,1] + 22 r[2,2] + 22 o[2,2]

```

When should you submit

```julia
11 r[1,1] + 11 o[1,1] + ....

```

What should I do?

---

<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 3, 2020, 5:26pm UTC](https://discourse.julialang.org/t/objective-function-error/42482/2 "2020-07-03T17:26:47Z")

</div>

JuMP will build the model that you formulate. If it builds something that you are not expecting, that means there is an error in your code.

For example, you might be missing `0.5 * sum( ... )`. Or it might be some other error, like you are not meant to sum over `W`.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 3, 2020, 6:18pm UTC](https://discourse.julialang.org/t/objective-function-error/42482/3 "2020-07-03T18:18:41Z")

</div>

Your objective function has a `for w in W`, this means for each pair of values `a` and `t` can assume (and, consequently, for each variable you have), you will have a loop with `w == 1` and then another with `w == 2`, so what you do is: `11 r[1,1] + 11 o[1,1] ... 11 r[2,2] + 11 o[2,2]` and then again `11 r[1,1] + 11 o[1,1] ... 11 r[2,2] + 11 o[2,2]`. JuMP simplifies this to the completely equivalent expression `22 r[1,1] + 22 o[1,1] ... 22 r[2,2] + 22 o[2,2]` (a single time). There is no bug in JuMP. Your code is doing exactly what is written it should do. There is no difference between two instances of `11 r[1, 1]` and one instance of `22 r[1, 1]`.
