problemreductions/rules/
cost.rs1use crate::rules::registry::ReductionOverhead;
4use crate::types::ProblemSize;
5
6pub trait PathCostFn {
8 fn edge_cost(&self, overhead: &ReductionOverhead, current_size: &ProblemSize) -> f64;
10}
11
12pub 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
21pub struct MinimizeSteps;
23
24impl PathCostFn for MinimizeSteps {
25 fn edge_cost(&self, _overhead: &ReductionOverhead, _size: &ProblemSize) -> f64 {
26 1.0
27 }
28}
29
30pub struct MinimizeOutputSize;
36
37impl PathCostFn for MinimizeOutputSize {
38 fn edge_cost(&self, overhead: &ReductionOverhead, size: &ProblemSize) -> f64 {
39 let output = overhead.evaluate_output_size(size);
40 output.total() as f64
41 }
42}
43
44pub struct MinimizeStepsThenOverhead;
50
51impl PathCostFn for MinimizeStepsThenOverhead {
52 fn edge_cost(&self, overhead: &ReductionOverhead, size: &ProblemSize) -> f64 {
53 const STEP_WEIGHT: f64 = 1e9;
57 let output = overhead.evaluate_output_size(size);
58 let overhead_tiebreaker = (1.0 + output.total() as f64).ln();
59 STEP_WEIGHT + overhead_tiebreaker
60 }
61}
62
63pub struct CustomCost<F>(pub F);
65
66impl<F: Fn(&ReductionOverhead, &ProblemSize) -> f64> PathCostFn for CustomCost<F> {
67 fn edge_cost(&self, overhead: &ReductionOverhead, size: &ProblemSize) -> f64 {
68 (self.0)(overhead, size)
69 }
70}
71
72#[cfg(test)]
73#[path = "../unit_tests/rules/cost.rs"]
74mod tests;