# Objective function declaration issue

**URL:** <https://discourse.julialang.org/t/objective-function-declaration-issue/45028>\
**Category:** Optimization (Mathematical)\
**Created:** [August 16, 2020, 7:44am UTC](https://discourse.julialang.org/t/objective-function-declaration-issue/45028 "2020-08-16T07:44:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mamo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mamo/32/25591_2.png) [@mamo](https://discourse.julialang.org/u/mamo)\
**Post date:** [August 16, 2020, 7:44am UTC](https://discourse.julialang.org/t/objective-function-declaration-issue/45028/1 "2020-08-16T07:44:19Z")

</div>

If I have a member of an objective function:  
sum((setup\_cost[f,p,j]\*PR[f,p,j,t]+oper\_cost[p,j]\*PRD[o,f,p,j,t]) for o in O,f in FAM, p in PROD[f], j in U, t in TIME)  
and I want to limit the counter o, f and p on 1…end-1 (in arrays), how can I do this?  
Thanks!  
marco

---

<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:** [August 17, 2020, 3:46am UTC](https://discourse.julialang.org/t/objective-function-declaration-issue/45028/2 "2020-08-17T03:46:06Z")

</div>

It’s easier to help if you provide a minimal working example. Take a read of the first post in [Please read: make it easier to help you - #81](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/81).

Presumably, you want something like

```nohighlight
@objective(
    model, 
    Min,
    sum(
        setup_cost[f, p, j] * PR[f, p, j, t] + 
        oper_cost[p, j] * PRD[o, f, p, j, t]
        for o in O[1:end-1]
        for f in FAM[1:end-1]
        for p in PROD[f][1:end-1]
        for j in U
        for t in TIME
    )
)

```

---

<div class="post-metadata">

**Author:** ![mamo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mamo/32/25591_2.png) [@mamo](https://discourse.julialang.org/u/mamo)\
**Post date:** [August 17, 2020, 7:29am UTC](https://discourse.julialang.org/t/objective-function-declaration-issue/45028/3 "2020-08-17T07:29:07Z")

</div>

> [@odow](#):
>
> ```julia
> @objective(
> model, 
> Min,
> sum(
> setup_cost[f, p, j] * PR[f, p, j, t] + 
> oper_cost[p, j] * PRD[o, f, p, j, t]
> for o in O[1:end-1]
> for f in FAM[1:end-1]
> for p in PROD[f][1:end-1]
> for j in U
> for t in TIME
> )
> )
> 
> ```

Thanks odow, so “end” is also valid operator, not only length(array)?

---

<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:** [August 17, 2020, 7:42am UTC](https://discourse.julialang.org/t/objective-function-declaration-issue/45028/4 "2020-08-17T07:42:33Z")

</div>

> so “end” is also valid operator,

Yes. Here is the documentation for Julia arrays: [Single- and multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing-1)

---

<div class="post-metadata">

**Author:** ![mamo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mamo/32/25591_2.png) [@mamo](https://discourse.julialang.org/u/mamo)\
**Post date:** [August 17, 2020, 7:43am UTC](https://discourse.julialang.org/t/objective-function-declaration-issue/45028/5 "2020-08-17T07:43:46Z")

</div>

Thanks odow.
