Skip to content

Commit baa3feb

Browse files
arnavk23tmigot
andauthored
Update NLPModelsIpopt.jl (#135)
* Update NLPModelsIpopt.jl * Update NLPModelsIpopt.jl * iter_count * tutorial * Update src/NLPModelsIpopt.jl Co-authored-by: Tangi Migot <[email protected]> * docs build * Update Project.toml Co-authored-by: Tangi Migot <[email protected]> * Update docs/Project.toml Co-authored-by: Tangi Migot <[email protected]> * Update NLPModelsIpopt.jl * Update tutorial.md * Delete build/NLPModelsIpopt.jl * Update docs/src/tutorial.md Co-authored-by: Tangi Migot <[email protected]> * Update docs/src/tutorial.md Co-authored-by: Tangi Migot <[email protected]> * Update docs/src/tutorial.md Co-authored-by: Tangi Migot <[email protected]> * Update docs/src/tutorial.md Co-authored-by: Tangi Migot <[email protected]> * Update tutorial.md --------- Co-authored-by: Tangi Migot <[email protected]>
1 parent 1270f6e commit baa3feb

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

docs/src/tutorial.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,74 @@ Here is an example using the constrained problem solve:
6262
stats.solver_specific[:internal_msg]
6363
```
6464

65+
## Monitoring optimization with callbacks
66+
67+
You can monitor the optimization process using a callback function. The callback allows you to access the current iterate and constraint violations at each iteration, which is useful for custom stopping criteria, logging, or real-time analysis.
68+
69+
### Callback parameters
70+
71+
The callback function receives the following parameters from Ipopt:
72+
73+
- `alg_mod`: algorithm mode (0 = regular, 1 = restoration phase)
74+
- `iter_count`: current iteration number
75+
- `obj_value`: current objective function value
76+
- `inf_pr`: primal infeasibility (constraint violation)
77+
- `inf_du`: dual infeasibility
78+
- `mu`: complementarity measure
79+
- `d_norm`: norm of the primal step
80+
- `regularization_size`: size of regularization
81+
- `alpha_du`: step size for dual variables
82+
- `alpha_pr`: step size for primal variables
83+
- `ls_trials`: number of line search trials
84+
85+
### Example usage
86+
87+
Here's a complete example showing how to use callbacks to monitor the optimization:
88+
89+
```@example ex4
90+
using ADNLPModels, NLPModelsIpopt
91+
92+
function my_callback(alg_mod, iter_count, obj_value, inf_pr, inf_du, mu, d_norm, regularization_size, alpha_du, alpha_pr, ls_trials, args...)
93+
# Log iteration information (these are the standard parameters passed by Ipopt)
94+
println("Iteration $iter_count:")
95+
println(" Objective value = ", obj_value)
96+
println(" Primal infeasibility = ", inf_pr)
97+
println(" Dual infeasibility = ", inf_du)
98+
println(" Complementarity = ", mu)
99+
100+
# Return true to continue, false to stop
101+
return iter_count < 5 # Stop after 5 iterations for this example
102+
end
103+
nlp = ADNLPModel(x -> (x[1] - 1)^2 + 100 * (x[2] - x[1]^2)^2, [-1.2; 1.0])
104+
stats = ipopt(nlp, callback = my_callback, print_level = 0)
105+
```
106+
107+
You can also use callbacks with the advanced solver interface:
108+
```@example ex4
109+
# Advanced usage with IpoptSolver
110+
solver = IpoptSolver(nlp)
111+
stats = solve!(solver, nlp, callback = my_callback, print_level = 0)
112+
```
113+
114+
### Custom stopping criteria
115+
116+
Callbacks are particularly useful for implementing custom stopping criteria:
117+
118+
```@example ex4
119+
function custom_stopping_callback(alg_mod, iter_count, obj_value, inf_pr, inf_du, mu, d_norm, regularization_size, alpha_du, alpha_pr, ls_trials, args...)
120+
# Custom stopping criterion: stop if objective gets close to optimum
121+
if obj_value < 0.01
122+
println("Custom stopping criterion met at iteration $iter_count")
123+
return false # Stop optimization
124+
end
125+
126+
return true # Continue optimization
127+
end
128+
129+
nlp = ADNLPModel(x -> (x[1] - 1)^2 + 100 * (x[2] - x[1]^2)^2, [-1.2; 1.0])
130+
stats = ipopt(nlp, callback = custom_stopping_callback, print_level = 0)
131+
```
132+
65133
## Manual input
66134

67135
In this section, we work through an example where we specify the problem and its derivatives manually. For this, we need to implement the following `NLPModel` API methods:

0 commit comments

Comments
 (0)