1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//! Powell's method for unconstrained optimization
use crate::error::OptimizeError;
use crate::unconstrained::result::OptimizeResult;
use crate::unconstrained::{Bounds, Options};
use scirs2_core::ndarray::{Array1, ArrayView1};
/// Implements Powell's method for unconstrained optimization with optional bounds support
#[allow(dead_code)]
pub fn minimize_powell<F, S>(
mut fun: F,
x0: Array1<f64>,
options: &Options,
) -> Result<OptimizeResult<S>, OptimizeError>
where
F: FnMut(&ArrayView1<f64>) -> S,
S: Into<f64> + Clone,
{
// Get options or use defaults
let ftol = options.ftol;
let max_iter = options.max_iter;
let bounds = options.bounds.as_ref();
// Initialize variables
let n = x0.len();
let mut x = x0.to_owned();
// If bounds are provided, ensure x0 is within bounds
if let Some(bounds) = bounds {
bounds.project(x.as_slice_mut().expect("Operation failed"));
}
let mut f = fun(&x.view()).into();
// Initialize the set of directions as the standard basis
let mut directions = Vec::with_capacity(n);
for i in 0..n {
let mut e_i = Array1::zeros(n);
e_i[i] = 1.0;
directions.push(e_i);
}
// Counters
let mut iter = 0;
let mut nfev = 1; // Initial function evaluation
// Powell's main loop
while iter < max_iter {
let x_old = x.clone();
let f_old = f;
// Keep track of the greatest function reduction
let mut f_reduction_max = 0.0;
let mut reduction_idx = 0;
// Line search along each direction
for (i, u) in directions.iter().enumerate().take(n) {
let f_before = f;
// Line search along direction u, respecting bounds
let (alpha, f_min) = line_search_powell(&mut fun, &x, u, f, &mut nfev, bounds);
// Update current position and function value
x = &x + &(alpha * u);
f = f_min;
// Update maximum reduction tracker
let reduction = f_before - f;
if reduction > f_reduction_max {
f_reduction_max = reduction;
reduction_idx = i;
}
}
// Compute the new direction
let new_dir = &x - &x_old;
// Check if the new direction is zero (happens if the point hits a bound and can't move)
let new_dir_norm = new_dir.mapv(|x| x.powi(2)).sum().sqrt();
if new_dir_norm < 1e-8 {
// We're likely at a bound constraint and can't make progress
break;
}
// Extra line search along the extrapolated direction
let (alpha, f_min) = line_search_powell(&mut fun, &x, &new_dir, f, &mut nfev, bounds);
x = &x + &(alpha * &new_dir);
f = f_min;
// Only *now* check for convergence
if 2.0 * (f_old - f) <= ftol * (f_old.abs() + f.abs() + 1e-10) {
break;
}
// Keep the basis full rank.
// If the extrapolated displacement is numerically zero we would
// lose a basis direction; just keep the old one instead.
if new_dir.iter().any(|v| v.abs() > 1e-12) {
// Update the set of directions by replacing the direction of greatest reduction
directions[reduction_idx] = new_dir;
}
iter += 1;
}
// Final check for bounds
if let Some(bounds) = bounds {
bounds.project(x.as_slice_mut().expect("Operation failed"));
}
// Use original function for final value
let final_fun = fun(&x.view());
// Create and return result
Ok(OptimizeResult {
x,
fun: final_fun,
nit: iter,
func_evals: nfev,
nfev,
success: iter < max_iter,
message: if iter < max_iter {
"Optimization terminated successfully.".to_string()
} else {
"Maximum iterations reached.".to_string()
},
jacobian: None,
hessian: None,
})
}
/// Calculate the range of the line search parameter to respect bounds.
///
/// For a point x and direction p, find a_min and a_max such that:
/// x + a * p stays within the bounds for all a in [a_min, a_max].
#[allow(dead_code)]
fn line_bounds(x: &Array1<f64>, direction: &Array1<f64>, bounds: Option<&Bounds>) -> (f64, f64) {
// If no bounds are provided, use unbounded line search
if bounds.is_none() {
return (f64::NEG_INFINITY, f64::INFINITY);
}
let bounds = bounds.expect("Operation failed");
// Start with unbounded range
let mut a_min = f64::NEG_INFINITY;
let mut a_max = f64::INFINITY;
// For each dimension, calculate the range restriction
for i in 0..x.len() {
let xi = x[i];
let pi = direction[i];
if pi.abs() < 1e-16 {
// No movement in this dimension
continue;
}
// Lower bound constraint: x_i + a * p_i >= lb_i
if let Some(lb) = bounds.lower[i] {
let a_lb = (lb - xi) / pi;
if pi > 0.0 {
a_min = a_min.max(a_lb);
} else {
a_max = a_max.min(a_lb);
}
}
// Upper bound constraint: x_i + a * p_i <= ub_i
if let Some(ub) = bounds.upper[i] {
let a_ub = (ub - xi) / pi;
if pi > 0.0 {
a_max = a_max.min(a_ub);
} else {
a_min = a_min.max(a_ub);
}
}
}
// Ensure a valid range exists
if a_min > a_max {
// No feasible movement, set range to zero
(0.0, 0.0)
} else {
(a_min, a_max)
}
}
/// Helper function for line search in Powell's method with bounds support
/// One-dimensional minimization along `x + α·direction`.
#[allow(dead_code)]
fn line_search_powell<F, S>(
fun: &mut F,
x: &Array1<f64>,
direction: &Array1<f64>,
f_x: f64,
nfev: &mut usize,
bounds: Option<&Bounds>,
) -> (f64, f64)
where
F: FnMut(&ArrayView1<f64>) -> S,
S: Into<f64>,
{
// Get bounds on the line search parameter
let (a_min, a_max) = line_bounds(x, direction, bounds);
// Degenerate direction ⇒ no movement
if direction.iter().all(|v| v.abs() <= 1e-16) {
return (0.0, f_x);
}
// helper ϕ(α)
let mut phi = |alpha: f64| -> f64 {
let y = x + &(direction * alpha);
// Project the point onto bounds if needed
let mut y_bounded = y.clone();
if let Some(bounds) = bounds {
bounds.project(y_bounded.as_slice_mut().expect("Operation failed"));
}
*nfev += 1;
fun(&y_bounded.view()).into()
};
// --------------------------------------------------------------
// 1) Find the downhill direction and an initial bracket
// --------------------------------------------------------------
let golden = 1.618_033_988_75_f64; // φ
let mut step = 1.0; // Δ
let mut a = f64::max(0.0, a_min); // Start from 0 or a_min if it's positive
let mut fa = f_x; // ϕ(0)
// probe +Δ and –Δ
let mut b = f64::min(step, a_max);
let mut fb = phi(b);
if fb > fa {
// uphill → try –Δ
b = f64::max(-step, a_min);
fb = phi(b);
if fb > fa {
// no downhill yet: shrink Δ until we find one
for _ in 0..20 {
step *= 0.5;
if step < 1e-12 {
break;
} // give up – extremely flat
b = f64::min(step, a_max);
fb = phi(b);
if fb < fa {
break;
}
b = f64::max(-step, a_min);
fb = phi(b);
if fb < fa {
break;
}
}
}
}
// if we *still* have no downhill point, stay put
if fb >= fa {
return (0.0, f_x);
}
// at this point 'a = 0' is higher, 'b' is lower
// grow the interval until we are uphill again
let mut c = f64::min(b + golden * (b - a), a_max);
let mut fc = phi(c);
for _ in 0..50 {
if fc > fb {
break;
} // bracket found
a = b;
fa = fb;
b = c;
fb = fc;
c = f64::min(b + golden * (b - a), a_max);
fc = phi(c);
}
// sort so that a < b < c
if a > c {
std::mem::swap(&mut a, &mut c);
std::mem::swap(&mut fa, &mut fc);
}
// --------------------------------------------------------------
// 2) Golden-section search inside the bracket
// --------------------------------------------------------------
let mut lo = a;
let mut hi = c;
let mut x1 = hi - (hi - lo) / golden;
let mut x2 = lo + (hi - lo) / golden;
let mut f1 = phi(x1);
let mut f2 = phi(x2);
const IT_MAX: usize = 100;
const TOL: f64 = 1e-8;
for _ in 0..IT_MAX {
if (hi - lo).abs() < TOL {
let alpha = 0.5 * (hi + lo);
return (alpha, phi(alpha)); // φ counts this eval
}
if f1 < f2 {
hi = x2;
x2 = x1;
f2 = f1;
x1 = hi - (hi - lo) / golden;
f1 = phi(x1);
} else {
lo = x1;
x1 = x2;
f1 = f2;
x2 = lo + (hi - lo) / golden;
f2 = phi(x2);
}
}
// fall-back: return the best of the two interior points
if f1 < f2 {
(x1, f1)
} else {
(x2, f2)
}
}
#[cfg(test)]
mod tests {
use super::*;
use approx::assert_abs_diff_eq;
#[test]
fn test_powell_simple() {
let quadratic = |x: &ArrayView1<f64>| -> f64 { x[0] * x[0] + x[1] * x[1] };
let x0 = Array1::from_vec(vec![1.0, 1.0]);
let options = Options::default();
let result = minimize_powell(quadratic, x0, &options).expect("Operation failed");
assert!(result.success);
assert_abs_diff_eq!(result.x[0], 0.0, epsilon = 1e-6);
assert_abs_diff_eq!(result.x[1], 0.0, epsilon = 1e-6);
}
#[test]
fn test_powell_rosenbrock() {
let rosenbrock = |x: &ArrayView1<f64>| -> f64 {
let a = 1.0;
let b = 100.0;
(a - x[0]).powi(2) + b * (x[1] - x[0].powi(2)).powi(2)
};
let x0 = Array1::from_vec(vec![0.0, 0.0]);
let options = Options::default();
let result = minimize_powell(rosenbrock, x0, &options).expect("Operation failed");
assert!(result.success);
assert_abs_diff_eq!(result.x[0], 1.0, epsilon = 1e-3);
assert_abs_diff_eq!(result.x[1], 1.0, epsilon = 1e-3);
}
#[test]
fn test_powell_with_bounds() {
let quadratic =
|x: &ArrayView1<f64>| -> f64 { (x[0] - 2.0).powi(2) + (x[1] - 3.0).powi(2) };
let x0 = Array1::from_vec(vec![0.0, 0.0]);
let mut options = Options::default();
// Constrain solution to [0, 1] x [0, 1]
let bounds = Bounds::new(&[(Some(0.0), Some(1.0)), (Some(0.0), Some(1.0))]);
options.bounds = Some(bounds);
let result = minimize_powell(quadratic, x0, &options).expect("Operation failed");
assert!(result.success);
// The optimal point (2, 3) is outside the bounds, so we should get (1, 1)
assert_abs_diff_eq!(result.x[0], 1.0, epsilon = 1e-6);
assert_abs_diff_eq!(result.x[1], 1.0, epsilon = 1e-6);
}
}