# Change value of a constant in the objective function

**URL:** <https://discourse.julialang.org/t/change-value-of-a-constant-in-the-objective-function/112082>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [March 25, 2024, 11:48am UTC](https://discourse.julialang.org/t/change-value-of-a-constant-in-the-objective-function/112082 "2024-03-25T11:48:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedroripper](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pedroripper/32/207976_2.png) [@pedroripper](https://discourse.julialang.org/u/pedroripper)\
**Post date:** [March 25, 2024, 11:48am UTC](https://discourse.julialang.org/t/change-value-of-a-constant-in-the-objective-function/112082/1 "2024-03-25T11:48:59Z")

</div>

Greetings,

I have a JuMP model that is being instantiated at some point of my code.

I will be running `optimize!(model)` several times and, for each, I would like to change the value of the coefficient given as a parameter to the `create_model()` function.

The following MWE is working for now, but I was wondering if there was a more efficient way (in terms of performance) to change the value of this coefficient.

```julia
using JuMP
using HiGHS

function create_model(value::Float64)
    model = Model(HiGHS.Optimizer)
    @variable(model, x[1:5] >= 5)
    @objective(model, Min, sum(x[i]*value for i in 1:5))
    return model
end

model = create_model(1.0)

MOI.modify(model.moi_backend,
    MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
    [MOI.ScalarCoefficientChange(MOI.VariableIndex(model[:x][i]), 2.0)
        for i in 1:5]
)

```

I thought about creating an expression `@expression(model, value, 0.0)` and changing its value, but could not find the documentation for it.

Does anyone know what is the best option?

Thanks in advance!

---

<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:** [March 25, 2024, 6:48pm UTC](https://discourse.julialang.org/t/change-value-of-a-constant-in-the-objective-function/112082/2 "2024-03-25T18:48:45Z")

</div>

Hey @pedroripper, you’re looking for `set_objective_coefficient`.

See the docs:

- [Objectives · JuMP](https://jump.dev/JuMP.jl/stable/manual/objective/#Modify-an-objective-coefficient)
- [JuMP · JuMP](https://jump.dev/JuMP.jl/stable/api/JuMP/#JuMP.set_objective_coefficient)

If you’re modifying lots of coefficients, you can also just set a new objective function:

```Julia
julia> using JuMP

julia> function create_model(value::Float64)
           model = Model()
           @variable(model, x[1:5])
           @objective(model, Min, sum(value * x[i] for i in 1:5))
           return model
       end
create_model (generic function with 1 method)

julia> model = create_model(1.0);

julia> print(model)
Min x[1] + x[2] + x[3] + x[4] + x[5]
Subject to

julia> set_objective_coefficient.(model, model[:x], 2.0);

julia> print(model)
Min 2 x[1] + 2 x[2] + 2 x[3] + 2 x[4] + 2 x[5]
Subject to

julia> @objective(model, Min, sum(3.0 * model[:x][i] for i in 1:5))
3 x[1] + 3 x[2] + 3 x[3] + 3 x[4] + 3 x[5]

julia> print(model)
Min 3 x[1] + 3 x[2] + 3 x[3] + 3 x[4] + 3 x[5]
Subject to

```

---

<div class="post-metadata">

**Author:** ![pedroripper](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pedroripper/32/207976_2.png) [@pedroripper](https://discourse.julialang.org/u/pedroripper)\
**Post date:** [March 26, 2024, 12:37am UTC](https://discourse.julialang.org/t/change-value-of-a-constant-in-the-objective-function/112082/3 "2024-03-26T00:37:38Z")

</div>

Thank you very much!
