# Macro to substitute parameters used for JuMP model

**URL:** <https://discourse.julialang.org/t/macro-to-substitute-parameters-used-for-jump-model/125936>\
**Category:** Optimization (Mathematical)\
**Tags:** macros, metaprogramming, optimization\
**Created:** [February 15, 2025, 11:10am UTC](https://discourse.julialang.org/t/macro-to-substitute-parameters-used-for-jump-model/125936 "2025-02-15T11:10:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KSepetanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksepetanc/32/10841_2.png) [@KSepetanc](https://discourse.julialang.org/u/KSepetanc)\
**Post date:** [February 15, 2025, 11:10am UTC](https://discourse.julialang.org/t/macro-to-substitute-parameters-used-for-jump-model/125936/1 "2025-02-15T11:10:00Z")

</div>

I read available topics on macros but still can not make working macro to substitute parameter. What I want is instead of creating new variable (for example Pmax) to use @Pmax macro that will expand @Pmax(g) to data[:gen][g][“pmax”].

My goal is to avoid performance penalty of copying `data` parameter to new parameters (just like Pmax) just to make model definition shorter. Sample code that I would like recoded with macro is provided below.

```julia
using JuMP, PowerModels

function OPF(file)

     data = build_ref(parse_file(file))[:it][:pm][:nw][0]

     G = keys(data[:gen])
     Pmin = Dict(g=>data[:gen][g]["pmin"] for g in G)
     Pmax = Dict(g=>data[:gen][g]["pmax"] for g in G)
     #more parameters come here

     pol = Model(Ipopt.Optimizer)

     @variable(pol,Pgen[g in G])
     #more variables come here
     Pmin = Dict(g=>data[:gen][g]["pmin"] for g in G)
     Pmax = Dict(g=>data[:gen][g]["pmax"] for g in G)

     @constraint(pol,pg12[g in G], Pmin[g] <= Pgen[g] <= Pmax[g] )
     #more constraints come here

    return optimize!(pol)
end

```

---

<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:** [February 15, 2025, 7:32pm UTC](https://discourse.julialang.org/t/macro-to-substitute-parameters-used-for-jump-model/125936/2 "2025-02-15T19:32:57Z")

</div>

As a user, you should _never_\* write your own macro. There is _always_ a simpler and easier way to achieve what you are trying to do.

For example, you could use a function:

```Julia
using JuMP, PowerModels
function OPF(file)
    data = build_ref(parse_file(file))[:it][:pm][:nw][0]
    G = keys(data[:gen])
    Pmin(g) = data[:gen][g]["pmin"]
    Pmax(g) = data[:gen][g]["pmax"]
    model = Model(Ipopt.Optimizer)
    @variable(model, Pmin(g) <= Pgen[g in G] <= Pmax(g))
    optimize!(model)
    return
end

```

\*with very few exceptions, which do not apply here.

---

<div class="post-metadata">

**Author:** ![KSepetanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksepetanc/32/10841_2.png) [@KSepetanc](https://discourse.julialang.org/u/KSepetanc)\
**Post date:** [February 16, 2025, 11:27pm UTC](https://discourse.julialang.org/t/macro-to-substitute-parameters-used-for-jump-model/125936/3 "2025-02-16T23:27:24Z")

</div>

Thanks. It works. I notice no penalty due to using functions.
