Changing penalty parameter in objective function in JuMP

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.

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

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> 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

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?

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.

1 Like