problemreductions/rules/
cost.rs

1//! Cost functions for reduction path optimization.
2
3use crate::rules::registry::ReductionOverhead;
4use crate::types::ProblemSize;
5
6/// User-defined cost function for path optimization.
7pub trait PathCostFn {
8    /// Compute cost of taking an edge given current problem size.
9    fn edge_cost(&self, overhead: &ReductionOverhead, current_size: &ProblemSize) -> f64;
10}
11
12/// Minimize a single output field.
13pub struct Minimize(pub &'static str);
14
15impl PathCostFn for Minimize {
16    fn edge_cost(&self, overhead: &ReductionOverhead, size: &ProblemSize) -> f64 {
17        overhead.evaluate_output_size(size).get(self.0).unwrap_or(0) as f64
18    }
19}
20
21/// Minimize number of reduction steps.
22pub struct MinimizeSteps;
23
24impl PathCostFn for MinimizeSteps {
25    fn edge_cost(&self, _overhead: &ReductionOverhead, _size: &ProblemSize) -> f64 {
26        1.0
27    }
28}
29
30/// Custom cost function from closure.
31pub struct CustomCost<F>(pub F);
32
33impl<F: Fn(&ReductionOverhead, &ProblemSize) -> f64> PathCostFn for CustomCost<F> {
34    fn edge_cost(&self, overhead: &ReductionOverhead, size: &ProblemSize) -> f64 {
35        (self.0)(overhead, size)
36    }
37}
38
39#[cfg(test)]
40#[path = "../unit_tests/rules/cost.rs"]
41mod tests;