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
//! Inflow non-negativity treatment method for SDDP subproblems.
//!
//! [`InflowNonNegativityMethod`] is a flat enum representing the strategies
//! for handling negative PAR(p) inflow realisations in the LP subproblems.
//! It is dispatched via `match` when constructing LP templates and extracting
//! simulation results. Enum dispatch is used because the variant set is closed
//! (enum dispatch for closed variant sets).
/// Inflow non-negativity treatment method.
///
/// Determines whether slack columns are added to the LP and what objective
/// coefficient they carry. The variant must be the same across all stages
/// (set once at solver initialisation from the loaded case config).
///
/// # Examples
///
/// ```rust
/// use cobre_sddp::InflowNonNegativityMethod;
///
/// let penalty = InflowNonNegativityMethod::Penalty { cost: 1000.0 };
/// assert!(penalty.has_slack_columns());
///
/// let none = InflowNonNegativityMethod::None;
/// assert!(!none.has_slack_columns());
/// ```
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum InflowNonNegativityMethod {
/// No inflow non-negativity enforcement.
///
/// The LP does not include slack columns. Negative inflow noise may cause
/// LP infeasibility if the PAR(p) noise realisation is sufficiently negative.
None,
/// Truncation-based enforcement: clamps negative PAR(p) inflows to zero
/// by modifying the noise vector before LP patching.
///
/// Does not add slack columns to the LP. Instead, the PAR(p) model is
/// evaluated outside the LP to obtain the full inflow value; if the result
/// is negative, the noise component is adjusted so that the inflow is zero.
/// This prevents LP infeasibility without perturbing the objective function.
Truncation,
/// Penalty-based enforcement with objective cost `penalty_cost` per m³/s
/// per stage-hour.
///
/// Appends `N` slack columns (`sigma_inf_h >= 0`) to the LP. Each slack
/// enters the water balance row for hydro `h` with coefficient
/// `tau_total * M3S_TO_HM3`, where `tau_total` is the total stage duration
/// in hours. The objective coefficient is `penalty_cost * tau_total`.
Penalty {
/// Penalty coefficient `c^{inf}` applied to each slack unit.
cost: f64,
},
}
impl InflowNonNegativityMethod {
/// Returns `true` when slack columns are appended to the LP.
///
/// # Examples
///
/// ```rust
/// use cobre_sddp::InflowNonNegativityMethod;
///
/// assert!(!InflowNonNegativityMethod::None.has_slack_columns());
/// assert!(InflowNonNegativityMethod::Penalty { cost: 100.0 }.has_slack_columns());
/// ```
#[must_use]
pub fn has_slack_columns(&self) -> bool {
matches!(self, InflowNonNegativityMethod::Penalty { .. })
}
/// Returns the penalty cost when slack columns are active, or `None`.
///
/// # Examples
///
/// ```rust
/// use cobre_sddp::InflowNonNegativityMethod;
///
/// assert_eq!(InflowNonNegativityMethod::Penalty { cost: 500.0 }.penalty_cost(), Some(500.0));
/// assert_eq!(InflowNonNegativityMethod::None.penalty_cost(), None);
/// ```
#[must_use]
pub fn penalty_cost(&self) -> Option<f64> {
match self {
InflowNonNegativityMethod::Penalty { cost } => Some(*cost),
InflowNonNegativityMethod::Truncation | InflowNonNegativityMethod::None => None,
}
}
}
impl From<&cobre_io::config::InflowNonNegativityConfig> for InflowNonNegativityMethod {
/// Convert from the cobre-io config type.
///
/// Recognised method strings are `"none"`, `"truncation"`, and `"penalty"`.
/// Any other value is treated as `None`. Method string validation is the
/// responsibility of the cobre-io loading pipeline (five-layer validation),
/// so unrecognised values indicate a programming error that should have been
/// caught upstream.
fn from(cfg: &cobre_io::config::InflowNonNegativityConfig) -> Self {
match cfg.method.as_str() {
"truncation" => InflowNonNegativityMethod::Truncation,
"penalty" => InflowNonNegativityMethod::Penalty {
cost: cfg.penalty_cost,
},
_ => InflowNonNegativityMethod::None,
}
}
}
#[cfg(test)]
mod tests {
use super::InflowNonNegativityMethod;
use cobre_io::config::InflowNonNegativityConfig;
// ── has_slack_columns ────────────────────────────────────────────────────
#[test]
fn none_has_no_slack_columns() {
assert!(!InflowNonNegativityMethod::None.has_slack_columns());
}
#[test]
fn truncation_has_no_slack_columns() {
assert!(!InflowNonNegativityMethod::Truncation.has_slack_columns());
}
#[test]
fn penalty_has_slack_columns() {
assert!(InflowNonNegativityMethod::Penalty { cost: 100.0 }.has_slack_columns());
}
// ── penalty_cost ─────────────────────────────────────────────────────────
#[test]
fn penalty_cost_for_penalty_variant() {
assert_eq!(
InflowNonNegativityMethod::Penalty { cost: 500.0 }.penalty_cost(),
Some(500.0)
);
}
#[test]
fn penalty_cost_none_for_none_variant() {
assert_eq!(InflowNonNegativityMethod::None.penalty_cost(), None);
}
#[test]
fn truncation_penalty_cost_is_none() {
assert_eq!(InflowNonNegativityMethod::Truncation.penalty_cost(), None);
}
// ── conversion from config ───────────────────────────────────────────────
#[test]
fn test_inflow_method_conversion_none() {
let cfg = InflowNonNegativityConfig {
method: "none".to_string(),
penalty_cost: 0.0,
};
assert_eq!(
InflowNonNegativityMethod::from(&cfg),
InflowNonNegativityMethod::None
);
}
#[test]
fn test_inflow_method_conversion_penalty() {
let cfg = InflowNonNegativityConfig {
method: "penalty".to_string(),
penalty_cost: 500.0,
};
assert_eq!(
InflowNonNegativityMethod::from(&cfg),
InflowNonNegativityMethod::Penalty { cost: 500.0 }
);
}
#[test]
fn test_inflow_method_conversion_truncation() {
let cfg = InflowNonNegativityConfig {
method: "truncation".to_string(),
penalty_cost: 0.0,
};
assert_eq!(
InflowNonNegativityMethod::from(&cfg),
InflowNonNegativityMethod::Truncation
);
}
#[test]
fn test_truncation_ignores_penalty_cost() {
let cfg = InflowNonNegativityConfig {
method: "truncation".to_string(),
penalty_cost: 999.0,
};
assert_eq!(
InflowNonNegativityMethod::from(&cfg),
InflowNonNegativityMethod::Truncation
);
}
#[test]
fn test_inflow_method_conversion_unknown_falls_back_to_none() {
let cfg = InflowNonNegativityConfig {
method: "unknown_method".to_string(),
penalty_cost: 100.0,
};
assert_eq!(
InflowNonNegativityMethod::from(&cfg),
InflowNonNegativityMethod::None
);
}
#[test]
fn test_penalty_config_propagation() {
let costs = [0.0, 100.0, 500.0, 1000.0, f64::MAX];
for &expected_cost in &costs {
let cfg = InflowNonNegativityConfig {
method: "penalty".to_string(),
penalty_cost: expected_cost,
};
let method = InflowNonNegativityMethod::from(&cfg);
assert_eq!(method.penalty_cost(), Some(expected_cost));
}
}
}