scirs2_optimize/
error.rs

1//! Error types for the SciRS2 optimization module
2
3use scirs2_core::error::{CoreError, CoreResult};
4use thiserror::Error;
5
6// Type aliases for compatibility
7pub type ScirsError = CoreError;
8pub type ScirsResult<T> = CoreResult<T>;
9
10/// Optimization error type
11#[derive(Error, Debug)]
12pub enum OptimizeError {
13    /// Computation error (generic error)
14    #[error("Computation error: {0}")]
15    ComputationError(String),
16
17    /// Convergence error (algorithm did not converge)
18    #[error("Convergence error: {0}")]
19    ConvergenceError(String),
20
21    /// Value error (invalid value)
22    #[error("Value error: {0}")]
23    ValueError(String),
24
25    /// Not implemented error
26    #[error("Not implemented: {0}")]
27    NotImplementedError(String),
28
29    /// Initialization error (failed to initialize optimizer)
30    #[error("Initialization error: {0}")]
31    InitializationError(String),
32
33    /// I/O error
34    #[error("I/O error: {0}")]
35    IOError(String),
36
37    /// Invalid input error
38    #[error("Invalid input: {0}")]
39    InvalidInput(String),
40
41    /// Invalid parameter error
42    #[error("Invalid parameter: {0}")]
43    InvalidParameter(String),
44
45    /// Maximum evaluations reached
46    #[error("Maximum evaluations reached")]
47    MaxEvaluationsReached,
48}
49
50/// Result type for optimization operations
51pub type OptimizeResult<T> = Result<T, OptimizeError>;
52
53// Implement conversion from SparseError to OptimizeError
54impl From<scirs2_sparse::error::SparseError> for OptimizeError {
55    fn from(error: scirs2_sparse::error::SparseError) -> Self {
56        match error {
57            scirs2_sparse::SparseError::ComputationError(msg) => {
58                OptimizeError::ComputationError(msg)
59            }
60            scirs2_sparse::SparseError::DimensionMismatch { expected, found } => {
61                OptimizeError::ValueError(format!(
62                    "Dimension mismatch: expected {}, found {}",
63                    expected, found
64                ))
65            }
66            scirs2_sparse::SparseError::IndexOutOfBounds { index, shape } => {
67                OptimizeError::ValueError(format!(
68                    "Index {:?} out of bounds for array with shape {:?}",
69                    index, shape
70                ))
71            }
72            scirs2_sparse::SparseError::InvalidAxis => {
73                OptimizeError::ValueError("Invalid axis specified".to_string())
74            }
75            scirs2_sparse::SparseError::InvalidSliceRange => {
76                OptimizeError::ValueError("Invalid slice range specified".to_string())
77            }
78            scirs2_sparse::SparseError::InconsistentData { reason } => {
79                OptimizeError::ValueError(format!("Inconsistent data: {}", reason))
80            }
81            scirs2_sparse::SparseError::NotImplemented(msg) => {
82                OptimizeError::NotImplementedError(msg)
83            }
84            scirs2_sparse::SparseError::SingularMatrix(msg) => {
85                OptimizeError::ComputationError(format!("Singular matrix error: {}", msg))
86            }
87            scirs2_sparse::SparseError::ValueError(msg) => OptimizeError::ValueError(msg),
88            scirs2_sparse::SparseError::ConversionError(msg) => {
89                OptimizeError::ValueError(format!("Conversion error: {}", msg))
90            }
91            scirs2_sparse::SparseError::OperationNotSupported(msg) => {
92                OptimizeError::NotImplementedError(format!("Operation not supported: {}", msg))
93            }
94            scirs2_sparse::SparseError::ShapeMismatch { expected, found } => {
95                OptimizeError::ValueError(format!(
96                    "Shape mismatch: expected {:?}, found {:?}",
97                    expected, found
98                ))
99            }
100            scirs2_sparse::SparseError::IterativeSolverFailure(msg) => {
101                OptimizeError::ConvergenceError(format!("Iterative solver failure: {}", msg))
102            }
103            scirs2_sparse::SparseError::IndexCastOverflow { value, target_type } => {
104                OptimizeError::ValueError(format!(
105                    "Index value {} cannot be represented in the target type {}",
106                    value, target_type
107                ))
108            }
109            scirs2_sparse::SparseError::ConvergenceError(msg) => {
110                OptimizeError::ConvergenceError(format!("Convergence error: {}", msg))
111            }
112            scirs2_sparse::SparseError::InvalidFormat(msg) => {
113                OptimizeError::ValueError(format!("Invalid format: {}", msg))
114            }
115            scirs2_sparse::SparseError::IoError(err) => {
116                OptimizeError::IOError(format!("I/O error: {}", err))
117            }
118            scirs2_sparse::SparseError::CompressionError(msg) => {
119                OptimizeError::ComputationError(format!("Compression error: {}", msg))
120            }
121            scirs2_sparse::SparseError::Io(msg) => {
122                OptimizeError::IOError(format!("I/O error: {}", msg))
123            }
124            scirs2_sparse::SparseError::BlockNotFound(msg) => {
125                OptimizeError::ValueError(format!("Block not found: {}", msg))
126            }
127            scirs2_sparse::SparseError::GpuError(err) => {
128                OptimizeError::ComputationError(format!("GPU error: {}", err))
129            }
130        }
131    }
132}
133
134// Implement conversion from GpuError to OptimizeError
135impl From<scirs2_core::GpuError> for OptimizeError {
136    fn from(error: scirs2_core::GpuError) -> Self {
137        OptimizeError::ComputationError(error.to_string())
138    }
139}
140
141// Implement conversion from OptimizeError to CoreError
142impl From<OptimizeError> for CoreError {
143    fn from(error: OptimizeError) -> Self {
144        match error {
145            OptimizeError::ComputationError(msg) => CoreError::ComputationError(
146                scirs2_core::error::ErrorContext::new(msg)
147                    .with_location(scirs2_core::error::ErrorLocation::new(file!(), line!())),
148            ),
149            OptimizeError::ConvergenceError(msg) => CoreError::ConvergenceError(
150                scirs2_core::error::ErrorContext::new(msg)
151                    .with_location(scirs2_core::error::ErrorLocation::new(file!(), line!())),
152            ),
153            OptimizeError::ValueError(msg) => CoreError::ValueError(
154                scirs2_core::error::ErrorContext::new(msg)
155                    .with_location(scirs2_core::error::ErrorLocation::new(file!(), line!())),
156            ),
157            OptimizeError::NotImplementedError(msg) => CoreError::NotImplementedError(
158                scirs2_core::error::ErrorContext::new(msg)
159                    .with_location(scirs2_core::error::ErrorLocation::new(file!(), line!())),
160            ),
161            OptimizeError::InitializationError(msg) => CoreError::ComputationError(
162                scirs2_core::error::ErrorContext::new(format!("Initialization error: {}", msg))
163                    .with_location(scirs2_core::error::ErrorLocation::new(file!(), line!())),
164            ),
165            OptimizeError::IOError(msg) => CoreError::IoError(
166                scirs2_core::error::ErrorContext::new(msg)
167                    .with_location(scirs2_core::error::ErrorLocation::new(file!(), line!())),
168            ),
169            OptimizeError::InvalidInput(msg) => CoreError::InvalidInput(
170                scirs2_core::error::ErrorContext::new(msg)
171                    .with_location(scirs2_core::error::ErrorLocation::new(file!(), line!())),
172            ),
173            OptimizeError::InvalidParameter(msg) => CoreError::InvalidArgument(
174                scirs2_core::error::ErrorContext::new(msg)
175                    .with_location(scirs2_core::error::ErrorLocation::new(file!(), line!())),
176            ),
177            OptimizeError::MaxEvaluationsReached => CoreError::ComputationError(
178                scirs2_core::error::ErrorContext::new(
179                    "Maximum number of function evaluations reached".to_string(),
180                )
181                .with_location(scirs2_core::error::ErrorLocation::new(file!(), line!())),
182            ),
183        }
184    }
185}