lp_parser_rs 3.4.1

A Rust parser for the LP file format.
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
//! Analysis and statistics for Linear Programming problems.
//!
//! This module provides comprehensive analysis capabilities for LP problems,
//! including summary statistics, issue detection, and structural metrics.
//!
//! # Example
//!
//! ```rust
//! use lp_parser::{LpProblem, analysis::ProblemAnalysis};
//!
//! fn analyze_problem(input: &str) -> Result<(), Box<dyn std::error::Error>> {
//!     let problem = LpProblem::parse(input)?;
//!     let analysis = problem.analyze();
//!
//!     println!("Variables: {}", analysis.summary.variable_count);
//!     println!("Density: {:.2}%", analysis.summary.density * 100.0);
//!
//!     for issue in &analysis.issues {
//!         println!("[{:?}] {}", issue.severity, issue.message);
//!     }
//!     Ok(())
//! }
//! ```

use std::collections::HashSet;
use std::fmt::{Display, Formatter, Result as FmtResult};

use crate::interner::NameId;
use crate::model::{ComparisonOp, Constraint, SOSType, VariableType};
use crate::problem::LpProblem;

/// Configuration for analysis behavior and thresholds.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone)]
pub struct AnalysisConfig {
    /// Coefficient magnitude threshold for "large" warnings (default: 1e9)
    pub large_coefficient_threshold: f64,
    /// Small coefficient threshold for warnings (default: 1e-9)
    pub small_coefficient_threshold: f64,
    /// RHS magnitude threshold for warnings (default: 1e9)
    pub large_rhs_threshold: f64,
    /// Coefficient ratio threshold for scaling warnings (default: 1e6)
    pub coefficient_ratio_threshold: f64,
}

impl Default for AnalysisConfig {
    fn default() -> Self {
        Self {
            large_coefficient_threshold: 1e9,
            small_coefficient_threshold: 1e-9,
            large_rhs_threshold: 1e9,
            coefficient_ratio_threshold: 1e6,
        }
    }
}

/// Complete analysis results for an LP problem.
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone)]
pub struct ProblemAnalysis {
    /// Basic summary statistics
    pub summary: ProblemSummary,
    /// Sparsity and structure metrics
    pub sparsity: SparsityMetrics,
    /// Variable analysis results
    pub variables: VariableAnalysis,
    /// Constraint analysis results
    pub constraints: ConstraintAnalysis,
    /// Coefficient analysis results
    pub coefficients: CoefficientAnalysis,
    /// Detected issues and warnings
    pub issues: Vec<AnalysisIssue>,
}

/// Basic problem summary statistics.
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone)]
pub struct ProblemSummary {
    /// Problem name if available
    pub name: Option<String>,
    /// Optimization sense (Minimize/Maximize)
    pub sense: String,
    /// Number of objectives
    pub objective_count: usize,
    /// Number of constraints
    pub constraint_count: usize,
    /// Number of variables
    pub variable_count: usize,
    /// Total non-zero coefficients across all constraints
    pub total_nonzeros: usize,
    /// Matrix density (nonzeros / (constraints * variables))
    pub density: f64,
}

/// Sparsity and structural metrics.
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone)]
pub struct SparsityMetrics {
    /// Minimum variables in any constraint
    pub min_vars_per_constraint: usize,
    /// Maximum variables in any constraint
    pub max_vars_per_constraint: usize,
}

/// Variable type distribution and analysis.
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone)]
pub struct VariableAnalysis {
    /// Distribution of variable types
    pub type_distribution: VariableTypeDistribution,
    /// Variables with no explicit bounds (truly free)
    pub free_variables: Vec<String>,
    /// Variables where lower bound equals upper bound
    pub fixed_variables: Vec<FixedVariable>,
    /// Variables with inconsistent bounds (lower > upper)
    pub invalid_bounds: Vec<InvalidBound>,
    /// Variables not appearing in any constraint or objective
    pub unused_variables: Vec<String>,
    /// Count of discrete (binary + integer) variables
    pub discrete_variable_count: usize,
}

/// Distribution of variable types.
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone, Default)]
pub struct VariableTypeDistribution {
    /// Free (unbounded) variables
    pub free: usize,
    /// General (non-negative) variables
    pub general: usize,
    /// Lower-bounded only
    pub lower_bounded: usize,
    /// Upper-bounded only
    pub upper_bounded: usize,
    /// Double-bounded (both lower and upper)
    pub double_bounded: usize,
    /// Binary variables
    pub binary: usize,
    /// Integer variables
    pub integer: usize,
    /// Semi-continuous variables
    pub semi_continuous: usize,
    /// SOS variables
    pub sos: usize,
}

/// A variable that is fixed (lower == upper).
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone)]
pub struct FixedVariable {
    /// Variable name
    pub name: String,
    /// Fixed value
    pub value: f64,
}

/// A variable with invalid bounds (lower > upper).
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone)]
pub struct InvalidBound {
    /// Variable name
    pub name: String,
    /// Lower bound value
    pub lower: f64,
    /// Upper bound value
    pub upper: f64,
}

/// Constraint analysis results.
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone)]
pub struct ConstraintAnalysis {
    /// Distribution of constraint types
    pub type_distribution: ConstraintTypeDistribution,
    /// Constraints with no variables
    pub empty_constraints: Vec<String>,
    /// Constraints with only one variable
    pub singleton_constraints: Vec<SingletonConstraint>,
    /// RHS value range statistics
    pub rhs_range: RangeStats,
    /// SOS constraint summary
    pub sos_summary: SOSSummary,
}

/// Distribution of constraint types.
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone, Default)]
pub struct ConstraintTypeDistribution {
    /// Equality constraints (=)
    pub equality: usize,
    /// Less-than-or-equal constraints (<=)
    pub less_than_equal: usize,
    /// Greater-than-or-equal constraints (>=)
    pub greater_than_equal: usize,
    /// Strict less-than constraints (<)
    pub less_than: usize,
    /// Strict greater-than constraints (>)
    pub greater_than: usize,
    /// SOS Type 1 constraints
    pub sos1: usize,
    /// SOS Type 2 constraints
    pub sos2: usize,
}

/// A singleton constraint (only one variable).
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone)]
pub struct SingletonConstraint {
    /// Constraint name
    pub name: String,
    /// The single variable in this constraint
    pub variable: String,
    /// Coefficient of the variable
    pub coefficient: f64,
    /// Comparison operator
    pub operator: String,
    /// Right-hand side value
    pub rhs: f64,
}

/// Summary of SOS constraints.
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone, Default)]
pub struct SOSSummary {
    /// Number of SOS Type 1 constraints
    pub s1_count: usize,
    /// Number of SOS Type 2 constraints
    pub s2_count: usize,
    /// Total variables involved in SOS constraints
    pub total_sos_variables: usize,
}

/// Coefficient analysis results.
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone)]
pub struct CoefficientAnalysis {
    /// Constraint coefficient range statistics
    pub constraint_coeff_range: RangeStats,
    /// Objective coefficient range statistics
    pub objective_coeff_range: RangeStats,
    /// Locations of very large coefficients
    pub large_coefficients: Vec<CoefficientLocation>,
    /// Locations of very small (non-zero) coefficients
    pub small_coefficients: Vec<CoefficientLocation>,
    /// Ratio of max to min absolute coefficient (scaling indicator)
    pub coefficient_ratio: f64,
}

/// Statistical range information.
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone, Default)]
pub struct RangeStats {
    /// Minimum value
    pub min: f64,
    /// Maximum value
    pub max: f64,
    /// Number of values
    pub count: usize,
}

impl RangeStats {
    /// Create a new empty `RangeStats` ready for incremental updates.
    const fn new() -> Self {
        Self { min: f64::INFINITY, max: f64::NEG_INFINITY, count: 0 }
    }

    /// Update the stats with a single value, avoiding intermediate allocations.
    const fn update(&mut self, value: f64) {
        self.min = self.min.min(value);
        self.max = self.max.max(value);
        self.count += 1;
    }

    /// Finalise the stats, normalising the sentinel values for empty sets.
    fn finalise(self) -> Self {
        if self.count == 0 { Self::default() } else { self }
    }

    /// Create range stats from a collection of values.
    #[cfg(test)]
    fn from_values(values: &[f64]) -> Self {
        if values.is_empty() {
            return Self::default();
        }
        let min = values.iter().copied().fold(f64::INFINITY, f64::min);
        let max = values.iter().copied().fold(f64::NEG_INFINITY, f64::max);

        Self { min, max, count: values.len() }
    }
}

/// Location of a coefficient in the problem.
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone)]
pub struct CoefficientLocation {
    /// Name of the constraint or objective
    pub location: String,
    /// Whether this is in an objective (true) or constraint (false)
    pub is_objective: bool,
    /// Variable name
    pub variable: String,
    /// Coefficient value
    pub value: f64,
}

/// Severity level for detected issues.
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IssueSeverity {
    /// Problem is likely unsolvable or invalid
    Error,
    /// May cause numerical issues or unexpected behavior
    Warning,
    /// Informational only
    Info,
}

impl Display for IssueSeverity {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            Self::Error => write!(f, "ERROR"),
            Self::Warning => write!(f, "WARNING"),
            Self::Info => write!(f, "INFO"),
        }
    }
}

/// Category of detected issue.
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IssueCategory {
    /// Invalid variable bounds
    InvalidBounds,
    /// Numerical scaling problems
    NumericalScaling,
    /// Empty constraint
    EmptyConstraint,
    /// Unused variable
    UnusedVariable,
    /// Fixed variable (may be intentional)
    FixedVariable,
    /// Singleton constraint
    SingletonConstraint,
    /// Other issues
    Other,
}

impl Display for IssueCategory {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            Self::InvalidBounds => write!(f, "Invalid Bounds"),
            Self::NumericalScaling => write!(f, "Numerical Scaling"),
            Self::EmptyConstraint => write!(f, "Empty Constraint"),
            Self::UnusedVariable => write!(f, "Unused Variable"),
            Self::FixedVariable => write!(f, "Fixed Variable"),
            Self::SingletonConstraint => write!(f, "Singleton Constraint"),
            Self::Other => write!(f, "Other"),
        }
    }
}

/// A detected issue in the LP problem.
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone)]
pub struct AnalysisIssue {
    /// Severity of the issue
    pub severity: IssueSeverity,
    /// Category of the issue
    pub category: IssueCategory,
    /// Human-readable message
    pub message: String,
    /// Additional details if available
    pub details: Option<String>,
}

impl Display for AnalysisIssue {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(f, "[{}] {}", self.severity, self.message)?;
        if let Some(ref details) = self.details {
            write!(f, " ({details})")?;
        }
        Ok(())
    }
}

impl Display for ProblemAnalysis {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        writeln!(f, "=== LP Problem Analysis ===")?;
        writeln!(f)?;

        // Summary
        writeln!(f, "Summary:")?;
        if let Some(ref name) = self.summary.name {
            writeln!(f, "  Name: {name}")?;
        }
        writeln!(f, "  Sense: {}", self.summary.sense)?;
        writeln!(
            f,
            "  Objectives: {} | Constraints: {} | Variables: {}",
            self.summary.objective_count, self.summary.constraint_count, self.summary.variable_count
        )?;
        writeln!(f, "  Non-zeros: {} | Density: {:.2}%", self.summary.total_nonzeros, self.summary.density * 100.0)?;
        writeln!(f)?;

        // Sparsity
        writeln!(f, "Sparsity:")?;
        writeln!(f, "  Vars per constraint: min={}, max={}", self.sparsity.min_vars_per_constraint, self.sparsity.max_vars_per_constraint)?;
        writeln!(f)?;

        // Variable types
        writeln!(f, "Variable Types:")?;
        let vt = &self.variables.type_distribution;
        writeln!(
            f,
            "  Continuous: {} | Binary: {} | Integer: {}",
            vt.general + vt.free + vt.lower_bounded + vt.upper_bounded + vt.double_bounded,
            vt.binary,
            vt.integer
        )?;
        if vt.semi_continuous > 0 {
            writeln!(f, "  Semi-continuous: {}", vt.semi_continuous)?;
        }
        if vt.sos > 0 {
            writeln!(f, "  SOS: {}", vt.sos)?;
        }
        writeln!(f)?;

        // Constraint types
        writeln!(f, "Constraint Types:")?;
        let ct = &self.constraints.type_distribution;
        writeln!(f, "  Equality (=): {} | (<=): {} | (>=): {}", ct.equality, ct.less_than_equal, ct.greater_than_equal)?;
        if ct.less_than > 0 || ct.greater_than > 0 {
            writeln!(f, "  Strict: (<): {} | (>): {}", ct.less_than, ct.greater_than)?;
        }
        if ct.sos1 > 0 || ct.sos2 > 0 {
            writeln!(f, "  SOS1: {} | SOS2: {}", ct.sos1, ct.sos2)?;
        }
        writeln!(f)?;

        // Coefficient analysis
        if self.coefficients.constraint_coeff_range.count > 0 {
            writeln!(f, "Coefficient Analysis:")?;
            let cr = &self.coefficients.constraint_coeff_range;
            writeln!(f, "  Constraint coeffs: min={:.2e}, max={:.2e}", cr.min, cr.max)?;
            if self.coefficients.objective_coeff_range.count > 0 {
                let or = &self.coefficients.objective_coeff_range;
                writeln!(f, "  Objective coeffs: min={:.2e}, max={:.2e}", or.min, or.max)?;
            }
            if self.coefficients.coefficient_ratio > 1.0 {
                writeln!(f, "  Coefficient ratio: {:.2e}", self.coefficients.coefficient_ratio)?;
            }
            writeln!(f)?;
        }

        // Issues
        if self.issues.is_empty() {
            writeln!(f, "No issues detected.")?;
        } else {
            writeln!(f, "Issues Found: {}", self.issues.len())?;
            for issue in &self.issues {
                writeln!(f, "  {issue}")?;
            }
        }

        Ok(())
    }
}

/// Collects coefficient statistics, classifying each as normal, large, or small.
struct CoeffCollector<'a> {
    range: &'a mut RangeStats,
    large: &'a mut Vec<CoefficientLocation>,
    small: &'a mut Vec<CoefficientLocation>,
}

impl<'a> CoeffCollector<'a> {
    /// Process a slice of coefficients for a given location.
    fn collect(
        &mut self,
        coefficients: &[crate::model::Coefficient],
        location_name: &str,
        is_objective: bool,
        config: &AnalysisConfig,
        interner: &crate::interner::NameInterner,
    ) {
        for coeff in coefficients {
            let abs_value = coeff.value.abs();
            self.range.update(abs_value);

            if abs_value > config.large_coefficient_threshold {
                self.large.push(CoefficientLocation {
                    location: location_name.to_string(),
                    is_objective,
                    variable: interner.resolve(coeff.name).to_string(),
                    value: coeff.value,
                });
            } else if abs_value > 0.0 && abs_value < config.small_coefficient_threshold {
                self.small.push(CoefficientLocation {
                    location: location_name.to_string(),
                    is_objective,
                    variable: interner.resolve(coeff.name).to_string(),
                    value: coeff.value,
                });
            }
        }
    }
}

/// Compute the ratio of max to min absolute coefficient across all coefficients.
fn compute_coefficient_ratio(constraint_range: &RangeStats, objective_range: &RangeStats) -> f64 {
    // Combine the two ranges to find global min/max of positive abs values.
    let has_values = constraint_range.count > 0 || objective_range.count > 0;

    if !has_values {
        return 1.0;
    }

    // Both ranges track abs values, so min is the smallest positive and max is the largest.
    let mut global_min = f64::INFINITY;
    let mut global_max: f64 = 0.0;
    let mut has_positive = false;

    for range in [constraint_range, objective_range] {
        if range.count > 0 && range.max > 0.0 {
            has_positive = true;
            // range.min could be 0.0 (abs of a zero coeff); skip zeros for ratio.
            if range.min > 0.0 && range.min < global_min {
                global_min = range.min;
            }
            if range.max > global_max {
                global_max = range.max;
            }
        }
    }

    let ratio = if has_positive && global_min > 0.0 && global_min < f64::INFINITY { global_max / global_min } else { 1.0 };
    debug_assert!(
        !has_positive || ratio >= 1.0 || global_min == f64::INFINITY,
        "postcondition: coefficient_ratio must be >= 1.0 when coefficients exist, got: {ratio}"
    );
    ratio
}

impl LpProblem {
    /// Perform comprehensive analysis on the LP problem with default configuration.
    #[must_use]
    pub fn analyze(&self) -> ProblemAnalysis {
        self.analyze_with_config(&AnalysisConfig::default())
    }

    /// Perform comprehensive analysis with custom configuration.
    #[must_use]
    pub fn analyze_with_config(&self, config: &AnalysisConfig) -> ProblemAnalysis {
        let summary = self.compute_summary();
        let sparsity = self.compute_sparsity_metrics();
        let variables = self.analyze_variables();
        let constraints = self.analyze_constraints();
        let coefficients = self.analyze_coefficients(config);
        let issues = self.detect_issues(&summary, &variables, &constraints, &coefficients, config);

        ProblemAnalysis { summary, sparsity, variables, constraints, coefficients, issues }
    }

    /// Compute basic summary statistics.
    fn compute_summary(&self) -> ProblemSummary {
        let total_nonzeros = self.count_nonzeros();
        let constraint_count = self.constraint_count();
        let variable_count = self.variable_count();

        #[allow(clippy::cast_precision_loss)]
        let density = if constraint_count > 0 && variable_count > 0 {
            total_nonzeros as f64 / (constraint_count as f64 * variable_count as f64)
        } else {
            0.0
        };

        debug_assert!(density >= 0.0, "postcondition: density must be non-negative, got: {density}");

        ProblemSummary {
            name: self.name.as_ref().map(std::string::ToString::to_string),
            sense: self.sense.to_string(),
            objective_count: self.objective_count(),
            constraint_count,
            variable_count,
            total_nonzeros,
            density,
        }
    }

    /// Count total non-zero coefficients in constraints.
    fn count_nonzeros(&self) -> usize {
        self.constraints
            .values()
            .map(|c| match c {
                Constraint::Standard { coefficients, .. } => coefficients.len(),
                Constraint::SOS { weights, .. } => weights.len(),
            })
            .sum()
    }

    /// Compute sparsity metrics.
    fn compute_sparsity_metrics(&self) -> SparsityMetrics {
        let (min_v, max_v) = self.constraints.values().fold((usize::MAX, 0usize), |(min_v, max_v), c| {
            let n = match c {
                Constraint::Standard { coefficients, .. } => coefficients.len(),
                Constraint::SOS { weights, .. } => weights.len(),
            };
            (min_v.min(n), max_v.max(n))
        });
        SparsityMetrics { min_vars_per_constraint: if min_v == usize::MAX { 0 } else { min_v }, max_vars_per_constraint: max_v }
    }

    /// Analyze variable types, bounds, and usage.
    fn analyze_variables(&self) -> VariableAnalysis {
        let mut type_distribution = VariableTypeDistribution::default();
        let mut free_variables = Vec::new();
        let mut fixed_variables = Vec::new();
        let mut invalid_bounds = Vec::new();

        for (name_id, variable) in &self.variables {
            let name_str = self.interner.resolve(*name_id);
            match &variable.var_type {
                VariableType::Free => {
                    type_distribution.free += 1;
                    free_variables.push(name_str.to_string());
                }
                VariableType::General => type_distribution.general += 1,
                VariableType::LowerBound(_) => type_distribution.lower_bounded += 1,
                VariableType::UpperBound(_) => type_distribution.upper_bounded += 1,
                VariableType::DoubleBound(lower, upper) => {
                    type_distribution.double_bounded += 1;
                    if (lower - upper).abs() < f64::EPSILON {
                        fixed_variables.push(FixedVariable { name: name_str.to_string(), value: *lower });
                    } else if lower > upper {
                        invalid_bounds.push(InvalidBound { name: name_str.to_string(), lower: *lower, upper: *upper });
                    }
                }
                VariableType::Binary => type_distribution.binary += 1,
                VariableType::Integer => type_distribution.integer += 1,
                VariableType::SemiContinuous => type_distribution.semi_continuous += 1,
                VariableType::SOS => type_distribution.sos += 1,
            }
        }

        let unused_variables = self.find_unused_variables();
        let discrete_variable_count = type_distribution.binary + type_distribution.integer;

        debug_assert_eq!(
            type_distribution.free
                + type_distribution.general
                + type_distribution.lower_bounded
                + type_distribution.upper_bounded
                + type_distribution.double_bounded
                + type_distribution.binary
                + type_distribution.integer
                + type_distribution.semi_continuous
                + type_distribution.sos,
            self.variables.len(),
            "postcondition: type distribution must sum to total variable count"
        );

        VariableAnalysis { type_distribution, free_variables, fixed_variables, invalid_bounds, unused_variables, discrete_variable_count }
    }

    /// Find variables declared but not referenced in any objective or constraint.
    fn find_unused_variables(&self) -> Vec<String> {
        let mut used_variables: HashSet<NameId> = HashSet::new();

        for objective in self.objectives.values() {
            for coeff in &objective.coefficients {
                used_variables.insert(coeff.name);
            }
        }

        for constraint in self.constraints.values() {
            match constraint {
                Constraint::Standard { coefficients, .. } => {
                    for coeff in coefficients {
                        used_variables.insert(coeff.name);
                    }
                }
                Constraint::SOS { weights, .. } => {
                    for weight in weights {
                        used_variables.insert(weight.name);
                    }
                }
            }
        }

        self.variables.keys().filter(|name_id| !used_variables.contains(name_id)).map(|id| self.interner.resolve(*id).to_string()).collect()
    }

    /// Analyze constraints.
    fn analyze_constraints(&self) -> ConstraintAnalysis {
        let mut type_distribution = ConstraintTypeDistribution::default();
        let mut empty_constraints = Vec::new();
        let mut singleton_constraints = Vec::new();
        let mut rhs_range = RangeStats::new();
        let mut sos_summary = SOSSummary::default();

        for (name_id, constraint) in &self.constraints {
            let name_str = self.interner.resolve(*name_id);
            match constraint {
                Constraint::Standard { coefficients, operator, rhs, .. } => {
                    match operator {
                        ComparisonOp::EQ => type_distribution.equality += 1,
                        ComparisonOp::LTE => type_distribution.less_than_equal += 1,
                        ComparisonOp::GTE => type_distribution.greater_than_equal += 1,
                        ComparisonOp::LT => type_distribution.less_than += 1,
                        ComparisonOp::GT => type_distribution.greater_than += 1,
                    }

                    rhs_range.update(*rhs);

                    if coefficients.is_empty() {
                        empty_constraints.push(name_str.to_string());
                    } else if coefficients.len() == 1 {
                        let coeff = &coefficients[0];
                        singleton_constraints.push(SingletonConstraint {
                            name: name_str.to_string(),
                            variable: self.interner.resolve(coeff.name).to_string(),
                            coefficient: coeff.value,
                            operator: operator.to_string(),
                            rhs: *rhs,
                        });
                    }
                }
                Constraint::SOS { sos_type, weights, .. } => {
                    match sos_type {
                        SOSType::S1 => {
                            type_distribution.sos1 += 1;
                            sos_summary.s1_count += 1;
                        }
                        SOSType::S2 => {
                            type_distribution.sos2 += 1;
                            sos_summary.s2_count += 1;
                        }
                    }
                    sos_summary.total_sos_variables += weights.len();
                }
            }
        }

        ConstraintAnalysis { type_distribution, empty_constraints, singleton_constraints, rhs_range: rhs_range.finalise(), sos_summary }
    }

    /// Analyze coefficients.
    fn analyze_coefficients(&self, config: &AnalysisConfig) -> CoefficientAnalysis {
        let mut constraint_range = RangeStats::new();
        let mut objective_range = RangeStats::new();
        let mut large_coefficients = Vec::new();
        let mut small_coefficients = Vec::new();

        let mut constraint_collector =
            CoeffCollector { range: &mut constraint_range, large: &mut large_coefficients, small: &mut small_coefficients };

        for (name_id, constraint) in &self.constraints {
            if let Constraint::Standard { coefficients, .. } = constraint {
                let name_str = self.interner.resolve(*name_id);
                constraint_collector.collect(coefficients, name_str, false, config, &self.interner);
            }
        }

        // Reborrow for objectives with a separate range tracker.
        let mut objective_collector =
            CoeffCollector { range: &mut objective_range, large: constraint_collector.large, small: constraint_collector.small };

        for (name_id, objective) in &self.objectives {
            let name_str = self.interner.resolve(*name_id);
            objective_collector.collect(&objective.coefficients, name_str, true, config, &self.interner);
        }

        let constraint_coeff_range = constraint_range.finalise();
        let objective_coeff_range = objective_range.finalise();
        let coefficient_ratio = compute_coefficient_ratio(&constraint_coeff_range, &objective_coeff_range);

        CoefficientAnalysis { constraint_coeff_range, objective_coeff_range, large_coefficients, small_coefficients, coefficient_ratio }
    }

    /// Detect issues and generate warnings.
    #[allow(clippy::unused_self)]
    fn detect_issues(
        &self,
        summary: &ProblemSummary,
        variables: &VariableAnalysis,
        constraints: &ConstraintAnalysis,
        coefficients: &CoefficientAnalysis,
        config: &AnalysisConfig,
    ) -> Vec<AnalysisIssue> {
        let mut issues = Vec::new();

        // Invalid bounds (ERROR)
        for invalid in &variables.invalid_bounds {
            issues.push(AnalysisIssue {
                severity: IssueSeverity::Error,
                category: IssueCategory::InvalidBounds,
                message: format!("Variable '{}' has invalid bounds: lower ({}) > upper ({})", invalid.name, invalid.lower, invalid.upper),
                details: None,
            });
        }

        // Empty constraints (WARNING)
        for name in &constraints.empty_constraints {
            issues.push(AnalysisIssue {
                severity: IssueSeverity::Warning,
                category: IssueCategory::EmptyConstraint,
                message: format!("Constraint '{name}' has no variables"),
                details: None,
            });
        }

        // Over-constrained check (WARNING) - may indicate degeneracy
        if summary.constraint_count >= summary.variable_count && summary.variable_count > 0 {
            issues.push(AnalysisIssue {
                severity: IssueSeverity::Warning,
                category: IssueCategory::Other,
                message: format!(
                    "Problem may be over-constrained: {} constraints for {} variables",
                    summary.constraint_count, summary.variable_count
                ),
                details: Some("Over-constrained problems often have degenerate or infeasible solutions".to_string()),
            });
        }

        // Large RHS warning
        if constraints.rhs_range.count > 0 && constraints.rhs_range.max > config.large_rhs_threshold {
            issues.push(AnalysisIssue {
                severity: IssueSeverity::Warning,
                category: IssueCategory::NumericalScaling,
                message: format!("Large RHS value ({:.2e}) may cause numerical issues", constraints.rhs_range.max),
                details: None,
            });
        }

        // Large coefficient ratio (WARNING)
        if coefficients.coefficient_ratio > config.coefficient_ratio_threshold {
            issues.push(AnalysisIssue {
                severity: IssueSeverity::Warning,
                category: IssueCategory::NumericalScaling,
                message: format!("Large coefficient ratio ({:.2e}) may cause numerical instability", coefficients.coefficient_ratio),
                details: Some("Consider rescaling the problem".to_string()),
            });
        }

        // Large coefficients
        for loc in &coefficients.large_coefficients {
            issues.push(AnalysisIssue {
                severity: IssueSeverity::Warning,
                category: IssueCategory::NumericalScaling,
                message: format!(
                    "Large coefficient ({:.2e}) for variable '{}' in {}",
                    loc.value,
                    loc.variable,
                    if loc.is_objective { "objective" } else { "constraint" }
                ),
                details: Some(loc.location.clone()),
            });
        }

        // Fixed variables (INFO)
        for fixed in &variables.fixed_variables {
            issues.push(AnalysisIssue {
                severity: IssueSeverity::Info,
                category: IssueCategory::FixedVariable,
                message: format!("Variable '{}' is fixed at value {}", fixed.name, fixed.value),
                details: None,
            });
        }

        // Singleton constraints (INFO)
        if !constraints.singleton_constraints.is_empty() {
            issues.push(AnalysisIssue {
                severity: IssueSeverity::Info,
                category: IssueCategory::SingletonConstraint,
                message: format!(
                    "{} singleton constraint(s) detected (may represent simple bounds)",
                    constraints.singleton_constraints.len()
                ),
                details: None,
            });
        }

        // Unused variables (INFO)
        for name in &variables.unused_variables {
            issues.push(AnalysisIssue {
                severity: IssueSeverity::Info,
                category: IssueCategory::UnusedVariable,
                message: format!("Variable '{name}' is not used in any constraint or objective"),
                details: None,
            });
        }

        issues
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_range_stats_empty() {
        let stats = RangeStats::from_values(&[]);
        assert_eq!(stats.count, 0);
    }

    #[test]
    #[allow(clippy::float_cmp)]
    fn test_range_stats_single() {
        let stats = RangeStats::from_values(&[5.0]);
        assert_eq!(stats.count, 1);
        assert_eq!(stats.min, 5.0);
        assert_eq!(stats.max, 5.0);
    }

    #[test]
    #[allow(clippy::float_cmp)]
    fn test_range_stats_multiple() {
        let stats = RangeStats::from_values(&[1.0, 2.0, 3.0, 4.0, 5.0]);
        assert_eq!(stats.count, 5);
        assert_eq!(stats.min, 1.0);
        assert_eq!(stats.max, 5.0);
    }

    #[test]
    fn test_issue_severity_display() {
        assert_eq!(IssueSeverity::Error.to_string(), "ERROR");
        assert_eq!(IssueSeverity::Warning.to_string(), "WARNING");
        assert_eq!(IssueSeverity::Info.to_string(), "INFO");
    }

    #[test]
    fn test_issue_category_display() {
        assert_eq!(IssueCategory::InvalidBounds.to_string(), "Invalid Bounds");
        assert_eq!(IssueCategory::NumericalScaling.to_string(), "Numerical Scaling");
    }

    #[test]
    fn test_analysis_issue_display() {
        let issue = AnalysisIssue {
            severity: IssueSeverity::Warning,
            category: IssueCategory::NumericalScaling,
            message: "Test message".to_string(),
            details: Some("Details here".to_string()),
        };
        let display = issue.to_string();
        assert!(display.contains("WARNING"));
        assert!(display.contains("Test message"));
        assert!(display.contains("Details here"));
    }

    #[test]
    #[allow(clippy::float_cmp)]
    fn test_default_config() {
        let config = AnalysisConfig::default();
        assert_eq!(config.large_coefficient_threshold, 1e9);
        assert_eq!(config.small_coefficient_threshold, 1e-9);
    }
}