# Counting non negative instances of a variable for use in constraint - efficient formulation?

**URL:** <https://discourse.julialang.org/t/counting-non-negative-instances-of-a-variable-for-use-in-constraint-efficient-formulation/60507>\
**Category:** Optimization (Mathematical)\
**Created:** [May 4, 2021, 8:29am UTC](https://discourse.julialang.org/t/counting-non-negative-instances-of-a-variable-for-use-in-constraint-efficient-formulation/60507 "2021-05-04T08:29:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gobs](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gobs](https://discourse.julialang.org/u/gobs)\
**Post date:** [May 4, 2021, 8:29am UTC](https://discourse.julialang.org/t/counting-non-negative-instances-of-a-variable-for-use-in-constraint-efficient-formulation/60507/1 "2021-05-04T08:29:12Z")

</div>

This is an optimisation and not JuMP related question, but I thought it worth asking here. I have a constraint in my model which limits the number of non-negative instances of a variable. Specifically, I have a power balance where there is a slack (load shedding) variable, and I limit the number of times that it can be non-zero:

\sum\_{g \in G} gen\_{g,t} = D\_t + ls\_t \quad t \in T \\ lsi\_t + eps \geq ls\_t / D\_t \\ \sum\_{t \in T} lsi\_t \leq LOLE

where lsi\_t is a binary variable and eps is a small number to avoid computational issues.

My question is whether there is literature on efficient formulations of the above, similarly to how, say, in unit commitment the formulation with 3 binary variables tends to be more efficient than with only 1. I imagine there must be some operations research literature on the subject, but searching for key words similar to the ones used in the title haven’t proved useful.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![joaquimg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquimg/32/223_2.png) [@joaquimg](https://discourse.julialang.org/u/joaquimg)\
**Post date:** [May 4, 2021, 11:42pm UTC](https://discourse.julialang.org/t/counting-non-negative-instances-of-a-variable-for-use-in-constraint-efficient-formulation/60507/2 "2021-05-04T23:42:32Z")

</div>

I’d say that [https://or.stackexchange.com/](https://or.stackexchange.com/) is a better place to ask.

You can take a look at the literature on Chance Constraints to deal with that problem.

Having worked with that problem, I would recommend not using LOLE constraints and using an expected energy constraint instead.

Shameless sales pitch: you can take a look on [[1910.12972] Reliability-Constrained Power System Expansion Planning: A Stochastic Risk-Averse Optimization Approach](https://arxiv.org/abs/1910.12972) which discusses a bit that kind of problem and alternatives.

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [May 5, 2021, 1:26am UTC](https://discourse.julialang.org/t/counting-non-negative-instances-of-a-variable-for-use-in-constraint-efficient-formulation/60507/3 "2021-05-05T01:26:04Z")

</div>

There is not much additional structure to work from, but the last constraint is related to the 0,1 knapsack problem.  
I can recommend “Integer Programming” by Michele Conforti, Gérard Cornuéjols and Giacomo Zambelli  
([https://www.doi.org/10.1007/978-3-319-11008-0](https://www.doi.org/10.1007/978-3-319-11008-0)) for good information on integer programming formulations.

---

<div class="post-metadata">

**Author:** ![gobs](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gobs](https://discourse.julialang.org/u/gobs)\
**Post date:** [May 5, 2021, 8:12am UTC](https://discourse.julialang.org/t/counting-non-negative-instances-of-a-variable-for-use-in-constraint-efficient-formulation/60507/4 "2021-05-05T08:12:25Z")

</div>

Thank you @joaquimg, I actually read your paper 🙂 It was very interesting, and I understand your comment on the expected energy constraint.

However, my interest in LOLE is that it’s the reliability indicator enshrined in EU Legislation now (see [here](https://www.acer.europa.eu/Official_documents/Acts_of_the_Agency/Individual%20decisions%20Annexes/ACER%20Decision%20No%2023-2020_Annexes/ACER%20Decision%2023-2020%20on%20VOLL%20CONE%20RS%20-%20Annex%20I.pdf)), and I want to investigate whether with storage and renewables a LOLE target is equivalent to an EENS target, or if it, say, favours conventional generation. It is also extremely irritating / difficult to include in an optimisation problem because it introduces binaries.

EDIT: I’ve also asked on Stack Overflow - covering all bases!

---

<div class="post-metadata">

**Author:** ![gobs](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gobs](https://discourse.julialang.org/u/gobs)\
**Post date:** [May 5, 2021, 8:12am UTC](https://discourse.julialang.org/t/counting-non-negative-instances-of-a-variable-for-use-in-constraint-efficient-formulation/60507/5 "2021-05-05T08:12:56Z")

</div>

Thank you @jd-foster, I will take a look to see if I can get any inspiration from there.

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [May 13, 2021, 3:41am UTC](https://discourse.julialang.org/t/counting-non-negative-instances-of-a-variable-for-use-in-constraint-efficient-formulation/60507/6 "2021-05-13T03:41:37Z")

</div>

@gobs Another equivalent formulation worth considering:

D\_t z\_t \leq \sum\_{g \in G} gen\_{g,t} \leq D\_t \quad \forall t \in T \\ \sum\_{t \in T} z\_t \, \geq |T| - LOLE \\ z\_t \quad \text{binary} \quad \forall t \in T 

(where |S| is the cardinality of the set S)

This difference is

- no intermediate load-shedding variables (but maybe you use it elsewhere in your formulation).
- the linear programming relaxation requires at least some of the binary variables z\_t to be non-zero and/or fractional, and will may change the branch-and-bound tree for better or worse,
- this might avoid using the eps…

---

<div class="post-metadata">

**Author:** ![gobs](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gobs](https://discourse.julialang.org/u/gobs)\
**Post date:** [May 13, 2021, 5:58am UTC](https://discourse.julialang.org/t/counting-non-negative-instances-of-a-variable-for-use-in-constraint-efficient-formulation/60507/7 "2021-05-13T05:58:21Z")

</div>

Thanks for the suggestion. As I understand it, you made z\_t = -lsi\_t, which at first glance I don’t see how this would help, but then again I’m not an expert on this.

I think eps would still be required to avoid numerical issues, but perhaps I’m being to pessimistic about how good / intelligent solvers are (I’m using Gurobi).

I will try it out and let you know!
