# JuMP - constraint for specific indices

**URL:** <https://discourse.julialang.org/t/jump-constraint-for-specific-indices/115550>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [June 12, 2024, 12:03pm UTC](https://discourse.julialang.org/t/jump-constraint-for-specific-indices/115550 "2024-06-12T12:03:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![priantop](https://avatars.discourse-cdn.com/v4/letter/p/e47774/32.png) [@priantop](https://discourse.julialang.org/u/priantop)\
**Post date:** [June 12, 2024, 12:03pm UTC](https://discourse.julialang.org/t/jump-constraint-for-specific-indices/115550/1 "2024-06-12T12:03:10Z")

</div>

Hello,

I am formulating LP problem using JuMP for energy system

one of the constraints is the nodal electricity balance, say I have indices of node, time, and technologies

**How do I make a certain expression valid for a specific index?**

In the example below, what I am trying to achieve is that I only want the first sum expression to apply when index x is CHP, though it will be expanded later

```julia
using DataFrames, CSV, XLSX, UnPack, JuMP, HiGHS

@constraints model begin

Electricity_Balance[i in NODES, k in NODES, t in HOURS],
            eldemand_data[i, t] + sum(Generation_Dispatch[i, x, t; x == "CHP"] / gentechdata[x; x == "CHP"].efficiency for x in GEN_TECHS) ≤
            sum(Generation_Dispatch[i, x, t] for x in GEN_TECHS) + Active_Flow[i, k, t] + 
            sum(Storage_Dispatch[i, s, t; s == "BAT"] for s in STO_TECHS)

end

```

but it throws this error

> LoadError: At c:\Users… : unsupported operator +  
> LoadError: syntax: unexpected semicolon in array expression around C:\Users…

How should I formulate it better?  
Or should I define different constraints that apply to these indices?

Thank you very much in advance!

---

<div class="post-metadata">

**Author:** ![BatyLeo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/batyleo/32/35017_2.png) [@BatyLeo](https://discourse.julialang.org/u/BatyLeo)\
**Post date:** [June 12, 2024, 1:14pm UTC](https://discourse.julialang.org/t/jump-constraint-for-specific-indices/115550/2 "2024-06-12T13:14:21Z")

</div>

Hello,

Would simply replacing `x` by `"CHP"` work ?  
Don’t need to use a sum if you only want one term.

Something like that:

```julia
eldemand_data[i, t] + Generation_Dispatch[i, "CHP", t] / gentechdata["CHP"].efficiency <= ...

```

Or do multiple elements in GEN\_TECHS can be equal to “CHP” ?  
If that’s the case, you can also do:

```julia
eldemand_data[i, t] + sum(Generation_Dispatch[i, x, t] / gentechdata[x].efficiency for x in GEN_TECHS if x == "CHP") ≤ ...

```

---

<div class="post-metadata">

**Author:** ![priantop](https://avatars.discourse-cdn.com/v4/letter/p/e47774/32.png) [@priantop](https://discourse.julialang.org/u/priantop)\
**Post date:** [June 12, 2024, 1:28pm UTC](https://discourse.julialang.org/t/jump-constraint-for-specific-indices/115550/3 "2024-06-12T13:28:55Z")

</div>

Hi,

more like GEN\_TECHS will include other techs beside CHP,  
for example CHP, HeatPumps, Boilers, etc.  
so it is like a subset of the GEN\_TECHS set

I did not put them just to show my minimum example

but maybe I can just put like this?

```julia
eldemand_data[i, t] + sum(Generation_Dispatch[i, x, t] / gentechdata[x].efficiency for x in GEN_TECHS if x == ["CHP", "HeatPumps", "Boilers"]) ≤ ...

```

thanks for the suggested code!

---

<div class="post-metadata">

**Author:** ![BatyLeo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/batyleo/32/35017_2.png) [@BatyLeo](https://discourse.julialang.org/u/BatyLeo)\
**Post date:** [June 12, 2024, 1:47pm UTC](https://discourse.julialang.org/t/jump-constraint-for-specific-indices/115550/4 "2024-06-12T13:47:16Z")

</div>

Oh, ok

If the subset is always included in GEN\_TECHS, you can write directly the sum on the subset like this:

```julia
eldemand_data[i, t] + sum(Generation_Dispatch[i, x, t] / gentechdata[x].efficiency for x in ["CHP", "HeatPumps", "Boilers"]) ≤ ...

```

---

<div class="post-metadata">

**Author:** ![priantop](https://avatars.discourse-cdn.com/v4/letter/p/e47774/32.png) [@priantop](https://discourse.julialang.org/u/priantop)\
**Post date:** [June 12, 2024, 2:17pm UTC](https://discourse.julialang.org/t/jump-constraint-for-specific-indices/115550/5 "2024-06-12T14:17:08Z")

</div>

Thanks!

this works as intended!
