Skip to content

Commit 85175e3

Browse files
committed
Simplify numeric cast audits and reuse evaluated objectives
1 parent 7bd3b06 commit 85175e3

2 files changed

Lines changed: 36 additions & 37 deletions

File tree

‎docs/paper/reductions.typ‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16869,7 +16869,9 @@ Problems parameterized by graph type, weight type, target type, or clause width
1686916869
let from = src.variant.at(field, default: none)
1687016870
let to = tgt.variant.at(field, default: none)
1687116871
let promotion = (from == "One" and to == "i64") or (from == "i64" and to == "f64")
16872-
promotion and src.variant.pairs().all(((key, value)) => key == field or tgt.variant.at(key, default: none) == value) and src.variant.len() == tgt.variant.len()
16872+
let promoted = src.variant
16873+
promoted.insert(field, to)
16874+
promotion and promoted == tgt.variant
1687316875
})
1687416876
}
1687516877
let source = (name: "MaximumIndependentSet", variant: (graph: "SimpleGraph", weight: "One"))

‎src/unit_tests/rules/weight_casts.rs‎

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,40 @@ use crate::traits::Problem;
99
use crate::types::{One, MAX_EXACT_F64_INTEGER};
1010
use crate::variant::KN;
1111
use serde::Serialize;
12+
use std::collections::BTreeSet;
1213
use std::fmt::Debug;
1314

1415
#[test]
1516
fn registered_numeric_variants_have_direct_promotions() {
16-
let variants = crate::registry::variant_entries();
17-
let edges = crate::rules::registry::reduction_entries();
17+
let variants: BTreeSet<_> = crate::registry::variant_entries()
18+
.into_iter()
19+
.map(|v| (v.name, v.variant_map()))
20+
.collect();
21+
let edges: BTreeSet<_> = crate::rules::registry::reduction_entries()
22+
.into_iter()
23+
.filter(|e| e.source_name == e.target_name && e.reduce_fn.is_some())
24+
.map(|e| {
25+
(
26+
e.source_name,
27+
ReductionGraph::variant_to_map(&e.source_variant()),
28+
ReductionGraph::variant_to_map(&e.target_variant()),
29+
)
30+
})
31+
.collect();
1832
let mut missing = Vec::new();
19-
for source in &variants {
20-
for (from, to) in [("One", "i64"), ("i64", "f64")] {
21-
for field in ["weight", "coefficient"] {
22-
let mut target = source.variant_map();
23-
if target.get(field).map(String::as_str) != Some(from) {
24-
continue;
25-
}
26-
target.insert(field.into(), to.into());
27-
if !variants
28-
.iter()
29-
.any(|v| v.name == source.name && v.variant_map() == target)
30-
{
31-
continue;
32-
}
33-
if !edges.iter().any(|e| {
34-
e.source_name == source.name
35-
&& e.target_name == source.name
36-
&& ReductionGraph::variant_to_map(&e.source_variant())
37-
== source.variant_map()
38-
&& ReductionGraph::variant_to_map(&e.target_variant()) == target
39-
&& e.reduce_fn.is_some()
40-
}) {
41-
missing.push(format!(
42-
"{} {:?} -> {:?}",
43-
source.name,
44-
source.variant_map(),
45-
target
46-
));
47-
}
33+
for (name, source) in &variants {
34+
for field in ["weight", "coefficient"] {
35+
let to = match source.get(field).map(String::as_str) {
36+
Some("One") => "i64",
37+
Some("i64") => "f64",
38+
_ => continue,
39+
};
40+
let mut target = source.clone();
41+
target.insert(field.into(), to.into());
42+
if variants.contains(&(*name, target.clone()))
43+
&& !edges.contains(&(*name, source.clone(), target.clone()))
44+
{
45+
missing.push(format!("{name} {source:?} -> {target:?}"));
4846
}
4947
}
5048
}
@@ -104,20 +102,19 @@ where
104102
let source_solution = solver.solve(&source).unwrap();
105103
let target_solutions = solver.find_all_witnesses(target).unwrap();
106104
if let Some(source_solution) = source_solution {
105+
let optimum = source.evaluate(&source_solution).unwrap();
107106
assert!(!target_solutions.is_empty());
108107
for solution in target_solutions {
109108
let recovered = chain.extract_solution::<S::Solution, _>(&solution).unwrap();
110109
assert_eq!(recovered, solution);
111-
assert_eq!(
112-
source.evaluate(&recovered).unwrap(),
113-
source.evaluate(&source_solution).unwrap()
114-
);
110+
let recovered_value = source.evaluate(&recovered).unwrap();
111+
assert_eq!(recovered_value, optimum);
115112
if maps_values {
116113
assert!(chain.has_value_mapping());
117114
let value = serde_json::to_value(target.evaluate(&solution).unwrap()).unwrap();
118115
assert_eq!(
119116
chain.extract_value(value).unwrap(),
120-
serde_json::to_value(source.evaluate(&recovered).unwrap()).unwrap()
117+
serde_json::to_value(recovered_value).unwrap()
121118
);
122119
}
123120
}

0 commit comments

Comments
 (0)