# Changing penalty parameter in objective function in JuMP

**URL:** <https://discourse.julialang.org/t/changing-penalty-parameter-in-objective-function-in-jump/129791>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [June 10, 2025, 9:25pm UTC](https://discourse.julialang.org/t/changing-penalty-parameter-in-objective-function-in-jump/129791 "2025-06-10T21:25:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lester](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lester/32/216413_2.png) [@Lester](https://discourse.julialang.org/u/Lester)\
**Post date:** [June 10, 2025, 9:25pm UTC](https://discourse.julialang.org/t/changing-penalty-parameter-in-objective-function-in-jump/129791/1 "2025-06-10T21:25:11Z")

</div>

Hi

Saying in JuMP, I want to a objective function being a conbination of the cost function `f(x)` and the constraint violation evaluation `h(x)`, as `@objective(model, Min, f(x) + μ * h(x))`.

Is there a way for me to change`μ` dynamically, based on saying `h(x)`?

Pretty much make JuMP work like a penalty-based solver.

---

<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:** [June 10, 2025, 10:19pm UTC](https://discourse.julialang.org/t/changing-penalty-parameter-in-objective-function-in-jump/129791/2 "2025-06-10T22:19:13Z")

</div>

If you are solving a nonlinear problem with a solver like Ipopt, use a parameter:

```Julia
julia> using JuMP, Ipopt

julia> f(x) = (x - 1)^2
f (generic function with 1 method)

julia> h(x) = 2 - x
h (generic function with 1 method)

julia> model = Model(Ipopt.Optimizer)
A JuMP Model
├ solver: Ipopt
├ objective_sense: FEASIBILITY_SENSE
├ num_variables: 0
├ num_constraints: 0
└ Names registered in the model: none

julia> set_silent(model)

julia> @variable(model, x)
x

julia> @variable(model, u in Parameter(0.0))
u

julia> @objective(model, Min, f(x) + u * h(x))
x² - u*x - 2 x + 2 u + 1

julia> set_parameter_value(u, 0.0)

julia> optimize!(model)

julia> value(x)
1.0

julia> set_parameter_value(u, 2.0)

julia> optimize!(model)

julia> value(x)
2.0

```

Alternatively, just rebuild the objective.

```julia
julia> using JuMP, Ipopt

julia> f(x) = (x - 1)^2
f (generic function with 1 method)

julia> h(x) = 2 - x
h (generic function with 1 method)

julia> model = Model(Ipopt.Optimizer)
A JuMP Model
├ solver: Ipopt
├ objective_sense: FEASIBILITY_SENSE
├ num_variables: 0
├ num_constraints: 0
└ Names registered in the model: none

julia> set_silent(model)

julia> @variable(model, x)
x

julia> u = 0.0
0.0

julia> @objective(model, Min, f(x) + u * h(x))
x² - 2 x + 1

julia> optimize!(model)

julia> value(x)
1.0

julia> u = 2.0
2.0

julia> @objective(model, Min, f(x) + u * h(x))
x² - 4 x + 5

julia> optimize!(model)

julia> value(x)
2.0

```

---

<div class="post-metadata">

**Author:** ![Lester](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lester/32/216413_2.png) [@Lester](https://discourse.julialang.org/u/Lester)\
**Post date:** [June 11, 2025, 11:22am UTC](https://discourse.julialang.org/t/changing-penalty-parameter-in-objective-function-in-jump/129791/3 "2025-06-11T11:22:33Z")

</div>

Thanks.

So if I want to do that every iteration to help the NLP solver to converge, I then basically set max\_iter to 1, changing objective based on return value, warmstart the problem and resolve?

---

<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:** [June 11, 2025, 7:33pm UTC](https://discourse.julialang.org/t/changing-penalty-parameter-in-objective-function-in-jump/129791/4 "2025-06-11T19:33:59Z")

</div>

Why would you want to do that? Solvers like Ipopt already have a sophisticated barrier algorithm. Just formulate your true model and let the solver solve it.

---

<div class="post-metadata">

**Author:** ![Lester](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lester/32/216413_2.png) [@Lester](https://discourse.julialang.org/u/Lester)\
**Post date:** [June 13, 2025, 11:37am UTC](https://discourse.julialang.org/t/changing-penalty-parameter-in-objective-function-in-jump/129791/5 "2025-06-13T11:37:07Z")

</div>

I’m thinking using a penalty like optimal control transcription but solve with SQP. Probabaly it can give me some benefit.
