problemreductions/rules/
threepartition_resourceconstrainedscheduling.rs1use crate::models::misc::{ResourceConstrainedScheduling, ThreePartition};
22use crate::reduction;
23use crate::rules::traits::{ReduceTo, ReductionResult};
24
25#[derive(Debug, Clone)]
27pub struct ReductionThreePartitionToRCS {
28 target: ResourceConstrainedScheduling,
29}
30
31impl ReductionResult for ReductionThreePartitionToRCS {
32 type Source = ThreePartition;
33 type Target = ResourceConstrainedScheduling;
34
35 fn target_problem(&self) -> &Self::Target {
36 &self.target
37 }
38
39 fn extract_solution(
42 &self,
43 target_solution: &<Self::Target as crate::traits::Problem>::Solution,
44 ) -> crate::rules::ExtractionResult<<Self::Source as crate::traits::Problem>::Solution> {
45 crate::rules::traits::validate_target_witness(
46 self.target_problem(),
47 target_solution,
48 |value| value.0,
49 "target witness does not satisfy the target problem",
50 )?;
51
52 Ok(target_solution.to_vec())
53 }
54}
55
56#[crate::aggregate_reduction(identity)]
57impl crate::rules::AggregateReductionResult for ReductionThreePartitionToRCS {}
58
59#[reduction(
60 transform = exact {
61 max_resource_bits = "max_numeric_magnitude_bits",
62 num_tasks = "num_elements",
63 deadline = "num_groups",
64 num_resources = "1",
65 }
66)]
67impl ReduceTo<ResourceConstrainedScheduling> for ThreePartition {
68 type Result = ReductionThreePartitionToRCS;
69
70 fn reduce_to(&self) -> Result<Self::Result, crate::rules::ReductionError> {
71 let m = self.num_groups();
72 let bound = self.bound();
73 let deadline = i64::try_from(m).map_err(|_| {
74 crate::rules::ReductionError::integer_overflow::<
75 ThreePartition,
76 ResourceConstrainedScheduling,
77 >("converting the number of groups to a scheduling deadline")
78 })?;
79
80 let resource_requirements: Vec<Vec<i64>> = self.sizes().iter().map(|&s| vec![s]).collect();
82
83 Ok(ReductionThreePartitionToRCS {
84 target: ResourceConstrainedScheduling::new(
85 3, vec![bound], resource_requirements,
88 deadline,
89 )
90 .map_err(|error| {
91 crate::rules::ReductionError::construction::<
92 ThreePartition,
93 ResourceConstrainedScheduling,
94 >(error)
95 })?,
96 })
97 }
98}
99
100#[cfg(feature = "example-db")]
101pub(crate) fn canonical_rule_example_specs() -> Vec<crate::example_db::specs::RuleExampleSpec> {
102 use crate::export::SolutionPair;
103
104 vec![crate::example_db::specs::RuleExampleSpec {
105 id: "threepartition_to_resourceconstrainedscheduling",
106 build: || {
107 crate::example_db::specs::rule_example_with_witness::<_, ResourceConstrainedScheduling>(
111 ThreePartition::new(vec![4, 5, 6, 4, 6, 5], 15),
112 SolutionPair {
113 source_config: serde_json::json!(vec![0, 0, 0, 1, 1, 1]),
114 target_config: serde_json::json!(vec![0, 0, 0, 1, 1, 1]),
115 },
116 )
117 },
118 }]
119}
120
121#[cfg(test)]
122#[path = "../unit_tests/rules/threepartition_resourceconstrainedscheduling.rs"]
123mod tests;