cobre-sddp 0.6.2

Stochastic Dual Dynamic Programming (SDDP) for hydrothermal dispatch and energy planning
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
//! Provenance report types and builder for the SDDP preprocessing pipeline.
//!
//! [`ModelProvenanceReport`] is a JSON-serializable summary of which data
//! sources were used for each role in the stochastic preprocessing pipeline:
//! seasonal statistics, AR coefficients, spatial correlation, and the opening
//! scenario tree.
//!
//! [`build_provenance_report`] constructs a [`ModelProvenanceReport`] from
//! the outputs already available after [`crate::setup::prepare_stochastic`]
//! returns.

use std::fmt;

use serde::Serialize;

use cobre_stochastic::{ComponentProvenance, StochasticProvenance};

use crate::estimation::{EstimationPath, EstimationReport};

// ── ProvenanceSource ──────────────────────────────────────────────────────────

/// Origin of a single data role in the preprocessing pipeline.
///
/// Used by [`ModelProvenanceReport`] to describe where seasonal stats, AR
/// coefficients, spatial correlation, and the opening tree came from.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ProvenanceSource {
    /// Computed from inflow history by the estimation pipeline.
    Estimated,
    /// Loaded from a user-supplied input file.
    UserFile,
    /// Not applicable — either the system has no hydro plants, or this role is
    /// not relevant for the chosen estimation path (e.g., no AR in the
    /// deterministic case).
    #[serde(rename = "n/a")]
    NotApplicable,
}

impl fmt::Display for ProvenanceSource {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Estimated => write!(f, "estimated"),
            Self::UserFile => write!(f, "user_file"),
            Self::NotApplicable => write!(f, "n/a"),
        }
    }
}

// ── ModelProvenanceReport ─────────────────────────────────────────────────────

/// Structured summary of which data sources were used in the stochastic
/// preprocessing pipeline.
///
/// Intended for JSON output via `serde_json::to_writer_pretty`. Each field
/// records the origin of one data role. Callers (CLI, Python bindings) pass
/// this struct directly to `serde_json` without additional transformation.
///
/// # Example
///
/// ```rust
/// use cobre_sddp::{ModelProvenanceReport, ProvenanceSource};
///
/// let report = ModelProvenanceReport {
///     estimation_path: "full_estimation".to_string(),
///     seasonal_stats_source: ProvenanceSource::Estimated,
///     ar_coefficients_source: ProvenanceSource::Estimated,
///     correlation_source: ProvenanceSource::Estimated,
///     opening_tree_source: ProvenanceSource::Estimated,
///     n_hydros: 3,
///     ar_method: Some("AIC".to_string()),
///     ar_max_order: Some(2),
///     white_noise_fallbacks: vec![],
///     historical_library_past_inflows_digest: None,
/// };
/// let json = serde_json::to_string_pretty(&report).unwrap();
/// assert!(json.contains("\"full_estimation\""));
/// ```
#[derive(Debug, Clone, Serialize)]
pub struct ModelProvenanceReport {
    /// Stable string label of the estimation path taken (from [`EstimationPath::as_str`]).
    pub estimation_path: String,
    /// Origin of seasonal mean/std data used in the inflow model.
    pub seasonal_stats_source: ProvenanceSource,
    /// Origin of the AR lag coefficients.
    pub ar_coefficients_source: ProvenanceSource,
    /// Origin of the spatial correlation decomposition.
    pub correlation_source: ProvenanceSource,
    /// Origin of the noise opening scenario tree.
    pub opening_tree_source: ProvenanceSource,
    /// Number of hydro plants in the system.
    pub n_hydros: usize,
    /// Order selection method used when AR coefficients were estimated
    /// (e.g., `"AIC"`, `"PACF"`). `None` when AR was not estimated.
    pub ar_method: Option<String>,
    /// Maximum AR order across all hydro plants when AR was estimated.
    /// `None` when AR was not estimated.
    pub ar_max_order: Option<usize>,
    /// IDs of hydro plants that fell back to white noise (empty AR,
    /// `residual_std_ratio = 1.0`). Populated only by
    /// [`EstimationPath::PartialEstimation`]; empty otherwise.
    pub white_noise_fallbacks: Vec<i32>,
    /// SipHash-1-3 fingerprint of the `initial_conditions.past_inflows` values
    /// used to seed the rolling η-inversion chain of the historical scenario
    /// library, when one was built.
    ///
    /// `None` when the historical inflow scheme is not active (no
    /// `HistoricalScenarioLibrary` was built). When `Some(d)`, a stale-library
    /// detector can fingerprint the current `past_inflows` and compare; a
    /// mismatch means η was inverted against a different x₀ and replay is no
    /// longer exact.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub historical_library_past_inflows_digest: Option<u64>,
}

// ── build_provenance_report ───────────────────────────────────────────────────

/// Map a [`ComponentProvenance`] to a [`ProvenanceSource`].
fn component_to_source(cp: ComponentProvenance) -> ProvenanceSource {
    match cp {
        ComponentProvenance::Generated => ProvenanceSource::Estimated,
        ComponentProvenance::UserSupplied => ProvenanceSource::UserFile,
        ComponentProvenance::NotApplicable => ProvenanceSource::NotApplicable,
    }
}

/// Build a [`ModelProvenanceReport`] from preprocessing outputs.
///
/// This function is infallible: all required inputs are guaranteed to be
/// available after [`crate::setup::prepare_stochastic`] returns.
///
/// The mapping from [`EstimationPath`] to per-role [`ProvenanceSource`] is:
///
/// | Path                  | Seasonal stats | AR coefficients |
/// |-----------------------|---------------|-----------------|
/// | `Deterministic`       | N/A           | N/A               |
/// | `UserStatsWhiteNoise` | `UserFile`    | N/A               |
/// | `UserProvidedNoHistory` | `UserFile`  | `UserFile`        |
/// | `FullEstimation`      | `Estimated`   | `Estimated`       |
/// | `UserArHistoryStats`  | `Estimated`   | `UserFile`        |
/// | `PartialEstimation`   | `UserFile`    | `Estimated`       |
/// | `UserProvidedAll`     | `UserFile`    | `UserFile`        |
///
/// Correlation and opening-tree sources are derived from the
/// [`StochasticProvenance`] embedded in the stochastic context.
///
/// When `estimation_report` is `Some`, `ar_method` and `ar_max_order` are
/// populated from its fields; otherwise both are `None`.
#[must_use]
pub fn build_provenance_report(
    estimation_path: EstimationPath,
    estimation_report: Option<&EstimationReport>,
    provenance: &StochasticProvenance,
    n_hydros: usize,
) -> ModelProvenanceReport {
    let (seasonal_stats_source, ar_coefficients_source) = match estimation_path {
        EstimationPath::Deterministic => (
            ProvenanceSource::NotApplicable,
            ProvenanceSource::NotApplicable,
        ),
        EstimationPath::UserStatsWhiteNoise => {
            (ProvenanceSource::UserFile, ProvenanceSource::NotApplicable)
        }
        EstimationPath::UserProvidedNoHistory => {
            (ProvenanceSource::UserFile, ProvenanceSource::UserFile)
        }
        EstimationPath::FullEstimation => {
            (ProvenanceSource::Estimated, ProvenanceSource::Estimated)
        }
        EstimationPath::UserArHistoryStats => {
            (ProvenanceSource::Estimated, ProvenanceSource::UserFile)
        }
        EstimationPath::PartialEstimation => {
            (ProvenanceSource::UserFile, ProvenanceSource::Estimated)
        }
        EstimationPath::UserProvidedAll => (ProvenanceSource::UserFile, ProvenanceSource::UserFile),
    };

    let correlation_source = component_to_source(provenance.correlation);
    let opening_tree_source = component_to_source(provenance.opening_tree);

    let (ar_method, ar_max_order, white_noise_fallbacks) = if let Some(report) = estimation_report {
        let max_order = report
            .entries
            .values()
            .map(|e| e.selected_order as usize)
            .max();
        let fallbacks: Vec<i32> = report.white_noise_fallbacks.iter().map(|id| id.0).collect();
        (Some(report.method.clone()), max_order, fallbacks)
    } else {
        (None, None, vec![])
    };

    ModelProvenanceReport {
        estimation_path: estimation_path.as_str().to_owned(),
        seasonal_stats_source,
        ar_coefficients_source,
        correlation_source,
        opening_tree_source,
        n_hydros,
        ar_method,
        ar_max_order,
        white_noise_fallbacks,
        historical_library_past_inflows_digest: None,
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    #![allow(
        clippy::unwrap_used,
        clippy::expect_used,
        clippy::panic,
        clippy::float_cmp
    )]

    use std::collections::BTreeMap;

    use cobre_core::EntityId;
    use cobre_stochastic::{ComponentProvenance, StochasticProvenance};

    use crate::estimation::{EstimationPath, EstimationReport, HydroEstimationEntry};

    use super::{ProvenanceSource, build_provenance_report};

    // Helper: StochasticProvenance with all Generated.
    fn prov_all_generated() -> StochasticProvenance {
        StochasticProvenance {
            opening_tree: ComponentProvenance::Generated,
            correlation: ComponentProvenance::Generated,
            inflow_model: ComponentProvenance::Generated,
            inflow_scheme: None,
            load_scheme: None,
            ncs_scheme: None,
        }
    }

    // Helper: StochasticProvenance for a deterministic (no-entity) system.
    fn prov_not_applicable() -> StochasticProvenance {
        StochasticProvenance {
            opening_tree: ComponentProvenance::NotApplicable,
            correlation: ComponentProvenance::NotApplicable,
            inflow_model: ComponentProvenance::NotApplicable,
            inflow_scheme: None,
            load_scheme: None,
            ncs_scheme: None,
        }
    }

    // Helper: StochasticProvenance with user-supplied tree.
    fn prov_user_tree() -> StochasticProvenance {
        StochasticProvenance {
            opening_tree: ComponentProvenance::UserSupplied,
            correlation: ComponentProvenance::Generated,
            inflow_model: ComponentProvenance::Generated,
            inflow_scheme: None,
            load_scheme: None,
            ncs_scheme: None,
        }
    }

    #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
    fn make_estimation_report(method: &str, orders: &[u32], fallbacks: &[i32]) -> EstimationReport {
        let entries: BTreeMap<EntityId, HydroEstimationEntry> = orders
            .iter()
            .enumerate()
            .map(|(i, &order)| {
                (
                    EntityId(i as i32 + 1),
                    HydroEstimationEntry {
                        selected_order: order,
                        coefficients: vec![],
                        contribution_reductions: vec![],
                    },
                )
            })
            .collect();
        EstimationReport {
            entries,
            method: method.to_owned(),
            white_noise_fallbacks: fallbacks.iter().map(|&id| EntityId(id)).collect(),
            lag_scale_warnings: Vec::new(),
            std_ratio_warnings: Vec::new(),
        }
    }

    // ── Path mapping tests ────────────────────────────────────────────────────

    #[test]
    fn build_provenance_report_omits_historical_digest_by_default() {
        let report = build_provenance_report(
            EstimationPath::FullEstimation,
            None,
            &prov_all_generated(),
            2,
        );
        assert!(
            report.historical_library_past_inflows_digest.is_none(),
            "builder must leave historical_library_past_inflows_digest unset; \
             callers populate it from setup.scenario_libraries.training.historical \
             when the historical scheme is active"
        );
    }

    #[test]
    fn historical_digest_field_round_trips_through_json() {
        let mut report = build_provenance_report(
            EstimationPath::FullEstimation,
            None,
            &prov_all_generated(),
            1,
        );
        let digest: u64 = 0xDEAD_BEEF_CAFE_F00D;
        report.historical_library_past_inflows_digest = Some(digest);
        let json = serde_json::to_string(&report).unwrap();
        assert!(
            json.contains("historical_library_past_inflows_digest"),
            "JSON must surface the digest field when populated: {json}"
        );
        assert!(
            json.contains(&digest.to_string()),
            "JSON must serialize the digest as a decimal u64 ({digest}); got: {json}"
        );
    }

    #[test]
    fn historical_digest_field_omitted_when_none() {
        let report = build_provenance_report(
            EstimationPath::Deterministic,
            None,
            &prov_not_applicable(),
            0,
        );
        let json = serde_json::to_string(&report).unwrap();
        assert!(
            !json.contains("historical_library_past_inflows_digest"),
            "JSON must NOT include digest field when None (Option::is_none skip): {json}"
        );
    }

    #[test]
    fn deterministic_path_both_na() {
        let report = build_provenance_report(
            EstimationPath::Deterministic,
            None,
            &prov_not_applicable(),
            0,
        );
        assert!(
            matches!(
                report.seasonal_stats_source,
                ProvenanceSource::NotApplicable
            ),
            "seasonal_stats_source must be NotApplicable for Deterministic"
        );
        assert!(
            matches!(
                report.ar_coefficients_source,
                ProvenanceSource::NotApplicable
            ),
            "ar_coefficients_source must be NotApplicable for Deterministic"
        );
        assert!(report.ar_method.is_none(), "ar_method must be None");
        assert!(report.ar_max_order.is_none(), "ar_max_order must be None");
        assert_eq!(report.estimation_path, "deterministic");
    }

    #[test]
    fn user_stats_white_noise_path() {
        let report = build_provenance_report(
            EstimationPath::UserStatsWhiteNoise,
            None,
            &prov_all_generated(),
            2,
        );
        assert!(
            matches!(report.seasonal_stats_source, ProvenanceSource::UserFile),
            "seasonal_stats_source must be UserFile for UserStatsWhiteNoise"
        );
        assert!(
            matches!(
                report.ar_coefficients_source,
                ProvenanceSource::NotApplicable
            ),
            "ar_coefficients_source must be NotApplicable for UserStatsWhiteNoise"
        );
        assert_eq!(report.estimation_path, "user_stats_white_noise");
    }

    #[test]
    fn user_provided_no_history_path() {
        let report = build_provenance_report(
            EstimationPath::UserProvidedNoHistory,
            None,
            &prov_all_generated(),
            2,
        );
        assert!(
            matches!(report.seasonal_stats_source, ProvenanceSource::UserFile),
            "seasonal_stats_source must be UserFile for UserProvidedNoHistory"
        );
        assert!(
            matches!(report.ar_coefficients_source, ProvenanceSource::UserFile),
            "ar_coefficients_source must be UserFile for UserProvidedNoHistory"
        );
        assert_eq!(report.estimation_path, "user_provided_no_history");
    }

    #[test]
    fn full_estimation_path() {
        let er = make_estimation_report("AIC", &[2, 3], &[]);
        let report = build_provenance_report(
            EstimationPath::FullEstimation,
            Some(&er),
            &prov_all_generated(),
            2,
        );
        assert!(
            matches!(report.seasonal_stats_source, ProvenanceSource::Estimated),
            "seasonal_stats_source must be Estimated for FullEstimation"
        );
        assert!(
            matches!(report.ar_coefficients_source, ProvenanceSource::Estimated),
            "ar_coefficients_source must be Estimated for FullEstimation"
        );
        assert_eq!(report.ar_method.as_deref(), Some("AIC"));
        assert_eq!(report.ar_max_order, Some(3));
        assert_eq!(report.estimation_path, "full_estimation");
    }

    #[test]
    fn user_ar_history_stats_path() {
        let er = make_estimation_report("PACF", &[1], &[]);
        let report = build_provenance_report(
            EstimationPath::UserArHistoryStats,
            Some(&er),
            &prov_all_generated(),
            1,
        );
        assert!(
            matches!(report.seasonal_stats_source, ProvenanceSource::Estimated),
            "seasonal_stats_source must be Estimated for UserArHistoryStats"
        );
        assert!(
            matches!(report.ar_coefficients_source, ProvenanceSource::UserFile),
            "ar_coefficients_source must be UserFile for UserArHistoryStats"
        );
        assert_eq!(report.estimation_path, "user_ar_history_stats");
    }

    #[test]
    fn partial_estimation_path() {
        let er = make_estimation_report("AIC", &[2], &[5, 7]);
        let report = build_provenance_report(
            EstimationPath::PartialEstimation,
            Some(&er),
            &prov_all_generated(),
            3,
        );
        assert!(
            matches!(report.seasonal_stats_source, ProvenanceSource::UserFile),
            "seasonal_stats_source must be UserFile for PartialEstimation"
        );
        assert!(
            matches!(report.ar_coefficients_source, ProvenanceSource::Estimated),
            "ar_coefficients_source must be Estimated for PartialEstimation"
        );
        assert_eq!(report.white_noise_fallbacks, vec![5, 7]);
        assert_eq!(report.estimation_path, "partial_estimation");
    }

    #[test]
    fn user_provided_all_path() {
        let report = build_provenance_report(
            EstimationPath::UserProvidedAll,
            None,
            &prov_all_generated(),
            4,
        );
        assert!(
            matches!(report.seasonal_stats_source, ProvenanceSource::UserFile),
            "seasonal_stats_source must be UserFile for UserProvidedAll"
        );
        assert!(
            matches!(report.ar_coefficients_source, ProvenanceSource::UserFile),
            "ar_coefficients_source must be UserFile for UserProvidedAll"
        );
        assert!(
            report.ar_method.is_none(),
            "ar_method must be None when no report"
        );
        assert_eq!(report.estimation_path, "user_provided_all");
    }

    // ── ComponentProvenance mapping tests ─────────────────────────────────────

    #[test]
    fn user_supplied_tree_maps_to_user_file() {
        let report =
            build_provenance_report(EstimationPath::FullEstimation, None, &prov_user_tree(), 2);
        assert!(
            matches!(report.opening_tree_source, ProvenanceSource::UserFile),
            "UserSupplied opening tree must map to UserFile"
        );
        assert!(
            matches!(report.correlation_source, ProvenanceSource::Estimated),
            "Generated correlation must map to Estimated"
        );
    }

    // ── JSON serialization tests ──────────────────────────────────────────────

    #[test]
    fn full_estimation_json_round_trip() {
        let er = make_estimation_report("AIC", &[2], &[]);
        let report = build_provenance_report(
            EstimationPath::FullEstimation,
            Some(&er),
            &prov_all_generated(),
            1,
        );
        let json = serde_json::to_string_pretty(&report).unwrap();
        assert!(
            json.contains("\"full_estimation\""),
            "JSON must contain estimation_path value"
        );
        assert!(
            json.contains("\"estimated\""),
            "JSON must contain estimated source"
        );
        // Verify it parses as a valid JSON object with expected keys.
        let value: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(value["estimation_path"], "full_estimation");
        assert_eq!(value["seasonal_stats_source"], "estimated");
        assert_eq!(value["ar_coefficients_source"], "estimated");
    }

    #[test]
    fn deterministic_json_na_variant() {
        let report = build_provenance_report(
            EstimationPath::Deterministic,
            None,
            &prov_not_applicable(),
            0,
        );
        let json = serde_json::to_string_pretty(&report).unwrap();
        let value: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(
            value["seasonal_stats_source"], "n/a",
            "NotApplicable must serialize as \"n/a\""
        );
        assert_eq!(value["ar_coefficients_source"], "n/a");
    }

    // ── ProvenanceSource Display tests ────────────────────────────────────────

    #[test]
    fn provenance_source_display() {
        assert_eq!(ProvenanceSource::Estimated.to_string(), "estimated");
        assert_eq!(ProvenanceSource::UserFile.to_string(), "user_file");
        assert_eq!(ProvenanceSource::NotApplicable.to_string(), "n/a");
    }

    // ── white_noise_fallbacks propagation ─────────────────────────────────────

    #[test]
    fn white_noise_fallbacks_propagated_as_raw_ids() {
        let er = make_estimation_report("AIC", &[1, 2], &[3, 7]);
        let report = build_provenance_report(
            EstimationPath::PartialEstimation,
            Some(&er),
            &prov_all_generated(),
            2,
        );
        assert_eq!(
            report.white_noise_fallbacks,
            vec![3, 7],
            "white_noise_fallbacks must carry raw i32 IDs"
        );
    }

    #[test]
    fn no_estimation_report_yields_empty_fallbacks() {
        let report = build_provenance_report(
            EstimationPath::Deterministic,
            None,
            &prov_not_applicable(),
            0,
        );
        assert!(
            report.white_noise_fallbacks.is_empty(),
            "white_noise_fallbacks must be empty when no estimation_report"
        );
    }
}