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 .+ (5010) .* rand(length(buses), length(hours))
fuelcost = 0.2
tradecost = 1 .+ (1001) .* 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:

Is condition for the variables within constraint is not allowed?

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

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

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!