# Flow conservation constraint

**URL:** <https://discourse.julialang.org/t/flow-conservation-constraint/83075>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, optimization\
**Created:** [June 20, 2022, 3:02pm UTC](https://discourse.julialang.org/t/flow-conservation-constraint/83075 "2022-06-20T15:02:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Takashy](https://avatars.discourse-cdn.com/v4/letter/t/eb8c5e/32.png) [@Takashy](https://discourse.julialang.org/u/Takashy)\
**Post date:** [June 20, 2022, 3:02pm UTC](https://discourse.julialang.org/t/flow-conservation-constraint/83075/1 "2022-06-20T15:02:21Z")

</div>

Hi there! I’m trying to write a flow conservation constrains. Yet it says key not found

```julia
# this is the constraint I'd like to write (and α(i,j) is a binary variable and A1 is a subset of nodes)
∑ᵢ α[i,j] = ∑ₖ α[j,k] ∀ j ∈ A1 # flow conservation on all nodes belong to A1

```

using JuMP, CPLEX

```julia

m = Model(CPLEX.Optimizer)
A1 = [1,2,5]
Arcs = ((3, 1), (1, 2), (2, 5), (7, 7), (4, 2))

@variable(m, α[i in Arcs]>= 0, Bin)

```

Do you know why the above is not working? any suggestion to write it the other way?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/224680_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [June 20, 2022, 3:22pm UTC](https://discourse.julialang.org/t/flow-conservation-constraint/83075/2 "2022-06-20T15:22:22Z")

</div>

Hi @Takashy! In order to make it easier to help you, please read the advice below. In particular, a reproducible chunk of code would help clarify your problem and look for solutions!

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![Takashy](https://avatars.discourse-cdn.com/v4/letter/t/eb8c5e/32.png) [@Takashy](https://discourse.julialang.org/u/Takashy)\
**Post date:** [June 20, 2022, 5:09pm UTC](https://discourse.julialang.org/t/flow-conservation-constraint/83075/3 "2022-06-20T17:09:31Z")

</div>

Hi @gdalle Thanks for your advice. I’ve made some change. Basically, there are some arcs in a newtrok and I need to ensure that for a subset of nodes (A1); flow conservation constraints hold.  
They require that, for each node in ` A1`, the sum of the flow into the node must be equal to the sum of the flow out of the node.

---

<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 20, 2022, 5:38pm UTC](https://discourse.julialang.org/t/flow-conservation-constraint/83075/4 "2022-06-20T17:38:10Z")

</div>

Something like this?

```julia
@constraint(
    model,
    [jj in A1],
    sum(α[(i, j)] for (i, j) in Arcs if j == jj) == sum(α[(j, k)] for (j, k) in Arcs if j == jj),
)

```
