Optimize slow constraint generation

This is somewhat related to my other post (Deleting containerized constraints) but here, I want to see if there is a way to formulate my constraint differently.

Following advice in this thread, I put all my variable and constraint creation statements inside individual functions. This improved model generation time significantly. However, the following constraint is taking 30 seconds to formulate with 369 nodes and 548 branches:

function gen_q_baseflow(v_baseflow,v_net_injection,ptdf_b_n)
    @constraint(m,
        q_baseflow[b in branch(), (t,s) in T_S],
        + v_baseflow[b,t,s]
        ==
        + sum(  ptdf_b_n[b,n] * v_net_injection[n,t,s] for n in injection_nodes if abs(ptdf_b_n[b,n]) > tx_pdtf_tolerance(settings=:TRANSMISSION))
    )
end

here ptdf_b_n[b,n] is AxisArray{Float64,2,Array{Float64,2},Tuple{Axis{:branch,Array{Symbol,1}},Axis{:node,Array{Symbol,1}}}}

What can I do to improve the creation time of this?

Ok, so I’ve got the constraint generation time down to < 2 seconds by constructing a Dict which holds for every b only the nodes where abs(ptdf_b_n[b,n] ) > tolerance

1 Like