Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/NLPModelsIpopt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,45 @@ end
set_callbacks(nlp::AbstractNLPModel)

Return the set of functions needed to instantiate an `IpoptProblem`.

You can use a callback to monitor the optimization process. The callback must have the signature:

function my_callback(alg_mod, iter_count, problem_ptr, args...)

The `problem_ptr` argument is required to access the current iterate and constraint violations using `Ipopt.GetIpoptCurrentIterate` and `Ipopt.GetIpoptCurrentViolations`.

-`Ipopt.GetIpoptCurrentIterate(problem_ptr)` returns:
- `x`: current primal variables
- `z_L`: current multipliers for lower bounds
- `z_U`: current multipliers for upper bounds
- `g`: current constraint values
- `lambda`: current multipliers for constraints

- `Ipopt.GetIpoptCurrentViolations(problem_ptr)` returns:
- `constr_viol`: constraint violation
- `dual_inf`: dual infeasibility
- `compl`: complementarity

Example:

```julia
function my_callback(alg_mod, iter_count, problem_ptr, args...)
# Get current iterate (primal and dual variables)
x, z_L, z_U, g, lambda = Ipopt.GetIpoptCurrentIterate(problem_ptr)
# Get current constraint violations
constr_viol, dual_inf, compl = Ipopt.GetIpoptCurrentViolations(problem_ptr)
@info "Iter \$iter_count: primal = \$x, dual = \$lambda, constr_viol = \$constr_viol, dual_inf = \$dual_inf, compl = \$compl"
return true # return false to stop
end

# Pass the callback to ipopt using the `callback` keyword:
stats = ipopt(nlp, callback = my_callback)

# For advanced access to the underlying problem struct:
nlp = ADNLPModel(...)
solver = IpoptSolver(nlp)
stats = solve!(solver, nlp, callback = my_callback)
```
"""
function set_callbacks(nlp::AbstractNLPModel)
eval_f(x) = obj(nlp, x)
Expand Down
Loading