pounce_algorithm/ipopt_data.rs
1//! Mutable algorithm state — port of `Algorithm/IpIpoptData.{hpp,cpp}`.
2//!
3//! Holds the iterate trio (`curr` / `trial` / `delta`), the affine
4//! step `delta_aff`, current barrier parameter `mu`, fraction-to-the-
5//! boundary `tau`, iteration count, tolerances, and the four PD
6//! perturbations. No additional-data plug in v1.0 (CG penalty is
7//! deferred to Phase 10).
8//!
9//! Phase 5 ships the full state holder; concrete strategies (line
10//! search, mu update, etc.) read/write fields here as their inputs
11//! and outputs.
12
13use crate::iterates_vector::IteratesVector;
14use pounce_common::timing::TimingStatistics;
15use pounce_common::types::{Index, Number};
16use pounce_linalg::SymMatrix;
17use std::cell::RefCell;
18use std::rc::Rc;
19
20/// Primal-dual perturbation triple (`delta_x`, `delta_s`, `delta_c`,
21/// `delta_d`) — port of the four `current_perturbation` fields in
22/// `IpIpoptData.hpp`.
23#[derive(Debug, Default, Clone, Copy)]
24pub struct PdPerturbations {
25 pub delta_x: Number,
26 pub delta_s: Number,
27 pub delta_c: Number,
28 pub delta_d: Number,
29}
30
31/// Mutable state passed down through the algorithm. Owned by
32/// `IpoptAlgorithm`; strategies access via `Rc<RefCell<IpoptData>>`.
33pub struct IpoptData {
34 pub curr: Option<IteratesVector>,
35 pub trial: Option<IteratesVector>,
36 pub delta: Option<IteratesVector>,
37 pub delta_aff: Option<IteratesVector>,
38 /// Pure centering step — solution of the primal-dual system with
39 /// RHS `(0, 0, 0, 0, μ̄·1, μ̄·1, μ̄·1, μ̄·1)` (where μ̄ = avrg_compl)
40 /// per upstream `IpQualityFunctionMuOracle.cpp:227-247`. Used by
41 /// the quality-function oracle to assemble σ-step trial points
42 /// without re-factorising for each candidate σ.
43 pub delta_cen: Option<IteratesVector>,
44
45 /// Hessian of the Lagrangian for the *current* iterate. Set by
46 /// `HessianUpdater` (exact or quasi-Newton). Mirrors `IpIpoptData::W_`.
47 pub w: Option<Rc<dyn SymMatrix>>,
48
49 pub iter_count: Index,
50 pub curr_mu: Number,
51 pub curr_tau: Number,
52 pub tol: Number,
53
54 pub perturbations: PdPerturbations,
55
56 /// Set after a successful trial-acceptance step in the line
57 /// search. Cleared on accept.
58 pub info_alpha_primal: Number,
59 pub info_alpha_dual: Number,
60
61 /// Mirrors `IpIpoptData::info_regu_x_`.
62 pub info_regu_x: Number,
63
64 /// Mirrors `IpIpoptData::info_skip_output_`.
65 pub info_skip_output: bool,
66
67 /// Mirrors `IpIpoptData::info_string_`. Free-form text the
68 /// iteration output appends to its line.
69 pub info_string: String,
70
71 /// Mirrors `IpIpoptData::tiny_step_flag_`. Set by the line search
72 /// when an alpha→0 trial is detected; the main loop reads it on
73 /// the next pass to decide between "tiny step accept" and bail.
74 pub tiny_step_flag: bool,
75
76 /// One-char marker the iteration output puts in front of
77 /// `alpha_primal` (e.g. `'f'` for filter, `'r'` for restoration,
78 /// `'h'` for the very first iterate). Mirrors
79 /// `IpIpoptData::info_alpha_primal_char_`.
80 pub info_alpha_primal_char: char,
81
82 /// Number of trial points evaluated in the most recent line
83 /// search. Mirrors `IpIpoptData::info_ls_count_`.
84 pub info_ls_count: Index,
85
86 /// The wall-clock at the last `OrigIterationOutput::WriteOutput`
87 /// pass. Phase 7 uses this to decide whether to re-print the
88 /// header. Mirrors `IpIpoptData::info_last_output_`.
89 pub info_last_output: Number,
90
91 /// Iterations since the iteration header was last printed. Phase
92 /// 7 reprints every `print_frequency_iter` lines. Mirrors
93 /// `IpIpoptData::info_iters_since_header_`.
94 pub info_iters_since_header: Index,
95
96 /// Shared per-subsystem timing accumulator. Mirrors upstream's
97 /// `IpoptData::TimingStats_`. `IpoptApplication` constructs a single
98 /// instance per solve and shares it (via `Rc`) with the algorithm,
99 /// NLP, and KKT solver so each can record its own contribution.
100 /// Defaults to a fresh empty instance for the structural unit tests
101 /// that don't go through `IpoptApplication`.
102 pub timing: Rc<TimingStatistics>,
103}
104
105impl Default for IpoptData {
106 fn default() -> Self {
107 Self::new()
108 }
109}
110
111impl IpoptData {
112 pub fn new() -> Self {
113 Self {
114 curr: None,
115 trial: None,
116 delta: None,
117 delta_aff: None,
118 delta_cen: None,
119 w: None,
120 iter_count: 0,
121 curr_mu: 0.1,
122 curr_tau: 0.99,
123 tol: 1e-8,
124 perturbations: PdPerturbations::default(),
125 info_alpha_primal: 0.0,
126 info_alpha_dual: 0.0,
127 info_regu_x: 0.0,
128 info_skip_output: false,
129 info_string: String::new(),
130 tiny_step_flag: false,
131 info_alpha_primal_char: ' ',
132 info_ls_count: 0,
133 info_last_output: -1.0,
134 info_iters_since_header: 0,
135 timing: Rc::new(TimingStatistics::new()),
136 }
137 }
138
139 /// Append text to `info_string`. Mirrors `IpIpoptData::Append_info_string`.
140 pub fn append_info_string(&mut self, s: &str) {
141 self.info_string.push_str(s);
142 }
143
144 /// Reset per-iteration info fields. Mirrors the top of
145 /// `IpoptAlgorithm::Optimize`'s loop body.
146 pub fn reset_info(&mut self) {
147 self.info_string.clear();
148 self.info_skip_output = false;
149 self.info_alpha_primal_char = ' ';
150 self.info_ls_count = 0;
151 self.info_regu_x = 0.0;
152 }
153
154 /// Replace `curr` with the previously-set `trial`. Mirrors
155 /// `IpIpoptData::AcceptTrialPoint`.
156 pub fn accept_trial_point(&mut self) {
157 self.curr = self.trial.take();
158 }
159
160 /// Set the trial iterate from a primal step `delta_x`/`delta_s`
161 /// scaled by `alpha_p` and a dual step scaled by `alpha_d`.
162 /// Phase 5 ships only the structural plumbing; the actual
163 /// arithmetic is implemented once the line search lands in
164 /// Phase 7.
165 pub fn set_trial(&mut self, trial: IteratesVector) {
166 self.trial = Some(trial);
167 }
168
169 pub fn set_curr(&mut self, curr: IteratesVector) {
170 self.curr = Some(curr);
171 }
172
173 pub fn set_delta(&mut self, d: IteratesVector) {
174 self.delta = Some(d);
175 }
176
177 pub fn set_delta_aff(&mut self, d: IteratesVector) {
178 self.delta_aff = Some(d);
179 }
180
181 pub fn set_delta_cen(&mut self, d: IteratesVector) {
182 self.delta_cen = Some(d);
183 }
184}
185
186/// Convenience handle. Mirrors how upstream passes the data object
187/// around as `SmartPtr<IpoptData>`.
188pub type IpoptDataHandle = Rc<RefCell<IpoptData>>;
189
190#[cfg(test)]
191mod tests {
192 use super::*;
193 use crate::iterates_vector::IteratesVector;
194 use pounce_linalg::dense_vector::DenseVectorSpace;
195 use pounce_linalg::Vector;
196 use std::rc::Rc as StdRc;
197
198 fn zero_iv() -> IteratesVector {
199 let z = |n| StdRc::new(DenseVectorSpace::new(n).make_new_dense()) as StdRc<dyn Vector>;
200 IteratesVector::new(z(2), z(1), z(1), z(1), z(2), z(2), z(1), z(1))
201 }
202
203 #[test]
204 fn accept_trial_point_promotes_trial_to_curr() {
205 let mut d = IpoptData::new();
206 d.set_trial(zero_iv());
207 assert!(d.curr.is_none());
208 d.accept_trial_point();
209 assert!(d.curr.is_some());
210 assert!(d.trial.is_none());
211 }
212}