scirs2-optimize 0.4.2

Optimization module for SciRS2 (scirs2-optimize)
Documentation
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
//! Transfer Bayesian Optimization.
//!
//! Enables knowledge transfer from previously optimized (source) tasks to a
//! new (target) task, dramatically reducing the number of expensive evaluations
//! required to find good solutions.
//!
//! # Approach
//!
//! 1. **Task similarity**: Each source task is assigned a weight proportional
//!    to `exp(-distance / temperature)` where `distance` is derived from the
//!    spatial overlap between source observations and the target search space.
//!
//! 2. **Weighted surrogate**: We maintain one GP per source task and one GP
//!    for the target task.  The acquisition function is the weighted combination
//!
//!    ```text
//!      acq(x) = w_t * EI_target(x) + (1 - w_t) * sum_s w_s * EI_source_s(x)
//!    ```
//!
//!    where `w_t` starts at 0 (pure transfer) and rises linearly toward 1 as
//!    more target evaluations accumulate (adaptive weighting).
//!
//! 3. **Warm-start**: Source task observations that fall inside the target
//!    domain are injected into the target GP as low-weight pseudo-observations,
//!    giving the model a head-start.
//!
//! # Quick Start
//!
//! ```rust
//! use scirs2_optimize::bayesian::transfer_bo::{
//!     TransferBo, TransferBoConfig, TaskObservations,
//! };
//! use scirs2_core::ndarray::{array, Array2, Array1};
//!
//! // Pretend we have a source task: f_src(x) = x^2, sampled at a few points.
//! let x_src = Array2::from_shape_vec((3, 1), vec![0.0, 1.0, 2.0]).expect("valid input");
//! let y_src = Array1::from_vec(vec![0.0, 1.0, 4.0]);
//! let src_task = TaskObservations {
//!     x: x_src,
//!     y: y_src,
//!     bounds: vec![(0.0_f64, 3.0_f64)],
//! };
//!
//! let target_bounds = vec![(0.0_f64, 3.0_f64)];
//! let config = TransferBoConfig { seed: Some(7), n_initial: 3, ..Default::default() };
//!
//! let mut tbo = TransferBo::new(vec![src_task], target_bounds, config)
//!     .expect("build tbo");
//!
//! let result = tbo.optimize(|x: &[f64]| (x[0] - 0.5_f64).powi(2), 15)
//!     .expect("optimize");
//!
//! println!("Best x: {:?}  f: {:.4}", result.x_best, result.f_best);
//! ```

use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::random::rngs::StdRng;
use scirs2_core::random::{Rng, SeedableRng};

use crate::error::{OptimizeError, OptimizeResult};

use super::gp::{GpSurrogate, GpSurrogateConfig, RbfKernel};
use super::sampling::{generate_samples, SamplingConfig, SamplingStrategy};

// ---------------------------------------------------------------------------
// Task observations
// ---------------------------------------------------------------------------

/// Observations from a completed optimization task.
#[derive(Debug, Clone)]
pub struct TaskObservations {
    /// Input matrix (n_obs × n_dims).
    pub x: Array2<f64>,
    /// Output vector (n_obs,).
    pub y: Array1<f64>,
    /// Search-space bounds used for the task [(lo, hi), ...].
    pub bounds: Vec<(f64, f64)>,
}

// ---------------------------------------------------------------------------
// Task similarity
// ---------------------------------------------------------------------------

/// Compute the similarity between a source task and the target search space.
///
/// The metric is based on the fraction of source observations that fall within
/// the target bounds, weighted by how centrally they sit.
///
/// Returns a value in `[0, 1]` where 1 means perfect overlap.
pub fn compute_task_similarity(
    source_obs: &TaskObservations,
    target_bounds: &[(f64, f64)],
) -> f64 {
    if source_obs.x.is_empty() || target_bounds.is_empty() {
        return 0.0;
    }

    let n_dims = target_bounds.len().min(source_obs.x.ncols());
    let n = source_obs.x.nrows();
    let mut total_score = 0.0;

    for row in 0..n {
        let mut point_score = 1.0;
        for d in 0..n_dims {
            let lo = target_bounds[d].0;
            let hi = target_bounds[d].1;
            let range = (hi - lo).abs().max(1e-12);
            let v = source_obs.x[[row, d]];

            if v < lo || v > hi {
                // Outside bounds: compute exponential penalty based on distance.
                let dist = if v < lo { lo - v } else { v - hi };
                point_score *= (-2.0 * dist / range).exp();
            } else {
                // Inside bounds: use a tent function peaking at the center.
                let center = 0.5 * (lo + hi);
                let half_range = 0.5 * range;
                let centrality = 1.0 - (v - center).abs() / half_range;
                point_score *= 0.5 + 0.5 * centrality; // in [0.5, 1]
            }
        }
        total_score += point_score;
    }

    (total_score / n as f64).clamp(0.0, 1.0)
}

// ---------------------------------------------------------------------------
// Transfer BO configuration & result
// ---------------------------------------------------------------------------

/// Configuration for Transfer Bayesian Optimization.
#[derive(Debug, Clone)]
pub struct TransferBoConfig {
    /// Number of initial random evaluations on the target task.
    pub n_initial: usize,
    /// Exploration bonus xi for EI.
    pub xi: f64,
    /// Number of random candidates evaluated when maximising the acquisition.
    pub n_candidates: usize,
    /// Temperature controlling how sharply task similarity translates to weight.
    pub similarity_temperature: f64,
    /// Random seed.
    pub seed: Option<u64>,
    /// Verbosity.
    pub verbose: usize,
}

impl Default for TransferBoConfig {
    fn default() -> Self {
        Self {
            n_initial: 5,
            xi: 0.01,
            n_candidates: 200,
            similarity_temperature: 0.5,
            seed: None,
            verbose: 0,
        }
    }
}

/// Result of transfer Bayesian optimization.
#[derive(Debug, Clone)]
pub struct TransferBoResult {
    /// Best input point found.
    pub x_best: Array1<f64>,
    /// Best objective value.
    pub f_best: f64,
    /// Source task similarity weights used.
    pub source_weights: Vec<f64>,
    /// Number of target evaluations performed.
    pub n_target_evals: usize,
    /// Trajectory: (iteration, f_value).
    pub history: Vec<(usize, f64)>,
}

// ---------------------------------------------------------------------------
// Internal source-task surrogate
// ---------------------------------------------------------------------------

struct SourceModel {
    gp: GpSurrogate,
    similarity: f64,
    weight: f64,
}

// ---------------------------------------------------------------------------
// Transfer BO
// ---------------------------------------------------------------------------

/// Transfer Bayesian Optimizer.
///
/// Uses source-task GP surrogates to accelerate optimization on the target task.
pub struct TransferBo {
    source_tasks: Vec<TaskObservations>,
    target_bounds: Vec<(f64, f64)>,
    config: TransferBoConfig,
}

impl TransferBo {
    /// Create a new transfer BO instance.
    pub fn new(
        source_tasks: Vec<TaskObservations>,
        target_bounds: Vec<(f64, f64)>,
        config: TransferBoConfig,
    ) -> OptimizeResult<Self> {
        if target_bounds.is_empty() {
            return Err(OptimizeError::InvalidInput(
                "Target bounds must not be empty".to_string(),
            ));
        }
        Ok(Self {
            source_tasks,
            target_bounds,
            config,
        })
    }

    /// Optimize the target objective using transfer from source tasks.
    ///
    /// `n_iterations` is the total number of target evaluations (excluding
    /// any pseudo-observations from source tasks).
    pub fn optimize<F>(&mut self, objective: F, n_iterations: usize) -> OptimizeResult<TransferBoResult>
    where
        F: Fn(&[f64]) -> f64,
    {
        if n_iterations == 0 {
            return Err(OptimizeError::InvalidInput(
                "n_iterations must be positive".to_string(),
            ));
        }

        let n_dims = self.target_bounds.len();
        let seed = self.config.seed.unwrap_or(42);
        let mut rng = StdRng::seed_from_u64(seed);

        // -----------------------------------------------------------------
        // 1. Build & fit source surrogates.
        // -----------------------------------------------------------------
        let mut source_models: Vec<SourceModel> = self
            .source_tasks
            .iter()
            .map(|task| {
                let gp_cfg = GpSurrogateConfig {
                    noise_variance: 1e-4,
                    optimize_hyperparams: true,
                    n_restarts: 2,
                    max_opt_iters: 30,
                };
                let mut gp = GpSurrogate::new(Box::new(RbfKernel::default()), gp_cfg);
                if !task.x.is_empty() {
                    let _ = gp.fit(&task.x, &task.y);
                }
                let similarity =
                    compute_task_similarity(task, &self.target_bounds);
                SourceModel {
                    gp,
                    similarity,
                    weight: similarity,
                }
            })
            .collect();

        // Normalise source weights (softmax-style via temperature).
        let temp = self.config.similarity_temperature.max(1e-6);
        let raw_weights: Vec<f64> = source_models
            .iter()
            .map(|m| (m.similarity / temp).exp())
            .collect();
        let w_sum: f64 = raw_weights.iter().sum::<f64>() + 1e-12;
        for (i, m) in source_models.iter_mut().enumerate() {
            m.weight = raw_weights[i] / w_sum;
        }

        let source_weights: Vec<f64> = source_models.iter().map(|m| m.weight).collect();

        // -----------------------------------------------------------------
        // 2. Warm-start the target GP with in-domain source observations.
        // -----------------------------------------------------------------
        let mut target_x_buf: Vec<Vec<f64>> = Vec::new();
        let mut target_y_buf: Vec<f64> = Vec::new();

        for (task, model) in self.source_tasks.iter().zip(source_models.iter()) {
            if model.similarity < 0.3 {
                continue; // Skip dissimilar tasks.
            }
            let ndims_task = task.x.ncols().min(n_dims);
            for row in 0..task.x.nrows() {
                let mut in_domain = true;
                for d in 0..ndims_task {
                    let v = task.x[[row, d]];
                    if v < self.target_bounds[d].0 || v > self.target_bounds[d].1 {
                        in_domain = false;
                        break;
                    }
                }
                if in_domain {
                    let mut x_row = vec![0.0f64; n_dims];
                    for d in 0..ndims_task {
                        x_row[d] = task.x[[row, d]];
                    }
                    target_x_buf.push(x_row);
                    target_y_buf.push(task.y[row]);
                }
            }
        }

        // -----------------------------------------------------------------
        // 3. Target GP.
        // -----------------------------------------------------------------
        let gp_cfg_target = GpSurrogateConfig {
            noise_variance: 1e-6,
            optimize_hyperparams: true,
            n_restarts: 3,
            max_opt_iters: 50,
        };
        let mut target_gp = GpSurrogate::new(Box::new(RbfKernel::default()), gp_cfg_target);

        // -----------------------------------------------------------------
        // 4. Initial random evaluations on the target.
        // -----------------------------------------------------------------
        let n_init = self.config.n_initial.min(n_iterations).max(2);
        let lhs_cfg = SamplingConfig {
            seed: Some(seed),
            ..SamplingConfig::default()
        };
        let x_init = generate_samples(
            n_init,
            &self.target_bounds,
            SamplingStrategy::LatinHypercube,
            Some(lhs_cfg),
        )?;

        let mut history: Vec<(usize, f64)> = Vec::new();
        let mut best_y = f64::INFINITY;
        let mut best_x: Option<Array1<f64>> = None;
        let mut n_target_evals = 0usize;

        for i in 0..x_init.nrows() {
            let xi = x_init.row(i).to_owned();
            let x_slice: Vec<f64> = xi.iter().copied().collect();
            let y = objective(&x_slice);
            n_target_evals += 1;

            target_x_buf.push(x_slice);
            target_y_buf.push(y);

            if y < best_y {
                best_y = y;
                best_x = Some(xi);
            }
            history.push((n_target_evals, y));
        }

        // Fit target GP with initial data.
        self.fit_target_gp(&mut target_gp, &target_x_buf, &target_y_buf, n_dims)?;

        // -----------------------------------------------------------------
        // 5. Main BO loop.
        // -----------------------------------------------------------------
        for iter in n_init..n_iterations {
            let n_cands = self.config.n_candidates;

            // Adaptive target weight: rises linearly from 0 to 1 as more
            // target observations accumulate.
            let target_weight = (iter as f64 / n_iterations as f64).min(1.0);

            // Sample candidates.
            let mut candidates = Array2::zeros((n_cands, n_dims));
            for r in 0..n_cands {
                for c in 0..n_dims {
                    let lo = self.target_bounds[c].0;
                    let hi = self.target_bounds[c].1;
                    candidates[[r, c]] = lo + rng.random::<f64>() * (hi - lo);
                }
            }

            let current_best = if best_y.is_finite() { best_y } else { 0.0 };

            // Evaluate weighted acquisition over all candidates.
            let mut best_acq = f64::NEG_INFINITY;
            let mut best_row = 0;

            for r in 0..n_cands {
                let x_row = candidates.row(r).to_owned();
                let x_mat = match x_row.into_shape_with_order((1, n_dims)) {
                    Ok(m) => m,
                    Err(_) => continue,
                };

                // Target EI.
                let target_ei = if target_gp.n_train() >= 2 {
                    ei_single(&x_mat, &target_gp, current_best, self.config.xi)
                        .unwrap_or(0.0)
                } else {
                    0.0
                };

                // Source EI (weighted sum).
                let mut source_ei = 0.0;
                for sm in &source_models {
                    if sm.gp.n_train() == 0 {
                        continue;
                    }
                    let s_ei = ei_single(&x_mat, &sm.gp, current_best, self.config.xi)
                        .unwrap_or(0.0);
                    source_ei += sm.weight * s_ei;
                }

                let acq = target_weight * target_ei + (1.0 - target_weight) * source_ei;

                if acq > best_acq {
                    best_acq = acq;
                    best_row = r;
                }
            }

            // Evaluate the chosen candidate.
            let x_next = candidates.row(best_row).to_owned();
            let x_slice: Vec<f64> = x_next.iter().copied().collect();
            let y_next = objective(&x_slice);
            n_target_evals += 1;

            target_x_buf.push(x_slice);
            target_y_buf.push(y_next);

            if y_next < best_y {
                best_y = y_next;
                best_x = Some(x_next);
            }

            history.push((n_target_evals, y_next));

            if self.config.verbose >= 2 {
                println!(
                    "[TBO] iter={} w_t={:.2} f={:.6} best={:.6}",
                    iter, target_weight, y_next, best_y
                );
            }

            // Refit target GP.
            self.fit_target_gp(&mut target_gp, &target_x_buf, &target_y_buf, n_dims)?;
        }

        if self.config.verbose >= 1 {
            println!("[TBO] Done. n_evals={} best_f={:.6}", n_target_evals, best_y);
        }

        let x_best = best_x.unwrap_or_else(|| Array1::zeros(n_dims));

        Ok(TransferBoResult {
            x_best,
            f_best: best_y,
            source_weights,
            n_target_evals,
            history,
        })
    }

    // -----------------------------------------------------------------------
    // Internal helpers
    // -----------------------------------------------------------------------

    fn fit_target_gp(
        &self,
        gp: &mut GpSurrogate,
        x_buf: &[Vec<f64>],
        y_buf: &[f64],
        n_dims: usize,
    ) -> OptimizeResult<()> {
        if x_buf.is_empty() {
            return Ok(());
        }
        let n = x_buf.len().min(y_buf.len());
        if n == 0 {
            return Ok(());
        }

        let mut x_mat = Array2::zeros((n, n_dims));
        let mut y_vec = Array1::zeros(n);

        for (i, (xv, &yv)) in x_buf.iter().zip(y_buf.iter()).enumerate().take(n) {
            for d in 0..n_dims.min(xv.len()) {
                x_mat[[i, d]] = xv[d];
            }
            y_vec[i] = yv;
        }

        gp.fit(&x_mat, &y_vec)
    }
}

// ---------------------------------------------------------------------------
// EI helper (uses gp.predict internally)
// ---------------------------------------------------------------------------

fn erf_approx(x: f64) -> f64 {
    let p = 0.3275911_f64;
    let (a1, a2, a3, a4, a5) = (
        0.254829592_f64,
        -0.284496736_f64,
        1.421413741_f64,
        -1.453152027_f64,
        1.061405429_f64,
    );
    let sign = if x < 0.0 { -1.0 } else { 1.0 };
    let xa = x.abs();
    let t = 1.0 / (1.0 + p * xa);
    let poly = (((a5 * t + a4) * t + a3) * t + a2) * t + a1;
    sign * (1.0 - poly * t * (-xa * xa).exp())
}

fn norm_cdf(z: f64) -> f64 {
    0.5 * (1.0 + erf_approx(z / std::f64::consts::SQRT_2))
}

fn norm_pdf(z: f64) -> f64 {
    (-0.5 * z * z).exp() / (2.0 * std::f64::consts::PI).sqrt()
}

fn ei_single(
    x_mat: &Array2<f64>,
    gp: &GpSurrogate,
    best_y: f64,
    xi: f64,
) -> OptimizeResult<f64> {
    let (mean, var) = gp.predict(x_mat)?;
    let mu = mean[0];
    let sigma = var[0].max(0.0).sqrt();
    if sigma < 1e-12 {
        return Ok(0.0);
    }
    let z = (best_y - mu - xi) / sigma;
    let ei = (best_y - mu - xi) * norm_cdf(z) + sigma * norm_pdf(z);
    Ok(ei.max(0.0))
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::{array, Array1, Array2};

    fn simple_source_task() -> TaskObservations {
        // f(x) = x^2 on [0, 3].
        let x = Array2::from_shape_vec((5, 1), vec![0.0, 0.75, 1.5, 2.25, 3.0]).expect("shape");
        let y = Array1::from_vec(vec![0.0, 0.5625, 2.25, 5.0625, 9.0]);
        TaskObservations {
            x,
            y,
            bounds: vec![(0.0_f64, 3.0_f64)],
        }
    }

    #[test]
    fn test_compute_task_similarity_same_bounds() {
        let task = simple_source_task();
        let target_bounds = vec![(0.0_f64, 3.0_f64)];
        let sim = compute_task_similarity(&task, &target_bounds);
        assert!(sim > 0.5 && sim <= 1.0, "similarity={}", sim);
    }

    #[test]
    fn test_compute_task_similarity_disjoint() {
        let task = simple_source_task();
        // Target domain is completely to the right of source data.
        let target_bounds = vec![(10.0_f64, 20.0_f64)];
        let sim = compute_task_similarity(&task, &target_bounds);
        // Should be very low (close to 0).
        assert!(sim < 0.1, "Expected low similarity, got {}", sim);
    }

    #[test]
    fn test_transfer_bo_optimizes() {
        let src = simple_source_task();
        let target_bounds = vec![(0.0_f64, 3.0_f64)];
        let config = TransferBoConfig {
            n_initial: 3,
            n_candidates: 50,
            seed: Some(99),
            verbose: 0,
            ..Default::default()
        };
        let mut tbo =
            TransferBo::new(vec![src], target_bounds, config).expect("build tbo");

        // Target: f(x) = (x - 0.5)^2
        let result = tbo.optimize(|x: &[f64]| (x[0] - 0.5_f64).powi(2), 12)
            .expect("optimize");

        assert!(result.f_best.is_finite());
        assert!(result.f_best < 1.5, "f_best={}", result.f_best);
        assert_eq!(result.n_target_evals, 12);
    }

    #[test]
    fn test_transfer_bo_no_sources() {
        // Should still work with empty source list.
        let target_bounds = vec![(0.0_f64, 5.0_f64)];
        let config = TransferBoConfig {
            n_initial: 3,
            n_candidates: 30,
            seed: Some(7),
            ..Default::default()
        };
        let mut tbo =
            TransferBo::new(vec![], target_bounds, config).expect("build tbo");
        let result = tbo.optimize(|x: &[f64]| (x[0] - 2.5_f64).powi(2), 8)
            .expect("optimize");
        assert!(result.f_best.is_finite());
    }

    #[test]
    fn test_transfer_bo_source_weights_sum_close_to_one() {
        let task1 = simple_source_task();
        let mut task2 = simple_source_task();
        // Shift task2 to a different domain.
        for i in 0..task2.x.nrows() {
            task2.x[[i, 0]] += 5.0;
        }
        task2.bounds = vec![(5.0_f64, 8.0_f64)];

        let target_bounds = vec![(0.0_f64, 3.0_f64)];
        let config = TransferBoConfig {
            n_initial: 2,
            n_candidates: 20,
            seed: Some(1),
            ..Default::default()
        };
        let mut tbo =
            TransferBo::new(vec![task1, task2], target_bounds, config).expect("build");
        let result = tbo.optimize(|x: &[f64]| x[0].powi(2), 5).expect("opt");

        let w_sum: f64 = result.source_weights.iter().sum();
        // Softmax-normalized weights sum to approximately 1 (they're raw weights
        // that we normalised against w_sum).
        assert!((w_sum - 1.0).abs() < 1e-6, "source weights sum={}", w_sum);
    }
}