Variables for power flow with condition in constraint

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

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

#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!

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)

2 Likes

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

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

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