# Sum for components with index smaller than another index in constraints

**URL:** <https://discourse.julialang.org/t/sum-for-components-with-index-smaller-than-another-index-in-constraints/82117>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [June 2, 2022, 9:45am UTC](https://discourse.julialang.org/t/sum-for-components-with-index-smaller-than-another-index-in-constraints/82117 "2022-06-02T09:45:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chenr86](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chenr86/32/36785_2.png) [@chenr86](https://discourse.julialang.org/u/chenr86)\
**Post date:** [June 2, 2022, 9:45am UTC](https://discourse.julialang.org/t/sum-for-components-with-index-smaller-than-another-index-in-constraints/82117/1 "2022-06-02T09:45:05Z")

</div>

Hello,  
I want to achieve summation of components with index smaller than i in the expression, and used in constraint. That is to say, the components in `level` is related to each other, e.g. `level[2]` is the sum of `level[1]` and another value. I have tried this, but it failed.

```julia
@expression(
    model, 
    level[i = 1:numtime],
    initial_level + inflow * sum(raintime[j] for j in 1:i) * 3600 /k 
)
for i in 1:numtime
    @constraint(model, min_level <= level[i] <= max_level)
end

```

I got error message. How can I post it correctly?

> At In[164]:122: `@constraint(model, min_level <= level[i] <= max_level)`: Unrecognized constraint building format. Tried to invoke `build_constraint(error, 274.0334413729147, 230, 275.17)`, but no such method exists. This is due to specifying an unrecognized function, constraint set, and/or extra positional/keyword arguments.
> 
> If you’re trying to create a JuMP extension, you need to implement `build_constraint` to accomodate these arguments.

---

<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 2, 2022, 7:22pm UTC](https://discourse.julialang.org/t/sum-for-components-with-index-smaller-than-another-index-in-constraints/82117/2 "2022-06-02T19:22:49Z")

</div>

Can you post a fully reproducible example?

It looks like one if your constraints has only constants, and no variables.

---

<div class="post-metadata">

**Author:** ![chenr86](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chenr86/32/36785_2.png) [@chenr86](https://discourse.julialang.org/u/chenr86)\
**Post date:** [June 6, 2022, 1:14am UTC](https://discourse.julialang.org/t/sum-for-components-with-index-smaller-than-another-index-in-constraints/82117/3 "2022-06-06T01:14:41Z")

</div>

Sure, I post my code here.

Infact, discharge[i] is related to level[i-1], and level[i-1] is related to discharge[i-2]… I don’t know how to coding this relationship, so I used a simple case: discharge is independent to water level, but the level is calculated with discharge.

It would be very nice for you to give me some advice for coding the real relationship between discharge flow rate and water level.

```julia
using JuMP
# using Juniper
using AmplNLWriter, Bonmin_jll
using Pavito
using Ipopt

totaltime = 3                          
deltatime = 12                       
numtime = Int(totaltime * 24 / deltatime)
totalraintime = 45                     
inflow = 500

raintime = [0 for i in 1:numtime]
for i in 1:numtime
    if (totalraintime - (i-1) * deltatime) > deltatime
        raintime[i] = deltatime
    else
        raintime[i] = max(0, totalraintime - (i-1) * deltatime)
    end
end

initial_level = 273                  
min_level = 230                     
max_level = 275.17                  
min_discharge = 0                    
max_discharge = 3140       
inflow = 500                        
d = 57000                           
k = 2.090104051e7

model = Model(() -> AmplNLWriter.Optimizer(Bonmin_jll.amplexe))

@variable(model, 0 <= n[1:numtime] <= 60, Int)
@expression(
    model, 
    flow[i = 1:numtime],
    (0.685 - 0.19 * n[i] * 0.05 / (initial_level - 261)) * 0.5 * n[i] * sqrt(2 * 9.81 * (initial_level - 261)) 
)

@expression(
    model, 
    level[i = 1:numtime],
    initial_level + (inflow * sum(raintime[j] for j in 1:i) * 3600 - sum(flow[j] for j in 1:i) * 43200)/k 
)

@objective(model, Min, sum(flow[i] for i in 1:numtime))
for i in 1:numtime
    @constraint(model, min_discharge <= flow[i] <= max_discharge)
    @constraint(model, min_level <= level[i] <= max_level)
end

optimize!(model)

@show termination_status(model)
@show primal_status(model)
@show dual_status(model)
@show objective_value(model)

for i in 1:numtime
    print(" $(n[i]) = $(value(n[i]))\t")
end

```
