# Using JuMP variables and expressions in conditional statements

**URL:** <https://discourse.julialang.org/t/using-jump-variables-and-expressions-in-conditional-statements/103414>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, gurobi\
**Created:** [August 31, 2023, 5:11pm UTC](https://discourse.julialang.org/t/using-jump-variables-and-expressions-in-conditional-statements/103414 "2023-08-31T17:11:19Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![AshwinSandeep1](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@AshwinSandeep1](https://discourse.julialang.org/u/AshwinSandeep1)\
**Post date:** [September 5, 2023, 3:53am UTC](https://discourse.julialang.org/t/using-jump-variables-and-expressions-in-conditional-statements/103414/21 "2023-09-05T03:53:26Z")

</div>

Because SOC is defined as current energy stored in the system divided by total energy capacity, both of which are problem variables which makes SOC an `@NLexpression`. Modeling SOC as an `@expression` would require that I explicitly fix the total energy capacity of the system instead of it being left to the optimization.

---

<div class="post-metadata">

**Author:** ![ccoffrin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccoffrin/32/400_2.png) [@ccoffrin](https://discourse.julialang.org/u/ccoffrin)\
**Post date:** [September 6, 2023, 3:39am UTC](https://discourse.julialang.org/t/using-jump-variables-and-expressions-in-conditional-statements/103414/22 "2023-09-06T03:39:55Z")

</div>

> [@AshwinSandeep1](#):
>
> To give more context about the model: I am trying to model the discharge and charge behaviour of an energy storage system like a battery.

You may find this report and example interesting, [[2004.14768] A Flexible Storage Model for Power Network Optimization](https://arxiv.org/abs/2004.14768), a few NL constraints should be sufficient for modeling very general energy storage devices.

A probably out of date reference implementation is here, [GitHub - lanl-ansi/energy-storage-example: An example of energy storage optimization using PowerModels](https://github.com/lanl-ansi/energy-storage-example)

---

<div class="post-metadata">

**Author:** ![AshwinSandeep1](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@AshwinSandeep1](https://discourse.julialang.org/u/AshwinSandeep1)\
**Post date:** [September 6, 2023, 5:32pm UTC](https://discourse.julialang.org/t/using-jump-variables-and-expressions-in-conditional-statements/103414/23 "2023-09-06T17:32:57Z")

</div>

Thank you for this! I will take a closer look and see if I can use splines to model the charge/discharge curves.

EDIT: Hi @jd-foster, I have had a look at the paper. My understanding is that splines allow a more complex interpolation compared to the standard linear interpolation methodology. However, based on the model variables and structure I have, I believe I would still run into the issue of having to use `ifelse` statements to determine at which point on the curve (i.e., which spline parameters to use) to calculate the discharge/charge power at a particular SOC.

---

<div class="post-metadata">

**Author:** ![AshwinSandeep1](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@AshwinSandeep1](https://discourse.julialang.org/u/AshwinSandeep1)\
**Post date:** [September 6, 2023, 5:49pm UTC](https://discourse.julialang.org/t/using-jump-variables-and-expressions-in-conditional-statements/103414/24 "2023-09-06T17:49:22Z")

</div>

Thank you for this! Actually, I only need 1 constraint (the aim was to replace a linear constraint with a piecewise-linear or piecewise-nonlinear constraint). But, it the piecewise route causes issues, I could use a single function to represent the relationship between SOC and power although this might be less accurate than the piecewise models.

---

<div class="post-metadata">

**Author:** ![AshwinSandeep1](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@AshwinSandeep1](https://discourse.julialang.org/u/AshwinSandeep1)\
**Post date:** [September 7, 2023, 4:22am UTC](https://discourse.julialang.org/t/using-jump-variables-and-expressions-in-conditional-statements/103414/26 "2023-09-07T04:22:37Z")

</div>

Hi @odow

When I run simulations after including the SOS constraints, Gurobi goes into the solution phase where it tries to reduce the MIP gap to below the tolerance although this is not a MIP model. In the attached screenshot, I have shown the terminal outputs of 2 cases - one with only 2 SOS constraints and the other with a lot. In the former, integer variables are somehow created after the presolve phase which I guess justifies Gurobi trying to reduce the MIP gap. Anyway, for this case, it exits this phase after the first iteration and the problem converges quickly. For the actual case of interest with 17520 SOS constraints, integer variables are not created after pre-solve but Gurobi is still trying to reduce the MIP gap for some reason. This case does not converge and the terminal shows that there are no incumbent or gap values. Do you know what the issue is here? Increasing the number of SOS constraints beyond a threshold seems to the be the root cause

 ![Screenshot 2023-09-06 at 23.15.21](https://global.discourse-cdn.com/julialang/original/3X/2/d/2d1f1e43d66ab162f1f8aef86a101b3e01a0387a.jpeg)  
.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 10, 2023, 10:30pm UTC](https://discourse.julialang.org/t/using-jump-variables-and-expressions-in-conditional-statements/103414/27 "2023-09-10T22:30:06Z")

</div>

Yes, Gurobi may struggle if you have a large number of SOS constraints. There’s no good solution here. Your model is just hard to solve.

---

<div class="post-metadata">

**Author:** ![AshwinSandeep1](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@AshwinSandeep1](https://discourse.julialang.org/u/AshwinSandeep1)\
**Post date:** [September 12, 2023, 2:41am UTC](https://discourse.julialang.org/t/using-jump-variables-and-expressions-in-conditional-statements/103414/28 "2023-09-12T02:41:23Z")

</div>

Ah, ok. Then I will try a simpler model. Thank you for this discussion Oscar! It has really helped, appreciate it.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 12, 2023, 3:35am UTC](https://discourse.julialang.org/t/using-jump-variables-and-expressions-in-conditional-statements/103414/29 "2023-09-12T03:35:52Z")

</div>

No problem. I’d look into some of the links others have provided above.

[Previous page](https://discourse.julialang.org/t/using-jump-variables-and-expressions-in-conditional-statements/103414.md?page=1)
