cobre-io 0.5.1

Case directory loading and validation for the Cobre power systems ecosystem
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
//! Reader for `training/convergence.parquet`.
//!
//! This module provides [`read_convergence_summary`], which reads the
//! convergence log written by the training pipeline and returns an
//! aggregated [`ConvergenceSummary`] suitable for display in post-run
//! reporting commands.

use std::path::Path;

use arrow::array::{Array, AsArray, RecordBatch};
use arrow::datatypes::{Float64Type, Int64Type};
use parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder;

use super::error::OutputError;

/// Aggregated summary extracted from `training/convergence.parquet`.
///
/// Produced by [`read_convergence_summary`]. Fields are summed or sampled
/// from the last row of the convergence table, so this struct is suitable
/// for display without holding a full per-iteration record list.
#[derive(Debug, Clone)]
pub struct ConvergenceSummary {
    /// Total number of LP solves summed across all iterations.
    pub total_lp_solves: u64,
    /// Total wall-clock time summed across all iterations (milliseconds).
    pub total_time_ms: u64,
    /// Lower bound value from the final iteration (0.0 when no rows).
    pub final_lower_bound: f64,
    /// Mean upper bound estimate from the final iteration (0.0 when no rows).
    pub final_upper_bound_mean: f64,
    /// Standard deviation of the upper bound from the final iteration (0.0 when no rows).
    pub final_upper_bound_std: f64,
    /// Relative gap from the final iteration, or `None` when no rows or gap was undefined.
    pub final_gap_percent: Option<f64>,
}

/// Read `training/convergence.parquet` and return an aggregated summary.
///
/// Reads all record batches from `path`, sums `lp_solves` and `time_total_ms`
/// across every row, and takes the bound and gap fields from the last row.
///
/// When the file contains zero rows, all numeric fields are zero and
/// `final_gap_percent` is `None`.
///
/// # Errors
///
/// - [`OutputError::IoError`] when `path` does not exist or cannot be opened.
/// - [`OutputError::SerializationError`] when the Parquet file is malformed or
///   the reader fails to iterate over batches.
/// - [`OutputError::SchemaError`] when a required column is absent from the file.
pub fn read_convergence_summary(path: &Path) -> Result<ConvergenceSummary, OutputError> {
    let file = std::fs::File::open(path).map_err(|e| OutputError::io(path, e))?;

    let reader = ParquetRecordBatchReaderBuilder::try_new(file)
        .map_err(|e| OutputError::SerializationError {
            entity: "convergence".to_string(),
            message: e.to_string(),
        })?
        .build()
        .map_err(|e| OutputError::SerializationError {
            entity: "convergence".to_string(),
            message: e.to_string(),
        })?;

    let mut totals = BatchTotals::default();

    for batch_result in reader {
        let batch = batch_result.map_err(|e| OutputError::SerializationError {
            entity: "convergence".to_string(),
            message: e.to_string(),
        })?;
        if batch.num_rows() > 0 {
            accumulate_batch(&batch, &mut totals)?;
        }
    }

    Ok(totals.into_summary())
}

// ── Private helpers ───────────────────────────────────────────────────────────

/// Mutable accumulator updated once per non-empty record batch.
#[derive(Default)]
struct BatchTotals {
    total_lp_solves: i64,
    total_time_ms: i64,
    final_lower_bound: f64,
    final_upper_bound_mean: f64,
    final_upper_bound_std: f64,
    final_gap_percent: Option<f64>,
    has_rows: bool,
}

impl BatchTotals {
    fn into_summary(self) -> ConvergenceSummary {
        if !self.has_rows {
            return ConvergenceSummary {
                total_lp_solves: 0,
                total_time_ms: 0,
                final_lower_bound: 0.0,
                final_upper_bound_mean: 0.0,
                final_upper_bound_std: 0.0,
                final_gap_percent: None,
            };
        }
        #[allow(clippy::cast_sign_loss)]
        ConvergenceSummary {
            total_lp_solves: self.total_lp_solves.max(0) as u64,
            total_time_ms: self.total_time_ms.max(0) as u64,
            final_lower_bound: self.final_lower_bound,
            final_upper_bound_mean: self.final_upper_bound_mean,
            final_upper_bound_std: self.final_upper_bound_std,
            final_gap_percent: self.final_gap_percent,
        }
    }
}

/// Read the first row's `gap_percent` value from `training/convergence.parquet`.
///
/// Used by the post-run summary to display "Gap: X% (started at Y%)".
/// Returns `None` when the file is empty, the gap column is null on the
/// first row (e.g. upper-bound evaluation disabled), or the file cannot
/// be read or parsed. Errors are not surfaced because the initial gap is
/// a cosmetic enhancement, not a load-bearing data point.
#[must_use]
pub fn read_initial_gap_percent(path: &Path) -> Option<f64> {
    let file = std::fs::File::open(path).ok()?;
    let reader = ParquetRecordBatchReaderBuilder::try_new(file)
        .ok()?
        .build()
        .ok()?;
    for batch_result in reader {
        let batch = batch_result.ok()?;
        if batch.num_rows() == 0 {
            continue;
        }
        let gap_arr = get_f64_column(&batch, "gap_percent").ok()?;
        return if gap_arr.is_valid(0) {
            Some(gap_arr.value(0))
        } else {
            None
        };
    }
    None
}

/// Extract an `Int64` column from `batch` by name, returning a schema error on failure.
fn get_i64_column<'a>(
    batch: &'a RecordBatch,
    name: &str,
) -> Result<&'a arrow::array::PrimitiveArray<Int64Type>, OutputError> {
    let col = batch
        .column_by_name(name)
        .ok_or_else(|| OutputError::SchemaError {
            file: "convergence.parquet".to_string(),
            column: name.to_string(),
            message: "column not found".to_string(),
        })?;
    col.as_primitive_opt::<Int64Type>()
        .ok_or_else(|| OutputError::SchemaError {
            file: "convergence.parquet".to_string(),
            column: name.to_string(),
            message: "expected Int64 column".to_string(),
        })
}

/// Extract a `Float64` column from `batch` by name, returning a schema error on failure.
fn get_f64_column<'a>(
    batch: &'a RecordBatch,
    name: &str,
) -> Result<&'a arrow::array::PrimitiveArray<Float64Type>, OutputError> {
    let col = batch
        .column_by_name(name)
        .ok_or_else(|| OutputError::SchemaError {
            file: "convergence.parquet".to_string(),
            column: name.to_string(),
            message: "column not found".to_string(),
        })?;
    col.as_primitive_opt::<Float64Type>()
        .ok_or_else(|| OutputError::SchemaError {
            file: "convergence.parquet".to_string(),
            column: name.to_string(),
            message: "expected Float64 column".to_string(),
        })
}

/// Update `totals` with data from a single non-empty record batch.
fn accumulate_batch(batch: &RecordBatch, totals: &mut BatchTotals) -> Result<(), OutputError> {
    let lp_solves_arr = get_i64_column(batch, "lp_solves")?;
    for i in 0..lp_solves_arr.len() {
        totals.total_lp_solves = totals
            .total_lp_solves
            .saturating_add(lp_solves_arr.value(i));
    }

    let time_arr = get_i64_column(batch, "time_total_ms")?;
    for i in 0..time_arr.len() {
        totals.total_time_ms = totals.total_time_ms.saturating_add(time_arr.value(i));
    }

    let last = batch.num_rows() - 1;

    totals.final_lower_bound = get_f64_column(batch, "lower_bound")?.value(last);
    totals.final_upper_bound_mean = get_f64_column(batch, "upper_bound_mean")?.value(last);
    totals.final_upper_bound_std = get_f64_column(batch, "upper_bound_std")?.value(last);

    let gap_arr = get_f64_column(batch, "gap_percent")?;
    // gap_percent is nullable: distinguish null from 0.0 using is_valid.
    totals.final_gap_percent = if gap_arr.is_valid(last) {
        Some(gap_arr.value(last))
    } else {
        None
    };

    totals.has_rows = true;
    Ok(())
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::float_cmp,
    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap
)]
mod tests {
    use super::*;
    use crate::output::{
        IterationRecord, OutputContext, RowPoolStatistics, SimulationOutput, TrainingOutput,
        write_results,
    };

    fn make_iteration_record(iteration: u32, lp_solves: u32) -> IterationRecord {
        IterationRecord {
            iteration,
            lower_bound: f64::from(iteration) * 10.0,
            upper_bound_mean: f64::from(iteration) * 10.0 + 2.0,
            upper_bound_std: 0.5,
            gap_percent: Some(1.0),
            cuts_added: 5,
            cuts_removed: 1,
            cuts_active: 4,
            time_forward_ms: 100,
            time_backward_ms: 200,
            time_total_ms: 300,
            forward_passes: 4,
            lp_solves,
            time_forward_wall_ms: 100,
            time_backward_wall_ms: 200,
            time_cut_selection_ms: 0,
            time_mpi_allreduce_ms: 0,
            time_cut_sync_ms: 0,
            time_lower_bound_ms: 0,
            time_state_exchange_ms: 0,
            time_cut_batch_build_ms: 0,
            time_bwd_setup_ms: 0,
            time_bwd_load_imbalance_ms: 0,
            time_bwd_scheduling_overhead_ms: 0,
            time_fwd_setup_ms: 0,
            time_fwd_load_imbalance_ms: 0,
            time_fwd_scheduling_overhead_ms: 0,
            time_overhead_ms: 0,
            solve_time_ms: 0.0,
        }
    }

    fn make_training_output(records: Vec<IterationRecord>) -> TrainingOutput {
        let n = records.len() as u32;
        TrainingOutput {
            convergence_records: records,
            final_lower_bound: 99.5,
            final_upper_bound: Some(101.0),
            final_gap_percent: Some(1.51),
            iterations_completed: n,
            converged: true,
            termination_reason: "gap tolerance reached".to_string(),
            total_time_ms: 5_000,
            cut_stats: RowPoolStatistics {
                total_generated: 200,
                total_active: 80,
                peak_active: 95,
            },
            cut_selection_records: vec![],
            worker_timing_records: vec![],
        }
    }

    fn make_system() -> cobre_core::System {
        cobre_core::SystemBuilder::new()
            .build()
            .expect("empty system must be valid")
    }

    fn make_config() -> crate::Config {
        use crate::config::{
            CheckpointingConfig, EstimationConfig, ExportsConfig, InflowNonNegativityConfig,
            ModelingConfig, PolicyConfig, PolicyMode, RowSelectionConfig, SimulationConfig,
            StoppingRuleConfig, TrainingConfig, TrainingSolverConfig, UpperBoundEvaluationConfig,
        };
        crate::Config {
            schema: None,
            modeling: ModelingConfig {
                inflow_non_negativity: InflowNonNegativityConfig::default(),
            },
            training: TrainingConfig {
                enabled: true,
                tree_seed: None,
                forward_passes: Some(4),
                stopping_rules: Some(vec![StoppingRuleConfig::IterationLimit { limit: 10 }]),
                stopping_mode: "any".to_string(),
                cut_formulation: None,
                forward_pass: None,
                cut_selection: RowSelectionConfig::default(),
                solver: TrainingSolverConfig::default(),
                scenario_source: None,
            },
            upper_bound_evaluation: UpperBoundEvaluationConfig::default(),
            policy: PolicyConfig {
                path: "./policy".to_string(),
                mode: PolicyMode::Fresh,
                validate_compatibility: true,
                checkpointing: CheckpointingConfig::default(),
                boundary: None,
            },
            simulation: SimulationConfig {
                enabled: false,
                num_scenarios: 0,
                policy_type: "outer".to_string(),
                output_path: None,
                output_mode: None,
                io_channel_capacity: 64,
                scenario_source: None,
            },
            exports: ExportsConfig::default(),
            estimation: EstimationConfig::default(),
        }
    }

    fn make_output_context() -> OutputContext {
        use crate::output::DistributionInfo;
        OutputContext {
            hostname: "test-host".to_string(),
            solver: "highs".to_string(),
            solver_version: None,
            started_at: "2026-01-17T08:00:00Z".to_string(),
            completed_at: "2026-01-17T12:30:00Z".to_string(),
            distribution: DistributionInfo {
                backend: "local".to_string(),
                world_size: 1,
                ranks_participated: 1,
                num_nodes: 1,
                threads_per_rank: 1,
                mpi_library: None,
                mpi_standard: None,
                thread_level: None,
                slurm_job_id: None,
            },
        }
    }

    fn write_convergence(
        tmp: &tempfile::TempDir,
        records: Vec<IterationRecord>,
    ) -> std::path::PathBuf {
        let training = make_training_output(records);
        write_results(
            tmp.path(),
            &training,
            None::<&SimulationOutput>,
            &make_system(),
            &make_config(),
            &make_output_context(),
        )
        .expect("write_results must succeed");
        tmp.path().join("training/convergence.parquet")
    }

    // ── Acceptance criteria ───────────────────────────────────────────────────

    #[test]
    fn read_convergence_summary_from_real_parquet() {
        let tmp = tempfile::tempdir().unwrap();
        // Three records with lp_solves = [40, 50, 60].
        let records = vec![
            make_iteration_record(1, 40),
            make_iteration_record(2, 50),
            make_iteration_record(3, 60),
        ];
        let path = write_convergence(&tmp, records);

        let summary = read_convergence_summary(&path).expect("read must succeed");

        assert_eq!(
            summary.total_lp_solves, 150,
            "total_lp_solves must equal sum of all records: 40+50+60=150"
        );
        // The last record has iteration=3, lower_bound = 3*10.0 = 30.0.
        assert_eq!(
            summary.final_lower_bound, 30.0,
            "final_lower_bound must come from the last row"
        );
        // total_time_ms: 3 records × 300 ms each = 900 ms.
        assert_eq!(
            summary.total_time_ms, 900,
            "total_time_ms must be sum across all rows"
        );
        // gap_percent is Some(1.0) for every record; last row should be Some(1.0).
        assert_eq!(
            summary.final_gap_percent,
            Some(1.0),
            "final_gap_percent must come from the last row"
        );
    }

    #[test]
    fn read_convergence_summary_empty_file() {
        let tmp = tempfile::tempdir().unwrap();
        let path = write_convergence(&tmp, vec![]);

        let summary = read_convergence_summary(&path).expect("read must succeed on empty file");

        assert_eq!(
            summary.total_lp_solves, 0,
            "total_lp_solves must be 0 for empty file"
        );
        assert_eq!(
            summary.total_time_ms, 0,
            "total_time_ms must be 0 for empty file"
        );
        assert_eq!(
            summary.final_lower_bound, 0.0,
            "final_lower_bound must be 0.0 for empty file"
        );
        assert_eq!(
            summary.final_upper_bound_mean, 0.0,
            "final_upper_bound_mean must be 0.0 for empty file"
        );
        assert_eq!(
            summary.final_upper_bound_std, 0.0,
            "final_upper_bound_std must be 0.0 for empty file"
        );
        assert!(
            summary.final_gap_percent.is_none(),
            "final_gap_percent must be None for empty file"
        );
    }

    #[test]
    fn read_convergence_summary_missing_file() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("nonexistent.parquet");

        let result = read_convergence_summary(&path);

        assert!(
            matches!(result, Err(OutputError::IoError { .. })),
            "missing file must return OutputError::IoError, got: {result:?}"
        );
    }

    #[test]
    fn read_convergence_summary_single_row() {
        let tmp = tempfile::tempdir().unwrap();
        let records = vec![make_iteration_record(1, 40)];
        let path = write_convergence(&tmp, records);

        let summary = read_convergence_summary(&path).expect("read must succeed");

        assert_eq!(summary.total_lp_solves, 40);
        assert_eq!(summary.total_time_ms, 300);
        assert_eq!(summary.final_lower_bound, 10.0);
        assert_eq!(summary.final_upper_bound_mean, 12.0);
        assert_eq!(summary.final_upper_bound_std, 0.5);
        assert_eq!(summary.final_gap_percent, Some(1.0));
    }
}