use crate::arith::{Capacity, Cost, Flow, Objective, Overflow};
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum BuildError {
#[error(
"arc {arc} references node {node}, but the network has {node_count} nodes (ids 0..{node_count})"
)]
NodeOutOfRange {
arc: usize,
node: usize,
node_count: usize,
},
#[error("arc {arc} has lower bound {lower} above its capacity {upper}: no flow satisfies it")]
InvalidBounds {
arc: usize,
lower: Capacity,
upper: Capacity,
},
#[error(
"supply set on node {node}, but the network has {node_count} nodes (ids 0..{node_count})"
)]
SupplyNodeOutOfRange {
node: usize,
node_count: usize,
},
#[error("node supplies sum to {total}, not 0: total supply must equal total demand")]
UnbalancedSupply {
total: Objective,
},
#[error("network has {count} {what}, above the supported maximum of {max}")]
TooLarge {
what: &'static str,
count: usize,
max: usize,
},
#[error(transparent)]
Overflow(#[from] Overflow),
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum SolveError {
#[error("instance is infeasible: {routable} of {required} units of supply can reach a demand")]
Infeasible {
routable: Objective,
required: Objective,
},
#[error(transparent)]
Overflow(#[from] Overflow),
#[error(
"operation budget of {budget} exhausted after {scaling_phases} scaling phases; raise SolverOptions::max_operations or lower the cost range"
)]
OperationBudgetExhausted {
budget: u64,
scaling_phases: u32,
},
#[error("solver produced a flow that failed certification: {0} (this is a bug in eo-mcf)")]
SelfCertificationFailed(VerifyError),
#[error("solver invariant violated: {what} (this is a bug in eo-mcf, please report it)")]
InvariantViolated {
what: &'static str,
},
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum VerifyError {
#[error("flow vector has {found} entries but the network has {expected} arcs")]
LengthMismatch {
expected: usize,
found: usize,
},
#[error("arc {arc} carries {flow}, outside its bounds [{lower}, {upper}]")]
CapacityViolated {
arc: usize,
flow: Flow,
lower: Capacity,
upper: Capacity,
},
#[error("node {node} has net outflow {net} but supply {supply}: flow is not conserved there")]
ConservationViolated {
node: usize,
net: Objective,
supply: Flow,
},
#[error(
"residual arc {tail}→{head} has reduced cost {reduced_cost} < 0 under the recomputed potentials: the flow is not optimal"
)]
NegativeReducedCost {
tail: usize,
head: usize,
reduced_cost: Cost,
},
#[error(
"residual graph admits no potential function: a negative-cost cycle reaches node {node}"
)]
PotentialsDoNotExist {
node: usize,
},
#[error(
"residual graph has a cycle of mean cost {numerator}/{denominator} < 0 through node {node}: the flow is not optimal"
)]
NegativeCycle {
numerator: i128,
denominator: i128,
node: usize,
},
#[error(
"certifiers disagree: the dual certificate says {dual_verdict} but negative-cycle detection says {cycle_verdict} — one of them is defective"
)]
CertifiersDisagree {
dual_verdict: &'static str,
cycle_verdict: &'static str,
},
#[error("minimum-cycle-mean search did not converge within {iterations} iterations")]
CycleSearchDidNotConverge {
iterations: u64,
},
#[error(transparent)]
Overflow(#[from] Overflow),
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum DimacsError {
#[error(
"no problem line: a DIMACS min-cost-flow file must contain one `p min <nodes> <arcs>` line"
)]
MissingProblemLine,
#[error("line {line}: second problem line; exactly one `p min <nodes> <arcs>` is allowed")]
DuplicateProblemLine {
line: usize,
},
#[error("line {line}: `{tag}` descriptor appears before the problem line")]
DescriptorBeforeProblem {
line: usize,
tag: char,
},
#[error("line {line}: unknown line type `{tag}`; expected one of c, p, n, a")]
UnknownLineType {
line: usize,
tag: char,
},
#[error("line {line}: `{tag}` line needs {expected} fields, found {found}")]
FieldCount {
line: usize,
tag: char,
expected: usize,
found: usize,
},
#[error("line {line}: field {field} (`{text}`) is not an integer in i64 range")]
BadInteger {
line: usize,
field: usize,
text: String,
},
#[error("line {line}: problem type `{found}` is not supported; this reader handles `min` only")]
UnsupportedProblemType {
line: usize,
found: String,
},
#[error("line {line}: node id {node} is outside 1..={node_count} declared on the problem line")]
NodeOutOfRange {
line: usize,
node: i64,
node_count: usize,
},
#[error("line {line}: supply for node {node} declared twice")]
DuplicateSupply {
line: usize,
node: usize,
},
#[error("problem line declares {declared} arcs but the file holds {found}")]
ArcCountMismatch {
declared: usize,
found: usize,
},
#[error(
"line {line}: {node_count} declared nodes need at least {required} bytes, above the reader's limit of {limit}; pass a larger limit to `dimacs::read_within` if the instance really is this large"
)]
DeclaredSizeExceedsLimit {
line: usize,
node_count: usize,
required: u64,
limit: u64,
},
#[error("line {line}: declared {what} of {value} is not a valid count")]
BadCount {
line: usize,
what: &'static str,
value: i64,
},
#[error(transparent)]
Build(#[from] BuildError),
}