# Write expressions in JuMP model based on condition

**URL:** <https://discourse.julialang.org/t/write-expressions-in-jump-model-based-on-condition/123696>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [December 11, 2024, 9:14am UTC](https://discourse.julialang.org/t/write-expressions-in-jump-model-based-on-condition/123696 "2024-12-11T09:14:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mast-lfl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mast-lfl/32/214092_2.png) [@mast-lfl](https://discourse.julialang.org/u/mast-lfl)\
**Post date:** [December 11, 2024, 9:14am UTC](https://discourse.julialang.org/t/write-expressions-in-jump-model-based-on-condition/123696/1 "2024-12-11T09:14:16Z")

</div>

I want to add serval named expressions in a JuMP model that are conditional on the indices p (set P) and t (set T). X and I are the decision variable and set P consists of two subsets, P\_1, P\_2. I want the expressions to be named to be able to use them later on. After adding the expression “ex” for the first subset P\_1, I am not able to add the expression for the second subset because I receive the following error:

**ERROR** : An object of name ex is already attached to this model.

How can I avoid this error and add the expression conditional on index value? Here is a MWE:

```julia
using JuMP, HiGHs

P_1 = ["a", "b", "c"]
P_2 = ["d", "e"]
P = vcat(P_1, P_2)
T = 1:10

model = JuMP.Model(HiGHs.Optimizer)

@variables model begin
    X[P,T], Bin
    I[P,T] >= 0
end

@expression(model, ex[p=P_1, t=T], I[p,t] * 5)
@expression(model, ex[p=P_2, t=T], I[p,t] * 7) 

```

---

<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:** [December 11, 2024, 8:37pm UTC](https://discourse.julialang.org/t/write-expressions-in-jump-model-based-on-condition/123696/2 "2024-12-11T20:37:42Z")

</div>

Hi @mast-lfl, welcome to the forum.

Correct. You cannot define an expression with the same name twice. Take a read of [Variables · JuMP](https://jump.dev/JuMP.jl/stable/manual/variables/#variable_names_and_bindings) (it is in the variables, section, but it applies equally to expressions).

One approach would be to write your model as:

```julia
@expression(
    model,
    ex[p=P, t=T],
    if p in P_1
        I[p,t] * 5
    else
        I[p,t] * 7
    end,
)

```

Another approach would be:

```julia
@expression(model, ex[p=P, t=T], I[p,t] * (p in P_1 ? 5 : 7))

```

Another approach would be:

```Julia
data = Dict("a" => 5, "b" => 5, "c" => 5, "d" => 7, "e" => 7)
@expression(model, ex[p=P, t=T], I[p,t] * data[p])

```

This hints at the strength and weakness of JuMP: we are flexible in how to write things, but because there is more than one way it can be hard to know (or document) what is the “best.” The answer is problem-dependent.
