problemreductions/rules/
precedenceconstrainedscheduling_ilp.rs1use crate::models::algebraic::{LinearConstraint, ObjectiveSense, ILP};
14use crate::models::misc::PrecedenceConstrainedScheduling;
15use crate::reduction;
16use crate::rules::traits::{ReduceTo, ReductionResult};
17
18#[derive(Debug, Clone)]
23pub struct ReductionPCSToILP {
24 target: ILP<bool>,
25 num_tasks: usize,
26 deadline: usize,
27}
28
29impl ReductionResult for ReductionPCSToILP {
30 type Source = PrecedenceConstrainedScheduling;
31 type Target = ILP<bool>;
32
33 fn target_problem(&self) -> &ILP<bool> {
34 &self.target
35 }
36
37 fn extract_solution(&self, target_solution: &[usize]) -> Vec<usize> {
42 let d = self.deadline;
43 (0..self.num_tasks)
44 .map(|j| {
45 (0..d)
46 .find(|&t| target_solution.get(j * d + t).copied().unwrap_or(0) == 1)
47 .unwrap_or(0)
48 })
49 .collect()
50 }
51}
52
53#[reduction(
54 overhead = {
55 num_vars = "num_tasks * deadline",
56 num_constraints = "num_tasks + deadline + num_tasks^2",
57 }
58)]
59impl ReduceTo<ILP<bool>> for PrecedenceConstrainedScheduling {
60 type Result = ReductionPCSToILP;
61
62 fn reduce_to(&self) -> Self::Result {
63 let n = self.num_tasks();
64 let m = self.num_processors();
65 let d = self.deadline();
66 let num_vars = n * d;
67
68 let var = |j: usize, t: usize| j * d + t;
70
71 let mut constraints = Vec::new();
72
73 for j in 0..n {
75 let terms: Vec<(usize, f64)> = (0..d).map(|t| (var(j, t), 1.0)).collect();
76 constraints.push(LinearConstraint::eq(terms, 1.0));
77 }
78
79 for t in 0..d {
81 let terms: Vec<(usize, f64)> = (0..n).map(|j| (var(j, t), 1.0)).collect();
82 constraints.push(LinearConstraint::le(terms, m as f64));
83 }
84
85 for &(i, j) in self.precedences() {
88 let mut terms: Vec<(usize, f64)> = Vec::new();
89 for t in 0..d {
90 terms.push((var(j, t), t as f64));
91 terms.push((var(i, t), -(t as f64)));
92 }
93 constraints.push(LinearConstraint::ge(terms, 1.0));
94 }
95
96 ReductionPCSToILP {
97 target: ILP::new(num_vars, constraints, vec![], ObjectiveSense::Minimize),
98 num_tasks: n,
99 deadline: d,
100 }
101 }
102}
103
104#[cfg(feature = "example-db")]
105pub(crate) fn canonical_rule_example_specs() -> Vec<crate::example_db::specs::RuleExampleSpec> {
106 vec![crate::example_db::specs::RuleExampleSpec {
107 id: "precedenceconstrainedscheduling_to_ilp",
108 build: || {
109 let source = PrecedenceConstrainedScheduling::new(3, 2, 2, vec![(0, 2)]);
111 crate::example_db::specs::rule_example_via_ilp::<_, bool>(source)
112 },
113 }]
114}
115
116#[cfg(test)]
117#[path = "../unit_tests/rules/precedenceconstrainedscheduling_ilp.rs"]
118mod tests;