# Parameters in a JuMP model

**URL:** <https://discourse.julialang.org/t/parameters-in-a-jump-model/87986>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [September 29, 2022, 12:46pm UTC](https://discourse.julialang.org/t/parameters-in-a-jump-model/87986 "2022-09-29T12:46:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_D](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Marc\_D](https://discourse.julialang.org/u/Marc_D)\
**Post date:** [September 29, 2022, 12:46pm UTC](https://discourse.julialang.org/t/parameters-in-a-jump-model/87986/1 "2022-09-29T12:46:11Z")

</div>

I am new to Julia and uses JuMP to model optimizations problems. I am trying to model a problem with parameters that I could change. I didn’t how to do this and don’t know if it is actually possible to do.

More concretely, what I would want to do is something like this, although the example is quite dumb.

```julia
using JuMP
using HiGHS

p = [1 2];
model = Model(HiGHS.Optimizer);
@variable(model, x1);
@variable(model, x2);
@constraint(model, c1, x1 == p[1]);
@constraint(model, c2, x2 == p[2]);
@objective(model, Min, x1+x2);
optimize!(model)

```

This will solve \min\_{x\_1,x\_2} x\_1+x\_2 such that x\_1 =1 and x\_2 = 2. Currently, when the following code is

```julia
p = [2 4];
optimize!(model)

```

it still solve the same problem as above. What I would want is to solve now is \min\_{x\_1,x\_2} x\_1+x\_2 such that x\_1 =2 and x\_2 = 4.

Is there a way to do that?

---

<div class="post-metadata">

**Author:** ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)\
**Post date:** [September 29, 2022, 1:48pm UTC](https://discourse.julialang.org/t/parameters-in-a-jump-model/87986/2 "2022-09-29T13:48:17Z")

</div>

Welcome! Once you create the model, the values of `p[1]` and `p[2]` are linked to that model (as far as I know). Thus, changing these values afterwards without updating the model will have no effect. You could do it in a loop, e.g.,

```julia
P = [[1 2], [2 4]]
for p in P
    model = Model(HiGHS.Optimizer);
    @variable(model, x1);
    @variable(model, x2);
    @constraint(model, c1, x1 == p[1]);
    @constraint(model, c2, x2 == p[2]);
    @objective(model, Min, x1+x2);
    optimize!(model)
    println("objective: ", objective_value(model))
    print(model)
end

```

or with a function taking your parameters as argument, e.g.,

```julia
function build_model(p)
    model = Model(HiGHS.Optimizer);
    @variable(model, x1);
    @variable(model, x2);
    @constraint(model, c1, x1 == p[1]);
    @constraint(model, c2, x2 == p[2]);
    @objective(model, Min, x1+x2);
    return model
end

P = [[1 2], [2 4]]
for p in P
    model = build_model(p)
    optimize!(model)
    println("objective: ", objective_value(model))
end

```

There is probably a more efficient way (with respect to performance) to do that, but I do not know.

---

<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 29, 2022, 7:48pm UTC](https://discourse.julialang.org/t/parameters-in-a-jump-model/87986/3 "2022-09-29T19:48:09Z")

</div>

There are a bunch of ways you can modify a JuMP model. For this particular example, take a look at

- [Constraints · JuMP](https://jump.dev/JuMP.jl/stable/manual/constraints/#Modify-a-constant-term)

There are also a bunch of tutorials which demonstrate how to optimize over parameters:

- [The factory schedule example · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/linear/factory_schedule/#What-happens-if-demand-increases)?
- [Power Systems · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/applications/power_systems/#Modifying-the-JuMP-model-in-place)

As a rule of thumb, @mike_k’s approach of creating a function and optimizing is a great place to start. In most cases, it is “fast enough” and it’s simple to mix a whole bunch of different parameters and modifications. But modifying the problem in-place can be a lot faster, particularly for bigger models.

---

<div class="post-metadata">

**Author:** ![Marc\_D](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Marc\_D](https://discourse.julialang.org/u/Marc_D)\
**Post date:** [September 30, 2022, 7:08am UTC](https://discourse.julialang.org/t/parameters-in-a-jump-model/87986/4 "2022-09-30T07:08:59Z")

</div>

Both work well for me. Thanks for the solutions and welcome.
