 # Add constraints to Binary Flow

Hi there,
i am trying to add a spinning reserve constraint to my operational model.
here is the code:

``````SPINNING_RESERVE=0.2
srblock = Block()

def spinning_reserve_rule(m, t):
expr = om.BinaryFlow.status[generator, b_el, t] * (om.flows[generator, b_el].max[t]*
om.flows[generator, b_el].nominal_value-
om.flow[generator,b_el,t])\
>=feedin['demand_el'][t] * SPINNING_RESERVE
return expr

srblock.sr_constr = Constraint(om.TIMESTEPS, rule=spinning_reserve_rule)
``````

When i run the code, i get:
“ValueError: Solver unable to handle quadratic expressions. Constraint at issue: ‘Spinning Reserve.sr_constr’”

I don’t really see the problem. Why is that constraint considered a qudratic expression? Basically its a binary variable multiplied with a flow variable, similar to the constraints added to BinaryFlows within oemof.solph.blocks.BinaryFlows.

Yeah the problem is that your constraint is… well: not linear, gurobi might be able to handle it, though I think it only allows to solve quadratic programs (only quadratic in objective function, but linear in constraints).

Inside our (oemof) implementation, there are only linear equations.

So you need to either find another constraint or you can replace multiplication of binary with continuous variables by 4 other constraints:

If we have a quadratic term: z = yx (y is binary, x is continuous)
and if x is is bounded by L ≤ x ≤ U we can transform the
z ≤ Uy
z ≥ Ly
z ≤ x − L(1 − y )
z ≥ x − U(1 − y )

Hope this helps

2 Likes

Thanks. for the fast response. It helped me to understand the problem.

I came up with a simple solution for the spinning reserve constraint.

Before i had this (simplified, for 1 generator):
z>=y*(max-x) … with max and f are constants , y[0,1], and x==flow
which would lead to the nonlinear correlation yx.
Now i changed this constraint to
z>=y*max-x
assuming that the requirement if x=0 then y=0 is already satisfied. So in case y=0 , yx wouln’t give me any new information.

I will implement it it now for the sum of all (n) generator flows as z>=sum(yi*maxi)-sum(xi) for i = 0,…,n