# Variables for power flow with condition in constraint

**URL:** https://discourse.julialang.org/t/variables-for-power-flow-with-condition-in-constraint/116343
**Category:** Optimization (Mathematical)
**Tags:** question, jump, power-flow
**Created:** [June 28, 2024, 8:58am UTC](https://discourse.julialang.org/t/variables-for-power-flow-with-condition-in-constraint/116343 "2024-06-28T08:58:53Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![priantop](https://avatars.discourse-cdn.com/v4/letter/p/e47774/32.png) [@priantop](https://discourse.julialang.org/u/priantop)
#### Post date: [June 28, 2024, 8:58am UTC](https://discourse.julialang.org/t/variables-for-power-flow-with-condition-in-constraint/116343/1 "2024-06-28T08:58:54Z")

</div>

Hi,

I am formulating LP problem using JuMP for electricity dispatch

below is the MWE  
I have variables for power flow and export/import to neighbouring regions defined as follows

```julia
using JuMP, HiGHS

m = Model(HiGHS.Optimizer)

buses = 1:5
trade_buses = 2:3
hours = 1:24

# power flow
@variable(m, P[i in buses, j in buses, t in hours ; i != j])

# export/import to region
@variable(m, X[i in trade_buses , t in hours])

@variable(m, gen[i in buses, t in hours] >= 0)

@variable(m, totalcost) # objective

```

those variables are unbounded since my logic is that when P\>0, P flows from i to j, else P flows from j to i.  
Same logic for X, X\>0 means exporting to other regions, otherwise importing.

then I define the following constraints and objective

```julia
#hypothetical data
demand = 10 .+ (50-10) .* rand(length(buses), length(hours))

fuelcost = 0.2

tradecost = 1 .+ (100-1) .* rand(length(hours))

# bus balance
@constraint(m, balance[i in buses, j in buses, t in hours ; i != j],
    demand[i, t] + P[i, j, t] + 
    (i in trade_buses ? X[i, t] : 0) <= gen[i, t]
)

# export/import to region
@constraint(m, cost,
    totalcost == sum(gen[i, t] * fuelcost for i in buses, t in hours) +
         sum(i in trade_buses && X < 0 ? X[i, t] * tradecost[t] : 0 for i in buses, t in hours)
)

```

for constraint cost, I only want the trade cost only incurred when i is in trade\_bus and when it is importing (i.e. X \< 0)

but it throws this error

> ERROR: MethodError: no method matching isless(::JuMP.Containers.DenseAxisArray{VariableRef, 2, Tuple{UnitRange{Int64}, UnitRange{Int64}}, Tuple{JuMP.Containers.\_AxisLookup{Tuple{Int64, Int64}}, JuMP.Containers.\_AxisLookup{Tuple{Int64, Int64}}}}, ::Int64)

> Closest candidates are:  
> isless(::Missing, ::Any)  
> @ Base missing.jl:87  
> isless(::Any, ::Missing)  
> @ Base missing.jl:88  
> isless(::ForwardDiff.Dual{Tx}, ::Integer) where Tx  
> @ ForwardDiff C:\Users\prianto.julia\packages\ForwardDiff\PcZ48\src\dual.jl:144  
> …

the following are my questions:

1. Is condition for the variables within constraint is not allowed?

2. or my syntaxing is incorrect and there is a better way?

3. or should i split the trade variable into export and import, when cost only incurred to the import?

4. if splitting into two variables is the way to go, should i apply the same for the P variable? e.g. Pfrom will be P from bus i, Pto will be P to bus i. This is in case later on I would incur cost of power flow.

Sorry if the question is too long!

Thank you very much in advance!

---

<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 29, 2024, 12:02am UTC](https://discourse.julialang.org/t/variables-for-power-flow-with-condition-in-constraint/116343/2 "2024-06-29T00:02:47Z")

</div>

> [@priantop](#):
>
> Is condition for the variables within constraint is not allowed?

It is not allowed.

The error message is not good because you have `X < 0`. Did you mean instead `X[i, t] < 0`? I think if you had done that you’d get a nicer error message.

For your other questions, yes, you need to model `X = X_plus - X_neg` where `X_plus` and `X_neg` are both `>= 0`. You’ll end up with a formulation that allows simultaneous flows in both directions, which is a known problem with this sort of LP formulation.

If you’re solving power flow problems, you should look up (if you haven’t already)

- [GitHub - lanl-ansi/PowerModels.jl: A Julia/JuMP Package for Power Network Optimization](https://github.com/lanl-ansi/PowerModels.jl)
- [PowerModelsAnnex.jl/src/model/dc-opf.jl at master · lanl-ansi/PowerModelsAnnex.jl · GitHub](https://github.com/lanl-ansi/PowerModelsAnnex.jl/blob/master/src/model/dc-opf.jl)

---

<div class="post-metadata">

### Author: ![priantop](https://avatars.discourse-cdn.com/v4/letter/p/e47774/32.png) [@priantop](https://discourse.julialang.org/u/priantop)
#### Post date: [June 29, 2024, 4:42am UTC](https://discourse.julialang.org/t/variables-for-power-flow-with-condition-in-constraint/116343/3 "2024-06-29T04:42:58Z")

</div>

> [@odow](#):
>
> > I think if you had done that you’d get a nicer error message.

Ah yes I completely missed that, now it gives this error, which is understandable

```julia
ERROR: TypeError: non-boolean (NonlinearExpr) used in boolean context

```

> [@odow](#):
>
> If you’re solving power flow problems, you should look up (if you haven’t already)

Thanks for the suggestion @odow! Will look into these!

I looked into JuMP Optimal Power Flow tutorial previously and did not find similar branch flow formulations there
